Commit cd42bfa6 authored by Rich Prohaska's avatar Rich Prohaska Committed by Yoni Fogel

#4091 replace mutex locking in memory.c with atomic ops refs[t:4091]

git-svn-id: file:///svn/toku/tokudb@36691 c7de825b-a66e-492c-adef-691d508d4ae1
parent 00e8c8c2
...@@ -17,23 +17,15 @@ static realloc_fun_t t_realloc = 0; ...@@ -17,23 +17,15 @@ static realloc_fun_t t_realloc = 0;
static realloc_fun_t t_xrealloc = 0; static realloc_fun_t t_xrealloc = 0;
static MEMORY_STATUS_S status; static MEMORY_STATUS_S status;
static toku_pthread_mutex_t memory_status_mutex = TOKU_PTHREAD_MUTEX_INITIALIZER;
static void static size_t
memory_status_lock(void) { my_malloc_usable_size(void *p UU()) {
int r = toku_pthread_mutex_lock(&memory_status_mutex); assert_zero(r); return malloc_usable_size(p);
}
static void
memory_status_unlock(void) {
int r = toku_pthread_mutex_unlock(&memory_status_mutex); assert_zero(r);
} }
void void
toku_memory_get_status(MEMORY_STATUS s) { toku_memory_get_status(MEMORY_STATUS s) {
memory_status_lock();
*s = status; *s = status;
memory_status_unlock();
} }
// max_in_use may be slightly off because use of max_in_use is not thread-safe. // max_in_use may be slightly off because use of max_in_use is not thread-safe.
...@@ -50,17 +42,13 @@ set_max(uint64_t sum_used, uint64_t sum_freed) { ...@@ -50,17 +42,13 @@ set_max(uint64_t sum_used, uint64_t sum_freed) {
void *toku_malloc(size_t size) { void *toku_malloc(size_t size) {
void *p = t_malloc ? t_malloc(size) : os_malloc(size); void *p = t_malloc ? t_malloc(size) : os_malloc(size);
if (p) { if (p) {
size_t used = malloc_usable_size(p); size_t used = my_malloc_usable_size(p);
memory_status_lock(); __sync_add_and_fetch(&status.malloc_count, 1);
status.malloc_count += 1; __sync_add_and_fetch(&status.requested,size);
status.requested += size; __sync_add_and_fetch(&status.used, used);
status.used += used; if (0) set_max(status.used, status.freed);
set_max(status.used, status.freed);
memory_status_unlock();
} else { } else {
memory_status_lock(); __sync_add_and_fetch(&status.malloc_fail, 1);
status.malloc_fail += 1;
memory_status_unlock();
} }
return p; return p;
} }
...@@ -75,21 +63,17 @@ toku_calloc(size_t nmemb, size_t size) { ...@@ -75,21 +63,17 @@ toku_calloc(size_t nmemb, size_t size) {
void * void *
toku_realloc(void *p, size_t size) { toku_realloc(void *p, size_t size) {
size_t used_orig = p ? malloc_usable_size(p) : 0; size_t used_orig = p ? my_malloc_usable_size(p) : 0;
void *q = t_realloc ? t_realloc(p, size) : os_realloc(p, size); void *q = t_realloc ? t_realloc(p, size) : os_realloc(p, size);
if (q) { if (q) {
size_t used = malloc_usable_size(q); size_t used = my_malloc_usable_size(q);
memory_status_lock(); __sync_add_and_fetch(&status.realloc_count, 1);
status.realloc_count += 1L; __sync_add_and_fetch(&status.requested, size);
status.requested += size; __sync_add_and_fetch(&status.used, used);
status.used += used; __sync_add_and_fetch(&status.freed, used_orig);
status.freed += used_orig; if (0) set_max(status.used, status.freed);
set_max(status.used, status.freed);
memory_status_unlock();
} else { } else {
memory_status_lock(); __sync_add_and_fetch(&status.realloc_fail, 1);
status.realloc_fail += 1;
memory_status_unlock();
} }
return q; return q;
} }
...@@ -109,11 +93,9 @@ toku_strdup(const char *s) { ...@@ -109,11 +93,9 @@ toku_strdup(const char *s) {
void void
toku_free(void *p) { toku_free(void *p) {
if (p) { if (p) {
size_t used = malloc_usable_size(p); size_t used = my_malloc_usable_size(p);
memory_status_lock(); __sync_add_and_fetch(&status.free_count, 1);
status.free_count += 1L; __sync_add_and_fetch(&status.freed, used);
status.freed += used;
memory_status_unlock();
if (t_free) if (t_free)
t_free(p); t_free(p);
else else
...@@ -131,13 +113,11 @@ toku_xmalloc(size_t size) { ...@@ -131,13 +113,11 @@ toku_xmalloc(size_t size) {
void *p = t_xmalloc ? t_xmalloc(size) : os_malloc(size); void *p = t_xmalloc ? t_xmalloc(size) : os_malloc(size);
if (p == NULL) // avoid function call in common case if (p == NULL) // avoid function call in common case
resource_assert(p); resource_assert(p);
size_t used = malloc_usable_size(p); size_t used = my_malloc_usable_size(p);
memory_status_lock(); __sync_add_and_fetch(&status.malloc_count, 1);
status.malloc_count += 1L; __sync_add_and_fetch(&status.requested, size);
status.requested += size; __sync_add_and_fetch(&status.used, used);
status.used += used; if (0) set_max(status.used, status.freed);
set_max(status.used, status.freed);
memory_status_unlock();
return p; return p;
} }
...@@ -151,18 +131,16 @@ toku_xcalloc(size_t nmemb, size_t size) { ...@@ -151,18 +131,16 @@ toku_xcalloc(size_t nmemb, size_t size) {
void * void *
toku_xrealloc(void *v, size_t size) { toku_xrealloc(void *v, size_t size) {
size_t used_orig = v ? malloc_usable_size(v) : 0; size_t used_orig = v ? my_malloc_usable_size(v) : 0;
void *p = t_xrealloc ? t_xrealloc(v, size) : os_realloc(v, size); void *p = t_xrealloc ? t_xrealloc(v, size) : os_realloc(v, size);
if (p == 0) // avoid function call in common case if (p == 0) // avoid function call in common case
resource_assert(p); resource_assert(p);
size_t used = malloc_usable_size(p); size_t used = my_malloc_usable_size(p);
memory_status_lock(); __sync_add_and_fetch(&status.realloc_count, 1);
status.realloc_count += 1L; __sync_add_and_fetch(&status.requested, size);
status.requested += size; __sync_add_and_fetch(&status.used, used);
status.used += used; __sync_add_and_fetch(&status.freed, used_orig);
status.freed += used_orig; if (0) set_max(status.used, status.freed);
set_max(status.used, status.freed);
memory_status_unlock();
return p; return p;
} }
......
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