blob: 4021bf87f2a340dca67fda9cff9407bb6eac6385 [file] [log] [blame]
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -08001#!/usr/bin/env python
Sasha Goldshtein50459642016-02-10 08:35:20 -08002#
Sasha Goldshtein0e856f42016-03-21 07:26:52 -07003# memleak Trace and display outstanding allocations to detect
4# memory leaks in user-mode processes and the kernel.
Sasha Goldshtein50459642016-02-10 08:35:20 -08005#
Sasha Goldshtein29e37d92016-02-14 06:56:07 -08006# USAGE: memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND]
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +03007# [--combined-only] [-s SAMPLE_RATE] [-T TOP] [-z MIN_SIZE]
8# [-Z MAX_SIZE] [-O OBJ]
Sasha Goldshtein0e856f42016-03-21 07:26:52 -07009# [interval] [count]
Sasha Goldshtein50459642016-02-10 08:35:20 -080010#
Sasha Goldshtein43fa0412016-02-10 22:17:26 -080011# Licensed under the Apache License, Version 2.0 (the "License")
Sasha Goldshtein50459642016-02-10 08:35:20 -080012# Copyright (C) 2016 Sasha Goldshtein.
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080013
Sasha Goldshtein49df9942017-02-08 23:22:06 -050014from bcc import BPF
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080015from time import sleep
Sasha Goldshteinc8148c82016-02-09 11:15:41 -080016from datetime import datetime
Yonghong Songeb6ddc02017-10-26 22:33:24 -070017import resource
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080018import argparse
19import subprocess
Sasha Goldshteincfce3112016-02-07 11:09:36 -080020import os
Brenden Blancoa296e1e2018-08-08 21:00:18 -070021import sys
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080022
Vicent Martie25ae032016-03-25 17:14:34 +010023class Allocation(object):
24 def __init__(self, stack, size):
25 self.stack = stack
26 self.count = 1
27 self.size = size
28
29 def update(self, size):
30 self.count += 1
31 self.size += size
Sasha Goldshtein29228612016-02-07 12:20:19 -080032
Sasha Goldshtein751fce52016-02-08 02:57:02 -080033def run_command_get_output(command):
Sasha Goldshtein33522d72016-02-08 03:39:44 -080034 p = subprocess.Popen(command.split(),
35 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
36 return iter(p.stdout.readline, b'')
Sasha Goldshtein29228612016-02-07 12:20:19 -080037
Sasha Goldshtein751fce52016-02-08 02:57:02 -080038def run_command_get_pid(command):
Sasha Goldshtein33522d72016-02-08 03:39:44 -080039 p = subprocess.Popen(command.split())
40 return p.pid
Sasha Goldshtein751fce52016-02-08 02:57:02 -080041
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080042examples = """
43EXAMPLES:
44
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080045./memleak -p $(pidof allocs)
Sasha Goldshtein33522d72016-02-08 03:39:44 -080046 Trace allocations and display a summary of "leaked" (outstanding)
47 allocations every 5 seconds
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080048./memleak -p $(pidof allocs) -t
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +030049 Trace allocations and display each individual allocator function call
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080050./memleak -ap $(pidof allocs) 10
Sasha Goldshtein33522d72016-02-08 03:39:44 -080051 Trace allocations and display allocated addresses, sizes, and stacks
52 every 10 seconds for outstanding allocations
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080053./memleak -c "./allocs"
Sasha Goldshtein33522d72016-02-08 03:39:44 -080054 Run the specified command and trace its allocations
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080055./memleak
Sasha Goldshtein33522d72016-02-08 03:39:44 -080056 Trace allocations in kernel mode and display a summary of outstanding
57 allocations every 5 seconds
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080058./memleak -o 60000
Sasha Goldshtein33522d72016-02-08 03:39:44 -080059 Trace allocations in kernel mode and display a summary of outstanding
60 allocations that are at least one minute (60 seconds) old
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080061./memleak -s 5
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -080062 Trace roughly every 5th allocation, to reduce overhead
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080063"""
64
65description = """
66Trace outstanding memory allocations that weren't freed.
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +030067Supports both user-mode allocations made with libc functions and kernel-mode
68allocations made with kmalloc/kmem_cache_alloc/get_free_pages and corresponding
69memory release functions.
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080070"""
71
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080072parser = argparse.ArgumentParser(description=description,
Sasha Goldshtein33522d72016-02-08 03:39:44 -080073 formatter_class=argparse.RawDescriptionHelpFormatter,
74 epilog=examples)
Sasha Goldshteind2241f42016-02-09 06:23:10 -080075parser.add_argument("-p", "--pid", type=int, default=-1,
Sasha Goldshtein33522d72016-02-08 03:39:44 -080076 help="the PID to trace; if not specified, trace kernel allocs")
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080077parser.add_argument("-t", "--trace", action="store_true",
Sasha Goldshtein33522d72016-02-08 03:39:44 -080078 help="print trace messages for each alloc/free call")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -080079parser.add_argument("interval", nargs="?", default=5, type=int,
Sasha Goldshtein33522d72016-02-08 03:39:44 -080080 help="interval in seconds to print outstanding allocations")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -080081parser.add_argument("count", nargs="?", type=int,
82 help="number of times to print the report before exiting")
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080083parser.add_argument("-a", "--show-allocs", default=False, action="store_true",
Sasha Goldshtein33522d72016-02-08 03:39:44 -080084 help="show allocation addresses and sizes as well as call stacks")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -080085parser.add_argument("-o", "--older", default=500, type=int,
Sasha Goldshtein33522d72016-02-08 03:39:44 -080086 help="prune allocations younger than this age in milliseconds")
Sasha Goldshtein29228612016-02-07 12:20:19 -080087parser.add_argument("-c", "--command",
Sasha Goldshtein33522d72016-02-08 03:39:44 -080088 help="execute and trace the specified command")
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +030089parser.add_argument("--combined-only", default=False, action="store_true",
90 help="show combined allocation statistics only")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -080091parser.add_argument("-s", "--sample-rate", default=1, type=int,
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -080092 help="sample every N-th allocation to decrease the overhead")
Sasha Goldshteinc8148c82016-02-09 11:15:41 -080093parser.add_argument("-T", "--top", type=int, default=10,
94 help="display only this many top allocating stacks (by size)")
Sasha Goldshtein50459642016-02-10 08:35:20 -080095parser.add_argument("-z", "--min-size", type=int,
96 help="capture only allocations larger than this size")
97parser.add_argument("-Z", "--max-size", type=int,
98 help="capture only allocations smaller than this size")
Maria Kacik9389ab42017-01-18 21:43:41 -080099parser.add_argument("-O", "--obj", type=str, default="c",
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300100 help="attach to allocator functions in the specified object")
Nathan Scottcf0792f2018-02-02 16:56:50 +1100101parser.add_argument("--ebpf", action="store_true",
102 help=argparse.SUPPRESS)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800103
104args = parser.parse_args()
105
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800106pid = args.pid
Sasha Goldshtein29228612016-02-07 12:20:19 -0800107command = args.command
108kernel_trace = (pid == -1 and command is None)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800109trace_all = args.trace
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800110interval = args.interval
111min_age_ns = 1e6 * args.older
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800112sample_every_n = args.sample_rate
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800113num_prints = args.count
Sasha Goldshteinc8148c82016-02-09 11:15:41 -0800114top_stacks = args.top
Sasha Goldshtein50459642016-02-10 08:35:20 -0800115min_size = args.min_size
116max_size = args.max_size
Maria Kacik9389ab42017-01-18 21:43:41 -0800117obj = args.obj
Sasha Goldshtein50459642016-02-10 08:35:20 -0800118
119if min_size is not None and max_size is not None and min_size > max_size:
120 print("min_size (-z) can't be greater than max_size (-Z)")
121 exit(1)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800122
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800123if command is not None:
124 print("Executing '%s' and tracing the resulting process." % command)
125 pid = run_command_get_pid(command)
Sasha Goldshtein29228612016-02-07 12:20:19 -0800126
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800127bpf_source = """
128#include <uapi/linux/ptrace.h>
129
130struct alloc_info_t {
131 u64 size;
132 u64 timestamp_ns;
Vicent Martie25ae032016-03-25 17:14:34 +0100133 int stack_id;
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800134};
135
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300136struct combined_alloc_info_t {
137 u64 total_size;
138 u64 number_of_allocs;
139};
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800140
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300141BPF_HASH(sizes, u64);
142BPF_TABLE("hash", u64, struct alloc_info_t, allocs, 1000000);
143BPF_HASH(memptrs, u64, u64);
Song Liu67ae6052018-02-01 14:59:24 -0800144BPF_STACK_TRACE(stack_traces, 10240);
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300145BPF_TABLE("hash", u64, struct combined_alloc_info_t, combined_allocs, 10240);
146
147static inline void update_statistics_add(u64 stack_id, u64 sz) {
148 struct combined_alloc_info_t *existing_cinfo;
149 struct combined_alloc_info_t cinfo = {0};
150
151 existing_cinfo = combined_allocs.lookup(&stack_id);
152 if (existing_cinfo != 0)
153 cinfo = *existing_cinfo;
154
155 cinfo.total_size += sz;
156 cinfo.number_of_allocs += 1;
157
158 combined_allocs.update(&stack_id, &cinfo);
159}
160
161static inline void update_statistics_del(u64 stack_id, u64 sz) {
162 struct combined_alloc_info_t *existing_cinfo;
163 struct combined_alloc_info_t cinfo = {0};
164
165 existing_cinfo = combined_allocs.lookup(&stack_id);
166 if (existing_cinfo != 0)
167 cinfo = *existing_cinfo;
168
169 if (sz >= cinfo.total_size)
170 cinfo.total_size = 0;
171 else
172 cinfo.total_size -= sz;
173
174 if (cinfo.number_of_allocs > 0)
175 cinfo.number_of_allocs -= 1;
176
177 combined_allocs.update(&stack_id, &cinfo);
178}
179
180static inline int gen_alloc_enter(struct pt_regs *ctx, size_t size) {
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800181 SIZE_FILTER
182 if (SAMPLE_EVERY_N > 1) {
183 u64 ts = bpf_ktime_get_ns();
184 if (ts % SAMPLE_EVERY_N != 0)
185 return 0;
186 }
187
188 u64 pid = bpf_get_current_pid_tgid();
189 u64 size64 = size;
190 sizes.update(&pid, &size64);
191
192 if (SHOULD_PRINT)
193 bpf_trace_printk("alloc entered, size = %u\\n", size);
194 return 0;
195}
196
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300197static inline int gen_alloc_exit2(struct pt_regs *ctx, u64 address) {
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800198 u64 pid = bpf_get_current_pid_tgid();
199 u64* size64 = sizes.lookup(&pid);
200 struct alloc_info_t info = {0};
201
202 if (size64 == 0)
203 return 0; // missed alloc entry
204
205 info.size = *size64;
206 sizes.delete(&pid);
207
208 info.timestamp_ns = bpf_ktime_get_ns();
Vicent Martie25ae032016-03-25 17:14:34 +0100209 info.stack_id = stack_traces.get_stackid(ctx, STACK_FLAGS);
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800210 allocs.update(&address, &info);
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300211 update_statistics_add(info.stack_id, info.size);
Sasha Goldshtein0e856f42016-03-21 07:26:52 -0700212
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800213 if (SHOULD_PRINT) {
Vicent Martie25ae032016-03-25 17:14:34 +0100214 bpf_trace_printk("alloc exited, size = %lu, result = %lx\\n",
215 info.size, address);
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800216 }
217 return 0;
218}
219
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300220static inline int gen_alloc_exit(struct pt_regs *ctx) {
221 return gen_alloc_exit2(ctx, PT_REGS_RC(ctx));
222}
223
224static inline int gen_free_enter(struct pt_regs *ctx, void *address) {
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800225 u64 addr = (u64)address;
226 struct alloc_info_t *info = allocs.lookup(&addr);
227 if (info == 0)
228 return 0;
229
230 allocs.delete(&addr);
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300231 update_statistics_del(info->stack_id, info->size);
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800232
233 if (SHOULD_PRINT) {
234 bpf_trace_printk("free entered, address = %lx, size = %lu\\n",
235 address, info->size);
236 }
237 return 0;
238}
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300239
240int malloc_enter(struct pt_regs *ctx, size_t size) {
241 return gen_alloc_enter(ctx, size);
242}
243
244int malloc_exit(struct pt_regs *ctx) {
245 return gen_alloc_exit(ctx);
246}
247
248int free_enter(struct pt_regs *ctx, void *address) {
249 return gen_free_enter(ctx, address);
250}
251
252int calloc_enter(struct pt_regs *ctx, size_t nmemb, size_t size) {
253 return gen_alloc_enter(ctx, nmemb * size);
254}
255
256int calloc_exit(struct pt_regs *ctx) {
257 return gen_alloc_exit(ctx);
258}
259
260int realloc_enter(struct pt_regs *ctx, void *ptr, size_t size) {
261 gen_free_enter(ctx, ptr);
262 return gen_alloc_enter(ctx, size);
263}
264
265int realloc_exit(struct pt_regs *ctx) {
266 return gen_alloc_exit(ctx);
267}
268
269int posix_memalign_enter(struct pt_regs *ctx, void **memptr, size_t alignment,
270 size_t size) {
271 u64 memptr64 = (u64)(size_t)memptr;
272 u64 pid = bpf_get_current_pid_tgid();
273
274 memptrs.update(&pid, &memptr64);
275 return gen_alloc_enter(ctx, size);
276}
277
278int posix_memalign_exit(struct pt_regs *ctx) {
279 u64 pid = bpf_get_current_pid_tgid();
280 u64 *memptr64 = memptrs.lookup(&pid);
281 void *addr;
282
283 if (memptr64 == 0)
284 return 0;
285
286 memptrs.delete(&pid);
287
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200288 if (bpf_probe_read(&addr, sizeof(void*), (void*)(size_t)*memptr64))
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300289 return 0;
290
291 u64 addr64 = (u64)(size_t)addr;
292 return gen_alloc_exit2(ctx, addr64);
293}
294
295int aligned_alloc_enter(struct pt_regs *ctx, size_t alignment, size_t size) {
296 return gen_alloc_enter(ctx, size);
297}
298
299int aligned_alloc_exit(struct pt_regs *ctx) {
300 return gen_alloc_exit(ctx);
301}
302
303int valloc_enter(struct pt_regs *ctx, size_t size) {
304 return gen_alloc_enter(ctx, size);
305}
306
307int valloc_exit(struct pt_regs *ctx) {
308 return gen_alloc_exit(ctx);
309}
310
311int memalign_enter(struct pt_regs *ctx, size_t alignment, size_t size) {
312 return gen_alloc_enter(ctx, size);
313}
314
315int memalign_exit(struct pt_regs *ctx) {
316 return gen_alloc_exit(ctx);
317}
318
319int pvalloc_enter(struct pt_regs *ctx, size_t size) {
320 return gen_alloc_enter(ctx, size);
321}
322
323int pvalloc_exit(struct pt_regs *ctx) {
324 return gen_alloc_exit(ctx);
325}
Sasha Goldshtein0e856f42016-03-21 07:26:52 -0700326"""
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300327
328bpf_source_kernel = """
329
330TRACEPOINT_PROBE(kmem, kmalloc) {
331 gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc);
332 return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr);
333}
334
335TRACEPOINT_PROBE(kmem, kmalloc_node) {
336 gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc);
337 return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr);
338}
339
340TRACEPOINT_PROBE(kmem, kfree) {
341 return gen_free_enter((struct pt_regs *)args, (void *)args->ptr);
342}
343
344TRACEPOINT_PROBE(kmem, kmem_cache_alloc) {
345 gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc);
346 return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr);
347}
348
349TRACEPOINT_PROBE(kmem, kmem_cache_alloc_node) {
350 gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc);
351 return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr);
352}
353
354TRACEPOINT_PROBE(kmem, kmem_cache_free) {
355 return gen_free_enter((struct pt_regs *)args, (void *)args->ptr);
356}
357
358TRACEPOINT_PROBE(kmem, mm_page_alloc) {
359 gen_alloc_enter((struct pt_regs *)args, PAGE_SIZE << args->order);
360 return gen_alloc_exit2((struct pt_regs *)args, args->pfn);
361}
362
363TRACEPOINT_PROBE(kmem, mm_page_free) {
364 return gen_free_enter((struct pt_regs *)args, (void *)args->pfn);
365}
366"""
367
368if kernel_trace:
369 bpf_source += bpf_source_kernel
370
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800371bpf_source = bpf_source.replace("SHOULD_PRINT", "1" if trace_all else "0")
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800372bpf_source = bpf_source.replace("SAMPLE_EVERY_N", str(sample_every_n))
Yonghong Songeb6ddc02017-10-26 22:33:24 -0700373bpf_source = bpf_source.replace("PAGE_SIZE", str(resource.getpagesize()))
Sasha Goldshtein50459642016-02-10 08:35:20 -0800374
375size_filter = ""
376if min_size is not None and max_size is not None:
377 size_filter = "if (size < %d || size > %d) return 0;" % \
378 (min_size, max_size)
379elif min_size is not None:
380 size_filter = "if (size < %d) return 0;" % min_size
381elif max_size is not None:
382 size_filter = "if (size > %d) return 0;" % max_size
383bpf_source = bpf_source.replace("SIZE_FILTER", size_filter)
384
Vicent Martie25ae032016-03-25 17:14:34 +0100385stack_flags = "BPF_F_REUSE_STACKID"
386if not kernel_trace:
387 stack_flags += "|BPF_F_USER_STACK"
388bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags)
389
Nathan Scottcf0792f2018-02-02 16:56:50 +1100390if args.ebpf:
391 print(bpf_source)
392 exit()
393
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200394bpf = BPF(text=bpf_source)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800395
396if not kernel_trace:
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300397 print("Attaching to pid %d, Ctrl+C to quit." % pid)
398
399 def attach_probes(sym, fn_prefix=None, can_fail=False):
400 if fn_prefix is None:
401 fn_prefix = sym
402
403 try:
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200404 bpf.attach_uprobe(name=obj, sym=sym,
405 fn_name=fn_prefix + "_enter",
406 pid=pid)
407 bpf.attach_uretprobe(name=obj, sym=sym,
408 fn_name=fn_prefix + "_exit",
409 pid=pid)
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300410 except Exception:
411 if can_fail:
412 return
413 else:
414 raise
415
416 attach_probes("malloc")
417 attach_probes("calloc")
418 attach_probes("realloc")
419 attach_probes("posix_memalign")
420 attach_probes("valloc")
421 attach_probes("memalign")
422 attach_probes("pvalloc")
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200423 attach_probes("aligned_alloc", can_fail=True) # added in C11
424 bpf.attach_uprobe(name=obj, sym="free", fn_name="free_enter",
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300425 pid=pid)
426
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800427else:
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300428 print("Attaching to kernel allocators, Ctrl+C to quit.")
429
430 # No probe attaching here. Allocations are counted by attaching to
431 # tracepoints.
432 #
433 # Memory allocations in Linux kernel are not limited to malloc/free
434 # equivalents. It's also common to allocate a memory page or multiple
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200435 # pages. Page allocator have two interfaces, one working with page
436 # frame numbers (PFN), while other working with page addresses. It's
437 # possible to allocate pages with one kind of functions, and free them
438 # with another. Code in kernel can easy convert PFNs to addresses and
439 # back, but it's hard to do the same in eBPF kprobe without fragile
440 # hacks.
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300441 #
442 # Fortunately, Linux exposes tracepoints for memory allocations, which
443 # can be instrumented by eBPF programs. Tracepoint for page allocations
444 # gives access to PFNs for both allocator interfaces. So there is no
445 # need to guess which allocation corresponds to which free.
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800446
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800447def print_outstanding():
Sasha Goldshteinc8148c82016-02-09 11:15:41 -0800448 print("[%s] Top %d stacks with outstanding allocations:" %
449 (datetime.now().strftime("%H:%M:%S"), top_stacks))
Vicent Martie25ae032016-03-25 17:14:34 +0100450 alloc_info = {}
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200451 allocs = bpf["allocs"]
452 stack_traces = bpf["stack_traces"]
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800453 for address, info in sorted(allocs.items(), key=lambda a: a[1].size):
Sasha Goldshtein60c41922017-02-09 04:19:53 -0500454 if BPF.monotonic_time() - min_age_ns < info.timestamp_ns:
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800455 continue
Vicent Martie25ae032016-03-25 17:14:34 +0100456 if info.stack_id < 0:
457 continue
458 if info.stack_id in alloc_info:
459 alloc_info[info.stack_id].update(info.size)
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800460 else:
Sasha Goldshtein49df9942017-02-08 23:22:06 -0500461 stack = list(stack_traces.walk(info.stack_id))
462 combined = []
463 for addr in stack:
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200464 combined.append(bpf.sym(addr, pid,
Sasha Goldshtein01553852017-02-09 03:58:09 -0500465 show_module=True, show_offset=True))
Sasha Goldshtein49df9942017-02-08 23:22:06 -0500466 alloc_info[info.stack_id] = Allocation(combined,
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300467 info.size)
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800468 if args.show_allocs:
469 print("\taddr = %x size = %s" %
470 (address.value, info.size))
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300471 to_show = sorted(alloc_info.values(),
472 key=lambda a: a.size)[-top_stacks:]
Vicent Martie25ae032016-03-25 17:14:34 +0100473 for alloc in to_show:
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800474 print("\t%d bytes in %d allocations from stack\n\t\t%s" %
Brenden Blanco42d60982017-04-24 14:31:28 -0700475 (alloc.size, alloc.count, b"\n\t\t".join(alloc.stack)))
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800476
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300477def print_outstanding_combined():
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200478 stack_traces = bpf["stack_traces"]
479 stacks = sorted(bpf["combined_allocs"].items(),
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300480 key=lambda a: -a[1].total_size)
481 cnt = 1
482 entries = []
483 for stack_id, info in stacks:
484 try:
485 trace = []
486 for addr in stack_traces.walk(stack_id.value):
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200487 sym = bpf.sym(addr, pid,
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300488 show_module=True,
489 show_offset=True)
490 trace.append(sym)
491 trace = "\n\t\t".join(trace)
492 except KeyError:
493 trace = "stack information lost"
494
495 entry = ("\t%d bytes in %d allocations from stack\n\t\t%s" %
496 (info.total_size, info.number_of_allocs, trace))
497 entries.append(entry)
498
499 cnt += 1
500 if cnt > top_stacks:
501 break
502
503 print("[%s] Top %d stacks with outstanding allocations:" %
504 (datetime.now().strftime("%H:%M:%S"), top_stacks))
505
506 print('\n'.join(reversed(entries)))
507
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800508count_so_far = 0
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800509while True:
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800510 if trace_all:
Paul Chaignon2e07ddc2017-10-07 11:07:10 +0200511 print(bpf.trace_fields())
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800512 else:
513 try:
514 sleep(interval)
515 except KeyboardInterrupt:
516 exit()
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300517 if args.combined_only:
518 print_outstanding_combined()
519 else:
520 print_outstanding()
Brenden Blancoa296e1e2018-08-08 21:00:18 -0700521 sys.stdout.flush()
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800522 count_so_far += 1
523 if num_prints is not None and count_so_far >= num_prints:
524 exit()