Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bcc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bcc
Commits
c8148c88
Commit
c8148c88
authored
Feb 09, 2016
by
Sasha Goldshtein
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added option to display only top N stacks by size
parent
dcee30d3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
5 deletions
+23
-5
man/man8/memleak.8
man/man8/memleak.8
+13
-1
tools/memleak.py
tools/memleak.py
+8
-3
tools/memleak_examples.txt
tools/memleak_examples.txt
+2
-1
No files found.
man/man8/memleak.8
View file @
c8148c88
...
...
@@ -2,7 +2,7 @@
.SH NAME
memleak \- Print a summary of outstanding allocations and their call stacks to detect memory leaks. Uses Linux eBPF/bcc.
.SH SYNOPSIS
.B memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] [-s SAMPLE_RATE] [-d STACK_DEPTH] [INTERVAL] [COUNT]
.B memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] [-s SAMPLE_RATE] [-d STACK_DEPTH] [
-T TOP] [
INTERVAL] [COUNT]
.SH DESCRIPTION
memleak traces and matches memory allocation and deallocation requests, and
collects call stacks for each allocation. memleak can then print a summary
...
...
@@ -11,6 +11,9 @@ of which call stacks performed allocations that weren't subsequently freed.
When tracing a specific process, memleak instruments malloc and free from libc.
When tracing all processes, memleak instruments kmalloc and kfree.
memleak may introduce significant overhead when tracing processes that allocate
and free many blocks very quickly. See the OVERHEAD section below.
The stack depth is limited to 10 by default (+1 for the current instruction pointer),
but it can be controlled using the \-d switch if deeper stacks are required.
...
...
@@ -45,6 +48,10 @@ Record roughly every SAMPLE_RATE-th allocation to reduce overhead.
Capture STACK_DEPTH frames (or less) when obtaining allocation call stacks.
The default value is 10.
.TP
\-t TOP
Print only the top TOP stacks (sorted by size).
The default value is 10.
.TP
INTERVAL
Print a summary of oustanding allocations and their call stacks every INTERVAL seconds.
The default interval is 5 seconds.
...
...
@@ -61,6 +68,11 @@ Print user outstanding allocation stacks and allocation details for the process
#
.B memleak -p 1005 -a
.TP
Sample roughly every 5th allocation (~20%) of the call stacks and print the top 5
stacks 10 times before quitting.
#
.B memleak -s 5 --top=5 10
.TP
Run ./allocs and print outstanding allocation stacks for that process:
#
.B memleak -c "./allocs"
...
...
tools/memleak.py
View file @
c8148c88
...
...
@@ -2,6 +2,7 @@
from
bcc
import
BPF
from
time
import
sleep
from
datetime
import
datetime
import
argparse
import
subprocess
import
ctypes
...
...
@@ -192,6 +193,8 @@ parser.add_argument("-s", "--sample-rate", default=1, type=int,
help
=
"sample every N-th allocation to decrease the overhead"
)
parser
.
add_argument
(
"-d"
,
"--stack-depth"
,
default
=
10
,
type
=
int
,
help
=
"maximum stack depth to capture"
)
parser
.
add_argument
(
"-T"
,
"--top"
,
type
=
int
,
default
=
10
,
help
=
"display only this many top allocating stacks (by size)"
)
args
=
parser
.
parse_args
()
...
...
@@ -204,6 +207,7 @@ min_age_ns = 1e6 * args.older
sample_every_n
=
args
.
sample_rate
num_prints
=
args
.
count
max_stack_size
=
args
.
stack_depth
+
2
top_stacks
=
args
.
top
if
command
is
not
None
:
print
(
"Executing '%s' and tracing the resulting process."
%
command
)
...
...
@@ -235,7 +239,8 @@ decoder = StackDecoder(pid, bpf_program)
def
print_outstanding
():
stacks
=
{}
print
(
"*** Outstanding allocations:"
)
print
(
"[%s] Top %d stacks with outstanding allocations:"
%
(
datetime
.
now
().
strftime
(
"%H:%M:%S"
),
top_stacks
))
allocs
=
bpf_program
.
get_table
(
"allocs"
)
for
address
,
info
in
sorted
(
allocs
.
items
(),
key
=
lambda
a
:
a
[
1
].
size
):
if
Time
.
monotonic_time
()
-
min_age_ns
<
info
.
timestamp_ns
:
...
...
@@ -249,8 +254,8 @@ def print_outstanding():
if
args
.
show_allocs
:
print
(
"
\
t
addr = %x size = %s"
%
(
address
.
value
,
info
.
size
))
for
stack
,
(
count
,
size
)
in
sorted
(
stacks
.
items
(),
key
=
lambda
s
:
s
[
1
][
1
])
:
to_show
=
sorted
(
stacks
.
items
(),
key
=
lambda
s
:
s
[
1
][
1
])[
-
top_stacks
:]
for
stack
,
(
count
,
size
)
in
to_show
:
print
(
"
\
t
%d bytes in %d allocations from stack
\
n
\
t
\
t
%s"
%
(
size
,
count
,
stack
.
replace
(
";"
,
"
\
n
\
t
\
t
"
)))
...
...
tools/memleak_examples.txt
View file @
c8148c88
...
...
@@ -151,7 +151,7 @@ USAGE message:
# ./memleak.py -h
usage: memleak.py [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND]
[-s SAMPLE_RATE] [-d STACK_DEPTH]
[-s SAMPLE_RATE] [-d STACK_DEPTH]
[-T TOP]
[interval] [count]
Trace outstanding memory allocations that weren't freed.
...
...
@@ -177,6 +177,7 @@ optional arguments:
sample every N-th allocation to decrease the overhead
-d STACK_DEPTH, --stack_depth STACK_DEPTH
maximum stack depth to capture
-T TOP, --top TOP display only this many top allocating stacks (by size)
EXAMPLES:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment