Commit 03872236 authored by Barry Perlman's avatar Barry Perlman Committed by Yoni Fogel

[t:3806] #3806 Single-thread all mallocator calls.

git-svn-id: file:///svn/toku/tokudb@34905 c7de825b-a66e-492c-adef-691d508d4ae1
parent fc5da105
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <toku_pthread.h>
#include "toku_assert.h" #include "toku_assert.h"
int toku_memory_check=0; int toku_memory_check=0;
...@@ -19,6 +20,23 @@ static realloc_fun_t t_xrealloc = 0; ...@@ -19,6 +20,23 @@ static realloc_fun_t t_xrealloc = 0;
static MEMORY_STATUS_S status; static MEMORY_STATUS_S status;
// single-thread all access to mallocator to protect from thread-safety bugs in mallocator, currently a problem in jemalloc
static toku_pthread_mutex_t memory_lock = PTHREAD_MUTEX_INITIALIZER;
static void
lock(void) {
int r = toku_pthread_mutex_lock(&memory_lock);
resource_assert_zero(r);
}
static void
unlock(void) {
int r = toku_pthread_mutex_unlock(&memory_lock);
resource_assert_zero(r);
}
void void
toku_memory_get_status(MEMORY_STATUS s) { toku_memory_get_status(MEMORY_STATUS s) {
*s = status; *s = status;
...@@ -27,6 +45,7 @@ toku_memory_get_status(MEMORY_STATUS s) { ...@@ -27,6 +45,7 @@ toku_memory_get_status(MEMORY_STATUS s) {
void *toku_malloc(size_t size) { void *toku_malloc(size_t size) {
lock();
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 = malloc_usable_size(p);
...@@ -36,6 +55,7 @@ void *toku_malloc(size_t size) { ...@@ -36,6 +55,7 @@ void *toku_malloc(size_t size) {
} }
else else
__sync_add_and_fetch(&status.malloc_fail, 1L); __sync_add_and_fetch(&status.malloc_fail, 1L);
unlock();
return p; return p;
} }
...@@ -49,6 +69,7 @@ toku_calloc(size_t nmemb, size_t size) { ...@@ -49,6 +69,7 @@ toku_calloc(size_t nmemb, size_t size) {
void * void *
toku_realloc(void *p, size_t size) { toku_realloc(void *p, size_t size) {
lock();
size_t used_orig = p ? malloc_usable_size(p) : 0; size_t used_orig = p ? 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) {
...@@ -60,6 +81,7 @@ toku_realloc(void *p, size_t size) { ...@@ -60,6 +81,7 @@ toku_realloc(void *p, size_t size) {
} }
else else
__sync_add_and_fetch(&status.realloc_fail, 1L); __sync_add_and_fetch(&status.realloc_fail, 1L);
unlock();
return q; return q;
} }
...@@ -77,6 +99,7 @@ toku_strdup(const char *s) { ...@@ -77,6 +99,7 @@ toku_strdup(const char *s) {
void void
toku_free(void *p) { toku_free(void *p) {
lock();
if (p) { if (p) {
size_t used = malloc_usable_size(p); size_t used = malloc_usable_size(p);
__sync_add_and_fetch(&status.free_count, 1L); __sync_add_and_fetch(&status.free_count, 1L);
...@@ -86,6 +109,7 @@ toku_free(void *p) { ...@@ -86,6 +109,7 @@ toku_free(void *p) {
else else
os_free(p); os_free(p);
} }
unlock();
} }
void void
...@@ -95,6 +119,7 @@ toku_free_n(void* p, size_t size __attribute__((unused))) { ...@@ -95,6 +119,7 @@ toku_free_n(void* p, size_t size __attribute__((unused))) {
void * void *
toku_xmalloc(size_t size) { toku_xmalloc(size_t size) {
lock();
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);
...@@ -102,6 +127,7 @@ toku_xmalloc(size_t size) { ...@@ -102,6 +127,7 @@ toku_xmalloc(size_t size) {
__sync_add_and_fetch(&status.malloc_count, 1L); __sync_add_and_fetch(&status.malloc_count, 1L);
__sync_add_and_fetch(&status.requested, size); __sync_add_and_fetch(&status.requested, size);
__sync_add_and_fetch(&status.used, used); __sync_add_and_fetch(&status.used, used);
unlock();
return p; return p;
} }
...@@ -115,6 +141,7 @@ toku_xcalloc(size_t nmemb, size_t size) { ...@@ -115,6 +141,7 @@ toku_xcalloc(size_t nmemb, size_t size) {
void * void *
toku_xrealloc(void *v, size_t size) { toku_xrealloc(void *v, size_t size) {
lock();
size_t used_orig = v ? malloc_usable_size(v) : 0; size_t used_orig = v ? 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
...@@ -124,6 +151,7 @@ toku_xrealloc(void *v, size_t size) { ...@@ -124,6 +151,7 @@ toku_xrealloc(void *v, size_t size) {
__sync_add_and_fetch(&status.requested, size); __sync_add_and_fetch(&status.requested, size);
__sync_add_and_fetch(&status.used, used); __sync_add_and_fetch(&status.used, used);
__sync_add_and_fetch(&status.freed, used_orig); __sync_add_and_fetch(&status.freed, used_orig);
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