forked from chrishantha/jfr-flame-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_flamegraphs.sh
executable file
·188 lines (157 loc) · 4.27 KB
/
create_flamegraphs.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
183
184
185
186
187
188
#!/bin/bash
# Copyright 2016 M. Isuru Tharanga Chrishantha Perera
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ----------------------------------------------------------------------------
# Create multiple flame graphs
# ----------------------------------------------------------------------------
set -e
JFG_DIR=$(dirname "$0")
if [ ! -x "$FLAMEGRAPH_DIR/flamegraph.pl" ]; then
echo "Please clone https://github.com/brendangregg/FlameGraph and set FLAMEGRAPH_DIR to the root directory"
exit 1
fi
function help {
echo ""
echo "Usage: "
echo "create-flamegraphs.sh [options]"
echo ""
echo "Options:"
echo "-f: JFR file"
echo "-d: Decompress the JFR file"
echo "-m: Interval in minutes. Default 10"
echo "-o: Output Directory. Default \"Output\""
echo "-i: Ignore line numbers"
echo ""
}
jfr_file=""
minutes=10
output_dir=""
decompress=""
ignore_lines=""
while getopts "df:m:i" opts
do
case $opts in
f)
jfr_file=${OPTARG}
;;
d)
decompress="-d"
;;
m)
minutes=${OPTARG}
;;
i)
ignore_lines="-i"
;;
\?)
help
exit 1
;;
esac
done
if [[ ! -f $jfr_file ]]; then
echo "Please specify the JFR file"
help
exit 1
fi
#If no directory was provided, we need to create the default one
if [[ ! -d $java_dir ]]; then
output_dir="output"
mkdir -p $output_dir
fi
#Validate output directory
if [[ ! -d $output_dir ]]; then
echo "Please specify a directory to create the flamegraphs"
exit 1
fi
jfr_filename=$(basename $jfr_file)
details=$(${JFG_DIR}/flamegraph-output.sh folded -f $jfr_file -j -t)
echo "$details"
startTimestamp=$(echo $details | sed -r 's/.*Min Start Event\s*: ([0-9]*).*/\1/')
endTimestamp=$(echo $details | sed -r 's/.*Max End Event\s*: ([0-9]*).*/\1/')
interval=$(($minutes * 60))
i=$startTimestamp
end=$endTimestamp
dateformat="%Y-%m-%d %I:%M:%S %p"
set +e
while [ $i -lt $end ]; do
s=$i
i=$(($i+$interval))
e=$i
if [ $e -gt $end ]; then
e=$end
fi
title=$(echo Flame Graph for $jfr_filename from $(date --date @$s +"$dateformat") to $(date --date @$e +"$dateformat"))
echo Generating $title
output_file=flamegraph-$s-$e.svg
# Use folded command
${JFG_DIR}/flamegraph-output.sh folded $decompress -f $jfr_file -x $s -y $e $ignore_lines | \
$FLAMEGRAPH_DIR/flamegraph.pl --title "$title" --width 1600 \
> $output_dir/$output_file
flamegraph_status=("${PIPESTATUS[@]}")
if [ ${flamegraph_status[1]} -eq 0 ]
then
# Create array
output_files+=($output_file)
else
rm $output_dir/$output_file
fi
done
#Generate HTML
index_file=$output_dir/index.html
cat << _EOF_ > $index_file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Flame Graphs</title>
</head>
<body>
<div>
<button onclick="plusDivs(-1)">Previous</button>
<button onclick="plusDivs(1)">Next</button>
_EOF_
for f in "${output_files[@]}"
do
echo "<object class=\"flamegraph\" data=\"$f\" type=\"image/svg+xml\" style=\"display: none\"></object>" >> $index_file
done
cat << _EOF_ >> $index_file
</div>
<script>
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("flamegraph");
if (n > x.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex - 1].style.display = "block";
}
</script>
</body>
</html>
_EOF_
echo Script executed in $SECONDS seconds.