Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
The following code is for monitoring the memory used by apache processes. But I got a problem that the data I got by this script is much larger than the physical memory. It was said that there are some libraries used simultaneously by many processes, so the data I got has some double counted part. Because apache has many httpd processes.
Anyone have an idea of getting the multi-processes memory used? Thanks a lot!
Code:
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
TOTAL_VM_SIZE=0
TOTAL_VM_RSS=0
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
let TOTAL_VM_SIZE+=VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
echo "$ELAPSED_TIME,$TOTAL_VM_SIZE,$TOTAL_VM_RSS" >> "$LOG_FILE"
sleep "$PERIOD"
ELAPSED_TIME=`date +%H:%M:%S:%N`
done
Maybe subtracting VmLib from each process will be more accurate...
cheers
but the VmLib is often larger than the VmRss e.g.
VmPeak: 67980 kB
VmSize: 67976 kB
VmLck: 0 kB
VmHWM: 5676 kB
VmRSS: 5672 kB
VmData: 4504 kB
VmStk: 84 kB
VmExe: 372 kB
VmLib: 15324 kB
And after I revising my script as the following
Code:
#!/bin/bash
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=0.5 # seconds
while :
do
TOTAL_VM_SIZE=0
TOTAL_VM_RSS=0
VmLib=0
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
VmLib=`awk '/VmLib/ {print $2}' < "/proc/$PID/status"`
if [ "$VmLib.X" = ".X" ]; then
continue
fi
let REAL_VM_SIZE=VM_SIZE-VmLib
let TOTAL_VM_SIZE+=REAL_VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
fi
done
echo "$ELAPSED_TIME,$TOTAL_VM_SIZE,$TOTAL_VM_RSS,$MemFree" >> "$LOG_FILE"
sleep "$PERIOD"
ELAPSED_TIME=`date +%H:%M:%S:%N`
done
I got the result : ElapsedTime VmSize VmRSS
14:08:14:487053684 31643556 3456272
14:08:28:750838950 31643556 3456272
It seems what you really want is the sum of private RSS ... someone has already written a script to do what you are trying to do though if that helps .. http://www.pixelbeat.org/scripts/ps_mem.py
It seems what you really want is the sum of private RSS ... someone has already written a script to do what you are trying to do though if that helps .. http://www.pixelbeat.org/scripts/ps_mem.py
cheers
Thank you very much! I think it will help alot. I will try it now.
It seems what you really want is the sum of private RSS ... someone has already written a script to do what you are trying to do though if that helps .. http://www.pixelbeat.org/scripts/ps_mem.py
cheers
It is good! But I need to record the output every several seconds in a log file. I don't know python. Is there a way to run it periodically can write the realtime output to file?
Maybe it's handling the load with the currently allocated resources .. ?
Is there a way to run this script in a high priority? Because when the server is saturated, there is no output until the workload is low. I want the script run inspite of high workload.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.