Skip to content

Instantly share code, notes, and snippets.

@aminamid
Last active August 29, 2015 14:18
Show Gist options
  • Save aminamid/ee0ec2697a91a3ea00e9 to your computer and use it in GitHub Desktop.
Save aminamid/ee0ec2697a91a3ea00e9 to your computer and use it in GitHub Desktop.
cpu usage per cpu per pid
#!/bin/bash
# from stackoverflow http://stackoverflow.com/questions/3342889/how-to-measure-separate-cpu-core-usage-for-a-process
# Note: These stats are based on process lifetime, not the last X seconds, so
# you'll need to restart your process to reset the counter.
pids=()
while [ $# != 0 ]; do
pids=("${pids[@]}" "$1")
shift
done
if [ -z "${pids[0]}" ]; then
echo "Usage: $0 <pid1> [pid2] ..."
exit 1
fi
for pid in "${pids[@]}"; do
if [ ! -e /proc/$pid ]; then
echo "Error: pid $pid doesn't exist"
exit 1
fi
done
while [ true ]; do
echo -e "\033[H\033[J"
for pid in "${pids[@]}"; do
ps -p $pid -L -o pid,tid,psr,pcpu,comm= | grep -v PSR | awk '{sum[$3" "$1" "$5]+=$4};END { for (key in sum) {print key,sum[key]}}'
done
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment