Commit c20a422a authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul Committed by Yoni Fogel

Reserve memory. Refs #2613. [t:2613].

git-svn-id: file:///svn/toku/tokudb@20241 c7de825b-a66e-492c-adef-691d508d4ae1
parent eaa5564c
...@@ -136,6 +136,8 @@ struct brtloader_s { ...@@ -136,6 +136,8 @@ struct brtloader_s {
const char *temp_file_template; const char *temp_file_template;
CACHETABLE cachetable; CACHETABLE cachetable;
uint64_t reserved_memory; // how much memory are we allowed to use?
/* To make it easier to recover from errors, we don't use FILE*, instead we use an index into the file_infos. */ /* To make it easier to recover from errors, we don't use FILE*, instead we use an index into the file_infos. */
struct file_infos file_infos; struct file_infos file_infos;
......
...@@ -267,8 +267,11 @@ static void brtloader_destroy (BRTLOADER bl, BOOL is_error) { ...@@ -267,8 +267,11 @@ static void brtloader_destroy (BRTLOADER bl, BOOL is_error) {
toku_free(bl->fractal_queues); toku_free(bl->fractal_queues);
toku_free(bl->fractal_threads_live); toku_free(bl->fractal_threads_live);
toku_cachetable_release_reserved_memory(bl->cachetable, bl->reserved_memory);
brt_loader_destroy_error_callback(&bl->error_callback); brt_loader_destroy_error_callback(&bl->error_callback);
brt_loader_destroy_poll_callback(&bl->poll_callback); brt_loader_destroy_poll_callback(&bl->poll_callback);
toku_free(bl); toku_free(bl);
} }
...@@ -311,6 +314,7 @@ int toku_brt_loader_open (/* out */ BRTLOADER *blp, ...@@ -311,6 +314,7 @@ int toku_brt_loader_open (/* out */ BRTLOADER *blp,
bl->generate_row_for_put = g; bl->generate_row_for_put = g;
bl->cachetable = cachetable; bl->cachetable = cachetable;
bl->reserved_memory = toku_cachetable_reserve_memory(cachetable, 0.5);
bl->src_db = src_db; bl->src_db = src_db;
bl->N = N; bl->N = N;
......
...@@ -119,6 +119,7 @@ struct ctpair { ...@@ -119,6 +119,7 @@ struct ctpair {
static void * const zero_value = 0; static void * const zero_value = 0;
static int const zero_size = 0; static int const zero_size = 0;
static int maybe_flush_some (CACHETABLE ct, long size);
static inline void static inline void
ctpair_add_ref(PAIR p) { ctpair_add_ref(PAIR p) {
...@@ -296,6 +297,29 @@ int toku_create_cachetable(CACHETABLE *result, long size_limit, LSN UU(initial_l ...@@ -296,6 +297,29 @@ int toku_create_cachetable(CACHETABLE *result, long size_limit, LSN UU(initial_l
return 0; return 0;
} }
uint64_t toku_cachetable_reserve_memory(CACHETABLE ct, double fraction) {
cachetable_lock(ct);
cachetable_wait_write(ct);
uint64_t reserved_memory = fraction*ct->size_limit;
{
int r = maybe_flush_some(ct, reserved_memory);
if (r) {
cachetable_unlock(ct);
return r;
}
}
ct->size_current += reserved_memory;
cachetable_unlock(ct);
return reserved_memory;
}
void toku_cachetable_release_reserved_memory(CACHETABLE ct, uint64_t reserved_memory) {
cachetable_lock(ct);
ct->size_current -= reserved_memory;
assert(ct->size_current >= 0);
cachetable_unlock(ct);
}
void void
toku_cachetable_set_env_dir(CACHETABLE ct, char *env_dir) { toku_cachetable_set_env_dir(CACHETABLE ct, char *env_dir) {
assert(!ct->set_env_dir); assert(!ct->set_env_dir);
......
...@@ -82,7 +82,6 @@ int toku_cachetable_openf (CACHEFILE *,CACHETABLE, const char */*fname_in_env*/, ...@@ -82,7 +82,6 @@ int toku_cachetable_openf (CACHEFILE *,CACHETABLE, const char */*fname_in_env*/,
// Returns the limit on the cachetable size // Returns the limit on the cachetable size
uint64_t toku_cachetable_get_size_limit(CACHETABLE ct); uint64_t toku_cachetable_get_size_limit(CACHETABLE ct);
// Bind a file to a new cachefile object. // Bind a file to a new cachefile object.
int toku_cachetable_openfd (CACHEFILE *,CACHETABLE, int /*fd*/, int toku_cachetable_openfd (CACHEFILE *,CACHETABLE, int /*fd*/,
const char *fname_relative_to_env); /*(used for logging)*/ const char *fname_relative_to_env); /*(used for logging)*/
...@@ -97,6 +96,12 @@ int toku_cachetable_reserve_filenum (CACHETABLE ct, FILENUM *reserved_filenum, B ...@@ -97,6 +96,12 @@ int toku_cachetable_reserve_filenum (CACHETABLE ct, FILENUM *reserved_filenum, B
void toku_cachetable_unreserve_filenum (CACHETABLE ct, FILENUM reserved_filenum); void toku_cachetable_unreserve_filenum (CACHETABLE ct, FILENUM reserved_filenum);
// Effect: Reserve a fraction of the cachetable memory.
// Returns the amount reserved.
// To return the memory to the cachetable, call toku_cachetable_release_reserved_memory
// Requires 0<fraction<1.
uint64_t toku_cachetable_reserve_memory(CACHETABLE, double fraction);
void toku_cachetable_release_reserved_memory(CACHETABLE, uint64_t);
// Get access to the asynchronous work queue // Get access to the asynchronous work queue
// Returns: a pointer to the work queue // Returns: a pointer to the work queue
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment