/
dstackprof.sh
69 lines (63 loc) · 2.13 KB
/
dstackprof.sh
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
#!/bin/ksh
#
# Copyright 2018 Tanel Poder. All rights reserved. More info at http://tanelpoder.com
# Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.
#################################################################################
#
# File name: dstackprof.sh v1.02 29-Aug-2008
# Purpose: Samples target process stack using DTrace, strips the PC function
# offsets from output and re-aggregates
#
# Author: Tanel Poder
# Copyright: (c) http://www.tanelpoder.com
#
# Usage: dstackprof.sh <PID> [SECONDS] [STACKS] [FRAMES]
#
#
# Other:
#
#
#
#################################################################################
DEFAULT_SECONDS=5
DEFAULT_FRAMES=100
DEFAULT_STACKS=20
FREQUENCY=1001
[ $# -lt 1 ] && echo " Usage: $0 <PID> [SECONDS] [STACKS] [FRAMES]\n" && exit 1
[ -z $2 ] && SECONDS=$DEFAULT_SECONDS || SECONDS=$2
[ -z $3 ] && STACKS=$DEFAULT_STACKS || STACKS=$3
[ -z $4 ] && FRAMES=$DEFAULT_FRAMES || FRAMES=$4
PROCESS=$1
echo
echo "DStackProf v1.02 by Tanel Poder ( http://www.tanelpoder.com )"
echo "Sampling pid $PROCESS for $SECONDS seconds with stack depth of $FRAMES frames..."
echo
dtrace -q -p $PROCESS -n '
profile-'$FREQUENCY'
/pid == $target/ {
@u[ustack('$FRAMES')] = count();
@k[stack('$FRAMES')] = count();
}
tick-1sec
/i++ >= '$SECONDS'/ {
exit(0);
}
END {
printa(@u);
printa(@k);
}
' | sed 's/^ *//;/^$/d;s/+.*$//;s/^oracle`//g' | \
awk '/^$/{ printf "\n" }/^[0-9]*$/{ printf ";%s\n", $1 }/[a-z]/{ printf "%s<", $1 }END{ printf "\n" }' | \
sed '/^;/d' | \
sort | \
awk -F";" '
/NR==1/{ sum=0; total=0; oldstack=$1 }
{
if (oldstack==$1) {sum+=$2;total+=$2}
else {printf "%d samples with stack below<__________________<%s\n", sum, oldstack; oldstack=$1; sum=$2; total+=$2}
}
END {printf "%d samples with stack below<__________________<%s\n%d Total samples captured\n", sum, oldstack, total}
' | \
sort -bn | \
tail -$((STACKS+1)) | \
tr '<' '\n'