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] |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 7 | # [--combined-only] [-s SAMPLE_RATE] [-T TOP] [-z MIN_SIZE] |
| 8 | # [-Z MAX_SIZE] [-O OBJ] |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 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 | 49df994 | 2017-02-08 23:22:06 -0500 | [diff] [blame] | 14 | from bcc import BPF |
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 |
Yonghong Song | eb6ddc0 | 2017-10-26 22:33:24 -0700 | [diff] [blame] | 17 | import resource |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 18 | import argparse |
| 19 | import subprocess |
Sasha Goldshtein | cfce311 | 2016-02-07 11:09:36 -0800 | [diff] [blame] | 20 | import os |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 21 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 22 | class Allocation(object): |
| 23 | def __init__(self, stack, size): |
| 24 | self.stack = stack |
| 25 | self.count = 1 |
| 26 | self.size = size |
| 27 | |
| 28 | def update(self, size): |
| 29 | self.count += 1 |
| 30 | self.size += size |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 31 | |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 32 | def run_command_get_output(command): |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 33 | p = subprocess.Popen(command.split(), |
| 34 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 35 | return iter(p.stdout.readline, b'') |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 36 | |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 37 | def run_command_get_pid(command): |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 38 | p = subprocess.Popen(command.split()) |
| 39 | return p.pid |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 40 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 41 | examples = """ |
| 42 | EXAMPLES: |
| 43 | |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 44 | ./memleak -p $(pidof allocs) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 45 | Trace allocations and display a summary of "leaked" (outstanding) |
| 46 | allocations every 5 seconds |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 47 | ./memleak -p $(pidof allocs) -t |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 48 | Trace allocations and display each individual allocator function call |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 49 | ./memleak -ap $(pidof allocs) 10 |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 50 | Trace allocations and display allocated addresses, sizes, and stacks |
| 51 | every 10 seconds for outstanding allocations |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 52 | ./memleak -c "./allocs" |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 53 | Run the specified command and trace its allocations |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 54 | ./memleak |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 55 | Trace allocations in kernel mode and display a summary of outstanding |
| 56 | allocations every 5 seconds |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 57 | ./memleak -o 60000 |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 58 | Trace allocations in kernel mode and display a summary of outstanding |
| 59 | allocations that are at least one minute (60 seconds) old |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 60 | ./memleak -s 5 |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 61 | Trace roughly every 5th allocation, to reduce overhead |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 62 | """ |
| 63 | |
| 64 | description = """ |
| 65 | Trace outstanding memory allocations that weren't freed. |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 66 | Supports both user-mode allocations made with libc functions and kernel-mode |
| 67 | allocations made with kmalloc/kmem_cache_alloc/get_free_pages and corresponding |
| 68 | memory release functions. |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 69 | """ |
| 70 | |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 71 | parser = argparse.ArgumentParser(description=description, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 72 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 73 | epilog=examples) |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 74 | parser.add_argument("-p", "--pid", type=int, default=-1, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 75 | help="the PID to trace; if not specified, trace kernel allocs") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 76 | parser.add_argument("-t", "--trace", action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 77 | help="print trace messages for each alloc/free call") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 78 | parser.add_argument("interval", nargs="?", default=5, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 79 | help="interval in seconds to print outstanding allocations") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 80 | parser.add_argument("count", nargs="?", type=int, |
| 81 | help="number of times to print the report before exiting") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 82 | parser.add_argument("-a", "--show-allocs", default=False, action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 83 | help="show allocation addresses and sizes as well as call stacks") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 84 | parser.add_argument("-o", "--older", default=500, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 85 | help="prune allocations younger than this age in milliseconds") |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 86 | parser.add_argument("-c", "--command", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 87 | help="execute and trace the specified command") |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 88 | parser.add_argument("--combined-only", default=False, action="store_true", |
| 89 | help="show combined allocation statistics only") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 90 | parser.add_argument("-s", "--sample-rate", default=1, type=int, |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 91 | help="sample every N-th allocation to decrease the overhead") |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 92 | parser.add_argument("-T", "--top", type=int, default=10, |
| 93 | help="display only this many top allocating stacks (by size)") |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 94 | parser.add_argument("-z", "--min-size", type=int, |
| 95 | help="capture only allocations larger than this size") |
| 96 | parser.add_argument("-Z", "--max-size", type=int, |
| 97 | help="capture only allocations smaller than this size") |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 98 | parser.add_argument("-O", "--obj", type=str, default="c", |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 99 | help="attach to allocator functions in the specified object") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 100 | parser.add_argument("--ebpf", action="store_true", |
| 101 | help=argparse.SUPPRESS) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 102 | |
| 103 | args = parser.parse_args() |
| 104 | |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 105 | pid = args.pid |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 106 | command = args.command |
| 107 | kernel_trace = (pid == -1 and command is None) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 108 | trace_all = args.trace |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 109 | interval = args.interval |
| 110 | min_age_ns = 1e6 * args.older |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 111 | sample_every_n = args.sample_rate |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 112 | num_prints = args.count |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 113 | top_stacks = args.top |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 114 | min_size = args.min_size |
| 115 | max_size = args.max_size |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 116 | obj = args.obj |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 117 | |
| 118 | if min_size is not None and max_size is not None and min_size > max_size: |
| 119 | print("min_size (-z) can't be greater than max_size (-Z)") |
| 120 | exit(1) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 121 | |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 122 | if command is not None: |
| 123 | print("Executing '%s' and tracing the resulting process." % command) |
| 124 | pid = run_command_get_pid(command) |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 125 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 126 | bpf_source = """ |
| 127 | #include <uapi/linux/ptrace.h> |
| 128 | |
| 129 | struct alloc_info_t { |
| 130 | u64 size; |
| 131 | u64 timestamp_ns; |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 132 | int stack_id; |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 135 | struct combined_alloc_info_t { |
| 136 | u64 total_size; |
| 137 | u64 number_of_allocs; |
| 138 | }; |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 139 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 140 | BPF_HASH(sizes, u64); |
| 141 | BPF_TABLE("hash", u64, struct alloc_info_t, allocs, 1000000); |
| 142 | BPF_HASH(memptrs, u64, u64); |
Song Liu | 67ae605 | 2018-02-01 14:59:24 -0800 | [diff] [blame] | 143 | BPF_STACK_TRACE(stack_traces, 10240); |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 144 | BPF_TABLE("hash", u64, struct combined_alloc_info_t, combined_allocs, 10240); |
| 145 | |
| 146 | static inline void update_statistics_add(u64 stack_id, u64 sz) { |
| 147 | struct combined_alloc_info_t *existing_cinfo; |
| 148 | struct combined_alloc_info_t cinfo = {0}; |
| 149 | |
| 150 | existing_cinfo = combined_allocs.lookup(&stack_id); |
| 151 | if (existing_cinfo != 0) |
| 152 | cinfo = *existing_cinfo; |
| 153 | |
| 154 | cinfo.total_size += sz; |
| 155 | cinfo.number_of_allocs += 1; |
| 156 | |
| 157 | combined_allocs.update(&stack_id, &cinfo); |
| 158 | } |
| 159 | |
| 160 | static inline void update_statistics_del(u64 stack_id, u64 sz) { |
| 161 | struct combined_alloc_info_t *existing_cinfo; |
| 162 | struct combined_alloc_info_t cinfo = {0}; |
| 163 | |
| 164 | existing_cinfo = combined_allocs.lookup(&stack_id); |
| 165 | if (existing_cinfo != 0) |
| 166 | cinfo = *existing_cinfo; |
| 167 | |
| 168 | if (sz >= cinfo.total_size) |
| 169 | cinfo.total_size = 0; |
| 170 | else |
| 171 | cinfo.total_size -= sz; |
| 172 | |
| 173 | if (cinfo.number_of_allocs > 0) |
| 174 | cinfo.number_of_allocs -= 1; |
| 175 | |
| 176 | combined_allocs.update(&stack_id, &cinfo); |
| 177 | } |
| 178 | |
| 179 | static inline int gen_alloc_enter(struct pt_regs *ctx, size_t size) { |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 180 | SIZE_FILTER |
| 181 | if (SAMPLE_EVERY_N > 1) { |
| 182 | u64 ts = bpf_ktime_get_ns(); |
| 183 | if (ts % SAMPLE_EVERY_N != 0) |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | u64 pid = bpf_get_current_pid_tgid(); |
| 188 | u64 size64 = size; |
| 189 | sizes.update(&pid, &size64); |
| 190 | |
| 191 | if (SHOULD_PRINT) |
| 192 | bpf_trace_printk("alloc entered, size = %u\\n", size); |
| 193 | return 0; |
| 194 | } |
| 195 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 196 | static inline int gen_alloc_exit2(struct pt_regs *ctx, u64 address) { |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 197 | u64 pid = bpf_get_current_pid_tgid(); |
| 198 | u64* size64 = sizes.lookup(&pid); |
| 199 | struct alloc_info_t info = {0}; |
| 200 | |
| 201 | if (size64 == 0) |
| 202 | return 0; // missed alloc entry |
| 203 | |
| 204 | info.size = *size64; |
| 205 | sizes.delete(&pid); |
| 206 | |
| 207 | info.timestamp_ns = bpf_ktime_get_ns(); |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 208 | info.stack_id = stack_traces.get_stackid(ctx, STACK_FLAGS); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 209 | allocs.update(&address, &info); |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 210 | update_statistics_add(info.stack_id, info.size); |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 211 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 212 | if (SHOULD_PRINT) { |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 213 | bpf_trace_printk("alloc exited, size = %lu, result = %lx\\n", |
| 214 | info.size, address); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 219 | static inline int gen_alloc_exit(struct pt_regs *ctx) { |
| 220 | return gen_alloc_exit2(ctx, PT_REGS_RC(ctx)); |
| 221 | } |
| 222 | |
| 223 | static inline int gen_free_enter(struct pt_regs *ctx, void *address) { |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 224 | u64 addr = (u64)address; |
| 225 | struct alloc_info_t *info = allocs.lookup(&addr); |
| 226 | if (info == 0) |
| 227 | return 0; |
| 228 | |
| 229 | allocs.delete(&addr); |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 230 | update_statistics_del(info->stack_id, info->size); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 231 | |
| 232 | if (SHOULD_PRINT) { |
| 233 | bpf_trace_printk("free entered, address = %lx, size = %lu\\n", |
| 234 | address, info->size); |
| 235 | } |
| 236 | return 0; |
| 237 | } |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 238 | |
| 239 | int malloc_enter(struct pt_regs *ctx, size_t size) { |
| 240 | return gen_alloc_enter(ctx, size); |
| 241 | } |
| 242 | |
| 243 | int malloc_exit(struct pt_regs *ctx) { |
| 244 | return gen_alloc_exit(ctx); |
| 245 | } |
| 246 | |
| 247 | int free_enter(struct pt_regs *ctx, void *address) { |
| 248 | return gen_free_enter(ctx, address); |
| 249 | } |
| 250 | |
| 251 | int calloc_enter(struct pt_regs *ctx, size_t nmemb, size_t size) { |
| 252 | return gen_alloc_enter(ctx, nmemb * size); |
| 253 | } |
| 254 | |
| 255 | int calloc_exit(struct pt_regs *ctx) { |
| 256 | return gen_alloc_exit(ctx); |
| 257 | } |
| 258 | |
| 259 | int realloc_enter(struct pt_regs *ctx, void *ptr, size_t size) { |
| 260 | gen_free_enter(ctx, ptr); |
| 261 | return gen_alloc_enter(ctx, size); |
| 262 | } |
| 263 | |
| 264 | int realloc_exit(struct pt_regs *ctx) { |
| 265 | return gen_alloc_exit(ctx); |
| 266 | } |
| 267 | |
| 268 | int posix_memalign_enter(struct pt_regs *ctx, void **memptr, size_t alignment, |
| 269 | size_t size) { |
| 270 | u64 memptr64 = (u64)(size_t)memptr; |
| 271 | u64 pid = bpf_get_current_pid_tgid(); |
| 272 | |
| 273 | memptrs.update(&pid, &memptr64); |
| 274 | return gen_alloc_enter(ctx, size); |
| 275 | } |
| 276 | |
| 277 | int posix_memalign_exit(struct pt_regs *ctx) { |
| 278 | u64 pid = bpf_get_current_pid_tgid(); |
| 279 | u64 *memptr64 = memptrs.lookup(&pid); |
| 280 | void *addr; |
| 281 | |
| 282 | if (memptr64 == 0) |
| 283 | return 0; |
| 284 | |
| 285 | memptrs.delete(&pid); |
| 286 | |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 287 | if (bpf_probe_read(&addr, sizeof(void*), (void*)(size_t)*memptr64)) |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 288 | return 0; |
| 289 | |
| 290 | u64 addr64 = (u64)(size_t)addr; |
| 291 | return gen_alloc_exit2(ctx, addr64); |
| 292 | } |
| 293 | |
| 294 | int aligned_alloc_enter(struct pt_regs *ctx, size_t alignment, size_t size) { |
| 295 | return gen_alloc_enter(ctx, size); |
| 296 | } |
| 297 | |
| 298 | int aligned_alloc_exit(struct pt_regs *ctx) { |
| 299 | return gen_alloc_exit(ctx); |
| 300 | } |
| 301 | |
| 302 | int valloc_enter(struct pt_regs *ctx, size_t size) { |
| 303 | return gen_alloc_enter(ctx, size); |
| 304 | } |
| 305 | |
| 306 | int valloc_exit(struct pt_regs *ctx) { |
| 307 | return gen_alloc_exit(ctx); |
| 308 | } |
| 309 | |
| 310 | int memalign_enter(struct pt_regs *ctx, size_t alignment, size_t size) { |
| 311 | return gen_alloc_enter(ctx, size); |
| 312 | } |
| 313 | |
| 314 | int memalign_exit(struct pt_regs *ctx) { |
| 315 | return gen_alloc_exit(ctx); |
| 316 | } |
| 317 | |
| 318 | int pvalloc_enter(struct pt_regs *ctx, size_t size) { |
| 319 | return gen_alloc_enter(ctx, size); |
| 320 | } |
| 321 | |
| 322 | int pvalloc_exit(struct pt_regs *ctx) { |
| 323 | return gen_alloc_exit(ctx); |
| 324 | } |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 325 | """ |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 326 | |
| 327 | bpf_source_kernel = """ |
| 328 | |
| 329 | TRACEPOINT_PROBE(kmem, kmalloc) { |
| 330 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 331 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 332 | } |
| 333 | |
| 334 | TRACEPOINT_PROBE(kmem, kmalloc_node) { |
| 335 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 336 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 337 | } |
| 338 | |
| 339 | TRACEPOINT_PROBE(kmem, kfree) { |
| 340 | return gen_free_enter((struct pt_regs *)args, (void *)args->ptr); |
| 341 | } |
| 342 | |
| 343 | TRACEPOINT_PROBE(kmem, kmem_cache_alloc) { |
| 344 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 345 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 346 | } |
| 347 | |
| 348 | TRACEPOINT_PROBE(kmem, kmem_cache_alloc_node) { |
| 349 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 350 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 351 | } |
| 352 | |
| 353 | TRACEPOINT_PROBE(kmem, kmem_cache_free) { |
| 354 | return gen_free_enter((struct pt_regs *)args, (void *)args->ptr); |
| 355 | } |
| 356 | |
| 357 | TRACEPOINT_PROBE(kmem, mm_page_alloc) { |
| 358 | gen_alloc_enter((struct pt_regs *)args, PAGE_SIZE << args->order); |
| 359 | return gen_alloc_exit2((struct pt_regs *)args, args->pfn); |
| 360 | } |
| 361 | |
| 362 | TRACEPOINT_PROBE(kmem, mm_page_free) { |
| 363 | return gen_free_enter((struct pt_regs *)args, (void *)args->pfn); |
| 364 | } |
| 365 | """ |
| 366 | |
| 367 | if kernel_trace: |
| 368 | bpf_source += bpf_source_kernel |
| 369 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 370 | 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] | 371 | bpf_source = bpf_source.replace("SAMPLE_EVERY_N", str(sample_every_n)) |
Yonghong Song | eb6ddc0 | 2017-10-26 22:33:24 -0700 | [diff] [blame] | 372 | bpf_source = bpf_source.replace("PAGE_SIZE", str(resource.getpagesize())) |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 373 | |
| 374 | size_filter = "" |
| 375 | if min_size is not None and max_size is not None: |
| 376 | size_filter = "if (size < %d || size > %d) return 0;" % \ |
| 377 | (min_size, max_size) |
| 378 | elif min_size is not None: |
| 379 | size_filter = "if (size < %d) return 0;" % min_size |
| 380 | elif max_size is not None: |
| 381 | size_filter = "if (size > %d) return 0;" % max_size |
| 382 | bpf_source = bpf_source.replace("SIZE_FILTER", size_filter) |
| 383 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 384 | stack_flags = "BPF_F_REUSE_STACKID" |
| 385 | if not kernel_trace: |
| 386 | stack_flags += "|BPF_F_USER_STACK" |
| 387 | bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags) |
| 388 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 389 | if args.ebpf: |
| 390 | print(bpf_source) |
| 391 | exit() |
| 392 | |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 393 | bpf = BPF(text=bpf_source) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 394 | |
| 395 | if not kernel_trace: |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 396 | print("Attaching to pid %d, Ctrl+C to quit." % pid) |
| 397 | |
| 398 | def attach_probes(sym, fn_prefix=None, can_fail=False): |
| 399 | if fn_prefix is None: |
| 400 | fn_prefix = sym |
| 401 | |
| 402 | try: |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 403 | bpf.attach_uprobe(name=obj, sym=sym, |
| 404 | fn_name=fn_prefix + "_enter", |
| 405 | pid=pid) |
| 406 | bpf.attach_uretprobe(name=obj, sym=sym, |
| 407 | fn_name=fn_prefix + "_exit", |
| 408 | pid=pid) |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 409 | except Exception: |
| 410 | if can_fail: |
| 411 | return |
| 412 | else: |
| 413 | raise |
| 414 | |
| 415 | attach_probes("malloc") |
| 416 | attach_probes("calloc") |
| 417 | attach_probes("realloc") |
| 418 | attach_probes("posix_memalign") |
| 419 | attach_probes("valloc") |
| 420 | attach_probes("memalign") |
| 421 | attach_probes("pvalloc") |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 422 | attach_probes("aligned_alloc", can_fail=True) # added in C11 |
| 423 | bpf.attach_uprobe(name=obj, sym="free", fn_name="free_enter", |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 424 | pid=pid) |
| 425 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 426 | else: |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 427 | print("Attaching to kernel allocators, Ctrl+C to quit.") |
| 428 | |
| 429 | # No probe attaching here. Allocations are counted by attaching to |
| 430 | # tracepoints. |
| 431 | # |
| 432 | # Memory allocations in Linux kernel are not limited to malloc/free |
| 433 | # equivalents. It's also common to allocate a memory page or multiple |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 434 | # pages. Page allocator have two interfaces, one working with page |
| 435 | # frame numbers (PFN), while other working with page addresses. It's |
| 436 | # possible to allocate pages with one kind of functions, and free them |
| 437 | # with another. Code in kernel can easy convert PFNs to addresses and |
| 438 | # back, but it's hard to do the same in eBPF kprobe without fragile |
| 439 | # hacks. |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 440 | # |
| 441 | # Fortunately, Linux exposes tracepoints for memory allocations, which |
| 442 | # can be instrumented by eBPF programs. Tracepoint for page allocations |
| 443 | # gives access to PFNs for both allocator interfaces. So there is no |
| 444 | # need to guess which allocation corresponds to which free. |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 445 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 446 | def print_outstanding(): |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 447 | print("[%s] Top %d stacks with outstanding allocations:" % |
| 448 | (datetime.now().strftime("%H:%M:%S"), top_stacks)) |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 449 | alloc_info = {} |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 450 | allocs = bpf["allocs"] |
| 451 | stack_traces = bpf["stack_traces"] |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 452 | 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] | 453 | if BPF.monotonic_time() - min_age_ns < info.timestamp_ns: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 454 | continue |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 455 | if info.stack_id < 0: |
| 456 | continue |
| 457 | if info.stack_id in alloc_info: |
| 458 | alloc_info[info.stack_id].update(info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 459 | else: |
Sasha Goldshtein | 49df994 | 2017-02-08 23:22:06 -0500 | [diff] [blame] | 460 | stack = list(stack_traces.walk(info.stack_id)) |
| 461 | combined = [] |
| 462 | for addr in stack: |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 463 | combined.append(bpf.sym(addr, pid, |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 464 | show_module=True, show_offset=True)) |
Sasha Goldshtein | 49df994 | 2017-02-08 23:22:06 -0500 | [diff] [blame] | 465 | alloc_info[info.stack_id] = Allocation(combined, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 466 | info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 467 | if args.show_allocs: |
| 468 | print("\taddr = %x size = %s" % |
| 469 | (address.value, info.size)) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 470 | to_show = sorted(alloc_info.values(), |
| 471 | key=lambda a: a.size)[-top_stacks:] |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 472 | for alloc in to_show: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 473 | 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] | 474 | (alloc.size, alloc.count, "\n\t\t".join(alloc.stack))) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 475 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 476 | def print_outstanding_combined(): |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 477 | stack_traces = bpf["stack_traces"] |
| 478 | stacks = sorted(bpf["combined_allocs"].items(), |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 479 | key=lambda a: -a[1].total_size) |
| 480 | cnt = 1 |
| 481 | entries = [] |
| 482 | for stack_id, info in stacks: |
| 483 | try: |
| 484 | trace = [] |
| 485 | for addr in stack_traces.walk(stack_id.value): |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 486 | sym = bpf.sym(addr, pid, |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 487 | show_module=True, |
| 488 | show_offset=True) |
| 489 | trace.append(sym) |
| 490 | trace = "\n\t\t".join(trace) |
| 491 | except KeyError: |
| 492 | trace = "stack information lost" |
| 493 | |
| 494 | entry = ("\t%d bytes in %d allocations from stack\n\t\t%s" % |
| 495 | (info.total_size, info.number_of_allocs, trace)) |
| 496 | entries.append(entry) |
| 497 | |
| 498 | cnt += 1 |
| 499 | if cnt > top_stacks: |
| 500 | break |
| 501 | |
| 502 | print("[%s] Top %d stacks with outstanding allocations:" % |
| 503 | (datetime.now().strftime("%H:%M:%S"), top_stacks)) |
| 504 | |
| 505 | print('\n'.join(reversed(entries))) |
| 506 | |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 507 | count_so_far = 0 |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 508 | while True: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 509 | if trace_all: |
Paul Chaignon | 2e07ddc | 2017-10-07 11:07:10 +0200 | [diff] [blame] | 510 | print(bpf.trace_fields()) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 511 | else: |
| 512 | try: |
| 513 | sleep(interval) |
| 514 | except KeyboardInterrupt: |
| 515 | exit() |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame] | 516 | if args.combined_only: |
| 517 | print_outstanding_combined() |
| 518 | else: |
| 519 | print_outstanding() |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 520 | count_so_far += 1 |
| 521 | if num_prints is not None and count_so_far >= num_prints: |
| 522 | exit() |