diff --git a/code/mperf.cc b/code/mperf.cc index 372d113..da73f8a 100644 --- a/code/mperf.cc +++ b/code/mperf.cc @@ -3,39 +3,46 @@ #include #include #include -#include +#include #include -int cur_usage = 0; -size_t time_base = 0; +static size_t time_base = 0; -char buf[255]; +static thread_local bool allow_record = true; -bool allow_record = true; +static const int MIN_RECORD = 32; // 选择要记录的最小内存单元 -const int MIN_RECORD = 32; // 选择要记录的最小内存单元 +static pthread_once_t init_once = PTHREAD_ONCE_INIT; +static void *(*malloc_cp)(size_t) = nullptr; +static void *(*realloc_cp)(void *, size_t) = nullptr; +static void *(*calloc_cp)(size_t, size_t) = nullptr; +static void (*free_cp)(void *) = nullptr; -size_t get_cur_ms() { - struct timeval tv; - gettimeofday(&tv, NULL); - size_t time_stamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; - return time_stamp; +static size_t get_cur_ms() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return static_cast(ts.tv_sec) * 1000 + + static_cast(ts.tv_nsec) / 1000000; +} + +static void init_real_functions() { + time_base = get_cur_ms(); + malloc_cp = reinterpret_cast(dlsym(RTLD_NEXT, "malloc")); + realloc_cp = + reinterpret_cast(dlsym(RTLD_NEXT, "realloc")); + calloc_cp = + reinterpret_cast(dlsym(RTLD_NEXT, "calloc")); + free_cp = reinterpret_cast(dlsym(RTLD_NEXT, "free")); + + char *err = dlerror(); + if (err != NULL) { + _exit(1); + } } void *malloc(size_t size) { - static int flag = 0; - static void *(*malloc_cp)(unsigned long); - - if (flag == 0) { - time_base = get_cur_ms(); - malloc_cp = (void *(*)(unsigned long))dlsym(RTLD_NEXT, "malloc"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); void *ptr = malloc_cp(size); @@ -50,6 +57,7 @@ void *malloc(size_t size) { printf("%s ", symbols[i]); } printf("\n"); + free_cp(symbols); allow_record = true; } @@ -58,18 +66,7 @@ void *malloc(size_t size) { void *realloc(void *ptr, size_t size) { - static int flag = 0; - static void *(*realloc_cp)(void *, size_t); - - if (flag == 0) { - time_base = get_cur_ms(); - realloc_cp = (void *(*)(void *, size_t))dlsym(RTLD_NEXT, "realloc"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); void *new_ptr = realloc_cp(ptr, size); @@ -84,6 +81,7 @@ void *realloc(void *ptr, size_t size) { printf("%s ", symbols[i]); } printf("\n"); + free_cp(symbols); allow_record = true; } @@ -91,18 +89,7 @@ void *realloc(void *ptr, size_t size) { } void *calloc(size_t nmemb, size_t size) { - static int flag = 0; - static void *(*calloc_cp)(size_t, size_t); - - if (flag == 0) { - time_base = get_cur_ms(); - calloc_cp = (void *(*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); void *ptr = calloc_cp(nmemb, size); @@ -117,6 +104,7 @@ void *calloc(size_t nmemb, size_t size) { printf("%s ", symbols[i]); } printf("\n"); + free_cp(symbols); allow_record = true; } @@ -125,19 +113,7 @@ void *calloc(size_t nmemb, size_t size) { void free(void *ptr) { - - static int flag = 0; - static void (*free_cp)(void *); - - if (flag == 0) { - time_base = get_cur_ms(); - free_cp = (void (*)(void *))dlsym(RTLD_NEXT, "free"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); if (allow_record == true) { allow_record = false; diff --git a/code/mperf_less.cc b/code/mperf_less.cc index 94addf4..6c5f8c4 100644 --- a/code/mperf_less.cc +++ b/code/mperf_less.cc @@ -3,39 +3,46 @@ #include #include #include -#include +#include #include -int cur_usage = 0; -size_t time_base = 0; +static size_t time_base = 0; -char buf[255]; +static thread_local bool allow_record = true; -bool allow_record = true; +static const int MIN_RECORD = 0; // 选择要记录的最小内存单元 -const int MIN_RECORD = 0; // 选择要记录的最小内存单元 +static pthread_once_t init_once = PTHREAD_ONCE_INIT; +static void *(*malloc_cp)(size_t) = nullptr; +static void *(*realloc_cp)(void *, size_t) = nullptr; +static void *(*calloc_cp)(size_t, size_t) = nullptr; +static void (*free_cp)(void *) = nullptr; -size_t get_cur_ms() { - struct timeval tv; - gettimeofday(&tv, NULL); - size_t time_stamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; - return time_stamp; +static size_t get_cur_ms() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return static_cast(ts.tv_sec) * 1000 + + static_cast(ts.tv_nsec) / 1000000; +} + +static void init_real_functions() { + time_base = get_cur_ms(); + malloc_cp = reinterpret_cast(dlsym(RTLD_NEXT, "malloc")); + realloc_cp = + reinterpret_cast(dlsym(RTLD_NEXT, "realloc")); + calloc_cp = + reinterpret_cast(dlsym(RTLD_NEXT, "calloc")); + free_cp = reinterpret_cast(dlsym(RTLD_NEXT, "free")); + + char *err = dlerror(); + if (err != NULL) { + _exit(1); + } } void *malloc(size_t size) { - static int flag = 0; - static void *(*malloc_cp)(unsigned long); - - if (flag == 0) { - time_base = get_cur_ms(); - malloc_cp = (void *(*)(unsigned long))dlsym(RTLD_NEXT, "malloc"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); void *ptr = malloc_cp(size); @@ -58,18 +65,7 @@ void *malloc(size_t size) { void *realloc(void *ptr, size_t size) { - static int flag = 0; - static void *(*realloc_cp)(void *, size_t); - - if (flag == 0) { - time_base = get_cur_ms(); - realloc_cp = (void *(*)(void *, size_t))dlsym(RTLD_NEXT, "realloc"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); void *new_ptr = realloc_cp(ptr, size); @@ -91,18 +87,7 @@ void *realloc(void *ptr, size_t size) { } void *calloc(size_t nmemb, size_t size) { - static int flag = 0; - static void *(*calloc_cp)(size_t, size_t); - - if (flag == 0) { - time_base = get_cur_ms(); - calloc_cp = (void *(*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); void *ptr = calloc_cp(nmemb, size); @@ -125,19 +110,7 @@ void *calloc(size_t nmemb, size_t size) { void free(void *ptr) { - - static int flag = 0; - static void (*free_cp)(void *); - - if (flag == 0) { - time_base = get_cur_ms(); - free_cp = (void (*)(void *))dlsym(RTLD_NEXT, "free"); - char *err; - if ((err = dlerror()) != NULL) { - _exit(1); - } - flag = 1; - } + pthread_once(&init_once, init_real_functions); if (allow_record == true) { allow_record = false;