| 1 |
#! /usr/bin/env stap |
| 2 |
|
| 3 |
# http://developerblog.redhat.com/2015/01/06/malloc-systemtap-probes-an-example/ |
| 4 |
|
| 5 |
|
| 6 |
global sbrk, waits, arenalist, mmap_threshold = 131072, heaplist |
| 7 |
|
| 8 |
|
| 9 |
# sbrk accounting |
| 10 |
|
| 11 |
probe process("/lib*/libc.so.6").mark("memory_sbrk_more") |
| 12 |
{ |
| 13 |
sbrk += $arg2 |
| 14 |
} |
| 15 |
|
| 16 |
probe process("/lib*/libc.so.6").mark("memory_sbrk_less") |
| 17 |
{ |
| 18 |
sbrk -= $arg2 |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
# threshold tracking |
| 23 |
|
| 24 |
probe process("/lib*/libc.so.6").mark("memory_mallopt_free_dyn_thresholds") |
| 25 |
{ |
| 26 |
printf("%d: New thresholds: mmap: %ld bytes, trim: %ld bytes\n", tid(), $arg1, |
| 27 |
$arg2) |
| 28 |
mmap_threshold = $arg1 |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
# arena accounting |
| 33 |
|
| 34 |
probe process("/lib*/libc.so.6").mark("memory_arena_new") |
| 35 |
{ |
| 36 |
printf ("%d: Created new arena\n", tid()) |
| 37 |
arenalist[$arg1, tid()] = 1 |
| 38 |
} |
| 39 |
|
| 40 |
probe process("/usr/lib*/libc.so.6").mark("memory_arena_reuse_wait") |
| 41 |
{ |
| 42 |
waits[tid()]++ |
| 43 |
} |
| 44 |
|
| 45 |
probe process("/usr/lib*/libc.so.6").mark("memory_arena_reuse") |
| 46 |
{ |
| 47 |
if ($arg2 != 0) |
| 48 |
{ |
| 49 |
printf ("%d: failed to allocate on own arena, trying another\n", tid()) |
| 50 |
arenalist[$arg1, tid()] = 1 |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
probe process("/usr/lib*/libc.so.6").mark("memory_arena_reuse_free_list") |
| 55 |
{ |
| 56 |
arenalist[$arg1, tid()] = 1 |
| 57 |
} |
| 58 |
|
| 59 |
probe process.thread.end |
| 60 |
{ |
| 61 |
/* Find the thread and mark its arena as unused. */ |
| 62 |
%( systemtap_v >= "2.6" |
| 63 |
%? |
| 64 |
delete arenalist[*, tid()] |
| 65 |
%: |
| 66 |
foreach ([a, t] in arenalist) |
| 67 |
if (t == tid()) |
| 68 |
break |
| 69 |
delete arenalist[a, t] |
| 70 |
%) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
# heap accounting |
| 75 |
|
| 76 |
probe process("/usr/lib*/libc.so.6").mark("memory_heap_new") |
| 77 |
{ |
| 78 |
printf("%d: New heap\n", tid()); |
| 79 |
heaplist[$arg1] = $arg2 |
| 80 |
} |
| 81 |
|
| 82 |
probe process("/usr/lib*/libc.so.6").mark("memory_heap_more") |
| 83 |
{ |
| 84 |
heaplist[$arg1] = $arg2 |
| 85 |
} |
| 86 |
|
| 87 |
probe process("/usr/lib*/libc.so.6").mark("memory_heap_less") |
| 88 |
{ |
| 89 |
heaplist[$arg1] = $arg2 |
| 90 |
} |
| 91 |
|
| 92 |
probe process("/usr/lib*/libc.so.6").mark("memory_heap_free") |
| 93 |
{ |
| 94 |
delete heaplist[$arg1] |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
# reporting |
| 99 |
|
| 100 |
probe begin |
| 101 |
{ |
| 102 |
if (target() == 0) error ("please specify target process with -c / -x") |
| 103 |
} |
| 104 |
|
| 105 |
probe end |
| 106 |
{ |
| 107 |
printf ("malloc information for pid %d\n", target()) |
| 108 |
printf ("Contention: \n") |
| 109 |
foreach (t in waits) |
| 110 |
printf ("\t%d: %d waits\n", t, waits[t]) |
| 111 |
|
| 112 |
print("Active arenas:\n") |
| 113 |
foreach ([a, t] in arenalist) |
| 114 |
{ |
| 115 |
if (arenalist[a, t]) |
| 116 |
printf ("\t%d -> %p\n", t, a) |
| 117 |
} |
| 118 |
|
| 119 |
print ("Allocated heaps:\n") |
| 120 |
foreach (h in heaplist) |
| 121 |
{ |
| 122 |
if (heaplist[h]) |
| 123 |
printf ("\t%p -> %ld bytes\n", h, heaplist[h]) |
| 124 |
} |
| 125 |
|
| 126 |
printf ("Total sbrk: %ld bytes\n", sbrk) |
| 127 |
printf ("Mmap threshold in the end: %ld kb\n", mmap_threshold / 1024) |
| 128 |
} |