Skip to content

Instantly share code, notes, and snippets.

@netmarkjp
Created January 9, 2014 12:32
Show Gist options
  • Save netmarkjp/8333420 to your computer and use it in GitHub Desktop.
Save netmarkjp/8333420 to your computer and use it in GitHub Desktop.
show PID:(Shared Ratio) Shared_* kB / RSS kB on each child process
#!/bin/bash
if [ "`whoami`" != "root" ];then
echo "ERROR: must run as root"
exit 1
fi
TARGET_PPID=$1
if [ "${TARGET_PPID}x" == "x" ]; then
echo "Usage: $0 <TARGET_PPID>"
exit 1
fi
TARGET_PIDS=$(pgrep -P ${TARGET_PPID:?})
if [ "${TARGET_PIDS}x" == "x" ]; then
echo "ERROR: no child found!"
echo "Usage: $0 <TARGET_PPID>"
exit 1
fi
echo "PID:(Shared Ratio) Shared_* kB/ RSS kB"
echo "------------------------------------------"
shared_mem_all=0
rss_mem_all=0
for TARGET_PID in ${TARGET_PIDS:?}
do
shared_mem_total=0
for shared_mem in `egrep '^Shared_(Clean|Dirty):' /proc/${TARGET_PID:?}/smaps | awk '{print $2}'`
do
shared_mem_total=$(($shared_mem_total + $shared_mem))
done
rss_mem_total=0
for rss_mem in `egrep '^Rss:' /proc/${TARGET_PID:?}/smaps | awk '{print $2}'`
do
rss_mem_total=$(($rss_mem_total + $rss_mem))
done
shared_ratio=$((100 * $shared_mem_total / $rss_mem_total))
echo "${TARGET_PID:?}:(${shared_ratio}%) ${shared_mem_total} kB / ${rss_mem_total} kB"
shared_mem_all=$(($shared_mem_all + $shared_mem_total))
rss_mem_all=$(($rss_mem_all + $rss_mem_total))
done
echo "------------------------------------------"
shared_ratio_all=$((100 * $shared_mem_all / $rss_mem_all))
echo "avg:${shared_ratio_all}%, total:${shared_mem_all} kB / ${rss_mem_all} kB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment