Skip to content

Instantly share code, notes, and snippets.

@JGrossholtz
Last active September 3, 2019 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JGrossholtz/a206952f8f01454b3466af69f5fec65c to your computer and use it in GitHub Desktop.
Save JGrossholtz/a206952f8f01454b3466af69f5fec65c to your computer and use it in GitHub Desktop.
Quick CPU/process consumption analysis using mpstat&top
# run mpstat and top for 5 minutes, at then end, get a loadaverage value
export DURATION=300 # run for 5 minutes
rm -rf /tmp/analysis; mkdir -p /tmp/analysis; $(timeout -t $DURATION -s2 sh -c './mpstat 1 >/tmp/analysis/mpstat.txt') & $(timeout -t $DURATION -s2 sh -c 'top -b -d1 >/tmp/analysis/top.txt') & sleep $DURATION; cat /proc/loadavg >/tmp/analysis/loadavg.txt
# Filter top output (necessary as the top binary on my embedded system lack many options)
export PROCESS_NAME=communication
cat /tmp/analysis/top.txt | grep $PROCESS_NAME | awk '{ print $7 }' | sed 's/%//g' > /tmp/analysis/top_filtered.txt
awk '{ total += $1 } END { print total/NR }' /tmp/analysis/top_filtered.txt > /tmp/analysis/top_avg.txt
cat /tmp/analysis/top_filtered.txt | sort -nr | uniq -c > /btmp/analysis/top_weighted_categories.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment