-
-
Save rcmorano/5053414 to your computer and use it in GitHub Desktop.
'mu' is a simple script to get real memory usage (not VSZ, not RSS but PSS) of a given PID or process pattern (uses pgrep).Check these didactic URLs for a nice explanation about memory consumption [1] and cache [2]:[1] http://emilics.com/blog/article/mconsumption.html[2] http://blog.scoutapp.com/articles/2010/10/06/determining-free-memory-on-lin…
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/bash | |
# | |
# Copyright (C) 2013, Roberto C. Morano <[email protected]> | |
# | |
# This software is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This software is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this library; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
# | |
# As a special exception, if you link this library with other files to | |
# produce an executable, this library does not by itself cause the | |
# resulting executable to be covered by the GNU General Public License. | |
# This exception does not however invalidate any other reasons why the | |
# executable file might be covered by the GNU General Public License. | |
function get_mu_by_pid() { | |
PID=$1 | |
SMAPS="/proc/$PID/smaps" | |
if [[ -z $(head -n3 $SMAPS 2>/dev/null) ]] | |
then | |
echo "Permission denied for PID $PID" | |
fi | |
for portion in $(grep Pss $SMAPS | awk '{print $2}'); do MU=$(expr $MU + $portion); done | |
if [ ! -z $MU ] | |
then | |
echo $MU | |
else | |
echo "Process does not use memory. Yeah, Really!" | |
fi | |
} | |
PID=$1 | |
MU="" | |
PID_MAX_LEN=$(cat /proc/sys/kernel/pid_max | wc -L) | |
if [ -z $PID ] | |
then | |
BASENAME=$(basename $0) | |
echo "$BASENAME: missing argument" | |
echo "Usage: $BASENAME PID|PROC_PATTERN" | |
exit 1 | |
else | |
# if pid is not numerical then pgrep string and print every ocurrency | |
if [[ -z $(echo $PID | grep "^[0-9]\{1,$PID_MAX_LEN\}$") ]] | |
then | |
for arg in $(pgrep $PID); do echo "$(get_mu_by_pid $arg) ($arg)"; done | |
else | |
get_mu_by_pid $PID | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment