Brought to you by : Chandan.
 
HomeHome  FAQFAQ  SearchSearch  RegisterRegister  MemberlistMemberlist  UsergroupsUsergroups  Log in  
Share | 
 

 How much RAM is used per program .

View previous topic View next topic Go down 
AuthorMessage
chandan



Number of posts: 11
Age: 29
Registration date: 2007-04-13

PostSubject: How much RAM is used per program .   Sun Sep 09, 2007 7:22 pm

Note : The following Script determines, the RAM used by a program, including accumulated usages for process spawn by a program.

This script is written by Pádraig Brady , He can be contacted at P@draigBrady.com. The script itself follows :

Code:

#!/usr/bin/env python


# Try to determine how much RAM is currently being used per program.
# Note the per program, not per process. So for example this script
# will report mem used by all httpd process together. In detail it reports:
# sum(all RSS for process instances) + max(shared mem for any process instance)
#
# The shared calculation below will factor out shared text and
# libs etc. within a program, but not between programs. So there
# will always be some overestimation. This will be the same for
# all processes that just use libc for e.g. but more for others
# that use larger shared libs like gnome, kde etc.


# Author: P@draigBrady.com

# V1.0      06 Jul 2005    Initial release
# V1.1      11 Aug 2006    root permission required for accuracy
# V1.2      08 Nov 2006    Add total to output
#                          Use KiB,MiB,... for units rather than K,M,...
# V1.3      22 Nov 2006    Ignore shared col from /proc/$pid/statm for
#                          2.6 kernels up to and including 2.6.9.
#                          There it represented the total file backed extent
# V1.4      23 Nov 2006    Remove total from output as it's meaningless
#                          (the shared values overlap with other programs).
#                          Display the shared column. This extra info is
#                          useful, especially as it overlaps between programs.
# V1.5      26 Mar 2007    Remove redundant recursion from human()
# V1.6      05 Jun 2007    Also report number of processes with a given name.
#                          Patch from riccardo.murri@gmail.com

# Notes:
#
# All interpreted programs where the interpreter is started
# by the shell or with env, will be merged to the interpreter
# (as that's what's given to exec). For e.g. all python programs
# starting with "#!/usr/bin/env python" will be grouped under python.
# You can change this by changing comm= to args= below but that will
# have the undesirable affect of splitting up programs started with
# differing parameters (for e.g. mingetty tty[1-6]).
#
# For 2.6 kernels up to and including 2.6.13 and later 2.4 redhat kernels
# (rmap vm without smaps) it can not be accurately determined how many pages
# are shared between processes in general or within a program in our case:
# http://lkml.org/lkml/2005/7/6/250
# A warning is printed if overestimation is possible.
# In addition for 2.6 kernels up to 2.6.9 inclusive, the shared
# value in /proc/$pid/statm is the total file-backed extent of a process.
# We ignore that, introducing more overestimation, again printing a warning.
#
# I don't take account of memory allocated for a program
# by other programs. For e.g. memory used in the X server for
# a program could be determined, but is not.
#
# This script assumes threads are already merged by ps

# TODO:
#
# use ps just to enumerate the pids and names
# so as to remove the race between reading rss and shared values

import sys, os, string

if os.geteuid() != 0:
    sys.stderr.write("Sorry, root permission required.\n");
    sys.exit(1)

PAGESIZE=os.sysconf("SC_PAGE_SIZE")/1024 #KiB
our_pid=os.getpid()

#(major,minor,release)
def kernel_ver():
    kv=open("/proc/sys/kernel/osrelease").readline().split(".")[:3]
    for char in "-_":
        kv[2]=kv[2].split(char)[0]
    return (int(kv[0]), int(kv[1]), int(kv[2]))

kv=kernel_ver()

def getShared(pid):
    if os.path.exists("/proc/"+str(pid)+"/smaps"):
        shared_lines=[line
                      for line in open("/proc/"+str(pid)+"/smaps").readlines()
                      if line.find("Shared")!=-1]
        return sum([int(line.split()[1]) for line in shared_lines])
    elif (2,6,1) <= kv <= (2,6,9):
        return 0 #lots of overestimation, but what can we do?
    else:
        return int(open("/proc/"+str(pid)+"/statm").readline().split()[2])*PAGESIZE

cmds={}
shareds={}
count={}
for line in os.popen("ps -e -o rss=,pid=,comm=").readlines():
    size, pid, cmd = map(string.strip,line.strip().split(None,2))
    if int(pid) == our_pid:
        continue #no point counting this process
    try:
        shared=getShared(pid)
    except:
        continue #ps gone away
    if shareds.get(cmd):
        if shareds[cmd] < shared:
            shareds[cmd]=shared
    else:
        shareds[cmd]=shared
    #Note shared is always a subset of rss (trs is not always)
    cmds[cmd]=cmds.setdefault(cmd,0)+int(size)-shared
    if count.has_key(cmd):
      count[cmd] += 1
    else:
      count[cmd] = 1

#Add max shared mem for each program
for cmd in cmds.keys():
    cmds[cmd]=cmds[cmd]+shareds[cmd]

sort_list = cmds.items()
sort_list.sort(lambda x,y:cmp(x[1],y[1]))
sort_list=filter(lambda x:x[1],sort_list) #get rid of zero sized processes (kernel threads)

#The following matches "du -h" output
#see also human.py
def human(num, power="Ki"):
    powers=["Ki","Mi","Gi","Ti"]
    while num >= 1000: #4 digits
        num /= 1024.0
        power=powers[powers.index(power)+1]
    return "%.1f %s" % (num,power)

def cmd_with_count(cmd, count):
    if count>1:
      return "%s (%u)" % (cmd, count)
    else:
      return cmd
print " Private  +  Shared  =  RAM used\tProgram \n"
for cmd in sort_list:
    print "%8sB + %8sB = %8sB\t%s" % (human(cmd[1]-shareds[cmd[0]]), human(shareds[cmd[0]]), human(cmd[1]),
                                      cmd_with_count(cmd[0], count[cmd[0]]))
print "\n Private  +  Shared  =  RAM used\tProgram \n"

#Warn of possible inaccuracies
#1 = accurate
#0 = some shared mem not reported
#-1= all shared mem not reported
def shared_val_accurate():
    """http://wiki.apache.org/spamassassin/TopSharedMemoryBug"""
    if kv[:2] == (2,4):
        if open("/proc/meminfo").read().find("Inact_") == -1:
            return 1
        return 0
    elif kv[:2] == (2,6):
        if os.path.exists("/proc/"+str(os.getpid())+"/smaps"):
            return 1
        if (2,6,1) <= kv <= (2,6,9):
            return -1
        return 0
    else:
        return 1

vm_accuracy = shared_val_accurate()
if vm_accuracy == -1:
    sys.stderr.write("Warning: Shared memory is not reported by this system.\n")
    sys.stderr.write("Values reported will be too large.\n")
elif vm_accuracy == 0:
    sys.stderr.write("Warning: Shared memory is not reported accurately by this system.\n")
    sys.stderr.write("Values reported could be too large.\n")


For example you can call the script, as following to see the memory usage of openoffice word program ( swriter ).

Code:
[root@chandan-gateway chandan]# ./ps_mem.py | grep swriter
 45.3 MiB +  6.4 MiB =  51.7 MiB      swriter.bin

now lets check how much my firefox is using :
Code:

[root@chandan-gateway chandan]# ./ps_mem.py | grep firefox
105.6 MiB +  8.4 MiB = 114.0 MiB      firefox-bin
[root@chandan-gateway chandan]#

wow, a whopping 140 MB.

Now lets check how much my kde friends are eating up .

Code:
[root@chandan-gateway chandan]# ./ps_mem.py | grep kde
 52.0 KiB +  84.0 KiB = 136.0 KiB      start_kdeinit
148.0 KiB +  1.0 MiB =  1.1 MiB      startkde
268.0 KiB +  3.4 MiB =  3.7 MiB      kdeinit
  3.3 MiB +  8.3 MiB =  11.6 MiB      kded
  4.1 MiB +  11.0 MiB =  15.1 MiB      kdesktop
]


Finally, if you want to see the RAM Usage for all programs ( remember not processes ) than you can do it as follows :

Code:
[root@chandan-gateway chandan]# ./ps_mem.py
 Private  +  Shared  =  RAM used      Program

 52.0 KiB +  84.0 KiB = 136.0 KiB      start_kdeinit
 68.0 KiB + 264.0 KiB = 332.0 KiB      kwrapper
112.0 KiB + 240.0 KiB = 352.0 KiB      gpm
 80.0 KiB + 320.0 KiB = 400.0 KiB      klogd
112.0 KiB + 296.0 KiB = 408.0 KiB      atd
 96.0 KiB + 340.0 KiB = 436.0 KiB      hidd
168.0 KiB + 284.0 KiB = 452.0 KiB      nasd
116.0 KiB + 380.0 KiB = 496.0 KiB      dbus-launch
300.0 KiB + 220.0 KiB = 520.0 KiB      ssh-agent
300.0 KiB + 236.0 KiB = 536.0 KiB      smartd
348.0 KiB + 220.0 KiB = 568.0 KiB      rpc.idmapd
112.0 KiB + 516.0 KiB = 628.0 KiB      pppoe
204.0 KiB + 432.0 KiB = 636.0 KiB      auditd
136.0 KiB + 500.0 KiB = 636.0 KiB      syslogd
224.0 KiB + 428.0 KiB = 652.0 KiB      audispd
152.0 KiB + 512.0 KiB = 664.0 KiB      dhcdbd
128.0 KiB + 536.0 KiB = 664.0 KiB      sdpd
132.0 KiB + 544.0 KiB = 676.0 KiB      init
260.0 KiB + 448.0 KiB = 708.0 KiB      rpcbind
272.0 KiB + 440.0 KiB = 712.0 KiB      thttpd
144.0 KiB + 592.0 KiB = 736.0 KiB      rpc.statd
260.0 KiB + 476.0 KiB = 736.0 KiB      hpiod
352.0 KiB + 404.0 KiB = 756.0 KiB      mingetty (6)
104.0 KiB + 688.0 KiB = 792.0 KiB      hald-addon-acpi
136.0 KiB + 732.0 KiB = 868.0 KiB      hald-addon-stor
172.0 KiB + 736.0 KiB = 908.0 KiB      hald-runner
432.0 KiB + 508.0 KiB = 940.0 KiB      sshd
632.0 KiB + 348.0 KiB = 980.0 KiB      udevd
548.0 KiB + 448.0 KiB = 996.0 KiB      dbus-daemon (2)
144.0 KiB + 868.0 KiB =  1.0 MiB      pppoe-connect
344.0 KiB + 704.0 KiB =  1.0 MiB      hald-addon-keyb (4)
224.0 KiB + 836.0 KiB =  1.0 MiB      hcid
468.0 KiB + 624.0 KiB =  1.1 MiB      pppd
568.0 KiB + 532.0 KiB =  1.1 MiB      crond
132.0 KiB + 984.0 KiB =  1.1 MiB      soffice
272.0 KiB + 880.0 KiB =  1.1 MiB      su
148.0 KiB +  1.0 MiB =  1.1 MiB      startkde
540.0 KiB + 632.0 KiB =  1.1 MiB      automount
836.0 KiB + 548.0 KiB =  1.4 MiB      pcscd
432.0 KiB + 996.0 KiB =  1.4 MiB      avahi-daemon (2)
520.0 KiB +  1.3 MiB =  1.8 MiB      dcopserver
596.0 KiB +  1.3 MiB =  1.8 MiB      bash (2)
884.0 KiB +  1.1 MiB =  2.0 MiB      iscsid (2)
856.0 KiB +  1.2 MiB =  2.0 MiB      console-kit-dae
  1.6 MiB + 956.0 KiB =  2.5 MiB      xfs
  1.8 MiB +  1.2 MiB =  3.0 MiB      hald
  2.1 MiB +  1.5 MiB =  3.6 MiB      gconfd-2
268.0 KiB +  3.4 MiB =  3.7 MiB      kdeinit
  3.7 MiB + 760.0 KiB =  4.5 MiB      python
308.0 KiB +  4.3 MiB =  4.6 MiB      kio_file
  1.1 MiB +  4.0 MiB =  5.0 MiB      gdm-binary (3)
432.0 KiB +  5.0 MiB =  5.5 MiB      kio_trash
520.0 KiB +  5.2 MiB =  5.8 MiB      klauncher
  1.1 MiB +  4.8 MiB =  5.9 MiB      ksmserver
  5.0 MiB +  2.0 MiB =  7.0 MiB      artsd
  1.1 MiB +  6.1 MiB =  7.1 MiB      kaccess
  2.2 MiB +  6.7 MiB =  8.9 MiB      kpowersave
  1.7 MiB +  7.4 MiB =  9.1 MiB      knotify
  1.8 MiB +  7.6 MiB =  9.4 MiB      klipper
  7.6 MiB +  2.8 MiB =  10.4 MiB      yum-updatesd
  3.3 MiB +  8.3 MiB =  11.6 MiB      kded
  2.6 MiB +  9.7 MiB =  12.3 MiB      kwin
  4.1 MiB +  11.0 MiB =  15.1 MiB      kdesktop
  5.2 MiB +  10.6 MiB =  15.8 MiB      kicker
  6.3 MiB +  11.2 MiB =  17.5 MiB      konsole
 20.9 MiB +  2.0 MiB =  22.9 MiB      Xorg
 45.3 MiB +  6.4 MiB =  51.7 MiB      swriter.bin
105.6 MiB +  8.4 MiB = 114.0 MiB      firefox-bin

 Private  +  Shared  =  RAM used      Program

[root@chandan-gateway chandan]#


See, it lists the memory usage for processes, sorted in reverse order, that means the last process listed is using the highest memory, in this case it is Firefox using 140 MB memory.



Hope this is useful.

Regards,

-- Chandan

Chandan.Maddanna@gmail.com
Back to top Go down
View user profile http://chandan-m.blogspot.com/
 

How much RAM is used per program .

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Fellowship Night Program
» Aries Program--Version 1.0
» New Minecraft Roleplay - Planning and building stage
» Pacifist Hacking Technomancer (First SR char, help requested)
» 110803 miss A Stunned by SNSD's Diet Plan: "We're Meat Lovers"

Permissions in this forum:You cannot reply to topics in this forum
 :: General Unix Admin stuff :: General Linux Admin Stuff-