I don remember where I got this really neat python script from , but
I generally use it to check out the memory usage, and have found it to
be quite accurate :
__________________________________________________________________________
import sys, os, string
if os.getuid() !=0:
sys.stderr.write("Running as non-root user\n");
# sys.exit(1)
#if os.getuid() !=500:
# sys.stderr.write("Other than root only Satish can run this script\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()
have_pss=0
#return Private,Shared
#Note shared is always a subset of rss (trs is not always)
def getMemStats(pid):
global have_pss
Private_lines=[]
Shared_lines=[]
Pss_lines=[]
Rss=int(open("/proc/"+str(pid)+"/statm").readline().split()[1])*PAGESIZE
if os.path.exists("/proc/"+str(pid)+"/smaps"): #stat
for line in open("/proc/"+str(pid)+"/smaps").readlines(): #open
if line.startswith("Shared"):
Shared_lines.append(line)
elif line.startswith("Private"):
Private_lines.append(line)
elif line.startswith("Pss"):
have_pss=1
Pss_lines.append(line)
Shared=sum([int(line.split()[1]) for line in Shared_lines])
Private=sum([int(line.split()[1]) for line in Private_lines])
#Note Shared + Private = Rss above
#The Rss in smaps includes video card mem etc.
if have_pss:
pss_adjust=0.5 #add 0.5KiB as this average error due to trunctation
Pss=sum([float(line.split()[1])+pss_adjust for line in Pss_lines])
Shared = Pss - Private
elif (2,6,1) <= kv <= (2,6,9):
Shared=0 #lots of overestimation, but what can we do?
Private = Rss
else:
Shared=int(open("/proc/"+str(pid)+"/statm").readline().split()[2])
Shared*=PAGESIZE
Private = Rss - Shared
return (Private, Shared)
def getCmdName(pid):
cmd = file("/proc/%d/status" % pid).readline()[6:-1]
exe = os.path.basename(os.path.realpath("/proc/%d/exe" % pid))
if exe.startswith(cmd):
cmd=exe #show non truncated version
#Note because we show the non truncated name
#one can have separated programs as follows:
#584.0 KiB + 1.0 MiB = 1.6 MiB mozilla-thunder (exe -> bash)
# 56.0 MiB + 22.2 MiB = 78.2 MiB mozilla-thunderbird-bin
return cmd
cmds={}
shareds={}
count={}
for pid in os.listdir("/proc/"):
try:
pid = int(pid) #note Thread IDs not listed in /proc/ which is good
if pid == our_pid: continue
except:
continue
try:
cmd = getCmdName(pid)
except:
#permission denied or
#kernel threads don't have exe links or
#process gone
continue
try:
private, shared = getMemStats(pid)
except:
continue #process gone
if shareds.get(cmd):
if have_pss: #add shared portion of PSS together
shareds[cmd]+=shared
elif shareds[cmd] < shared: #just take largest shared val
shareds[cmd]=shared
else:
shareds[cmd]=shared
cmds[cmd]=cmds.setdefault(cmd,0)+private
if count.has_key(cmd):
count[cmd] += 1
else:
count[cmd] = 1
#Add shared mem for each program
total=0
for cmd in cmds.keys():
cmds[cmd]=cmds[cmd]+shareds[cmd]
total+=cmds[cmd] #valid if PSS available
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
#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]]))
if have_pss:
print "-" * 33
print " " * 24 + "%8sB" % human(total)
print "=" * 33
print "\n Private + Shared = RAM used\tProgram \n"
#Warn of possible inaccuracies
#2 = accurate & can total
#1 = accurate only considering each process in isolation
#0 = some shared mem not reported
#-1= all shared mem not reported
def shared_val_accuracy():
"""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"):
if open("/proc/"+str(os.getpid())+"/smaps").read().find("Pss:")!=-1:
return 2
else:
return 1
if (2,6,1) <= kv <= (2,6,9):
return -1
return 0
else:
return 1
vm_accuracy = shared_val_accuracy()
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, and totals are not reported\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, and totals are not reported\n"
)
elif vm_accuracy == 1:
sys.stderr.write(
"Warning: Shared memory is slightly over-estimated by this system\n"
"for each program, so totals are not reported.\n"
)
_______________________________________________________________
I think it basically uses the info from the proc file system, try
running it and see for yourself if it suits your needs, on my system
the output is something like this :
Private + Shared = RAM used Program
72.0 KiB + 17.0 KiB = 89.0 KiB ck-xinit-session
92.0 KiB + 17.5 KiB = 109.5 KiB gnome-pty-helper
120.0 KiB + 28.0 KiB = 148.0 KiB hald-addon-generic-backlight
120.0 KiB + 29.0 KiB = 149.0 KiB hald-addon-acpi
116.0 KiB + 34.5 KiB = 150.5 KiB acpid
132.0 KiB + 27.0 KiB = 159.0 KiB hald-addon-cpufreq
132.0 KiB + 29.0 KiB = 161.0 KiB hald-addon-input
140.0 KiB + 32.0 KiB = 172.0 KiB hald-addon-storage
160.0 KiB + 15.5 KiB = 175.5 KiB init
160.0 KiB + 26.5 KiB = 186.5 KiB hald-runner
176.0 KiB + 22.5 KiB = 198.5 KiB dbus-launch
216.0 KiB + 33.0 KiB = 249.0 KiB xinetd
240.0 KiB + 64.5 KiB = 304.5 KiB gconf-im-settings-daemon
248.0 KiB + 75.5 KiB = 323.5 KiB im-settings-daemon
176.0 KiB + 147.5 KiB = 323.5 KiB run-mozilla.sh
264.0 KiB + 65.0 KiB = 329.0 KiB mingetty (5)
296.0 KiB + 60.0 KiB = 356.0 KiB gvfsd-burn
312.0 KiB + 46.0 KiB = 358.0 KiB udevd
284.0 KiB + 96.0 KiB = 380.0 KiB gconf-helper
368.0 KiB + 36.5 KiB = 404.5 KiB ssh-agent
396.0 KiB + 54.5 KiB = 450.5 KiB gvfsd
368.0 KiB + 112.0 KiB = 480.0 KiB im-info-daemon
476.0 KiB + 39.0 KiB = 515.0 KiB pppd
464.0 KiB + 69.5 KiB = 533.5 KiB gvfs-fuse-daemon
464.0 KiB + 84.0 KiB = 548.0 KiB gvfsd-trash
456.0 KiB + 104.5 KiB = 560.5 KiB gnome-keyring-daemon-wrapper
532.0 KiB + 52.5 KiB = 584.5 KiB bluetoothd
584.0 KiB + 29.5 KiB = 613.5 KiB crond
524.0 KiB + 119.5 KiB = 643.5 KiB gnome-keyring-daemon
600.0 KiB + 44.0 KiB = 644.0 KiB rsyslogd
680.0 KiB + 52.0 KiB = 732.0 KiB gvfs-gphoto2-volume-monitor
652.0 KiB + 118.0 KiB = 770.0 KiB kerneloops (deleted)
740.0 KiB + 93.5 KiB = 833.5 KiB gvfs-hal-volume-monitor
604.0 KiB + 312.0 KiB = 916.0 KiB nm-system-settings (deleted)
804.0 KiB + 140.0 KiB = 944.0 KiB console-kit-daemon
856.0 KiB + 102.5 KiB = 958.5 KiB bonobo-activation-server
808.0 KiB + 228.0 KiB = 1.0 MiB kerneloops-applet
724.0 KiB + 501.0 KiB = 1.2 MiB bash (2)
1.2 MiB + 63.0 KiB = 1.3 MiB wvdial
1.2 MiB + 328.5 KiB = 1.5 MiB dbus-daemon (2)
1.2 MiB + 383.0 KiB = 1.5 MiB notification-area-applet
1.8 MiB + 294.5 KiB = 2.1 MiB disp_pref
2.0 MiB + 515.5 KiB = 2.5 MiB bluetooth-applet
2.0 MiB + 640.0 KiB = 2.6 MiB evolution-alarm-notify
2.3 MiB + 406.5 KiB = 2.7 MiB evolution-data-server-2.24
2.7 MiB + 62.5 KiB = 2.7 MiB hald
2.8 MiB + 72.5 KiB = 2.8 MiB gconfd-2
2.5 MiB + 792.0 KiB = 3.2 MiB metacity
2.7 MiB + 683.0 KiB = 3.4 MiB gnome-session
2.9 MiB + 645.5 KiB = 3.5 MiB notification-daemon
3.3 MiB + 258.5 KiB = 3.5 MiB imsettings-applet
3.2 MiB + 883.0 KiB = 4.0 MiB wnck-applet
3.4 MiB + 760.5 KiB = 4.2 MiB nm-applet (deleted)
3.5 MiB + 1.1 MiB = 4.5 MiB stickynotes_applet
3.7 MiB + 1.1 MiB = 4.8 MiB mixer_applet2
3.8 MiB + 1.1 MiB = 5.0 MiB clock-applet (deleted)
4.8 MiB + 413.5 KiB = 5.2 MiB pulseaudio
5.2 MiB + 1.3 MiB = 6.4 MiB gnome-power-manager
5.6 MiB + 1.2 MiB = 6.8 MiB python
8.2 MiB + 555.5 KiB = 8.7 MiB gnome-screensaver (deleted)
8.0 MiB + 1.1 MiB = 9.2 MiB gnome-settings-daemon
10.9 MiB + 1.8 MiB = 12.7 MiB /usr/bin/sealer
11.7 MiB + 1.8 MiB = 13.5 MiB gnome-panel
13.4 MiB + 1.2 MiB = 14.6 MiB gnome-terminal
21.3 MiB + 758.0 KiB = 22.0 MiB setroubleshootd
24.4 MiB + 4.5 MiB = 28.9 MiB nautilus
32.5 MiB + 3.3 MiB = 35.9 MiB Xorg
77.5 MiB + 1.1 MiB = 78.6 MiB firefox
---------------------------------
315.8 MiB
=================================
Private + Shared = RAM used Program
Thanks
Satish
--
http://satish.playdrupal.com
--
http://satish.playdrupal.com
_______________________________________________
To unsubscribe, email [email protected] with
"unsubscribe <password> <address>"
in the subject or body of the message.
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc