blob: 421801702c11b66195c1728f562ac09c6306412c [file] [log] [blame]
Sasha Goldshteindda47692016-02-08 03:10:13 -08001Demonstrations of memleak.
2
3
4memleak traces and matches memory allocation and deallocation requests, and
5collects call stacks for each allocation. memleak can then print a summary
6of which call stacks performed allocations that weren't subsequently freed.
7For example:
8
Sasha Goldshtein29e37d92016-02-14 06:56:07 -08009# ./memleak -p $(pidof allocs)
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +030010Attaching to pid 5193, Ctrl+C to quit.
Sasha Goldshtein828edb52016-02-09 11:18:32 -080011[11:16:33] Top 2 stacks with outstanding allocations:
Sasha Goldshteindda47692016-02-08 03:10:13 -080012 80 bytes in 5 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050013 main+0x6d [allocs]
14 __libc_start_main+0xf0 [libc-2.21.so]
Sasha Goldshteindda47692016-02-08 03:10:13 -080015
Sasha Goldshtein828edb52016-02-09 11:18:32 -080016[11:16:34] Top 2 stacks with outstanding allocations:
Sasha Goldshteindda47692016-02-08 03:10:13 -080017 160 bytes in 10 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050018 main+0x6d [allocs]
19 __libc_start_main+0xf0 [libc-2.21.so]
Sasha Goldshteindda47692016-02-08 03:10:13 -080020
21
Sasha Goldshteinb9b9ad32016-02-08 03:41:43 -080022Each entry printed is a set of allocations that originate from the same call
23stack, and that weren't freed yet. The number of bytes and number of allocs
24are followed by the call stack, top to bottom, of the allocation site.
25
Sasha Goldshtein33522d72016-02-08 03:39:44 -080026As time goes on, it becomes apparent that the main function in the allocs
27process is leaking memory, 16 bytes at a time. Fortunately, you don't have to
28inspect each allocation individually -- you get a nice summary of which stack
29is responsible for a large leak.
30
31Occasionally, you do want the individual allocation details. Perhaps the same
32stack is allocating various sizes and you want to confirm which sizes are
33prevalent. Use the -a switch:
34
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080035# ./memleak -p $(pidof allocs) -a
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +030036Attaching to pid 5193, Ctrl+C to quit.
Sasha Goldshtein828edb52016-02-09 11:18:32 -080037[11:16:33] Top 2 stacks with outstanding allocations:
Sasha Goldshtein33522d72016-02-08 03:39:44 -080038 addr = 948cd0 size = 16
39 addr = 948d10 size = 16
40 addr = 948d30 size = 16
41 addr = 948cf0 size = 16
42 64 bytes in 4 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050043 main+0x6d [allocs]
44 __libc_start_main+0xf0 [libc-2.21.so]
Sasha Goldshtein33522d72016-02-08 03:39:44 -080045
Sasha Goldshtein828edb52016-02-09 11:18:32 -080046[11:16:34] Top 2 stacks with outstanding allocations:
Sasha Goldshtein33522d72016-02-08 03:39:44 -080047 addr = 948d50 size = 16
48 addr = 948cd0 size = 16
49 addr = 948d10 size = 16
50 addr = 948d30 size = 16
51 addr = 948cf0 size = 16
52 addr = 948dd0 size = 16
53 addr = 948d90 size = 16
54 addr = 948db0 size = 16
55 addr = 948d70 size = 16
56 addr = 948df0 size = 16
57 160 bytes in 10 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050058 main+0x6d [allocs]
59 __libc_start_main+0xf0 [libc-2.21.so]
Sasha Goldshtein33522d72016-02-08 03:39:44 -080060
61
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +030062When using the -p switch, memleak traces the libc allocations of a particular
63process. Without this switch, kernel allocations are traced instead.
Sasha Goldshteindda47692016-02-08 03:10:13 -080064For example:
65
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080066# ./memleak
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +030067Attaching to kernel allocators, Ctrl+C to quit.
Sasha Goldshteindda47692016-02-08 03:10:13 -080068...
69 248 bytes in 4 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050070 bpf_prog_load [kernel]
71 sys_bpf [kernel]
Sasha Goldshteindda47692016-02-08 03:10:13 -080072
73 328 bytes in 1 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050074 perf_mmap [kernel]
75 mmap_region [kernel]
76 do_mmap [kernel]
77 vm_mmap_pgoff [kernel]
78 sys_mmap_pgoff [kernel]
79 sys_mmap [kernel]
Sasha Goldshteindda47692016-02-08 03:10:13 -080080
81 464 bytes in 1 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050082 traceprobe_command [kernel]
83 traceprobe_probes_write [kernel]
84 probes_write [kernel]
85 __vfs_write [kernel]
86 vfs_write [kernel]
87 sys_write [kernel]
88 entry_SYSCALL_64_fastpath [kernel]
Sasha Goldshteindda47692016-02-08 03:10:13 -080089
90 8192 bytes in 1 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -050091 alloc_and_copy_ftrace_hash.constprop.59 [kernel]
92 ftrace_set_hash [kernel]
93 ftrace_set_filter_ip [kernel]
94 arm_kprobe [kernel]
95 enable_kprobe [kernel]
96 kprobe_register [kernel]
97 perf_trace_init [kernel]
98 perf_tp_event_init [kernel]
Sasha Goldshteindda47692016-02-08 03:10:13 -080099
100
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800101Here you can see that arming the kprobe to which our eBPF program is attached
102consumed 8KB of memory. Loading the BPF program also consumed a couple hundred
103bytes (in bpf_prog_load).
104
Sasha Goldshteindda47692016-02-08 03:10:13 -0800105memleak stores each allocated block along with its size, timestamp, and the
106stack that allocated it. When the block is deleted, this information is freed
107to reduce the memory overhead.
108
109To avoid false positives, allocations younger than a certain age (500ms by
110default) are not printed. To change this threshold, use the -o switch.
111
112By default, memleak prints its output every 5 seconds. To change this
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800113interval, pass the interval as a positional parameter to memleak. You can
114also control the number of times the output will be printed before exiting.
115For example:
116
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800117# ./memleak 1 10
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800118
119... will print the outstanding allocation statistics every second, for ten
120times, and then exit.
Sasha Goldshteindda47692016-02-08 03:10:13 -0800121
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800122memleak may introduce considerable overhead if your application or kernel is
123allocating and freeing memory at a very high rate. In that case, you can
124control the overhead by sampling every N-th allocation. For example, to sample
125roughly 10% of the allocations and print the outstanding allocations every 5
126seconds, 3 times before quitting:
127
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800128# ./memleak -p $(pidof allocs) -s 10 5 3
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300129Attaching to pid 2614, Ctrl+C to quit.
Sasha Goldshtein828edb52016-02-09 11:18:32 -0800130[11:16:33] Top 2 stacks with outstanding allocations:
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800131 16 bytes in 1 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -0500132 main+0x6d [allocs]
133 __libc_start_main+0xf0 [libc-2.21.so]
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800134
Sasha Goldshtein828edb52016-02-09 11:18:32 -0800135[11:16:38] Top 2 stacks with outstanding allocations:
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800136 16 bytes in 1 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -0500137 main+0x6d [allocs]
138 __libc_start_main+0xf0 [libc-2.21.so]
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800139
Sasha Goldshtein828edb52016-02-09 11:18:32 -0800140[11:16:43] Top 2 stacks with outstanding allocations:
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800141 32 bytes in 2 allocations from stack
Sasha Goldshtein49df9942017-02-08 23:22:06 -0500142 main+0x6d [allocs]
143 __libc_start_main+0xf0 [libc-2.21.so]
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800144
145Note that even though the application leaks 16 bytes of memory every second,
146the report (printed every 5 seconds) doesn't "see" all the allocations because
147of the sampling rate applied.
148
Edward Wucd81f132020-07-06 12:39:02 +0800149Profiling in memory part is hard to be accurate because of BPF infrastructure.
150memleak keeps misjudging memory leak on the complicated environment which has
151the action of free in hard/soft irq.
152Add workaround to alleviate misjudgments when free is missing:
153
154# ./memleak --wa-missing-free
155Attaching to kernel allocators, Ctrl+C to quit.
156...
157 248 bytes in 4 allocations from stack
158 bpf_prog_load [kernel]
159 sys_bpf [kernel]
160
161 328 bytes in 1 allocations from stack
162 perf_mmap [kernel]
163 mmap_region [kernel]
164 do_mmap [kernel]
165 vm_mmap_pgoff [kernel]
166 sys_mmap_pgoff [kernel]
167 sys_mmap [kernel]
168
Sasha Goldshteindda47692016-02-08 03:10:13 -0800169
170USAGE message:
171
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800172# ./memleak -h
Maria Kacik9389ab42017-01-18 21:43:41 -0800173usage: memleak.py [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND]
Edward Wucd81f132020-07-06 12:39:02 +0800174 [--combined-only] [--wa-missing-free] [-s SAMPLE_RATE]
175 [-T TOP] [-z MIN_SIZE] [-Z MAX_SIZE] [-O OBJ]
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800176 [interval] [count]
Sasha Goldshteindda47692016-02-08 03:10:13 -0800177
178Trace outstanding memory allocations that weren't freed.
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300179Supports both user-mode allocations made with libc functions and kernel-mode
180allocations made with kmalloc/kmem_cache_alloc/get_free_pages and corresponding
181memory release functions.
Sasha Goldshteindda47692016-02-08 03:10:13 -0800182
Maria Kacik9389ab42017-01-18 21:43:41 -0800183positional arguments:
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800184 interval interval in seconds to print outstanding allocations
185 count number of times to print the report before exiting
186
Sasha Goldshteindda47692016-02-08 03:10:13 -0800187optional arguments:
188 -h, --help show this help message and exit
189 -p PID, --pid PID the PID to trace; if not specified, trace kernel
190 allocs
191 -t, --trace print trace messages for each alloc/free call
Sasha Goldshteindda47692016-02-08 03:10:13 -0800192 -a, --show-allocs show allocation addresses and sizes as well as call
193 stacks
194 -o OLDER, --older OLDER
195 prune allocations younger than this age in
196 milliseconds
197 -c COMMAND, --command COMMAND
198 execute and trace the specified command
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300199 --combined-only show combined allocation statistics only
Edward Wucd81f132020-07-06 12:39:02 +0800200 --wa-missing-free Workaround to alleviate misjudgments when free is
201 missing
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800202 -s SAMPLE_RATE, --sample-rate SAMPLE_RATE
203 sample every N-th allocation to decrease the overhead
Sasha Goldshteinc8148c82016-02-09 11:15:41 -0800204 -T TOP, --top TOP display only this many top allocating stacks (by size)
Sasha Goldshtein50459642016-02-10 08:35:20 -0800205 -z MIN_SIZE, --min-size MIN_SIZE
206 capture only allocations larger than this size
207 -Z MAX_SIZE, --max-size MAX_SIZE
208 capture only allocations smaller than this size
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300209 -O OBJ, --obj OBJ attach to allocator functions in the specified object
Sasha Goldshteindda47692016-02-08 03:10:13 -0800210
211EXAMPLES:
212
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800213./memleak -p $(pidof allocs)
Sasha Goldshteindda47692016-02-08 03:10:13 -0800214 Trace allocations and display a summary of "leaked" (outstanding)
215 allocations every 5 seconds
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800216./memleak -p $(pidof allocs) -t
Rinat Ibragimov2c1799c2017-07-11 21:14:08 +0300217 Trace allocations and display each individual allocator function call
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800218./memleak -ap $(pidof allocs) 10
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800219 Trace allocations and display allocated addresses, sizes, and stacks
220 every 10 seconds for outstanding allocations
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800221./memleak -c "./allocs"
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800222 Run the specified command and trace its allocations
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800223./memleak
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800224 Trace allocations in kernel mode and display a summary of outstanding
225 allocations every 5 seconds
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800226./memleak -o 60000
Sasha Goldshtein75ba13f2016-02-09 06:03:46 -0800227 Trace allocations in kernel mode and display a summary of outstanding
228 allocations that are at least one minute (60 seconds) old
Sasha Goldshtein29e37d92016-02-14 06:56:07 -0800229./memleak -s 5
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800230 Trace roughly every 5th allocation, to reduce overhead