Skip to content

Commit

Permalink
添加slab测试程序
Browse files Browse the repository at this point in the history
  • Loading branch information
syxl-time committed Jan 17, 2025
1 parent d0fac07 commit f0a4dbb
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion eBPF_Supermarket/Memory_Subsystem/mem_watcher/test/test_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ static struct env {
bool mem_leak;
bool mem_unleak;
bool mem_stress_test;
bool simulate_leak;
} env = {
.overall_leak_test = false,
.mem_leak = false,
.mem_unleak = false,
.mem_stress_test = false
.mem_stress_test = false,
.simulate_leak = false
};

static volatile bool running = true; // 控制程序是否继续运行
Expand All @@ -36,6 +38,7 @@ static const struct argp_option opts[] = {
{ "detect-leak", 'l', NULL, 0, "Detect memory leaks", 3 },
{ "no-leak", 'n', NULL, 0, "No memory leaks expected", 3 },
{ "stress-test", 's', NULL, 0, "Perform memory stress test", 4 },
{ "simulate-leak", 'm', NULL, 0, "Simulate memory leak with complex objects", 5 },
{ NULL, 'h', NULL, OPTION_HIDDEN, "show the full help", 0 },
{ NULL, 0, NULL, 0, NULL, 0 }
};
Expand All @@ -56,6 +59,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
case 's':
env.mem_stress_test = true;
break;
case 'm':
env.simulate_leak = true;
break;
case 'h':
argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
break;
Expand All @@ -65,6 +71,45 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
return 0;
}

typedef struct {
int* data;
} ComplexObject;

ComplexObject* createComplexObject() {
ComplexObject* obj = (ComplexObject*)malloc(sizeof(ComplexObject));
if (obj != NULL) {
obj->data = (int*)malloc(1000 * sizeof(int));
if (obj->data != NULL) {
for (int i = 0; i < 1000; ++i) {
obj->data[i] = rand() % 100; // 填充数据
}
return obj;
} else {
free(obj);
return NULL;
}
} else {
return NULL;
}
}

void destroyComplexObject(ComplexObject* obj) {
if (obj != NULL) {
if (obj->data != NULL) {
free(obj->data);
}
free(obj);
}
}

void simulateMemoryLeak() {
// 动态分配一个复杂对象的数组
ComplexObject* objects[1000];
for (int i = 0; i < 1000; ++i) {
objects[i] = createComplexObject();
}
}

// 模拟一些处理,通过写入分配的内存
static void process_data(void *ptr, int size) {
memset(ptr, 0, size);
Expand Down Expand Up @@ -203,6 +248,13 @@ int main(int argc, char **argv) {
}
}

if (env.simulate_leak) {
// 模拟复杂对象内存泄漏
for (int i = 0; i < 1000; ++i) {
simulateMemoryLeak();
}
}

if (env.mem_stress_test) {
// 打印当前进程的进程号(PID)
pid_t pid = getpid();
Expand Down

0 comments on commit f0a4dbb

Please sign in to comment.