Created
June 22, 2015 23:17
-
-
Save nemunaire/f9fb88ea1033c58c29d6 to your computer and use it in GitHub Desktop.
Display memory used by a program, using Linux cgroup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
CGROUP_dir=/sys/fs/cgroup/memory | |
CGROUP_name=${0%.*} | |
CGROUP_limit=550502400 | |
if [ "$EUID" != "0" ] | |
then | |
sudo $0 $@ | |
exit $? | |
fi | |
displ() { | |
MEM_USAGE=$(cat ${CGROUP_dir}/${CGROUP_name}/memory.usage_in_bytes) | |
echo -e "\r~~~ $CPID ~~~ Current memory usage: ${MEM_USAGE}/${CGROUP_limit} ($((${MEM_USAGE} * 100 / ${CGROUP_limit}))%)" | |
} | |
mkdir -p ${CGROUP_dir}/${CGROUP_name} | |
echo $CGROUP_limit > ${CGROUP_dir}/${CGROUP_name}/memory.limit_in_bytes | |
trap quit TERM INT KILL | |
$@ & | |
CPID=$! | |
echo $CPID > ${CGROUP_dir}/${CGROUP_name}/tasks | |
zz=$$ | |
( | |
while kill -0 $CPID 2> /dev/null | |
do | |
sleep 1 | |
displ | |
done | |
kill $zz 2> /dev/null | |
) & | |
DPID=$! | |
quit() { | |
kill -9 $CPID $DPID 2> /dev/null | |
} | |
while kill -0 $CPID 2> /dev/null && read -s 2> /dev/null | |
do | |
displ | |
done | |
quit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment