#! /usr/bin/python -t import os # Decent (UK/US English only) number formatting. import locale locale.setlocale(locale.LC_ALL, '') def loc_num(x): """ Return a string of a number in the readable "locale" format. """ return locale.format("%d", int(x), True) pids = [] for d in os.listdir("/proc/"): try: pid = int(d) pids.append(pid) except: pass def map_sz(x): (beg, end) = x.split('-') return (int(end, 16) - int(beg, 16)) files = {} for pid in pids: try: try: lines = open("/proc/%d/smaps" % pid).readlines() smaps = True except: lines = open("/proc/%d/maps" % pid).readlines() smaps = False off = 0 while off < len(lines): line = lines[off] off += 1 try: int(line[0]) except: continue data = line.split(None, 5) try: ino = int(data[4]) dev = int(data[3].split(":", 2)[0], 16) except: print "DBG: Bad line:", lines[off - 1] print "DBG: data=", data continue if dev == 0: continue if ino == 0: continue if '(deleted)' not in data[5]: continue key = "%s:%d" % (data[3], ino) if key not in files: files[key] = lambda x: x # Hack files[key].s_size = 0 files[key].s_rss = 0 files[key].s_shared_clean = 0 files[key].s_shared_dirty = 0 files[key].s_private_clean = 0 files[key].s_private_dirty = 0 files[key].referenced = 0 files[key].vsz = 0 files[key].pids = [] files[key].name = data[5] files[key].vsz += map_sz(data[0]) try: if smaps: files[key].s_size += int(lines[off].split(None, 3)[1]) off += 1 files[key].s_rss += int(lines[off].split(None, 3)[1]) off += 1 files[key].s_shared_clean += int(lines[off].split(None, 3)[1]) off += 1 files[key].s_shared_dirty += int(lines[off].split(None, 3)[1]) off += 1 files[key].s_private_clean += int(lines[off].split(None, 3)[1]) off += 1 files[key].s_private_dirty += int(lines[off].split(None, 3)[1]) off += 1 try: files[key].referenced += int(lines[off].split(None, 3)[1]) off += 1 except: pass except: print "DBG: Bad data:", lines[off - 1] files[key].pids.append(pid) except: pass vsz = 0 s_size = 0 s_rss = 0 s_shared_clean = 0 s_shared_dirty = 0 s_private_clean = 0 s_private_dirty = 0 referenced = 0 for x in files.values(): vsz += x.vsz s_size += x.s_size s_rss += x.s_rss s_shared_clean += x.s_shared_clean s_shared_dirty += x.s_shared_dirty s_private_clean += x.s_private_clean s_private_dirty += x.s_private_dirty referenced += x.referenced print x.pids, loc_num(x.vsz), x.name, print "\ ==============================================================================" print "num = %-20s" % loc_num(len(files)) print "vsz = %-20s" % loc_num(vsz) print "\ ------------------------------------------------------------------------------" print "s_size = %-20s" % loc_num(s_size * 1024) print "s_rss = %-20s" % loc_num(s_rss * 1024) print "s_shared_clean = %-20s" % loc_num(s_shared_clean * 1024) print "s_shared_dirty = %-20s" % loc_num(s_shared_dirty * 1024) print "s_private_clean = %-20s" % loc_num(s_private_clean * 1024) print "s_private_dirty = %-20s" % loc_num(s_private_dirty * 1024) print "referenced = %-20s" % loc_num(referenced * 1024) print "\ =============================================================================="