forked from 361way/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_pid.py
36 lines (28 loc) · 1.34 KB
/
get_pid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
import os
from collections import namedtuple
PAGESIZE = os.sysconf("SC_PAGE_SIZE")
print PAGESIZE
pextmem = namedtuple('pextmem', 'rss vms shared text lib data dirty')
def pids():
return [int(x) for x in os.listdir(b'/proc') if x.isdigit()]
def memory_info_ex(pid):
# ============================================================
# | FIELD | DESCRIPTION | AKA | TOP |
# ============================================================
# | rss | resident set size | | RES |
# | vms | total program size | size | VIRT |
# | shared | shared pages (from shared mappings) | | SHR |
# | text | text ('code') | trs | CODE |
# | lib | library (unused in Linux 2.6) | lrs | |
# | data | data + stack | drs | DATA |
# | dirty | dirty pages (unused in Linux 2.6) | dt | |
# ============================================================
with open("/proc/%s/statm" % pid, "rb") as f:
vms, rss, shared, text, lib, data, dirty = \
[int(x) * PAGESIZE for x in f.readline().split()[:7]]
return pextmem(rss, vms, shared, text, lib, data, dirty)
pids = pids()
for pid in pids:
mem_info = memory_info_ex(pid)
print pid,mem_info