内存监控:任何人都可以发布C ++代码来打印Linux中可访问进程的PSS(比例集大小)。 给定python代码执行相同的操作:
导入os,sys,re,pwd
'''
Print the user name, pid, pss, and the command line for all accessable
processes in pss descending order.
'''
# Get the user name, pid, pss, and the command line information for all
# processes that are accessable. Ignore processes where the permission is
# denied.
ls = [] # [(user, pid, pss, cmd)]
for pid in filter(lambda x: x.isdigit(), os.listdir('/proc')):
try:
ls.append((owner_of_process(pid), pid, pss_of_process(pid), cmdline_of_process(pid)))
except IOError:
pass
# Calculate the max length of the user name, pid, and pss in order to
# print them in aligned columns.
userlen = 0
pidlen = 0
psslen = 0
for (user, pid, pss, cmd) in ls:
userlen = max(userlen, len(user))
pidlen = max(pidlen, len(pid))
psslen = max(psslen, len(str(pss)))
# Get the width of the terminal.
with os.popen('tput cols') as fp:
term_width = int(fp.read().strip())
# Print the information. Ignore kernel modules since they allocate memory
# from the kernel land, not the user land, and PSS is the memory
# consumption of processes in user land.
fmt = '%%-%ds %%%ds %%%ds %%s' % (userlen, pidlen, psslen)
print(fmt % ('USER', 'PID', 'PSS', 'COMMAND'))
for (user, pid, pss, cmd) in sorted(ls, lambda x, y: cmp(y[2], x[2])):
if cmd != '':
print((fmt % (user, pid, pss, cmd))[:term_width - 1])
def pss_of_process(pid): ''' 返回由KiB(1024字节单位)中的pid指定的进程的PSS
@param pid process ID
@return PSS value
'''
with file('/proc/%s/smaps' % pid) as fp:
return sum([int(x) for x in re.findall('^Pss:\s+(\d+)', fp.read(), re.M)])
def cmdline_of_process(pid): ''' 返回由pid指定的进程的命令行。
@param pid process ID
@return command line
'''
with file('/proc/%s/cmdline' % pid) as fp:
return fp.read().replace('\0', ' ').strip()
def owner_of_process(pid): ''' 返回pid指定的进程的所有者。
@param pid process ID
@return owner
'''
return pwd.getpwuid(os.stat('/proc/%s' % pid).st_uid).pw_name
如果名称 ==' 主要': pss_main()