blob: dc5088b5db676696f335cd20becebbc5444ac009 [file] [log] [blame]
Ved-vampir98a99172015-03-17 14:58:15 +03001#!/usr/bin/env python
2
Ved-vampir501065c2015-03-17 16:16:57 +03003# Based on ps_mem.py:
Ved-vampir98a99172015-03-17 14:58:15 +03004# Licence: LGPLv2
5# Author: P@draigBrady.com
6# Source: http://www.pixelbeat.org/scripts/ps_mem.py
Ved-vampir98a99172015-03-17 14:58:15 +03007# http://github.com/pixelb/scripts/commits/master/scripts/ps_mem.py
8
Ved-vampir98a99172015-03-17 14:58:15 +03009
koder aka kdanilovdd60c502015-03-17 16:23:42 +020010# Note shared is always a subset of rss (trs is not always)
11def get_mem_stats(pid):
12 """ Return memory data of pid in format (private, shared) """
Ved-vampir98a99172015-03-17 14:58:15 +030013
koder aka kdanilovdd60c502015-03-17 16:23:42 +020014 fname = '/proc/{0}/{1}'.format(pid, "smaps")
15 lines = open(fname).readlines()
Ved-vampir98a99172015-03-17 14:58:15 +030016
koder aka kdanilovdd60c502015-03-17 16:23:42 +020017 shared = 0
18 private = 0
19 pss = 0
Ved-vampir98a99172015-03-17 14:58:15 +030020
koder aka kdanilovdd60c502015-03-17 16:23:42 +020021 # add 0.5KiB as this avg error due to trunctation
22 pss_adjust = 0.5
Ved-vampir98a99172015-03-17 14:58:15 +030023
koder aka kdanilovdd60c502015-03-17 16:23:42 +020024 for line in lines:
25 if line.startswith("Shared"):
26 shared += int(line.split()[1])
Ved-vampir98a99172015-03-17 14:58:15 +030027
koder aka kdanilovdd60c502015-03-17 16:23:42 +020028 if line.startswith("Private"):
29 private += int(line.split()[1])
Ved-vampir98a99172015-03-17 14:58:15 +030030
koder aka kdanilovdd60c502015-03-17 16:23:42 +020031 if line.startswith("Pss"):
32 pss += float(line.split()[1]) + pss_adjust
Ved-vampir98a99172015-03-17 14:58:15 +030033
koder aka kdanilovdd60c502015-03-17 16:23:42 +020034 # Note Shared + Private = Rss above
35 # The Rss in smaps includes video card mem etc.
Ved-vampir98a99172015-03-17 14:58:15 +030036
koder aka kdanilovdd60c502015-03-17 16:23:42 +020037 if pss != 0:
38 shared = int(pss - private)
Ved-vampir501065c2015-03-17 16:16:57 +030039
koder aka kdanilovdd60c502015-03-17 16:23:42 +020040 return (private, shared)