Skip to content

Commit

Permalink
tools: add percpu allocator tracing to memleak
Browse files Browse the repository at this point in the history
Add an ability to trace percpu allocations using the memleak tool.
Example:

./memleak.py --percpu
Attaching to kernel allocators, Ctrl+C to quit.
[14:15:46] Top 10 stacks with outstanding allocations:
	8 bytes in 1 allocations from stack
		pcpu_alloc+0x3d8 [kernel]
		pcpu_alloc+0x3d8 [kernel]
		perf_trace_event_init+0xc9 [kernel]
		perf_trace_init+0x69 [kernel]
		perf_tp_event_init+0x1b [kernel]
		perf_try_init_event+0x42 [kernel]
		perf_event_alloc+0x620 [kernel]
		__do_sys_perf_event_open+0x188 [kernel]
		do_syscall_64+0x48 [kernel]
		entry_SYSCALL_64_after_hwframe+0x44 [kernel]
	16 bytes in 1 allocations from stack
		pcpu_alloc+0x3d8 [kernel]
		pcpu_alloc+0x3d8 [kernel]
		bpf_prog_alloc+0x33 [kernel]
		bpf_prog_load+0xf5 [kernel]
		perf_event_for_each_child+0x34 [kernel]
		_perf_ioctl+0x1d7 [kernel]
		alloc_file_pseudo+0xa7 [kernel]
		__do_sys_bpf+0x953 [kernel]
		perf_ioctl+0x40 [kernel]
		do_vfs_ioctl+0xa5 [kernel]
		do_syscall_64+0x48 [kernel]
		entry_SYSCALL_64_after_hwframe+0x44 [kernel]
  • Loading branch information
rgushchin authored and yonghong-song committed Apr 17, 2019
1 parent 9d035b8 commit 9c8e2a6
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tools/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def run_command_get_pid(command):
help="attach to allocator functions in the specified object")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
parser.add_argument("--percpu", default=False, action="store_true",
help="trace percpu allocations")

args = parser.parse_args()

Expand Down Expand Up @@ -365,8 +367,23 @@ def run_command_get_pid(command):
}
"""

bpf_source_percpu = """
TRACEPOINT_PROBE(percpu, percpu_alloc_percpu) {
gen_alloc_enter((struct pt_regs *)args, args->size);
return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr);
}
TRACEPOINT_PROBE(percpu, percpu_free_percpu) {
return gen_free_enter((struct pt_regs *)args, (void *)args->ptr);
}
"""

if kernel_trace:
bpf_source += bpf_source_kernel
if args.percpu:
bpf_source += bpf_source_percpu
else:
bpf_source += bpf_source_kernel

bpf_source = bpf_source.replace("SHOULD_PRINT", "1" if trace_all else "0")
bpf_source = bpf_source.replace("SAMPLE_EVERY_N", str(sample_every_n))
Expand Down

0 comments on commit 9c8e2a6

Please sign in to comment.