Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
xenctx_prof.sh
#!/bin/bash
#
# xenctx_prof - xenctx based unikernel profiler. Takes a single stack sample.
#
# WARNING: current version is a PROOF OF CONCEPT and is vastly inefficient.
# Check for future work.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; under version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
#
# 26-Jan-2016 Brendan Gregg Created this.
# Uncommand and edit the following to add a path to xenctx
#PATH=/usr/lib/xen-4.4/bin:$PATH
if ! which xenctx > /dev/null; then
echo >&2 "ERROR: need xenctx in \$PATH (or edit $0)"
exit 2
fi
if ! which awk > /dev/null; then
echo >&2 "ERROR: need awk"
exit 2
fi
function usage {
echo >&2 "USAGE: $0 [-s symbol.map] domU_ID"
exit 1
}
flags=""
while getopts s: opt
do
case $opt in
s) flags="$flags -s $OPTARG" ;;
h|?) usage ;;
esac
done
shift $(( $OPTIND - 1 ))
(( $# < 1 )) && usage
dom=$1
xenctx -C $flags $dom | awk '
# parse output of xenctx. sample output lines inline.
# rbp: 000000000019fe50 r8: 000000000000000d r9: 000000000000000d
$1 == "rbp:" { rbp = $2 ":" }
# 000000000019feb0: [<0000000000000000>] .comment
instack && $2 ~ /\[<0000000000000000>\]/ { exit }
# 000000000019fe50: [<000000000019fe70>] stack+0x1be30
# 000000000019fe58: [<0000000000007312>] camlUnikernel____pa_lwt_loop_1376+0x72
# ...
instack {
addr = sub(/:/, "", $1)
if ($NF !~ /0x..../) {
print $NF
} else {
# offset suspiciously large (probably wrong symbol); print hex instead
addr = $2; gsub(/[\[\]<>]/, "", addr); print "0x" addr
}
}
# [<0000000000049ad7>] camlLwt__return_1319+0x27 <--
$NF == "<--" { print $(NF - 1); instack = 1 }
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment