Skip to content

Instantly share code, notes, and snippets.

@chrishantha
Created February 5, 2018 11:07
Show Gist options
  • Save chrishantha/33c8769ee8121bb80d0ffceb82417874 to your computer and use it in GitHub Desktop.
Save chrishantha/33c8769ee8121bb80d0ffceb82417874 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright 2018 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.
#
# ----------------------------------------------------------------------------
# Analyze thread dumps taken from https://gist.github.com/bsenduran/02e8bf024fcaaa7707a6bb2321e097a8
# ----------------------------------------------------------------------------
function process_line() {
filename=$1
values=$2
tid=$(echo $values | awk '{print $2}')
cpu_usage=$(echo $values | awk '{print $3}')
nid=$(printf '%x' $tid)
thread_dump_file=$(echo $filename | sed 's/usage/dump/')
thread_dump_lines=$(grep -A 20 nid=0x$nid $thread_dump_file)
thread_details=$(echo "$thread_dump_lines" | head -1 | sed -E 's/"(.*)".*nid=(0x[0-9a-z]*) (.*) .*/"\1", \2, "\3"/')
echo -e " ,$cpu_usage,$thread_details"
echo "$thread_dump_lines" | tail -n +2 | while read line; do
if [[ -z "${line// }" ]]; then
break
else
echo "$line, , , , "
fi
done
}
function analyze() {
echo "File/Stack Trace,CPU%,Thread Name,Native ID,Status"
for f in thread_usage_*; do
echo "-----,-----,-----,-----,-----"
echo "## "$f" #####################"
output=$(tail -n +2 $f | sort -nr -k3 | head -6)
echo "$output" | while read line; do process_line $f "$line"; done
done
}
analyze | column -t -s,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment