There are a couple of ways to accomplish what you re asking here. One involves parsing procfs
which doesn t require "root" privileges and the other is using the Taskstats (a Netlink interface) which does.
如果你能够利用Generic Netlinksocketsbyex获得最优的间接费用。
I have a working solution using the first solution that parses procfs
:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import time
class ShMemMonitorProcess(object):
def __init__(self, ppid):
self.ppid = ppid
# Create a Sub-Class for Thread module
self.memory = { kB :1024, mB :1024*1024, gB :1024*1024*1024}
self.scrape_procfs
def calculate_memory(self, vmrss):
memory, size = vmrss.split()
return int(memory) * self.memory[size]
@property
def scrape_procfs(self):
rss = 0
child_ps = dict()
start = time.time()
while (time.time() - start) < 100:
for pid in filter(lambda p: p.isdigit(), os.listdir( /proc )):
try:
# open /proc/PID/status but catch as some can be ephemeral
with open( /proc/{}/status .format(pid)) as fh:
ppid = [i for i in fh.readlines() if i.startswith( PPid )]
if not ppid:
continue
ppid = ppid[0].split( )[1].strip(
)
if ppid == self.ppid:
with open( /proc/{}/status .format(pid)) as fh:
vmrss = fh.readlines()
vmrss = [i for i in vmrss if i.startswith( VmRSS )]
if not vmrss:
continue
vmrss = vmrss[0].split( )[1].strip(
)
ps_rss = self.calculate_memory(vmrss)
if not child_ps.get(pid):
with open( /proc/{}/comm .format(pid)) as fh:
comm = fh.readlines()
print( Found Child Process {}! .format(pid))
print(comm)
print( Adding Process to Hash... )
child_ps[pid] = ps_rss
rss += ps_rss
time.sleep(1)
else:
# re-calculate process memory
print( Process was already found running... )
print( Re-calculating memory usage... )
previous_rss_used = child_ps[pid]
if previous_rss_used > ps_rss:
memory_adj = previous_rss_used - ps_rss
rss -= memory_adj
elif previous_rss_used <= ps_rss:
memory_adj = ps_rss - previous_rss_used
rss += memory_adj
child_ps[pid] = ps_rss
print( Bytes....{} .format(rss))
except FileNotFoundError:
continue
if __name__ == __main__ :
# Pass the Process ID through STDIN on calling
pid = sys.argv[1]
shmem = ShMemMonitorProcess(pid)
然后,我就与人民民主党一起问,什么进程是“Parent”,什么是你想要找回的RSS总数。
python ./Find_RSS.py 1234
Found Child Process 12541!
22:54:40 [12/222]
[ Privileged Cont
]
Adding Process to Hash...
Bytes....202661888
Found Child Process 12606!
[ WebExtensions
]
Adding Process to Hash...
Bytes....667672576
Found Child Process 12666!
[ Utility Process
]
Adding Process to Hash...
Bytes....709660672
Found Child Process 14803!
[ RDD Process
]