【01】valgrind
view
valgrind --log-file='valgrind_report.log' --time-stamp=yes --tool=memcheck --leak-check=full --show-leak-kinds=all ./exec
【02】gdb
view
gdb -iex 'set pagination off' -iex 'set confirm off' -iex 'set print elements 0' -ex 'thread apply all bt' -ex 'quit' attach -p
gdb 条件断点 字符串比较 $_streq()
【03】查看消息队列状态
view
ipcs -qp | awk '/3193/{print "ipcs -qp -i "$1}' | bash
【05】perf
view
cs_pid=$(cat /var/run/cs.pid); perf record -F 99 -p $cs_pid --call-graph dwarf -- sleep 60
直接在控制台上查看:perf report
或者拷贝到本地生成火焰图:
perf script --header > out.stacks
---
生成FlameGraph
拷贝out.stacks文件到本地PC
git clone https://github.com/brendangregg/FlameGraph
cd FlameGraph/
./stackcollapse-perf.pl < ../out.stacks | ./flamegraph.pl --hash > out.svg
【06】top
view
top -n 1 -p $(pidof cs) -H -b
打印某个进程占用CPU的线程:
top -H -p $(cat /var/run/$1.pid) -o PPID -d 1 -b -w 512 | awk -F' ' '/(^top)|(^\s*[0-9])/{
if ($1~/[0-9]/) {
if ($9 > 90) arr[$1]=$12"["$9"]";
} else {
for (i in arr) line=line sep arr[i];
if (line != "") print $3,line;
delete arr;line="";
}
}'
【07】tail
view
tail命令 对于有进行日志分割的使用-F替代-f,详细说明见 tail --help
【08】mysql binlog:
view
for name in $(ls -rt binlog.0*); do echo $name;/usr/bin/mysqlbinlog --no-defaults --base64-output=decode-rows --start-datetime='2023-09-22 20:30:00' --stop-datetime='2023-09-25 09:30:00' -d stbDb -v /var/lib/mysql/${name} --result-file=4141_${name}.sql; done
【09】日志分析
view
统计请求时间分布: egrep --no-filename '^2023-10-09 17:2[3-4].*_WsServerCallBack.*Sending message' $(ls -rt cs*log | tail -n 500) -A 3 | grep forwardMsg -B 2 | egrep '^2023' | cut -c12-19 | awk '{x[$1]++;} END{for(i in x) print(i ":" x[i])}' | sort
> echo "2023-10-09 17:24:07.857568" | cut -c12-19 => 17:24:07
【10】日志分析
view
# 使用 bash mlog-save.sh "2023-10-10 09:00:00"
[ "$1" != "" ] && {
dir_name="mlog_${1}"
dir_name=${dir_name/ /-}
mkdir $dir_name
echo "FILTER LOGS..."
log_start=$(egrep -l "^$1" $(ls -rt cs*log | tail -n 500) | head -n 1)
[ "$log_start" != "" ] && {
echo "FILTER LOGS SUCCESS, SAVE LOG"
cp -a $(ls -rt cs*log | egrep -w "^${log_start}$" -A 500) $dir_name/
cd $dir_name || exit
log_list=$(ls -rt cs*log)
echo "$log_list" > log-list.txt
echo -e "$dir_name:\n$log_list\n"
[ "$log_list" != "" ] && {
first_line=$(egrep -nr -m 1 "^$1" $log_list | head -n 1)
[ "$first_line" != "" ] && {
first_line_file_name=$(awk -F':' '{print $1}' <<< "$first_line")
first_line_start_num=$(awk -F':' '{print $2}' <<< "$first_line")
[ "$first_line_file_name" != "" ] && {
[ $first_line_start_num -gt 1 ] && {
echo "trim log: sed -i \"1,${first_line_start_num}d\" $first_line_file_name"
sed -i "1,${first_line_start_num}d" $first_line_file_name
}
}
echo "数据统计:"
# ...
}
}
}
}