smem工具介紹
smem是Linux系統上的一款可以生成多種內存耗用報告的命令行工具。與現有工具不一樣的是smem可以報告實際使用的物理內存(PSS),這是一種更有意義的指標。可以衡量虛擬內存系統的庫和應用程序所占用的內存數量。
由於大部分的物理內存通常在多個應用程序之間共享,名為實際使用物理內存(RSS)的這個標准的內存耗用衡量指標會大大高估內存耗用情況。PSS這個參數而是衡量了每個應用程序在每個共享內存區中的“公平分配”,給出了一個切合實際的衡量指標。
smem有許多功能特性:
系統概況列表
按進程、映射和用戶列表
按用戶、映射或用戶過濾
來自多個數據源的可配置列
可配置的輸出單位和百分比
可配置的標題和總和
從/proc讀取活動數據
從目錄鏡像或經過壓縮的打包文件讀取數據快照
面向嵌入式系統的輕型捕獲工具
內置的圖表生成功能
smem的系統要求
系統內核為2.6.27以上
Python版本必須為2.4或以上
依賴matplotlib庫生成圖表(可選的,自動檢測)依賴matplotlib,是因為smem除了一般的文本信息報告外,smem 也可以生成條狀或餅狀圖。
對應的英文原文如下:
smem is a tool that can give numerous reports on memory usage on Linux systems. Unlike existing tools, smem can report proportional set size (PSS), which is a more meaningful representation of the amount of memory used by libraries and applications in a virtual memory system.
Because large portions of physical memory are typically shared among multiple applications, the standard measure of memory usage known as resident set size (RSS) will significantly overestimate memory usage. PSS instead measures each application's "fair share" of each shared area to give a realistic measure.
smem has many features:
system overview listing
listings by process, mapping, user
filtering by process, mapping, or user
configurable columns from multiple data sources
configurable output units and percentages
configurable headers and totals
reading live data from /proc
reading data snapshots from directory mirrors or compressed tarballs
lightweight capture tool for embedded systems
built-in chart generation
smem has a few requirements:
a reasonably modern kernel (> 2.6.27 or so)
a reasonably recent version of Python (2.4 or so)
the matplotlib library for chart generation (optional, auto-detected)
Linux使用到了虛擬內存(virtual memory),因此要准確的計算一個進程實際使用的物理內存就不是那么簡單。只知道進程的虛擬內存大小也並沒有太大的用處,因為還是無法獲取到實際分配的物理內存大小。
RSS(Resident set size),使用top命令可以查詢到,是最常用的內存指標,表示進程占用的物理內存大小。但是,將各進程的RSS值相加,通常會超出整個系統的內存消耗,這是因為RSS中包含了各進程間共享的內存。
PSS(Proportional set size)所有使用某共享庫的程序均分該共享庫占用的內存時,每個進程占用的內存。顯然所有進程的PSS之和就是系統的內存使用量。它會更准確一些,它將共享內存的大小進行平均后,再分攤到各進程上去。
USS(Unique set size )進程獨自占用的內存,它是PSS中自己的部分,它只計算了進程獨自占用的內存大小,不包含任何共享的部分。
VSS – Virtual Set Size 虛擬耗用內存(包含共享庫占用的內存)
RSS – Resident Set Size 實際使用物理內存(包含共享庫占用的內存)
PSS – Proportional Set Size 實際使用的物理內存(比例分配共享庫占用的內存)
USS – Unique Set Size 進程獨自占用的物理內存(不包含共享庫占用的內存)
smem工具安裝
首先去官方網址https://www.selenic.com/smem/ 下載對應的smem安裝包,目前最新的版本為smem-1.4. 下面安裝是在RHEL 5.7上安裝,不同版本系統、以及不同安裝方法都有一些區別。
1: [root@DB-Server tmp]# tar -xzvf smem-1.4.tar.gz
2: smem-1.4/.hg_archival.txt
3: smem-1.4/.hgtags
4: smem-1.4/COPYING
5: smem-1.4/smem
6: smem-1.4/smem.8
7: smem-1.4/smemcap.c
8: [root@DB-Server tmp]# cd smem-1.4
9: [root@DB-Server smem-1.4]# ls
10: COPYING smem smem.8 smemcap.c
11: [root@DB-Server smem-1.4]# cp /tmp/smem-1.4/smem /usr/bin
12: [root@DB-Server smem-1.4]# chmod +x /usr/bin/smem
smem工具使用
查看smem命令的相關幫助信息
1: [root@DB-Server tmp]# smem -h
2: usage: smem [options]
3:
4: options:
5: -h, --help show this help message and exit
6: -H, --no-header disable header line
7: -c COLUMNS, --columns=COLUMNS
8: columns to show
9: -t, --totals show totals
10: -R REALMEM, --realmem=REALMEM
11: amount of physical RAM
12: -K KERNEL, --kernel=KERNEL
13: path to kernel image
14: -m, --mappings show mappings
15: -u, --users show users
16: -w, --system show whole system
17: -P PROCESSFILTER, --processfilter=PROCESSFILTER
18: process filter regex
19: -M MAPFILTER, --mapfilter=MAPFILTER
20: map filter regex
21: -U USERFILTER, --userfilter=USERFILTER
22: user filter regex
23: -n, --numeric numeric output
24: -s SORT, --sort=SORT field to sort on
25: -r, --reverse reverse sort
26: -p, --percent show percentage
27: -k, --abbreviate show unit suffixes
28: --pie=PIE show pie graph
29: --bar=BAR show bar graph
30: -S SOURCE, --source=SOURCE
31: /proc data source
1:無參數時顯示所有進程的內存使用情況
2:參數-u 顯示每個用戶所耗用的內存總量
smem -u
3: 參數-p 查看耗用內存情況的百分比。
smem -p
4: 參數-w 查看系統內存使用情況
1: [root@DB-Server01 ~]# smem -w
2: Area Used Cache Noncache
3: firmware/hardware 0 0 0
4: kernel image 0 0 0
5: kernel dynamic memory 22021892 21721584 300308
6: userspace memory 1899296 122424 1776872
7: free memory 686340 686340 0
8: [root@DB-Server01 ~]# smem -w -p
9: Area Used Cache Noncache
10: firmware/hardware 0.00% 0.00% 0.00%
11: kernel image 0.00% 0.00% 0.00%
12: kernel dynamic memory 89.49% 88.27% 1.22%
13: userspace memory 7.73% 0.50% 7.23%
14: free memory 2.78% 2.78% 0.00%
5: 參數-R REALMEM,REALMEM這個值是指物理內存數量。此參數可以讓smem在整個系統(-w)的輸出中發現固件/硬件所耗用的內存數量(對比上下即可發現,注意firmware/hardware)
1: [root@DB-Server01 ~]# smem -R 24G -w
2: Area Used Cache Noncache
3: firmware/hardware 558296 0 558296
4: kernel image 0 0 0
5: kernel dynamic memory 22024108 21722972 301136
6: userspace memory 1907676 122436 1785240
7: free memory 675744 675744 0
6:參數-c 用來顯示需要展示的列。
1: [root@DB-Server01 ~]# smem -c "name user pss"
2: Name User PSS
3: mingetty root 18
4: mingetty root 18
5: mingetty root 18
6: mingetty root 18
7: mingetty root 18
8: bash root 911
9: sftp-server root 939
10: cupsd root 1208
11: rsyslogd root 1403
12: smbd tibco 1474
13: hald haldaemon 1477
14: pickup postfix 1487
15: vmtoolsd root 2309
16: sshd root 2521
17: python root 5464
18: beremote root 7585
19: java weblogic 18454
20: java weblogic 31431
21: java weblogic 69389
22: java tomcat 339977
23: java tomcat 355037
24: java tomcat 421133
25: java weblogic 640531
7: 參數-s 根據某一列(例如 rss)來排序.
8: 參數-r 一般與參數-s結合使用,表示反轉排序(從升序改為降序)
9:參數-M 過濾相關進程。Show processes filtered by mapping
1: [root@DB-Server tmp]# smem -M mysql
2: PID User Command Swap USS PSS RSS
3: 4172 mysql /usr/sbin/mysqld --basedir= 0 3924 3924 3924
4: [root@DB-Server tmp]# smem -M mysql -p
5: PID User Command Swap USS PSS RSS
6: 4172 mysql /usr/sbin/mysqld --basedir= 0.00% 0.38% 0.38% 0.38%
7: [root@DB-Server tmp]#
10:參數-U 按用戶過濾信息
另外一些參數如何展示條狀圖或餅狀圖的功能,個人感覺這些參數的用處不大,首先服務器一般都沒有安裝桌面系統,都是命令界面維護、管理。無法生成相關圖形。其次服務器有桌面系統,也需要安裝相關依賴包,相當麻煩。除非是為了生成報告需要。
Read data from capture tarball smem --source capture.tar.gz
Show a bar chart labeled by pid smem --bar pid -c "pss uss"
Show a pie chart of RSS labeled by name smem --pie name -s rss
參考資料