如何find正在使用的Linux交换或什么是交换?

我有虚拟的Linux(Fedora 17)服务器与28GB内存和2GB交换。 服务器正在运行一个MySQL数据库,它被设置为使用大部分的RAM。

经过一段时间的运行后,服务器开始使用swap来换出未经处理的页面。 这很好,因为我的swappiness默认为60,这是预期的行为。

奇怪的是,top / meminfo中的数字与来自进程的信息不一致。 即服务器正在报告这些数字:

/proc/meminfo: SwapCached: 24588 kB SwapTotal: 2097148 kB SwapFree: 865912 kB top: Mem: 28189800k total, 27583776k used, 606024k free, 163452k buffers Swap: 2097148k total, 1231512k used, 865636k free, 6554356k cached 

如果我使用https://serverfault.com/a/423603/98204中的脚本,它会报告合理的数字(由bash'es,systemd等交换的less量MB)和MySQL的一个大的分配(我省略了大量的输出行):

 892 [2442] qmgr -l -t fifo -u 896 [2412] /usr/libexec/postfix/master 904 [28382] mysql -u root 976 [27559] -bash 984 [27637] -bash 992 [27931] SCREEN 1000 [27932] /bin/bash 1192 [27558] sshd: admin@pts/0 1196 [27556] sshd: admin [priv] 1244 [1] /usr/lib/systemd/systemd 9444 [26626] /usr/bin/perl /bin/innotop 413852 [31039] /usr/libexec/mysqld --basedir=/usr --datadir=/data/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/data/mysql/err --open-files-limit=8192 --pid-file=/data/mysql/pid --socket=/data/mysql/mysql.sock --port=3306 449264 Total Swap Used 

所以,如果我得到脚本输出正确的总交换使用应该是449264K = ca. 与MySQL使用约440MB。 90%的交换。

问题是为什么这和顶部和meminfo数字有很大的不同呢? 有没有办法如何“转储”交换信息,看看它实际上是什么,而不是总结所有进程的交换用法?

在分析这个问题时,我提出了不同的想法,但他们似乎都是错的:

  1. 脚本输出不在KB中。 即使以512或4KB为单位,也不会匹配。 其实比例(1200:440)大概是3:1,这个数字是“奇怪的”。
  2. 交换中有一些页面在进程之间以某种方式共享,如https://serverfault.com/a/477664/98204中所述 。 如果这是真的,我怎么能find这样的内存使用的实际数量? 我的意思是它需要使cca 800MB的差异。 这在这种情况下听起来不正确。
  3. 已经完成的进程使用的交换中有一些“旧”页面。 我不介意,如果我能够找出这个“可自由交换”有多less。
  4. 交换中的页面已被交换回内存,并在交换中,以防在RAM中没有更改,并需要按照https://serverfault.com/a/100636/98204中提到的那样再次交换。 但SwapCached值只有24MB。

奇怪的是,交换使用率正在缓慢增加,而从脚本输出的总和大致相同。 在过去的3天内,使用的交换量从1100MB增加到当前的1230MB,而总和从430MB增加到449MB(ca.)。

服务器有足够的免费(可用)RAM,所以我可以关掉交换并重新打开它。 或者我可以设置swappiness为0,所以只有在没有其他方式时才能使用交换。 但是我想解决这个问题,或者至less弄清楚是什么原因造成的。

Fedora 18以上的smem在回购smem中都smem 。 您可以下载python脚本并从源代码安装。

以下是我的机器上的示例输出(稍微剪切和匿名):

 # smem -s swap -t -k -n PID User Command Swap USS PSS RSS 20917 1001 bash 0 1.1M 1.1M 1.9M 28329 0 python /bin/smem -s swap -t 0 6.3M 6.5M 7.4M 2719 1001 gnome-pty-helper 16.0K 72.0K 73.0K 516.0K 619 0 @sbin/mdadm --monitor --sca 28.0K 72.0K 73.0K 248.0K [big snip] 32079 42 gnome-shell --mode=gdm 41.9M 1.9M 2.0M 5.0M 32403 1001 /opt/google/chrome/chrome - 43.1M 118.5M 119.4M 132.3M 4844 1002 /opt/google/chrome/chrome 48.1M 38.1M 41.9M 51.9M 5411 1002 /opt/google/chrome/chrome - 54.6M 33.4M 33.5M 36.8M 5624 1002 /opt/google/chrome/chrome - 72.4M 54.9M 55.5M 65.7M 24328 1002 /opt/Adobe/Reader9/Reader/i 77.5M 1.9M 2.0M 5.2M 4921 1002 /opt/google/chrome/chrome - 147.2M 258.4M 259.4M 272.0M ------------------------------------------------------------------------------- 214 14 1.1G 1.1G 1.2G 1.7G 

源代码还提供smemcap用于存储所有相关数据的smemcap ,以便稍后可以运行它。

  To capture memory statistics on resource-constrained systems, the the smem source includes a utility named smemcap. smemcap captures all /proc entries required by smem and outputs them as an uncompressed .tar file to STDOUT. smem can analyze the output using the --source option. smemcap is small and does not require Python. 

你应该在另一台机器上检查这个脚本,因为我的系统显示正确的交换使用:

 # Your_script.sh 111280 Total Swap Used # free Swap: 33551716 120368 33431348 

非常近111280〜= 120368。

另外,看看这个脚本:

for proc in / proc / *; 做cat $ proc / smaps 2> / dev / null | awk'/ Swap / {swap + = $ 2} END {print swap“\ readlink $proc/exe '”}'; 完成| sort -n | awk'{total + = $ 1} / [0-9] /; END {print total“\ tTotal”}'

从这个线程:

https://unix.stackexchange.com/questions/71714/linux-total-swap-used-swap-used-by-processes