Skip to content

Instantly share code, notes, and snippets.

@andresvia
Last active January 4, 2016 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andresvia/8657732 to your computer and use it in GitHub Desktop.
Save andresvia/8657732 to your computer and use it in GitHub Desktop.
solaris process memory overhead with pmap python wrapper for all processes
#!/usr/bin/env python
# this is only for Solaris
# just when i think I have understanded solaris memory usage
# I get prstat outputs which sum more than 100% on memory usage
# this is apparently a memory overhead caused by sharing of memory between procs.
# this program gives a list of proceses
# rss memory and not shared mememory
# according to https://blogs.oracle.com/nickstephen/entry/solaris_measuring_the_memory_overhead
# this is the python version of the ksh script on that blog post that does not use temp files
import os
import subprocess
import re
import sys
pmaps = {}
objects = {}
totalmem = {}
def unique_object(object, sproc):
for proc in objects:
if sproc != proc:
if object in objects[proc]:
return False
return True
def mem_for_object(object, proc):
mem = 0
for pmap in pmaps[proc].splitlines():
pmap = pmap.split()
try:
if re.match("^[0-9A-F]+$", pmap[0]):
if object == " ".join(pmap[6:]):
thisanon = 0
try:
thisanon = int(pmap[3])
except:
pass
thismem = int(pmap[2])
mem += thismem - thisanon
except:
pass
return mem
def anon_for_proc(proc):
anon = 0
for pmap in pmaps[proc].splitlines():
pmap = pmap.split()
try:
if pmap[0] == "total":
anon = int(pmap[4])
except:
pass
return anon
def rss_for_proc(proc):
rss = 0
for pmap in pmaps[proc].splitlines():
pmap = pmap.split()
try:
if pmap[0] == "total":
rss = int(pmap[3])
except:
pass
return rss
def head_for_proc(proc):
try:
return pmaps[proc].splitlines()[0]
except:
return proc + ": " + "None"
print >> sys.stderr, "Running pmap for every process",
for proc in os.listdir("/proc"):
pmap = subprocess.Popen(["/usr/bin/pmap", "-x", proc], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pmaps[proc] = pmap.stdout.read()
print >> sys.stderr, ".",
print >> sys.stderr, "done"
for proc in pmaps:
pobjects = []
for pmap in pmaps[proc].splitlines():
pmap = pmap.split()
try:
if re.match("^[0-9A-F]+$", pmap[0]):
pobjects.append(" ".join(pmap[6:]))
except:
pass
pobjects = list(set(pobjects))
objects[proc] = pobjects
for proc in objects:
proc_mem = 0
anon_mem = 0
for object in objects[proc]:
if(unique_object(object, proc)):
proc_mem += mem_for_object(object, proc)
anon_mem = anon_for_proc(proc)
totalmem[proc] = proc_mem + anon_mem
print "proc total rss"
for proc in objects:
print head_for_proc(proc), totalmem[proc], rss_for_proc(proc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment