Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save khatwaniNikhil/147acdb23b99780b0a977dccf4246eee to your computer and use it in GitHub Desktop.
Save khatwaniNikhil/147acdb23b99780b0a977dccf4246eee to your computer and use it in GitHub Desktop.
Python Script - Calculate stack memory used by a Java process (made changes to https://github.com/apangin/jstackmem)
sudo python ./jstackmem.py 2495
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import bisect
import re
import subprocess
import sys
if len(sys.argv) < 2:
print 'Calculate stack memory used by a Java process'
print 'Usage: python jstackmem.py <pid>'
sys.exit()
# Read memory map of the target process
try:
pid = sys.argv[1]
with open('/proc/' + pid + '/smaps') as f:
smaps = f.readlines()
except Exception, e:
print 'Failed to open process memory map'
print e
sys.exit()
# Parse memory map
last_addr = None
addr = []
pss = []
shared = []
for line in smaps:
m = re.match('([0-9a-f]+)-([0-9a-f]+) (..).+', line)
if m:
last_addr = (int(m.group(1), 16) if m.group(3) == 'rw'
else None)
elif last_addr:
m = re.match('Pss: +([0-9]+) kB', line)
if m:
addr.append(last_addr)
pss.append(int(m.group(1)))
shared.append(False)
def demote(user_uid, user_gid):
def result():
report_ids('starting demotion')
os.setgid(user_gid)
os.setuid(user_uid)
report_ids('finished demotion')
return result
def report_ids(msg):
print 'uid, gid = %d, %d; %s' % (os.getuid(), os.getgid(), msg)
# Match stack addresses from jstack output with the memory map
try:
stack_total = 0
jstack = subprocess.Popen(['jstack', pid],
preexec_fn=demote(1004,1004),
stdout=subprocess.PIPE)
while True:
line = jstack.stdout.readline()
if not line:
break
m = re.match("\"([^\"]*)\" .+\[0x([0-9a-f]+)\]\n", line)
if m:
addr_index = bisect.bisect(addr, int(m.group(2), 16)) - 1
if shared[addr_index]:
# Region already counted
print '%6d` %s' % (pss[addr_index], m.group(1))
else:
print '%6d %s' % (pss[addr_index], m.group(1))
stack_total += pss[addr_index]
shared[addr_index] = True
except Exception, e:
print 'Failed to execute jstack'
print e
sys.exit()
print '%6d (Total)' % stack_total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment