Skip to content

Commit

Permalink
memleak: linter cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pchaigno committed Oct 7, 2017
1 parent 7297af0 commit 2e07ddc
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions tools/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def run_command_get_pid(command):
memptrs.delete(&pid);
if (bpf_probe_read(&addr, sizeof(void*), (void*)(size_t)*memptr64) != 0)
if (bpf_probe_read(&addr, sizeof(void*), (void*)(size_t)*memptr64))
return 0;
u64 addr64 = (u64)(size_t)addr;
Expand Down Expand Up @@ -382,7 +382,7 @@ def run_command_get_pid(command):
stack_flags += "|BPF_F_USER_STACK"
bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags)

bpf_program = BPF(text=bpf_source)
bpf = BPF(text=bpf_source)

if not kernel_trace:
print("Attaching to pid %d, Ctrl+C to quit." % pid)
Expand All @@ -392,12 +392,12 @@ def attach_probes(sym, fn_prefix=None, can_fail=False):
fn_prefix = sym

try:
bpf_program.attach_uprobe(name=obj, sym=sym,
fn_name=fn_prefix+"_enter",
pid=pid)
bpf_program.attach_uretprobe(name=obj, sym=sym,
fn_name=fn_prefix+"_exit",
pid=pid)
bpf.attach_uprobe(name=obj, sym=sym,
fn_name=fn_prefix + "_enter",
pid=pid)
bpf.attach_uretprobe(name=obj, sym=sym,
fn_name=fn_prefix + "_exit",
pid=pid)
except Exception:
if can_fail:
return
Expand All @@ -411,8 +411,8 @@ def attach_probes(sym, fn_prefix=None, can_fail=False):
attach_probes("valloc")
attach_probes("memalign")
attach_probes("pvalloc")
attach_probes("aligned_alloc", can_fail=True) # added in C11
bpf_program.attach_uprobe(name=obj, sym="free", fn_name="free_enter",
attach_probes("aligned_alloc", can_fail=True) # added in C11
bpf.attach_uprobe(name=obj, sym="free", fn_name="free_enter",
pid=pid)

else:
Expand All @@ -423,11 +423,12 @@ def attach_probes(sym, fn_prefix=None, can_fail=False):
#
# Memory allocations in Linux kernel are not limited to malloc/free
# equivalents. It's also common to allocate a memory page or multiple
# pages. Page allocator have two interfaces, one working with page frame
# numbers (PFN), while other working with page addresses. It's possible
# to allocate pages with one kind of functions, and free them with
# another. Code in kernel can easy convert PFNs to addresses and back,
# but it's hard to do the same in eBPF kprobe without fragile hacks.
# pages. Page allocator have two interfaces, one working with page
# frame numbers (PFN), while other working with page addresses. It's
# possible to allocate pages with one kind of functions, and free them
# with another. Code in kernel can easy convert PFNs to addresses and
# back, but it's hard to do the same in eBPF kprobe without fragile
# hacks.
#
# Fortunately, Linux exposes tracepoints for memory allocations, which
# can be instrumented by eBPF programs. Tracepoint for page allocations
Expand All @@ -438,8 +439,8 @@ def print_outstanding():
print("[%s] Top %d stacks with outstanding allocations:" %
(datetime.now().strftime("%H:%M:%S"), top_stacks))
alloc_info = {}
allocs = bpf_program["allocs"]
stack_traces = bpf_program["stack_traces"]
allocs = bpf["allocs"]
stack_traces = bpf["stack_traces"]
for address, info in sorted(allocs.items(), key=lambda a: a[1].size):
if BPF.monotonic_time() - min_age_ns < info.timestamp_ns:
continue
Expand All @@ -451,7 +452,7 @@ def print_outstanding():
stack = list(stack_traces.walk(info.stack_id))
combined = []
for addr in stack:
combined.append(bpf_program.sym(addr, pid,
combined.append(bpf.sym(addr, pid,
show_module=True, show_offset=True))
alloc_info[info.stack_id] = Allocation(combined,
info.size)
Expand All @@ -465,16 +466,16 @@ def print_outstanding():
(alloc.size, alloc.count, "\n\t\t".join(alloc.stack)))

def print_outstanding_combined():
stack_traces = bpf_program["stack_traces"]
stacks = sorted(bpf_program["combined_allocs"].items(),
stack_traces = bpf["stack_traces"]
stacks = sorted(bpf["combined_allocs"].items(),
key=lambda a: -a[1].total_size)
cnt = 1
entries = []
for stack_id, info in stacks:
try:
trace = []
for addr in stack_traces.walk(stack_id.value):
sym = bpf_program.sym(addr, pid,
sym = bpf.sym(addr, pid,
show_module=True,
show_offset=True)
trace.append(sym)
Expand All @@ -498,7 +499,7 @@ def print_outstanding_combined():
count_so_far = 0
while True:
if trace_all:
print(bpf_program.trace_fields())
print(bpf.trace_fields())
else:
try:
sleep(interval)
Expand Down

0 comments on commit 2e07ddc

Please sign in to comment.