Skip to content

Commit

Permalink
tools/memleak: Use compact size of key for sizes and memptrs maps
Browse files Browse the repository at this point in the history
As sizes and memptrs map are to store the temporal information of each thread,
use only tid portion of the return value of bpf_get_current_pid_tgid as a key
of sizes and memptrs map. This change could reduce the key size of each map
from 64 bits to 32 bits. The lower 32 bits portion of the return value of
bpf_get_current_pid_tgid is kernel's view of the pid, which in user space is
usually presented as the tid.

Please refer this document for further information:
https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md#4-bpf_get_current_pid_tgid
  • Loading branch information
Bojun-Seo authored and yonghong-song committed Jan 13, 2024
1 parent bafc056 commit 5e92141
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions tools/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ def run_command_get_pid(command):
u64 number_of_allocs;
};
BPF_HASH(sizes, u64);
BPF_HASH(sizes, u32, u64);
BPF_HASH(allocs, u64, struct alloc_info_t, 1000000);
BPF_HASH(memptrs, u64, u64);
BPF_HASH(memptrs, u32, u64);
BPF_STACK_TRACE(stack_traces, 10240);
BPF_HASH(combined_allocs, u64, struct combined_alloc_info_t, 10240);
Expand Down Expand Up @@ -208,25 +208,25 @@ def run_command_get_pid(command):
return 0;
}
u64 pid = bpf_get_current_pid_tgid();
u32 tid = bpf_get_current_pid_tgid();
u64 size64 = size;
sizes.update(&pid, &size64);
sizes.update(&tid, &size64);
if (SHOULD_PRINT)
bpf_trace_printk("alloc entered, size = %u\\n", size);
return 0;
}
static inline int gen_alloc_exit2(struct pt_regs *ctx, u64 address) {
u64 pid = bpf_get_current_pid_tgid();
u64* size64 = sizes.lookup(&pid);
u32 tid = bpf_get_current_pid_tgid();
u64* size64 = sizes.lookup(&tid);
struct alloc_info_t info = {0};
if (size64 == 0)
return 0; // missed alloc entry
info.size = *size64;
sizes.delete(&pid);
sizes.delete(&tid);
if (address != 0) {
info.timestamp_ns = bpf_ktime_get_ns();
Expand Down Expand Up @@ -307,21 +307,21 @@ def run_command_get_pid(command):
int posix_memalign_enter(struct pt_regs *ctx, void **memptr, size_t alignment,
size_t size) {
u64 memptr64 = (u64)(size_t)memptr;
u64 pid = bpf_get_current_pid_tgid();
u32 tid = bpf_get_current_pid_tgid();
memptrs.update(&pid, &memptr64);
memptrs.update(&tid, &memptr64);
return gen_alloc_enter(ctx, size);
}
int posix_memalign_exit(struct pt_regs *ctx) {
u64 pid = bpf_get_current_pid_tgid();
u64 *memptr64 = memptrs.lookup(&pid);
u32 tid = bpf_get_current_pid_tgid();
u64 *memptr64 = memptrs.lookup(&tid);
void *addr;
if (memptr64 == 0)
return 0;
memptrs.delete(&pid);
memptrs.delete(&tid);
if (bpf_probe_read_user(&addr, sizeof(void*), (void*)(size_t)*memptr64))
return 0;
Expand Down

0 comments on commit 5e92141

Please sign in to comment.