Skip to content

Instantly share code, notes, and snippets.

@unhammer
Last active November 29, 2016 10:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unhammer/4e91821075c2485999eb to your computer and use it in GitHub Desktop.
Save unhammer/4e91821075c2485999eb to your computer and use it in GitHub Desktop.
poormansprofiler, but with some ocaml specific extensions
#!/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