Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Ved-vampir | 501065c | 2015-03-17 16:16:57 +0300 | [diff] [blame] | 3 | # Based on ps_mem.py: |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 4 | # Licence: LGPLv2 |
| 5 | # Author: P@draigBrady.com |
| 6 | # Source: http://www.pixelbeat.org/scripts/ps_mem.py |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 7 | # http://github.com/pixelb/scripts/commits/master/scripts/ps_mem.py |
| 8 | |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 9 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 10 | # Note shared is always a subset of rss (trs is not always) |
| 11 | def get_mem_stats(pid): |
| 12 | """ Return memory data of pid in format (private, shared) """ |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 13 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 14 | fname = '/proc/{0}/{1}'.format(pid, "smaps") |
| 15 | lines = open(fname).readlines() |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 16 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 17 | shared = 0 |
| 18 | private = 0 |
| 19 | pss = 0 |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 20 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 21 | # add 0.5KiB as this avg error due to trunctation |
| 22 | pss_adjust = 0.5 |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 23 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 24 | for line in lines: |
| 25 | if line.startswith("Shared"): |
| 26 | shared += int(line.split()[1]) |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 27 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 28 | if line.startswith("Private"): |
| 29 | private += int(line.split()[1]) |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 30 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 31 | if line.startswith("Pss"): |
| 32 | pss += float(line.split()[1]) + pss_adjust |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 33 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 34 | # Note Shared + Private = Rss above |
| 35 | # The Rss in smaps includes video card mem etc. |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 36 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 37 | if pss != 0: |
| 38 | shared = int(pss - private) |
Ved-vampir | 501065c | 2015-03-17 16:16:57 +0300 | [diff] [blame] | 39 | |
koder aka kdanilov | dd60c50 | 2015-03-17 16:23:42 +0200 | [diff] [blame] | 40 | return (private, shared) |