Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
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 ...
  1. #1
    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,

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,204
    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=Arch
    Registered Linux User #388732

  3. #3
    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,

  4. #4
    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,

  5. #5
    Just Joined!
    Join Date
    Nov 2005
    Location
    /home/eclipse
    Posts
    18

    solution

    are u tried with top?


    Code:
    top

    regards.

  6. #6
    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?

  7. #7
    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.

  8. #8
    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/ &#123;print $2&#125;' < /proc/$PROCESS_PID/status`
       if &#91; "$VM_SIZE.X" = ".X" &#93;; then
          continue
       fi
       VM_RSS=`awk '/VmRSS/ &#123;print $2&#125;' < /proc/$PROCESS_PID/status`
       if &#91; "$VM_RSS.X" = ".X" &#93;; 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&#40;&#41; &#123;
       kill -9 `/sbin/pidof hello.out`
       kill -9 `ps ux | awk '/GetMemUsage.sh/ &#123;print $2&#125;'`
       exit $1
    &#125;
    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.

  9. #9
    Just Joined!
    Join Date
    Jun 2005
    Location
    Blackburn, North West England (UK)
    Posts
    50
    Cool, I shall have a go at it this weekend.

  10. #10
    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&#58; $0 processName"
    
    if &#91; $# -ne 1 &#93;; 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 &#58;
    do
       PROCESS_PID=`/sbin/pidof $1`
    
       if &#91; "$PROCESS_PID.X" != ".X" &#93;; 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 &#58;
    do
       VM_SIZE=`awk '/VmSize/ &#123;print $2&#125;' < /proc/$PROCESS_PID/status`
       if &#91; "$VM_SIZE.X" = ".X" &#93;; then
          continue
       fi
       VM_RSS=`awk '/VmRSS/ &#123;print $2&#125;' < /proc/$PROCESS_PID/status`
       if &#91; "$VM_RSS.X" = ".X" &#93;; then
          continue
       fi
       echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> $LOG_FILE
       sleep $PERIOD
       VM_SIZE=""
       VM_RSS=""
       ELAPSED_TIME=`date +%H&#58;%M&#58;%S&#58;%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.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •