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