-
-
Save kevinbin/27c371abf3ef5b80c03e05357afcfccd to your computer and use it in GitHub Desktop.
analyze mysql backtrace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright 2021 hongbin | |
# | |
# 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. | |
# pstack collect callstack info | |
if [ $# = 0 ]; then | |
echo "No such file" | |
exit 1 | |
fi | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
NC='\033[0m' # No Color | |
# If unable to get code from GitHub. Please try setting up a http(s) proxy | |
export https_proxy=http://127.0.0.1:6152;export http_proxy=http://127.0.0.1:6152;export all_proxy=socks5://127.0.0.1:6153 | |
# main step | |
# 1. filter the function location of each thread waiting mutex when MySQL Hang | |
# awk '/inline_mysql_mutex_lock/ { getline; print $0 }' | |
# 2. Locate the source location that the function is waiting for | |
# 3. De duplication statistical grouping and Ascending sort | |
# 4. Read the code content for the location | |
# 5. filter the function name | |
# ff=`echo $line | awk '{sub("\\(\\)","",$3); print $3}'` | |
# 6. Starting from waiting for mutex functions, search forward for functions in each call stack until pfs_spawn_thread | |
# Search only for the first occurrence | |
# sed "/$ff/,/pfs_spawn_thread/!d;/pfs_spawn_thread/q" $1 | |
# 7. Format call stack | |
# 8. Filter out the mutex operation between the entry and the step in point of each call stack function | |
# Mutex that appears on the call stack may be held | |
# less -r summary_callstack or detail_callstack | |
SUMMARY_FILE="summary_callstack" | |
DETAIL_FILE="detail_callstack" | |
FILE=$1 | |
echo "${RED}######################### Call Stack Waiting Summary #########################${NC}" | |
> $SUMMARY_FILE | |
cat $FILE \ | |
| awk '/inline_mysql_mutex_lock/ { getline; print $0 }' \ | |
| sed -e 's$ (.*-$ https://github.com/mysql/mysql-server/blob/mysql-$g' -e 's/\(.*\):/\1#L/g' \ | |
| sort | uniq -c | sort | tee -a $SUMMARY_FILE \ | |
| awk -F'-' '{split($3,c,"#L"); location="curl -s https://raw.githubusercontent.com/mysql/mysql-server/mysql-" c[1] "|sed -n \"s/^[[:space:]]*//;" c[2] "p\""; \ | |
printf $0 "\033[31m Waiting \033[0m";system(location)}' | |
echo "\nEnter y, Continue to analyze each function call stack?" | |
read input | |
if [ $input = "y" ]; then | |
echo "${RED}######################### Call Stack Analyze Start #########################${NC}" | |
> $DETAIL_FILE | |
cat $SUMMARY_FILE | while read line ; do | |
echo $line |awk -F'-' '{split($3,c,"#L"); \ | |
location="curl -s https://raw.githubusercontent.com/mysql/mysql-server/mysql-" c[1] "|sed -n \"s/^[[:space:]]*//;" c[2] "p\""; \ | |
printf $0 "\033[31m Waiting \033[0m";system(location)}' | |
ff=`echo $line | awk '{sub("\\(\\)","",$3); print $3}'` | |
count=`echo $line | awk '{print $1}'` | |
echo "${GREEN}========================= Call Stack Start =========================${NC}" | |
if [ $count -gt 1 ]; then | |
cmd="sed '/$ff/,/pfs_spawn_thread/!d;/pfs_spawn_thread/q' $FILE" | |
else | |
cmd="sed -n '/$ff/,/pfs_spawn_thread/p' $FILE" | |
fi | |
eval $cmd \ | |
| sed -e 's/0x.* in //g' -e 's$ (.*-$ https://github.com/mysql/mysql-server/blob/mysql-$g' -e 's/\(.*\):/\1#L/g' \ | |
| grep -Ev "^$" \ | |
| awk -F'-' '{split($3,c,"#L"); split($1,f," "); \ | |
mutex="curl -s https://raw.githubusercontent.com/mysql/mysql-server/mysql-" c[1] "|sed -n \"/" f[2] "(/," c[2] "p\" | egrep --color=always \"mysql_mutex_lock|mysql_mutex_unlock\"|sed \"s/^[ \t]*/\t/g\""; \ | |
location="curl -s https://raw.githubusercontent.com/mysql/mysql-server/mysql-" c[1] "|sed -n \"s/^[[:space:]]*//;" c[2] "p\""; \ | |
printf $0 "\033[31m Location \033[0m"; system(location); if ($0 ~ "pfs_spawn_thread" ) {printf("\n")}; system(mutex);}' | |
# Mutex point between the beginning of the function and the call position | |
echo "${GREEN}========================= Call Stack End.. =========================${NC}\n\n" | |
done | tee -a $DETAIL_FILE | |
echo "${RED}######################### Call Stack Analyze End.. #########################${NC}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment