[colug-432] Memory usage

Travis Sidelinger travissidelinger at gmail.com
Fri Apr 27 00:02:17 EDT 2012


Colug'ers,

I'm trying find a better way to do this.

Problem:
  How much memory is my multi-process Apache+PHP web service using?

Solution 1:  (The ps command)

The ps command has some ability to report memory usage.  Here is a
small script I wrote where I add up the memory usage from each
process.

    # Yes I know this is not atomic
    PATTERN="webpool1"
    SIZETOTAL=`ps -eo pid,ppid,uid,state,%cpu,size,rss,vsize,time,args
| grep "$PATTERN" | awk -v col=6 '{sum += $col} END {print sum}'`
    RSSTOTAL=`ps -eo pid,ppid,uid,state,%cpu,size,rss,vsize,time,args
| grep "$PATTERN" | awk -v col=7 '{sum += $col} END {print sum}'`
    VSIZETOTAL=`ps -eo
pid,ppid,uid,state,%cpu,size,rss,vsize,time,args | grep "$PATTERN" |
awk -v col=8 '{sum += $col} END {print sum}'`
    echo "  PID  PPID   UID S %CPU    SZ   RSS    VSZ     TIME COMMAND"
    echo "Totals: Size=$SIZETOTAL, RSS=$RSSTOTAL, VSIZE=$VSIZETOTAL"

  Issue with this method: The text memory segments will be shared and
thus summing them will throw off the results.  I need to grab data +
space segments only, but the text segments only needs counted once.

Solution 2: (Using top)

    # run top, enable the memory field "s", then write a .toprc file
    top -bc -n 1 -u webpool1-prod | awk -v col=12 '{sum += $col} END
{print sum}'

  Issue with 2: The memory units are "human readable", and not the
same for each process.  Thus, I can't just add them all up.  Yes, I
could write a perl wrapper that is smart enough to make this work.

Solution 3: (using pmap)

pmap can show the memory usage of a process.  If I add up a programs
"anon" and "stack" memory allocation I can then find the total usage.

Here is an example script:

        pmap)
                if [ -n "$PATTERN" ]
                then
                        MEM_TOTAL=0
                        PROC_COUNT=0
                        MEM_LOW=99999999
                        MEM_HIGH=0
                        for PID in `pgrep -f "$PATTERN"`;
                        do
                            MEM_PROC=`pmap $PID | grep '\[ anon \]' |
awk -v col=2 '{sum += $col} END {print sum}'`;
                            if [ "$MEM_PROC" ];
                            then
                                if [ $MEM_PROC -gt $MEM_HIGH ]; then
MEM_HIGH=$MEM_PROC; fi
                                if [ $MEM_PROC -lt $MEM_LOW  ]; then
MEM_LOW=$MEM_PROC;  fi
                                echo "$PID ${MEM_PROC}K"
                                MEM_TOTAL=$((MEM_TOTAL + MEM_PROC));
                                PROC_COUNT=$((PROC_COUNT+1))
                            fi
                        done
                        MEM_AVG=$((MEM_TOTAL/PROC_COUNT))
                        echo "Allocated size total: ${MEM_TOTAL}K,
Avg: ${MEM_AVG}K, Low: ${MEM_LOW}K, High: ${MEM_HIGH}K"
                        echo "Process count: $PROC_COUNT"
                else
                        echo "Useage: ps anon PATTERN"
                fi
                ;;

I think this method is actuate.  Can anyone vouch for this?

I could just ready /proc/#####/maps directly, but I'm not quite sure
how to determine what's shared or allocated yet.

Are there any other standard built in linux tools I should take a look at?

~Travis


More information about the colug-432 mailing list