Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 2 | # |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 3 | # memleak Trace and display outstanding allocations to detect |
| 4 | # memory leaks in user-mode processes and the kernel. |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 5 | # |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 6 | # USAGE: memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 7 | # [-s SAMPLE_RATE] [-d STACK_DEPTH] [-T TOP] [-z MIN_SIZE] |
| 8 | # [-Z MAX_SIZE] |
| 9 | # [interval] [count] |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 10 | # |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 11 | # Licensed under the Apache License, Version 2.0 (the "License") |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 12 | # Copyright (C) 2016 Sasha Goldshtein. |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 13 | |
Sasha Goldshtein | 8737c6e | 2016-03-21 10:05:29 -0700 | [diff] [blame] | 14 | from bcc import BPF, ProcessSymbols |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 15 | from time import sleep |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 16 | from datetime import datetime |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 17 | import argparse |
| 18 | import subprocess |
Sasha Goldshtein | cfce311 | 2016-02-07 11:09:36 -0800 | [diff] [blame] | 19 | import os |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 20 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 21 | class KStackDecoder(object): |
| 22 | def refresh(self): |
| 23 | pass |
| 24 | |
| 25 | def __call__(self, addr): |
| 26 | return "%s [kernel] (%x)" % (BPF.ksym(addr), addr) |
| 27 | |
| 28 | class UStackDecoder(object): |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 29 | def __init__(self, pid): |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 30 | self.pid = pid |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 31 | self.proc_sym = ProcessSymbols(pid) |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 32 | |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 33 | def refresh(self): |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 34 | self.proc_sym.refresh_code_ranges() |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 35 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 36 | def __call__(self, addr): |
| 37 | return "%s (%x)" % (self.proc_sym.decode_addr(addr), addr) |
| 38 | |
| 39 | class Allocation(object): |
| 40 | def __init__(self, stack, size): |
| 41 | self.stack = stack |
| 42 | self.count = 1 |
| 43 | self.size = size |
| 44 | |
| 45 | def update(self, size): |
| 46 | self.count += 1 |
| 47 | self.size += size |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 48 | |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 49 | def run_command_get_output(command): |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 50 | p = subprocess.Popen(command.split(), |
| 51 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 52 | return iter(p.stdout.readline, b'') |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 53 | |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 54 | def run_command_get_pid(command): |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 55 | p = subprocess.Popen(command.split()) |
| 56 | return p.pid |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 57 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 58 | examples = """ |
| 59 | EXAMPLES: |
| 60 | |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 61 | ./memleak -p $(pidof allocs) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 62 | Trace allocations and display a summary of "leaked" (outstanding) |
| 63 | allocations every 5 seconds |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 64 | ./memleak -p $(pidof allocs) -t |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 65 | Trace allocations and display each individual call to malloc/free |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 66 | ./memleak -ap $(pidof allocs) 10 |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 67 | Trace allocations and display allocated addresses, sizes, and stacks |
| 68 | every 10 seconds for outstanding allocations |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 69 | ./memleak -c "./allocs" |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 70 | Run the specified command and trace its allocations |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 71 | ./memleak |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 72 | Trace allocations in kernel mode and display a summary of outstanding |
| 73 | allocations every 5 seconds |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 74 | ./memleak -o 60000 |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 75 | Trace allocations in kernel mode and display a summary of outstanding |
| 76 | allocations that are at least one minute (60 seconds) old |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 77 | ./memleak -s 5 |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 78 | Trace roughly every 5th allocation, to reduce overhead |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 79 | """ |
| 80 | |
| 81 | description = """ |
| 82 | Trace outstanding memory allocations that weren't freed. |
| 83 | Supports both user-mode allocations made with malloc/free and kernel-mode |
| 84 | allocations made with kmalloc/kfree. |
| 85 | """ |
| 86 | |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 87 | parser = argparse.ArgumentParser(description=description, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 88 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 89 | epilog=examples) |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 90 | parser.add_argument("-p", "--pid", type=int, default=-1, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 91 | help="the PID to trace; if not specified, trace kernel allocs") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 92 | parser.add_argument("-t", "--trace", action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 93 | help="print trace messages for each alloc/free call") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 94 | parser.add_argument("interval", nargs="?", default=5, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 95 | help="interval in seconds to print outstanding allocations") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 96 | parser.add_argument("count", nargs="?", type=int, |
| 97 | help="number of times to print the report before exiting") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 98 | parser.add_argument("-a", "--show-allocs", default=False, action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 99 | help="show allocation addresses and sizes as well as call stacks") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 100 | parser.add_argument("-o", "--older", default=500, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 101 | help="prune allocations younger than this age in milliseconds") |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 102 | parser.add_argument("-c", "--command", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 103 | help="execute and trace the specified command") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 104 | parser.add_argument("-s", "--sample-rate", default=1, type=int, |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 105 | help="sample every N-th allocation to decrease the overhead") |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 106 | parser.add_argument("-T", "--top", type=int, default=10, |
| 107 | help="display only this many top allocating stacks (by size)") |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 108 | parser.add_argument("-z", "--min-size", type=int, |
| 109 | help="capture only allocations larger than this size") |
| 110 | parser.add_argument("-Z", "--max-size", type=int, |
| 111 | help="capture only allocations smaller than this size") |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 112 | parser.add_argument("-O", "--obj", type=str, default="c", |
| 113 | help="attach to malloc & free in the specified object") |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 114 | |
| 115 | args = parser.parse_args() |
| 116 | |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 117 | pid = args.pid |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 118 | command = args.command |
| 119 | kernel_trace = (pid == -1 and command is None) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 120 | trace_all = args.trace |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 121 | interval = args.interval |
| 122 | min_age_ns = 1e6 * args.older |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 123 | sample_every_n = args.sample_rate |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 124 | num_prints = args.count |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 125 | top_stacks = args.top |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 126 | min_size = args.min_size |
| 127 | max_size = args.max_size |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 128 | obj = args.obj |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 129 | |
| 130 | if min_size is not None and max_size is not None and min_size > max_size: |
| 131 | print("min_size (-z) can't be greater than max_size (-Z)") |
| 132 | exit(1) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 133 | |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 134 | if command is not None: |
| 135 | print("Executing '%s' and tracing the resulting process." % command) |
| 136 | pid = run_command_get_pid(command) |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 137 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 138 | bpf_source = """ |
| 139 | #include <uapi/linux/ptrace.h> |
| 140 | |
| 141 | struct alloc_info_t { |
| 142 | u64 size; |
| 143 | u64 timestamp_ns; |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 144 | int stack_id; |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | BPF_HASH(sizes, u64); |
| 148 | BPF_HASH(allocs, u64, struct alloc_info_t); |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 149 | BPF_STACK_TRACE(stack_traces, 1024) |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 150 | |
| 151 | int alloc_enter(struct pt_regs *ctx, size_t size) |
| 152 | { |
| 153 | SIZE_FILTER |
| 154 | if (SAMPLE_EVERY_N > 1) { |
| 155 | u64 ts = bpf_ktime_get_ns(); |
| 156 | if (ts % SAMPLE_EVERY_N != 0) |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | u64 pid = bpf_get_current_pid_tgid(); |
| 161 | u64 size64 = size; |
| 162 | sizes.update(&pid, &size64); |
| 163 | |
| 164 | if (SHOULD_PRINT) |
| 165 | bpf_trace_printk("alloc entered, size = %u\\n", size); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int alloc_exit(struct pt_regs *ctx) |
| 170 | { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 171 | u64 address = PT_REGS_RC(ctx); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 172 | u64 pid = bpf_get_current_pid_tgid(); |
| 173 | u64* size64 = sizes.lookup(&pid); |
| 174 | struct alloc_info_t info = {0}; |
| 175 | |
| 176 | if (size64 == 0) |
| 177 | return 0; // missed alloc entry |
| 178 | |
| 179 | info.size = *size64; |
| 180 | sizes.delete(&pid); |
| 181 | |
| 182 | info.timestamp_ns = bpf_ktime_get_ns(); |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 183 | info.stack_id = stack_traces.get_stackid(ctx, STACK_FLAGS); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 184 | allocs.update(&address, &info); |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 185 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 186 | if (SHOULD_PRINT) { |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 187 | bpf_trace_printk("alloc exited, size = %lu, result = %lx\\n", |
| 188 | info.size, address); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int free_enter(struct pt_regs *ctx, void *address) |
| 194 | { |
| 195 | u64 addr = (u64)address; |
| 196 | struct alloc_info_t *info = allocs.lookup(&addr); |
| 197 | if (info == 0) |
| 198 | return 0; |
| 199 | |
| 200 | allocs.delete(&addr); |
| 201 | |
| 202 | if (SHOULD_PRINT) { |
| 203 | bpf_trace_printk("free entered, address = %lx, size = %lu\\n", |
| 204 | address, info->size); |
| 205 | } |
| 206 | return 0; |
| 207 | } |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 208 | """ |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 209 | bpf_source = bpf_source.replace("SHOULD_PRINT", "1" if trace_all else "0") |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 210 | bpf_source = bpf_source.replace("SAMPLE_EVERY_N", str(sample_every_n)) |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 211 | |
| 212 | size_filter = "" |
| 213 | if min_size is not None and max_size is not None: |
| 214 | size_filter = "if (size < %d || size > %d) return 0;" % \ |
| 215 | (min_size, max_size) |
| 216 | elif min_size is not None: |
| 217 | size_filter = "if (size < %d) return 0;" % min_size |
| 218 | elif max_size is not None: |
| 219 | size_filter = "if (size > %d) return 0;" % max_size |
| 220 | bpf_source = bpf_source.replace("SIZE_FILTER", size_filter) |
| 221 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 222 | stack_flags = "BPF_F_REUSE_STACKID" |
| 223 | if not kernel_trace: |
| 224 | stack_flags += "|BPF_F_USER_STACK" |
| 225 | bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags) |
| 226 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 227 | bpf_program = BPF(text=bpf_source) |
| 228 | |
| 229 | if not kernel_trace: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 230 | print("Attaching to malloc and free in pid %d, Ctrl+C to quit." % pid) |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 231 | bpf_program.attach_uprobe(name=obj, sym="malloc", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 232 | fn_name="alloc_enter", pid=pid) |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 233 | bpf_program.attach_uretprobe(name=obj, sym="malloc", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 234 | fn_name="alloc_exit", pid=pid) |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 235 | bpf_program.attach_uprobe(name=obj, sym="free", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 236 | fn_name="free_enter", pid=pid) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 237 | else: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 238 | print("Attaching to kmalloc and kfree, Ctrl+C to quit.") |
| 239 | bpf_program.attach_kprobe(event="__kmalloc", fn_name="alloc_enter") |
| 240 | bpf_program.attach_kretprobe(event="__kmalloc", fn_name="alloc_exit") |
| 241 | bpf_program.attach_kprobe(event="kfree", fn_name="free_enter") |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 242 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 243 | decoder = KStackDecoder() if kernel_trace else UStackDecoder(pid) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 244 | |
| 245 | def print_outstanding(): |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 246 | print("[%s] Top %d stacks with outstanding allocations:" % |
| 247 | (datetime.now().strftime("%H:%M:%S"), top_stacks)) |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 248 | alloc_info = {} |
| 249 | allocs = bpf_program["allocs"] |
| 250 | stack_traces = bpf_program["stack_traces"] |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 251 | for address, info in sorted(allocs.items(), key=lambda a: a[1].size): |
Sasha Goldshtein | 60c4192 | 2017-02-09 04:19:53 -0500 | [diff] [blame^] | 252 | if BPF.monotonic_time() - min_age_ns < info.timestamp_ns: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 253 | continue |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 254 | if info.stack_id < 0: |
| 255 | continue |
| 256 | if info.stack_id in alloc_info: |
| 257 | alloc_info[info.stack_id].update(info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 258 | else: |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 259 | stack = list(stack_traces.walk(info.stack_id, decoder)) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 260 | alloc_info[info.stack_id] = Allocation(stack, |
| 261 | info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 262 | if args.show_allocs: |
| 263 | print("\taddr = %x size = %s" % |
| 264 | (address.value, info.size)) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 265 | to_show = sorted(alloc_info.values(), |
| 266 | key=lambda a: a.size)[-top_stacks:] |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 267 | for alloc in to_show: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 268 | print("\t%d bytes in %d allocations from stack\n\t\t%s" % |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 269 | (alloc.size, alloc.count, "\n\t\t".join(alloc.stack))) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 270 | |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 271 | count_so_far = 0 |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 272 | while True: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 273 | if trace_all: |
Brenden Blanco | c94ab7a | 2016-03-11 15:34:29 -0800 | [diff] [blame] | 274 | print(bpf_program.trace_fields()) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 275 | else: |
| 276 | try: |
| 277 | sleep(interval) |
| 278 | except KeyboardInterrupt: |
| 279 | exit() |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 280 | decoder.refresh() |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 281 | print_outstanding() |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 282 | count_so_far += 1 |
| 283 | if num_prints is not None and count_so_far >= num_prints: |
| 284 | exit() |