Skip to content

Instantly share code, notes, and snippets.

@mallipeddi
Created April 25, 2013 00:27
Show Gist options
  • Save mallipeddi/5456673 to your computer and use it in GitHub Desktop.
Save mallipeddi/5456673 to your computer and use it in GitHub Desktop.
Playing around with smem output.
#!/usr/bin/env python
"""
smem_test.py - test smem's USS/PSS/RSS values
More info on smem: http://www.selenic.com/smem/
Sample run:
# ./smem_test.py
Running main pid=4489
Running shared_map (pid=4497)
Running shared_fault (pid=4498)
Running shared_fault_half (pid=4499)
Running private_map (pid=4500)
Running private_fault (pid=4501)
PID User Command Swap USS PSS RSS
4497 harishm smem_test.py 0 256.0K 1.1M 5.6M
4500 harishm smem_test.py 0 284.0K 1.2M 5.7M
4489 harishm smem_test.py 0 672.0K 2.0M 7.8M
4502 harishm smem_test.py 0 8.2M 8.7M 10.5M
4499 harishm smem_test.py 0 300.0K 108.8M 221.0M
4498 harishm smem_test.py 0 6.7M 115.2M 227.3M
4501 harishm smem_test.py 0 194.6M 195.5M 200.1M
PID User Command Swap USS PSS RSS
4497 harishm smem_test.py 0 256.0K 1.1M 5.6M
4500 harishm smem_test.py 0 284.0K 1.2M 5.7M
4489 harishm smem_test.py 0 700.0K 2.0M 7.8M
4612 harishm smem_test.py 0 8.2M 8.7M 10.5M
4499 harishm smem_test.py 0 316.0K 1.3G 2.5G
4498 harishm smem_test.py 0 2.5G 3.8G 5.0G
4501 harishm smem_test.py 0 5.0G 5.0G 5.0G
"""
import mmap
import struct
import os
import ctypes
import time
import subprocess
import sys
buf_shr = None
buf_pvt1 = None
buf_pvt2 = None
BUF_SIZE=5*1024*1024*1024
SLEEP_TIME=20*60
def shared_map():
global buf_shr
buf_shr = mmap.mmap(-1, BUF_SIZE, mmap.MAP_SHARED)
def private_map():
global buf_pvt1
buf_pvt1 = mmap.mmap(-1, BUF_SIZE, mmap.MAP_PRIVATE)
time.sleep(SLEEP_TIME)
def private_fault():
global buf_pvt2
buf_pvt2 = mmap.mmap(-1, BUF_SIZE, mmap.MAP_PRIVATE)
do_fault(buf_pvt2)
time.sleep(SLEEP_TIME)
def shared_map_child():
time.sleep(SLEEP_TIME)
def run_proc(proc_name, func):
child_pid = os.fork()
if child_pid == 0:
func()
else:
print "Running %s (pid=%d)" % (proc_name, child_pid)
def do_fault(buf, fault_pct=100):
myint = ctypes.c_int.from_buffer(buf)
myint_size = struct.calcsize(myint._type_)
offset = 0
while (offset < int((BUF_SIZE-myint_size)*(fault_pct/100.0))):
myint = ctypes.c_int.from_buffer(buf, offset)
myint.value = 30
offset += mmap.PAGESIZE
# print "offset=%d,intsize=%d" % (offset, myint_size)
def shared_fault():
global buf_shr
do_fault(buf_shr)
time.sleep(SLEEP_TIME)
def shared_fault_half():
global buf_shr
do_fault(buf_shr, fault_pct=50)
time.sleep(SLEEP_TIME)
def print_smem_output():
cmd=["smem","-k","-P","python"]
subprocess.call(cmd,stdin=sys.stdin,stderr=sys.stderr)
def main():
print "Running main pid=%d" % os.getpid()
shared_map()
run_proc("shared_map", shared_map_child)
run_proc("shared_fault", shared_fault)
run_proc("shared_fault_half", shared_fault_half)
run_proc("private_map", private_map)
run_proc("private_fault", private_fault)
while True:
print_smem_output()
time.sleep(60)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment