Skip to content

Instantly share code, notes, and snippets.

@aprell
Created October 8, 2018 13:53
Show Gist options
  • Select an option

  • Save aprell/cd6e079ae3ec6d360fd7da8e65554d9e to your computer and use it in GitHub Desktop.

Select an option

Save aprell/cd6e079ae3ec6d360fd7da8e65554d9e to your computer and use it in GitHub Desktop.
A poor man's visualization
#!/usr/bin/env bash
# Code inspired by https://poormansprofiler.org
set -eu
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <program>"
exit 0
fi
# If pidof doesn't find a process ID for the given program name, it exits with
# status 1, causing this script to exit as well (set -e).
pid=$(pidof "$1")
sleeptime=0
logfile="${1}_${pid}.log"
# Sample as long as the process is running
while ps -p "$pid" > /dev/null; do
gdb -ex "thread apply all p deque->num_tasks" -batch -p "$pid" | awk -f print_tasks.awk
sleep $sleeptime
done > "$logfile"
# Generate script for visualization
visfile="vis_${logfile%.*}.sh"
> "$visfile"
cat > "$visfile" <<CODE
#!/usr/bin/env bash
# Displays sparklines (https://github.com/holman/spark)
set -eu
while read -r line; do
clear
printf "\n\n\n\n\n"
printf " "
spark "\$line"
printf " "
echo "\$line" | awk '{
for (i = 1; i <= NF; i++) {
if (\$i == 0) printf "0"
else printf " "
}
printf "\n"
}'
printf "\n\n\n\n\n"
sleep 0.3
done < "$logfile"
CODE
chmod u+x "$visfile"
read -p "Start visualization? [y/n] " -n 1
echo
if [[ $REPLY == "y" ]]; then
./"$visfile"
fi
# Process debugging output that looks like this:
#
# Thread 8 (Thread 0x7ffff4812700 (LWP 8635)):
# $9 = 3
#
# Thread 7 (Thread 0x7ffff5013700 (LWP 8634)):
# $10 = 1
#
# Thread 6 (Thread 0x7ffff5814700 (LWP 8633)):
# $11 = 0
#
# [...]
/^Thread/ {
curr_thread = $2
}
/^\$/ {
tasks[curr_thread] = $3
}
END {
for (i in tasks) {
printf "%d ", tasks[i]
}
printf "\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment