Last active
November 29, 2016 10:06
-
-
Save unhammer/4e91821075c2485999eb to your computer and use it in GitHub Desktop.
poormansprofiler, but with some ocaml specific extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e -u | |
nsamples=20 | |
sleeptime=0.1 | |
if [[ $# -ne 1 && $# -ne 2 ]]; then | |
cat <<EOF | |
Usage: | |
$0 PROG_NAME | |
$0 --mem PROG_NAME | |
where PROG_NAME is given to pgrep. Note that long program names may be | |
truncated in the process list; you can do 'pgrep -l SUB' (for some | |
substring of the program name) to see what it's truncated to, and use | |
that as PROG_NAME. | |
EOF | |
exit 1 | |
fi | |
if [[ $1 = -m || $1 = --mem ]]; then | |
shift | |
# https://sympa.inria.fr/sympa/arc/caml-list/2011-08/msg00050.html | |
cmd () { | |
sudo gdb -ex "b caml_call_gc" -ex "c" -ex "bt" "$@" | |
} | |
else | |
cmd () { | |
sudo gdb -ex "set pagination 0" -ex "thread apply all bt" "$@" | |
} | |
fi | |
prog=$1 | |
pid () { pgrep --newest "${prog}"; } | |
# avoid the "No such file" from gdb by adding this: | |
#ocamlsrc=' -d "$(opam config var lib)/ocaml"' | |
for x in $(seq 1 "${nsamples}"); do | |
cmd -p "$(pid)" -batch -n -q | |
sleep "${sleeptime}" | |
done | awk ' | |
BEGIN { s = ""; prev = "" } | |
/^(Thread|Breakpoint)/ { if(s) print s; s = ""; prev = "" } | |
/^\#/ && $4 != prev { prev = $4; if (s != "" ) { s = s "," $4} else { s = $4 } } | |
END { print s }' | sort | uniq -c | sort -r -n -k 1,1 | |
# Like http://poormansprofiler.org/ but collapse same-names (so much recursion in ocaml) | |
# TODO: grep out malloc.c stuff | |
# http://stackoverflow.com/questions/9220853/call-to-malloc-failing-in-gdb-session |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment