forked from hatem-mahmoud/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_profiler.sh
199 lines (141 loc) · 5.39 KB
/
cpu_profiler.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# This script generate extended off_cpu,on_cpu,Hot/Cold flamegaph for a specific Oracle session
# Author : Hatem Mahmoud <[email protected]>
# BLOG : https://mahmoudhatem.wordpress.com
#
# Example : cpu_profiler.sh PID SAMPLE_TIME_SEC
#
# Run eventsname.sql and place the output file eventsname.sed in the current directory
# Update the folowing variable before running the script
#
oracle_path=/oracle11/install/bin/oracle
flamegraph_base=/home/oracle/scripts/FlameGraph-master
echo "**********************"
echo "Begin data collection for " $2 " Seconds"
echo "Target process " $1
echo "**********************"
rm -f wait.data*
rm -f wait_*
rm -f on_perf.data*
rm -f off_perf.data*
rm -f all_perf.data*
perf probe -x $oracle_path kskthbwt event=%dx
perf probe -x $oracle_path kskthewt event=%si
perf record -e probe_oracle:kskthbwt -e probe_oracle:kskthewt -o wait.data.raw -p $1 sleep $2 &
perf record -F 999 -g -o on_perf.data0 -p $1 sleep $2 &
perf record -e sched:sched_stat_sleep -e sched:sched_switch -e sched:sched_process_exit -g -o off_perf.data.raw -p $1 sleep $2
echo "**********************"
echo "Begin of data analysis"
echo "**********************"
#Wait event analysis
perf script -i wait.data.raw > wait.data0
wait_number=`wc -l wait.data0 | awk '{print $1}'`
wait_begin=0
wait_end=0
wait_event=0
wait_line_nb=1
wait_end=0
#Formating Wait event trace as "wait_begin wait_end wait_event#"
while test $wait_number -gt $wait_line_nb
do
if [[ $wait_line_nb == 1 && `sed -n "$wait_line_nb"p wait.data0` == *"kskthewt"* ]]
then
wait_line_nb=$(($wait_line_nb+1))
fi
wait_begin=`sed -n "$wait_line_nb"p wait.data0 | awk '{split($4,array,".") ; print array[1] substr(array[2],1,length(array[2])-1)}'`
wait_event=`sed -n "$wait_line_nb"p wait.data0 | awk '{print $7}'`
wait_line_nb=$(($wait_line_nb+1))
wait_end=`sed -n "$wait_line_nb"p wait.data0 | awk '{split($4,array,".") ; print array[1] substr(array[2],1,length(array[2])-1)}'`
wait_line_nb=$(($wait_line_nb+1))
echo $wait_begin" "$wait_end" "$wait_event >> wait_list.txt
done
#Off cpu analysis
echo "---"
echo "Generating Off cpu flamegraph"
echo "---"
perf inject -v -s -i off_perf.data.raw -o off_perf.data0
perf script -F comm,pid,tid,cpu,time,period,event,ip,sym,dso,trace -i off_perf.data0 > off_perf.data1
wait_line_nb=-1
while read -r line
do
if [[ $line == "oracle"* ]]
then
stack_timestamp=`echo "$line" | awk '{split($4,array,".") ; print array[1] substr(array[2],1,length(array[2])-1)}'`
echo "$line" >> off_perf.data2
wait_event="On_cpu(oracle)"
while read -r wait_line
do
wait_line_nb=`echo $wait_line | awk '{print $1}'`
w_b=`echo $wait_line | awk '{print $2}'`
w_e=`echo $wait_line | awk '{print $3}'`
w_n=`echo $wait_line | awk '{print $4}'`
if [[ $stack_timestamp -ge $w_b && $stack_timestamp -le $w_e ]]
then
wait_event=$w_n
break
elif [[ $stack_timestamp -lt $w_e ]]
then
break
fi
done <<EOF
$(nl wait_list.txt | awk -v var="$wait_line_nb" ' NR >= var { print $0}')
EOF
elif [[ -z $line ]]
then
echo "xxxxxxxxxx Off_cpu(system)()" >> off_perf.data2
echo "xxxxxxxxxx "$wait_event "()">> off_perf.data2
echo $line >> off_perf.data2
else
echo $line >> off_perf.data2
fi
done < off_perf.data1
nb_cpu=`lscpu -p=cpu | grep -v "#" | wc -l`
cat off_perf.data2 | awk -v var=$nb_cpu ' NF > 4 { exec = $1; period_ms = int($5 / 1000000 / var) } NF > 1 && NF <= 4 && period_ms > 0 { print $2 } NF < 2 && period_ms > 0 { printf "%s\n%d\n\n", exec, period_ms }' | $flamegraph_base/stackcollapse.pl | sed -f eventsname.sed > off_perf.data3
cat off_perf.data3| $flamegraph_base/flamegraph.pl --countname=ms --title="Off-CPU Time Flame Graph" --colors=io > offcpu.svg
#On cpu analysis
echo "---"
echo "Generating On cpu flamegraph"
echo "---"
perf script -i on_perf.data0 > on_perf.data1
wait_line_nb=-1
while read -r line
do
if [[ $line == "oracle"* ]]
then
stack_timestamp=`echo "$line" | awk '{split($3,array,".") ; print array[1] substr(array[2],1,length(array[2])-1)}'`
echo "$line" >> on_perf.data2
wait_event="On_cpu(oracle) ()"
while read -r wait_line
do
wait_line_nb=`echo $wait_line | awk '{print $1}'`
w_b=`echo $wait_line | awk '{print $2}'`
w_e=`echo $wait_line | awk '{print $3}'`
w_n=`echo $wait_line | awk '{print $4}'`
if [[ $stack_timestamp -ge $w_b && $stack_timestamp -le $w_e ]]
then
wait_event=$w_n
break
elif [[ $stack_timestamp -lt $w_e ]]
then
break
fi
done <<EOF
$(nl wait_list.txt | awk -v var="$wait_line_nb" ' NR >= var { print $0}')
EOF
elif [[ -z $line ]]
then
echo "xxxxxxxxxx On_cpu(system) ()" >> on_perf.data2
echo "xxxxxxxxxx "$wait_event" ()" >> on_perf.data2
echo $line >> on_perf.data2
else
echo $line >> on_perf.data2
fi
done < on_perf.data1
cat on_perf.data2 | $flamegraph_base/stackcollapse-perf.pl | sed -f eventsname.sed > on_perf.data3
cat on_perf.data3 | $flamegraph_base/flamegraph.pl --title="On-CPU Time Flame Graph" > oncpu.svg
#On Cpu /Off cpu Mixed flame graph
echo "---"
echo "Generating Mixed cpu flamegraph"
echo "---"
cat off_perf.data3 > all_perf.data
cat on_perf.data3 >> all_perf.data
cat all_perf.data | $flamegraph_base/flamegraph.pl --countname=ms --title="Mixed-CPU Time Flame Graph" > allcpu.svg