iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

why i use memleak, trace kmalloc tracepoint, run 12hours, but only counted 64 stackid....

583654109 opened this issue · comments

version: kernel-5.10
i would like to ask maybe i used it incorrectly?
i set max_entries is 10240
use yours demo
client print stackid (step by 1s)
i record kernel stack to file
one days
i count stackid nums
only 64....
what fk...
i don't believe...

https://github.com/iovisor/bcc/blob/master/libbpf-tools/memleak.bpf.c

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
__uint(key_size, sizeof(u32));
__uint(value_size, 127* sizeof(u64));
__uint(max_entries, 10240);
} stack_traces SEC(".maps");

SEC("tracepoint/kmem/kmalloc")
int memleak__kmalloc(void *ctx)
{
void *ptr = 0;
size_t bytes_alloc;
struct trace_event_raw_kmem_alloc *args = ctx;

bpf_probe_read(&ptr, sizeof(ptr), &args->ptr);
bpf_probe_read(&bytes_alloc, sizeof(bytes_alloc), &args->bytes_alloc);

gen_alloc_enter(bytes_alloc);

return gen_alloc_exit2(ctx, (u64)ptr);

}
`
while (running) {
sleep(1);
unsigned int key = 0;
unsigned int next_key = 0;
union combined_alloc_info com_ai = { 0 };

	while (bpf_map_get_next_key(combined_allocs_mapfd, &key, &next_key) == 0) {
		if (bpf_map_lookup_elem(combined_allocs_mapfd, &next_key, &com_ai) != 0) {
			break;
		}
		unsigned int stackid = next_key;
		struct allocation alloc = {
			.size = com_ai.total_size,
			.count = com_ai.number_of_allocs
		};
		printf("stack id: %u, alloc size: %zu, count: %zu\n", stackid, alloc.size, alloc.count);

		unsigned long stack[PERF_MAX_STACK_DEPTH];
		if (bpf_map_lookup_elem(stack_traces_mapfd, &stackid, stack) != 0) {
			break;
		}
		print_kern_stack(stack, PERF_MAX_STACK_DEPTH);

		key = next_key;
	}
	printf("================\n");
}

`
int main(int argc, char **argv) {
std::cout << "test running" << std::endl;
if (argc != 2) {
return -1;
}
FileIn fi(argv[1]);
std::string rule = "stack id: ";
auto vec_src(fi.readfile());
std::setstd::string st_size;
for (int i = 0; i < vec_src.size(); ++i) {
const std::string& line = vec_src[i];
if (line.find(rule) == 0) {
std::string str = line.substr(rule.size());
auto one = StringUtils::split(str, ",", true)[0];
st_size.insert(one);
}
}
std::cout << "st size is: " << st_size.size() << std::endl;

return 0;

}

[root@localhost 025memlog]# ./app xdmemleak.log
test running
the end of file
st size is: 64
10360 10519 11152 11243 11291 11314 11418 11750 11805 11857 1227 1229 12291 12358 12578 1265 12839 12840 12907 13178 13288 13309 13606 13843 14558 14573 14702 14750 14825 15482 15494 15664 15825 2353 2565 2889 3032 4213 4485 4498 475 4887 5146 5397 5597 5949 613 6152 6357 6716 7614 7698 7720 7956 8098 8182 8297 8775 911 9135 9534 9602 9613 9977
[root@localhost 025memlog]#