Last active
August 29, 2015 13:57
-
-
Save mjf/9384397 to your computer and use it in GitHub Desktop.
procmemstat - Show total and average memory usage of processes
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 | |
# procmemstat - Show total and average memory usage of processes | |
# Copyright (C) 2012 Matous J. Fialka, <http://mjf.cz/> | |
# Released under the terms of The MIT License | |
# Note: This is just a quick dirty hack... | |
# | |
# Check and parse arguments | |
# | |
if [ $# -lt 1 ] | |
then | |
printf -- 'Usage: procmemstat [procname1 procname2 .. procnameN]\n' | |
exit 0 | |
fi | |
# | |
# Print header | |
# | |
printf -- ' %-14s\t % 7s\t % 7s\t%-32s\n' PROCESS TOTAL AVERAGE REMARK | |
# | |
# For every given process do print memory total and average usage | |
# | |
for _PROCESS in $@ | |
do | |
ps -ylC "$_PROCESS" | | |
awk -vPROCESS="$_PROCESS" ' | |
{ | |
x += $8 | |
y += 1 | |
} | |
END { | |
z = 0 | |
if((y - 1) < 1) { | |
y = 2 | |
z = 1 | |
} | |
printf "%-15s\t%7.1fM\t%7.1fM", PROCESS, x/1024, x/((y-1)*1024) | |
if(z) | |
printf "\tno such process" | |
printf "\n" | |
} | |
' | |
done | | |
# | |
# Print summary | |
# | |
awk ' | |
{ | |
sub(/M$/, nil, $2) | |
sub(/M$/, nil, $3) | |
x += $2 | |
y += $3 | |
} | |
END { | |
printf "\n\t\t%7.1fM\t%7.1fM\tTOTAL\n", x, y | |
} | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it as follows:
Very useful when calculating Apache2 performance tunning parameters.
Note 1: To get rid of the header line, use:
Note 2: To get rid of the total line, use: