博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mac下python实现vmstat
阅读量:5361 次
发布时间:2019-06-15

本文共 2083 字,大约阅读时间需要 6 分钟。

mac下没有linux/unix 的vmstat,只有vm_stat;

sh-3.2# vm_stat

Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 191876.
Pages active: 433086.
Pages inactive: 141819.
Pages speculative: 23119.
Pages throttled: 0.
Pages wired down: 254606.
Pages purgeable: 11895.
"Translation faults": 20445131.
Pages copy-on-write: 1435992.
Pages zero filled: 10414067.
Pages reactivated: 39459.
Pages purged: 23388.
File-backed pages: 172905.
Anonymous pages: 425119.
Pages stored in compressor: 12595.
Pages occupied by compressor: 3399.
Decompressions: 5824.
Compressions: 26966.
Pageins: 1626450.
Pageouts: 4218.
Swapins: 1487.
Swapouts: 5130.

不想用下面这个复杂top命令

sh-3.2# top -l 1 -s 0 | grep PhysMem

PhysMem: 3277M used (992M wired), 817M unused.

用下面这个python

#!/usr/bin/pythonimport subprocessimport re# Get process infops = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0]vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0]# Iterate processesprocessLines = ps.split('\n')sep = re.compile('[\s]+')rssTotal = 0 # kBfor row in range(1,len(processLines)):    rowText = processLines[row].strip()    rowElements = sep.split(rowText)    try:        rss = float(rowElements[0]) * 1024    except:        rss = 0 # ignore...    rssTotal += rss# Process vm_statvmLines = vm.split('\n')sep = re.compile(':[\s]+')vmStats = {}for row in range(1,len(vmLines)-2):    rowText = vmLines[row].strip()    rowElements = sep.split(rowText)    vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 4096print 'Wired Memory:\t\t%d MB' % ( vmStats["Pages wired down"]/1024/1024 )print 'Active Memory:\t\t%d MB' % ( vmStats["Pages active"]/1024/1024 )print 'Inactive Memory:\t%d MB' % ( vmStats["Pages inactive"]/1024/1024 )print 'Free Memory:\t\t%d MB' % ( vmStats["Pages free"]/1024/1024 )print 'Real Mem Total (ps):\t%.3f MB' % ( rssTotal/1024/1024 )

 sh-3.2# ./mem.py

Wired Memory: 1013 MB

Active Memory: 1951 MB
Inactive Memory: 573 MB
Free Memory: 423 MB
Real Mem Total (ps): 3180.027 MB

转载于:https://www.cnblogs.com/runfeng/p/4889389.html

你可能感兴趣的文章
Vim: Overwrite read-only file without quiting vim
查看>>
mysql几个常见错误记录
查看>>
MySQL的一次优化记录 (IN子查询和索引优化)
查看>>
compiz隐藏最大化窗口标题栏
查看>>
C++判断字符是否是元音字母
查看>>
[转][图]性能瓶颈分析图
查看>>
HTTP长连接
查看>>
fqPkzJetPK
查看>>
线程安全
查看>>
jvm
查看>>
Spring
查看>>
Spring事务
查看>>
java 线程池
查看>>
docker 部署ftp
查看>>
git配置
查看>>
websocket 和 http的区别
查看>>
docker 基础
查看>>
http 状态码
查看>>
docker 部署 tomcat
查看>>
docker 配置sonatype/nexus3
查看>>