#! /usr/bin/env stap # http://developerblog.redhat.com/2015/01/06/malloc-systemtap-probes-an-example/ global sbrk, waits, arenalist, mmap_threshold = 131072, heaplist global trim_threshold = 0 global context_creation_sbrk = 0 global cnt_sbrk_more = 0 global cnt_sbrk_less = 0 global palloced_mem = 0 global pfreed_mem = 0 global sum_sbrk_more = 0 # sbrk accounting probe process("/lib*/libc.so.6").mark("memory_sbrk_more") { sbrk += $arg2 cnt_sbrk_more += 1 sum_sbrk_more += $arg2 if ($arg2 < 0) { printf("sbrk_more called with %ld", $arg2) } } probe process("/lib*/libc.so.6").mark("memory_sbrk_less") { sbrk -= $arg2 cnt_sbrk_less += 1 sum_sbrk_less += $arg2 } # threshold tracking probe process("/lib*/libc.so.6").mark("memory_mallopt_free_dyn_thresholds") { if ($arg1 != mmap_threshold || $arg2 != trim_threshold) { printf("%d: New thresholds: mmap: %ld bytes, trim: %ld bytes\n", tid(), $arg1, $arg2) } mmap_threshold = $arg1 trim_threshold = $arg2 } probe process("/home/ro/dev/postgresql-install/bin/postgres").mark("generation_alloc_malloc") { palloced_mem += $arg1 } probe process("/home/ro/dev/postgresql-install/bin/postgres").mark("generation_alloc_free") { pfreed_mem += $arg1 } probe process("/home/ro/dev/postgresql-install/bin/postgres").mark("generation_alloc_create") { palloced_mem = 0; pfreed_mem = 0; cnt_sbrk_more = 0; cnt_sbrk_less = 0; sum_sbrk_more = 0; context_creation_sbrk = sbrk; } probe process("/home/ro/dev/postgresql-install/bin/postgres").mark("generation_alloc_destroy") { printf("Palloced mem in generation: %ld\n", palloced_mem); printf("Pfreed mem in generation: %ld\n", pfreed_mem); printf("Sbrk moved: %ld\n", sbrk - context_creation_sbrk); printf("Sbrk calls: More: %d Less: %d\n", cnt_sbrk_more, cnt_sbrk_less); printf("Sbrk bytes more: %d\n", sum_sbrk_more); } # reporting probe begin { if (target() == 0) error ("please specify target process with -c / -x") }