Skip to content

Instantly share code, notes, and snippets.

@SunRunAway
Last active July 3, 2020 12:42
Show Gist options
  • Save SunRunAway/b9ab1ef6a3408bec0e583a481daf92e8 to your computer and use it in GitHub Desktop.
Save SunRunAway/b9ab1ef6a3408bec0e583a481daf92e8 to your computer and use it in GitHub Desktop.
dump_profile.bash <pid> <status_port> <memory threshold(byte)>
#!/bin/bash
# usage:
# dump_profile.bash <pid> <db_port> <status_port> <memory threshold(byte)>
pid=$1
db_port=$2
status_port=$3
mem_threshold=$4
count=0
while true; do
sleep 1;
ret=$(ps -eo rss,pid | awk '$2 == pid && $1*1024>=mem_threshold' pid="${pid}" mem_threshold="${mem_threshold}")
if [[ $(echo "${ret}" | wc -l) == "1" ]]; then
mem_used=$(echo "${ret}" | awk '{print $1*1024}')
now=$(date +%s)
fname_sql="/tmp/${now}.processlist"
fname="/tmp/${now}.heap.profile"
echo "pid ${pid} exceeds threshold(${mem_used}>$mem_threshold), captures a heap profile into ${fname}, captures a processlist in ${fname_sql}"
mysql -h127.0.0.1 -uroot -P"${db_port}" <<< "select * from information_schema.processlist order by MEM desc" > "${fname_sql}"
curl -s "http://localhost:${status_port}/debug/pprof/heap" > "${fname}"
count=$((count+1))
if (( count >= 3 )); then
exit 0
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment