linux 进程pss,内存监控:用C++代码打印Linux中可访问进程的PSS(比例集合大小)...

内存监控:任何人都可以发布一个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 \u of \u进程(pid):

'''

返回pid指定进程的PSS,单位为KiB(1024字节)@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\U进程(pid):

'''

返回pid指定的进程的命令行。你知道吗@param pid process ID

@return command line

'''

with file('/proc/%s/cmdline' % pid) as fp:

return fp.read().replace('\0', ' ').strip()

def所有者\u进程(pid):

'''

返回pid指定的进程的所有者。你知道吗@param pid process ID

@return owner

'''

return pwd.getpwuid(os.stat('/proc/%s' % pid).st_uid).pw_name

如果名称=='main':

pss\u主()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值