Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 36 additions & 60 deletions code/mperf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,46 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

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<size_t>(ts.tv_sec) * 1000 +
static_cast<size_t>(ts.tv_nsec) / 1000000;
}

static void init_real_functions() {
time_base = get_cur_ms();
malloc_cp = reinterpret_cast<void *(*)(size_t)>(dlsym(RTLD_NEXT, "malloc"));
realloc_cp =
reinterpret_cast<void *(*)(void *, size_t)>(dlsym(RTLD_NEXT, "realloc"));
calloc_cp =
reinterpret_cast<void *(*)(size_t, size_t)>(dlsym(RTLD_NEXT, "calloc"));
free_cp = reinterpret_cast<void (*)(void *)>(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);

Expand All @@ -50,6 +57,7 @@ void *malloc(size_t size) {
printf("%s ", symbols[i]);
}
printf("\n");
free_cp(symbols);
allow_record = true;
}

Expand All @@ -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);

Expand All @@ -84,25 +81,15 @@ void *realloc(void *ptr, size_t size) {
printf("%s ", symbols[i]);
}
printf("\n");
free_cp(symbols);
allow_record = true;
}

return new_ptr;
}

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);

Expand All @@ -117,6 +104,7 @@ void *calloc(size_t nmemb, size_t size) {
printf("%s ", symbols[i]);
}
printf("\n");
free_cp(symbols);
allow_record = true;
}

Expand All @@ -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;
Expand Down
93 changes: 33 additions & 60 deletions code/mperf_less.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,46 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

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<size_t>(ts.tv_sec) * 1000 +
static_cast<size_t>(ts.tv_nsec) / 1000000;
}

static void init_real_functions() {
time_base = get_cur_ms();
malloc_cp = reinterpret_cast<void *(*)(size_t)>(dlsym(RTLD_NEXT, "malloc"));
realloc_cp =
reinterpret_cast<void *(*)(void *, size_t)>(dlsym(RTLD_NEXT, "realloc"));
calloc_cp =
reinterpret_cast<void *(*)(size_t, size_t)>(dlsym(RTLD_NEXT, "calloc"));
free_cp = reinterpret_cast<void (*)(void *)>(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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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;
Expand Down