Linux의 메모리 사용
9500 단어 devopscloudopzlinuxmonitoring
이 서류 안에 무엇이 있느냐
🚀 기술 용어
1. 페이지
🚀 VSZ(가상 메모리 크기) 및 주문형 페이지
🚀 RSS(상주 세트 크기) 및 공유 라이브러리
🚀 PSS(배율 설정 크기)
🚀 PSS에 대한 Python 스크립트 가져오기
https://github.com/vumdao/pss-memory-get/blob/master/pss.py
#! /usr/bin/env python3
# coding: utf-8
##-----------------------------------------------------------------------------
## pss.py --- Print the PSS (Proportional Set Size) of accessable processes
##-----------------------------------------------------------------------------
import os, sys, re, pwd
from functools import cmp_to_key as cmp
##-----------------------------------------------------------------------------
def pss_main():
'''
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, key=cmp(lambda x, y: (y[2] - x[2]))):
if cmd != '':
print((fmt % (user, pid, pss, cmd))[:term_width - 1])
##-----------------------------------------------------------------------------
def pss_of_process(pid):
'''
Return the PSS of the process specified by pid in KiB (1024 bytes unit)
@param pid process ID
@return PSS value
'''
with open('/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):
'''
Return the command line of the process specified by pid.
@param pid process ID
@return command line
'''
with open('/proc/%s/cmdline' % pid) as fp:
return fp.read().replace('\0', ' ').strip()
##-----------------------------------------------------------------------------
def owner_of_process(pid):
'''
Return the owner of the process specified by pid.
@param pid process ID
@return owner
'''
try:
owner_pid = pwd.getpwuid(os.stat('/proc/%s' % pid).st_uid).pw_name
except Exception:
return 'docker'
return owner_pid
##-----------------------------------------------------------------------------
if __name__ == '__main__':
pss_main()
sudo python pss.py
docker
일 경우userID999
ref·
Github
·
Web
·
·
·
Page
·
Reference
이 문제에 관하여(Linux의 메모리 사용), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/vumdao/memory-consumption-in-linux-3b55텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)