Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > Your Distro > Redhat / Fedora Linux Help > how to programmatically monitor a process memory usage?

Forgot Password?
 Redhat / Fedora Linux Help   Help and discussion related to Redhat and Fedora Linux.

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 11-16-2005   #1 (permalink)
Just Joined!
 
Join Date: Jun 2005
Posts: 23
how to programmatically monitor a process memory usage?

I need to obtain the memory usage (real and virtual) of a particular process every couple of seconds and write it to a log file. I'm thinking of writing either shell script (sh or bash) or a C/C++ program. What shell commands and/or C/C++ API's are available for this task?

Thanks,
thanhvn is offline  


Old 11-16-2005   #2 (permalink)
Trusted Penguin
 
Cabhan's Avatar
 
Join Date: Jan 2005
Location: Boston, MA, USA
Posts: 2,691
You might want to take a look at the
Code:
ps aux
command for determining process memory usage.

Personally, I'd probably use a regular expression to match the line corresponding to the program and grab out the memory usage, then simply print that to the log file. You could set it to run as a cron job at whatever interval you need it.
__________________
DISTRO=Gentoo
Registered Linux User #388732
Gentoo Linux, 410 GB HD, 1.2 GB RAM, Fluxbox, These are a Few of my Favorite Things
Cabhan is offline  
Old 11-16-2005   #3 (permalink)
Just Joined!
 
Join Date: Jun 2005
Posts: 23
I am wondering:

1) how to get this GetMemUsage script and the program it monitors to start up at the same time? I don't want to start up the program then open a second console (the program writes to stdout) to launch the script. This means I would miss the first second or two of the program.

2) how to get this GetMemUsage script to shut down cleanly and automatically when the program it monitors shutdowns, even it shuts down abnormally? I would like the ability to batch run the program several times.

Thanks,
thanhvn is offline  
Old 11-16-2005   #4 (permalink)
Just Joined!
 
Join Date: Jun 2005
Posts: 23
Also, how to get the elapsed time since the program started (preferrably in milliseconds or better resolution)? other than saving the start time and subtract it from the current time? what if I wanted CPU time and not wall-clock time?

Thanks,
thanhvn is offline  
Old 11-16-2005   #5 (permalink)
Just Joined!
 
Join Date: Nov 2005
Location: /home/eclipse
Posts: 18
Send a message via MSN to zOrK
solution

are u tried with top?


Code:
top

regards.
zOrK is offline  
Old 11-16-2005   #6 (permalink)
Just Joined!
 
Join Date: Jun 2005
Posts: 23
zOrk, top is interactive; i don't want interactive, i want a script/program that is:
a) launched automatically when the monitored process is launched,
b) obtain the elapsed time and mem usage, log it to a file, (as efficiently as possible; i don't want this script to have anything other than a negligible effect on the monitored process's performance)
c) goes to sleep for x seconds
d) wakes up and do steps b & c again
e) cleanly and automatically shutdown/exit when the monitored process is shutdown/exit (even if it is exited abnormally or interrupted with Ctrl-C)
any ideas? anyone?
thanhvn is offline  
Old 11-17-2005   #7 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Blackburn, North West England (UK)
Posts: 50
Have you attempted to script this yourself? If so, let us have a look at how far you have got them maybe we could help from there. As i am loath to do your admin work from scratch without anything other from you than your requirements.
skrye is offline  
Old 11-21-2005   #8 (permalink)
Just Joined!
 
Join Date: Jun 2005
Posts: 23
Fair enough. Here's what I've got so far. This is the GetMemUsage script:

Code:
#!/bin/sh

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=`/sbin/pidof $1`

   if [ "$PROCESS_PID.X" != ".X" ]; then
      break
   fi
done

LOG_FILE="memusage.csv"

echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE

ELAPSED_TIME=0
PERIOD=2        # seconds

# Monitor memory usage forever until script is killed
while :        
do
   VM_SIZE=`awk '/VmSize/ {print $2}' < /proc/$PROCESS_PID/status`
   if [ "$VM_SIZE.X" = ".X" ]; then
      continue
   fi
   VM_RSS=`awk '/VmRSS/ {print $2}' < /proc/$PROCESS_PID/status`
   if [ "$VM_RSS.X" = ".X" ]; then
      continue
   fi
   echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> $LOG_FILE
   sleep $PERIOD
   VM_SIZE=""
   VM_RSS=""
   # Needs to get actual elapsed time instead of doing this
   ELAPSED_TIME=`expr $ELAPSED_TIME + $PERIOD`
done
Anyone knows how to get the actual elapsed time of a process (preferrably in milliseconds resolution but seconds resolution is fine)?

I cannot think of any way to have the GetMemUsage script launches and shuts down in synchronization with the monitored process other than to have a launch script that launches and shuts down both processes. Here's the launch script:

Code:
#!/bin/sh

Cleanup() {
   kill -9 `/sbin/pidof hello.out`
   kill -9 `ps ux | awk '/GetMemUsage.sh/ {print $2}'`
   exit $1
}
trap 'Cleanup 1' 1 2 3 15
./GetMemUsage.sh hello.out 2>&1 > /dev/null &
./hello.out
Cleanup 0
I realized this is not the most elegant way of doing it. Any suggestions for improvements are more than welcome.
thanhvn is offline  
Old 11-25-2005   #9 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Blackburn, North West England (UK)
Posts: 50
Cool, I shall have a go at it this weekend.
skrye is offline  
Old 11-26-2005   #10 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Blackburn, North West England (UK)
Posts: 50
I like the thinking behind this script, I've decided to have a go at re-writing it (maybe in perl) and making it a little more robust because its somthing I myself will find usful i think.

In the interim however i've found a solution to your time issue i think. Its a little basic, and only meant to be a tempory solution but here it is:
Code:
#!/bin/sh
#GetmemUsageModified

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=`/sbin/pidof $1`

   if [ "$PROCESS_PID.X" != ".X" ]; then
      break
   fi
done

LOG_FILE="memusage.csv"

echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE

ELAPSED_TIME=`date`
PERIOD=2        # seconds

# Monitor memory usage forever until script is killed
while :
do
   VM_SIZE=`awk '/VmSize/ {print $2}' < /proc/$PROCESS_PID/status`
   if [ "$VM_SIZE.X" = ".X" ]; then
      continue
   fi
   VM_RSS=`awk '/VmRSS/ {print $2}' < /proc/$PROCESS_PID/status`
   if [ "$VM_RSS.X" = ".X" ]; then
      continue
   fi
   echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> $LOG_FILE
   sleep $PERIOD
   VM_SIZE=""
   VM_RSS=""
   ELAPSED_TIME=`date +%H:%M:%S:%N`
done
As you can see this is a pretty minimal and basic hack, hopefully it will fill your needs untill my attempt at a re-code. :P Oh and i'm working on the shutdown issue, hopefully when i'm finished it will run as a deamon, shutdown at process death, write to a new log each time ( currently as you probably know it overwrites the last log ) and maybe be able to track more than one process.
skrye is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 09:47 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2