1. Iotop安装
git clone git://repo.or.cz/iotop.git
cd iotop
python setup.py install
2.Iotop命令
Iotop的用法有点类似top,直接在terminal输入命令iotop
即可查看所有进程或线程的io活动状况.
iotop的结果会显示disk read, disk write, percentage of time spent while swapping, percentage of time spent swap space used and percentage of time spent waiting on I/O.
可以使用:“左箭头”/“右箭头”键来选择哪一列进行排序,使用R键可以反向排序该列.O字母键可以仅仅显示正在进行io活动的进程/线程.
iotop的参数用法可以通过man iotop
或是iotop -h
查看.
-b:批处理模式,适合用于通过脚本将I/O的使用状况记录到文件中.
-o:只报告有I/O活动进行的进程
-t:在显示结果中增加时间
–iter=# : 限制iotop运行时候的采样次数
-q:让显示结果在第一次运行之后不再显示结果列的头部信息. “-qq”则完全不显示头部信息, “-qqq”则不显示头部和I/O汇总信息.
iotop -bto --iter=1
显示结果:
Total DISK READ: 0.00 B/s | Total DISK WRITE: 27.64 K/s
TIME TID PRIO USER DISK READ DISK WRITE SWAPIN IO COMMAND
17:08:25 177 be/4 root 0.00 B/s 7.90 K/s 0.00 % 0.05 % [kjournald]
3.脚本使用
使用cron脚本,每隔一分钟就记录一次I/O状况:
编辑文件:
vi /etc/cron.d/iotop
加入并保存:
# Run iotop and log any detected activity.
* * * * * root iotop -botqqq --iter=3 >> /data/logs/iotop
cron.d就会使iotop每分钟运行一次(每运行一次iotop采样3次,间隔为5秒),记录I/O状况到日志文件/data/logs/iotop中.
如果不想单个日志过大的话,可以使用logrotate来切割存储日志.
创建”/etc/logrotate.d/iotop”文件,加入并保存:
/data/logs/iotop {
rotate 5
weekly
compress
missingok
notifempty
}
4.类似命令:
1) vmstat : vmstat 1
查看bi和bo列.
2) iostat : iostat -k -x -d 5 3
3) dstat : dstat -d
4) pidstat : pidstat -d -p pid
可以查看单个进程I/O使用情况
dstat是python写的一个linux系统资源监控工具,它汇集了vmstat, iostat, ifstat, netstat等命令功能.与sysstat相比, dstat拥有一个彩色的界面, 在手动观察性能状况时, 数据比较显眼容易观察. 执行dstat命令, 它默认会收集-cpu-、-disk-、-net-、-paging-、-system-的数据.
1. dstat安装
wget http://pkgs.repoforge.org/dstat/dstat-0.7.2-1.el5.rfx.noarch.rpm
rpm -Uvh dstat-0.7.2-1.el5.rfx.noarch.rpm
2. dstat -h
Usage: dstat [-afv] [options..] [delay [count]]
Versatile tool for generating system resource statistics
Dstat options:
-c, –cpu enable cpu stats
-C 0,3,total include cpu0, cpu3 and total
-d, –disk enable disk stats
-D total,hda include hda and total
-g, –page enable page stats
-i, –int enable interrupt stats
-I 5,eth2 include int5 and interrupt used by eth2
-l, –load enable load stats
-m, –mem enable memory stats
-n, –net enable network stats
-N eth1,total include eth1 and total
-p, –proc enable process stats
-s, –swap enable swap stats
-S swap1,total include swap1 and total
-t, –time enable time/date output
-T, –epoch enable time counter (seconds since epoch)
-y, –sys enable system stats
–ipc enable ipc stats
–lock enable lock stats
–raw enable raw stats
–tcp enable tcp stats
–udp enable udp stats
–unix enable unix stats
-M stat1,stat2 enable external stats
–mods stat1,stat2
-a, –all equals -cdngy (default)
-f, –full expand -C, -D, -I, -N and -S discovery lists
-v, –vmstat equals -pmgdsc -D total
–integer show integer values
–nocolor disable colors (implies –noupdate)
–noheaders disable repetitive headers
–noupdate disable intermediate updates
–output file write CSV output to file
delay is the delay in seconds between each update
count is the number of updates to display before exiting
The default delay is 1 and count is unspecified (unlimited)
参考:
http://dag.wieers.com/home-made/dstat/
在查看系统资源使用情况时,很多工具为我们提供了从设备角度查看的方法。例如使用iostat查看磁盘io统计信息:
linux:~ # iostat -d 3
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda 1.67 0.00 40.00 0 120
以上显示的是从sda的角度统计的结果。当我们需要从进程的角度,查看每个进程使用系统资源的情况,有什么方法吗?
使用pidstat工具可以获取每个进程使用cpu、内存和磁盘等系统资源的统计信息,pidstat由sysstat rpm包提供,可在suse11使用。下面我们来看pidstat的具体用法。
默认输出
执行pidstat,将输出系统启动后所有活动进程的cpu统计信息:
linux:~ # pidstat
Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_
11:37:19 PID %usr %system %guest %CPU CPU Command
……
11:37:19 11452 0.00 0.00 0.00 0.00 2 bash
11:37:19 11509 0.00 0.00 0.00 0.00 3 dd
以上输出,除最开头一行显示内核版本、主机名、日期和cpu架构外,主要列含义如下:
- 11:37:19: pidstat获取信息时间点
- PID: 进程pid
- %usr: 进程在用户态运行所占cpu时间比率
- %system: 进程在内核态运行所占cpu时间比率
- %CPU: 进程运行所占cpu时间比率
- CPU: 指示进程在哪个核运行
- Command: 拉起进程对应的命令
执行pidstat默认输出信息为系统启动后到执行时间点的统计信息,因而即使当前某进程的cpu占用率很高,输出中的值有可能仍为0。
指定采样周期和采样次数
像sar、iostat等命令一样,也可以给pidstat命令指定采样周期和采样次数,命令形式为”pidstat [option] interval [count]”,以下pidstat输出以2秒为采样周期,输出2次cpu使用统计信息:
linux:~ # pidstat 2 2
Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_
14:40:39 PID %usr %system %guest %CPU CPU Command
14:40:41 9567 0.50 1.49 0.00 1.98 2 atop
14:40:41 12405 0.00 0.50 0.00 0.50 6 pidstat
14:40:41 PID %usr %system %guest %CPU CPU Command
14:40:43 7830 0.50 0.50 0.00 1.00 7 runHpiAlarm
14:40:43 12405 0.00 1.00 0.00 1.00 6 pidstat
若不指定统计次数count,则pidstat将一直输出统计信息。
cpu使用情况统计(-u)
使用-u选项,pidstat将显示各活动进程的cpu使用统计,执行”pidstat -u”与单独执行”pidstat”的效果一样。
内存使用情况统计(-r)
使用-r选项,pidstat将显示各活动进程的内存使用统计:
linux:~ # pidstat -r -p 13084 1
Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_
15:08:18 PID minflt/s majflt/s VSZ RSS %MEM Command
15:08:19 13084 133835.00 0.00 15720284 15716896 96.26 mmmm
15:08:20 13084 35807.00 0.00 15863504 15849756 97.07 mmmm
15:08:21 13084 19273.87 0.00 15949040 15792944 96.72 mmmm
以上各列输出的含义如下:
- minflt/s: 每秒次缺页错误次数(minor page faults),次缺页错误次数意即虚拟内存地址映射成物理内存地址产生的page fault次数
- majflt/s: 每秒主缺页错误次数(major page faults),当虚拟内存地址映射成物理内存地址时,相应的page在swap中,这样的page fault为major page fault,一般在内存使用紧张时产生
- VSZ: 该进程使用的虚拟内存(以kB为单位)
- RSS: 该进程使用的物理内存(以kB为单位)
- %MEM: 该进程使用内存的百分比
- Command: 拉起进程对应的命令
IO情况统计(-d)
使用-d选项,我们可以查看进程IO的统计信息:
linux:~ # pidstat -d 1 2
Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_
17:11:36 PID kB_rd/s kB_wr/s kB_ccwr/s Command
17:11:37 14579 124988.24 0.00 0.00 dd
17:11:37 PID kB_rd/s kB_wr/s kB_ccwr/s Command
17:11:38 14579 105441.58 0.00 0.00 dd
以上主要输出的含义如下:
- kB_rd/s: 每秒进程从磁盘读取的数据量(以kB为单位)
- kB_wr/s: 每秒进程向磁盘写的数据量(以kB为单位)
- Command: 拉起进程对应的命令
针对特定进程统计(-p)
使用-p选项,我们可以查看特定进程的系统资源使用情况:
linux:~ # pidstat -r -p 1 1
Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_
18:26:17 PID minflt/s majflt/s VSZ RSS %MEM Command
18:26:18 1 0.00 0.00 10380 640 0.00 init
18:26:19 1 0.00 0.00 10380 640 0.00 init
……
以上pidstat命令以1秒为采样时间间隔,查看init进程的内存使用情况。
pidstat常用命令
使用pidstat进行问题定位时,以下命令常被用到:
pidstat -u 1
pidstat -r 1
pidstat -d 1
以上命令以1秒为信息采集周期,分别获取cpu、内存和磁盘IO的统计信息。
【iostat命令描述】
iostat
用途
报告中央处理器(CPU)统计信息和整个系统、适配器、tty 设备、磁盘和 CD-ROM 的输入/输出统计信息。
语法
iostat [ -s ] [ -a ] [ -d | -t ] [ -T ][ -m ][ PhysicalVolume ... ] [ Interval [ Count ] ]
描述
iostat 命令用来监视系统输入/输出设备负载,这通过观察与它们的平均传送速率相关的物理磁盘的活动时间来实现。iostat 命令生成的报告可以用来更改系统配置来更好地平衡物理磁盘和适配器之间的输入/输出负载。
由 iostat 命令生成的第一份报告提供了关于自从系统被引导后的时间统计信息。后继的每一份报告都包含自上一次报告以来的时间。每次运行 iostat 命令时,就报告所有的统计信息。报告由紧接着一行 tty 和 CPU 统计信息的 tty 和 CPU 头行组成。在多处理器系统上,CPU 统计信息是系统范围计算的,是所有处理器的平均值。
如果指定 -s 标志,则显示系统头行,随后是一行整个系统的统计信息。系统的主机名被打印在系统头上。
如果指定 -a 标志,就会显示一个适配器头行,随后是一行适配器的统计信息。这后面将回有一个磁盘头行和连接到适配器的所有磁盘/CD-ROM 的统计信息。为所有与系统连接的磁盘适配器生成这种报告。
显示一个磁盘头行,随后是一行配置的磁盘的统计信息。如果指定 PhysicalVolume 参数,则只显示那些指定的名称。
如果指定 PhysicalVolume 参数,那么可以指定一个或者更多的字母或者字母数字的物理卷。如果指定 PhysicalVolume 参数,就会显示 tty 和 CPU 报告并且磁盘报告包含指定驱动器的统计信息。如果没有发现指定逻辑驱动器名,那么报告将列出指定的名称并且显示没有找到驱动器的消息。如果没有指定逻辑驱动器名,报告则包含所有已配置的磁盘和 CD-ROM 的统计信息。如果系统上没有配置驱动器,则不生成磁盘报告。PhysicalVolume 参数中的第一个字符不能为数字型。
Interval 参数指定了在每个报告之间的以秒计算的时间量。第一份报告包含了自系统启动(引导)以来的时间统计信息。每一份后继报告都包含在和前一份报告的时间间隔之间收集的统计信息。Count 参数可被指定来连接 Interval 参数。如果指定了 Count 参数,它的记数值就确定在 Interval 秒间生成的报告数。如果指定了 Interval 参数但没有 Count 参数,iostat 命令就会不断生成报告。
iostat 命令用来确定一个物理卷是否正在形成一个性能瓶颈,以及是否有可能改善这种情况。物理卷的 % 使用率字段表明了文件活动在驱动器中分布多均匀。物理卷的高 % 使用率是表明也许存在这个资源的争用很好的征兆。由于 CPU 使用率的统计信息同样适用于 iostat 报告,CPU 在 I/O 等待队列中的时间的百分比可以在同一时间确定。如果 I/O 等待时间是有效数字并且磁盘使用率不是在卷上均匀分布,那么就要考虑在驱动器上分布数据。
注: 一些系统资源被消耗是为了为 iostat 命令维护磁盘 I/O 的历史记录。使用sysconfig子例程,或者系统管理接口工具(SMIT)来停止历史记录账户。报告iostat 命令生成四种类型的报告,tty 和 CPU 使用率报告、磁盘使用率报告、系统吞吐量报告和适配器吞吐量报告。
tty 和 CPU 使用率报告由 iostat 命令生成的第一份报告是 tty 和 CPU 使用率报告。对于多处理器系统,CPU 值是所有处理器的总平均。同时,I/O 等待状态是系统级定义的,而不是每个处理器。报告有以下格式:
栏 描述
tin 显示了系统为所有 tty 读取的字符总数。
tout 显示了系统为所有 tty 写入的字符总数。
% user 显示了在用户级(应用程序)执行时产生的 CPU 使用率百分比。
% sys 显示了在系统级(内核)执行时产生的 CPU 使用率百分比。
% idle 显示了在 CPU 空闲并且系统没有未完成的磁盘 I/O 请求时的时间百分比。
% iowait 显示了 CPU 空闲期间系统有未完成的磁盘 I/O 请求时的时间百分比。
每过一定时间间隔,内核就更新这条信息(一般每秒六十次)。tty 报告提供了从系统中所有终端的收到的每秒字符总数,以及和每秒输出到系统所有终端的字符的总数。
用来计算 CPU 磁盘 I/O 等待时间的方法操作系统 V4.3.3 和后来的版本包含用来估算 CPU 在磁盘 I/O(wio 时间)等待上的所花时间的百分比的增强方法。用在 AIX 4.3.2 和操作系统的早期版本上的方法在一定条件下,能够给出 SMP 上的 wio 时间的一个放大的视图。wio 时间是根据命令 sar(%wio)、 vmstat(wa)和 iostat(% iowait)报告出来的。
在 AIX 4.3.2 中和早期版本中使用的方法如下:在每个处理器(每处理器一秒一百次)的每个时钟中断上,将确定四个类别(usr/sys/wio/idle)中的哪一个放 置在最后的 10ms 内。如果在时钟中断的时刻 CPU 以 usr 模式中处于忙状态,那么 usr 获得这个时间计点并归于此类。如果在时钟中断时刻 CPU 以内核模式中处于忙状态,那么 sys 类别将获得该计时点。如果 CPU 不处于忙状态,将检查是否在进行任何磁盘 I/O。如果在进行任何磁盘 I/O,则 wio 类别将增加。如果磁盘在进行 I/O 操作并且 CPU 不忙,那么 idle 类别将获取计时点。wio 时间的放大视图是由于所有空闲 CPU 被归为 wio 而不管在 I/O 上等待的线程数所导致。例如,仅有一个线程执行 I/O 的系统可以报告超过 90% 的 wio 时间而不管其 CPU 数。
在 AIX 4.3.3 中和后继版本中使用的方法如下:如果在那个 CPU 上启动一个未完成的的 I/O,那么操作系统 V4.3.3 中的更改仅把一个空闲 CPU 标为 wio。当只有少数线程正在执行 I/O 否则系统就空闲的情况下,这种方法可以报告更少的 wio 时间。例如,一个有四个 CPU 且只有一个线程执行 I/O 的系统将报告一个最大值是 25% 的 wio 时间。一个有 12 个 CPU 且仅有一个线程执行 I/O 的系统将报告一个最大值为 8% 的 wio 时间。 NFS 客户机通过 VMM 读/写,并且为了完成一个 I/O 而在 vmm 等待中用的时间现在将被报告为 I/O 等待时间。
磁盘使用率报告由 iostat 命令生成的第二个报告是磁盘使用率报告。磁盘报告提供了在每个物理磁盘基础上的统计信息。这个报告有以下类似的格式:
% tm_act 表示物理磁盘处于活动状态的时间百分比(驱动器的带宽使用率)。
Kbps 表示以 KB 每秒为单位的传输(读或写)到驱动器的数据量。
tps 表示每秒钟输出到物理磁盘的传输次数。一次传输就是一个对物理磁盘的 I/O 请求。多个逻辑请求可被并为对磁盘的一个单一 I/O 请求。传输具有中等的大小。
Kb_read 读取的 KB 总数。
Kb_wrtn 写入的 KB 总数。
CD-ROM 设备的统计信息也要报告。
对于配置有大量磁盘的大型系统配置,当 iostat 没有执行时,系统可以设置为避免收集物理硬盘的输入/输出数据。如果系统用上述的方式配置,那么第一个磁盘报告将显示消息引导不可用后的磁盘历史记录而不是磁盘统计信息。由 iostat 命令生成的后继时间间隔报告包含在报告时间间隔期间收集的磁盘统计信息。引导后的任何 tty 和 CPU 都不会影响。如果一个系统管理命令用来重新保留磁盘统计信息,那么第一个 iostat 命令报告会显示从启用磁盘输入/输出统计信息那一刻时间间隔起点起的行为。
系统吞吐量报告如果指定 -s 标志将生成这个报告。这份报告提供了整个系统的统计信息。这份报告有以下格式:
Kbps 表示了每秒以 KB 为单位的传输(读或写)到整个系统的数据量。
tps 表示每秒传输到整个系统的传输次数。
Kb_read 从整个系统中读取的 KB 总数。
Kb_wrtn 写到整个系统的 KB 总数。
适配器吞吐量报告如果指定 -a 标志将产生该报告。这份报告提供了适配器上的统计信息。
Kbps 表示每秒钟以 KB 为单位的传输到(读或写)到适配器的数据量。
tps 表示每秒钟输出到适配器的传输次数。
Kb_read 从适配器读取的 KB 总数。
Kb_wrtn 写到适配器的 KB 总数。
磁盘输入/输出历史记录要提高性能,已经禁用了磁盘输入/输出统计信息集合。要启用该数据的集合,请输入:
chdev -l sys0 -a iostat=true要显示当前设置,请输入:
lsattr -E -l sys0 -a iostat如果禁用了磁盘输入/输出历史记录的集合,那么 iostat 输出的第一个磁盘报告将显示消息引导不可用后的磁盘历史记录而不是磁盘统计信息。如前,由 iostat 命令生成后继时间间隔报告包含了在报告时间间隔期间收集的磁盘统计信息。
标志
-a 显示适配器吞吐量报告。
-d -d 标志不能和 -t 标志合用,且仅显示磁盘使用率报告。
-s 显示系统吞吐量报告。
-m -m 标志将为以下打印路径统计信息 到启用 MPIO(Multi-Path I/O)设备的路径。 ESS 机器中的路径。 吞吐量是每个设备的。设备所有路径的吞吐量符合该设备的吞吐量。 对于 ESS 机器,vpaths 将被当作磁盘,而 hdisks 将被看作路径。在内部 vpaths 是磁盘,hdisks 是它们的路径。对于启用 MPIO 的设备,路径名将表示为 Path0、Path1、Path2 等等。数字0、1、2 等都是 lspath 命令提供的路径标识。由于一个设备的路径可以附加到任何适配器上,所以适配器报告将报告每个适配器下的路径信息。磁盘名称是所有路径的前缀。对于所有启用 MPIO 的设备,适配器报告将打印路径名为 hdisk10_Path0 、hdisk0_Path1等等。对于所有 ESS 机器,适配器报告将打印路径名为 vpath0_hdisk3 、vpath10_hdisk25等等。
-t -t 标志排除了 -d 标志且仅显示了 tty 和 cpu 用法报告。
-T 打印 iostat输出的每一行边上的时间戳记。时间戳记以 HH:MM:SS 格式显示。
注:
-s 和 -a 标记都能指定来显示系统和适配器吞吐量报告。 如果同时指定 -a 标志和 -t 标志,将显示 tty 和 CPU 报告,随后是适配器吞吐量报告。连接在适配器上的磁盘的使用率报告将不会显示在适配器吞吐量报告后面。 如果同时指定 -a 标志和 -d 标志,将不会显示 tty 和 CPU 报告。如果指定物理卷参数,那么指定卷的磁盘使用率报告将在它所属于的相应适配器下打印出来。
示例
要为所有 tty、CPU 和磁盘显示引导后的单一历史记录报告,请输入: iostat 要为逻辑名是 disk1 的磁盘显示一个以两秒为时间间隔的持续磁盘报告,请输入: iostat -d disk1 2 要为逻辑名是 disk1 的磁盘显示以两秒为时间间隔的六个报告,请输入: iostat disk1 2 6 要为所有磁盘显示以两秒为时间间隔的六个报告,请输入: iostat -d 2 6 要为三个名称分别为 disk1、disk2、disk3 的磁盘显示以两秒为时间间隔的六个报告,请输入: iostat disk1 disk2 disk3 2 6 要打印系统吞吐量报告,请输入:
iostat -s 要打印适配器吞吐量报告,请输入: iostat -a 要打印系统和适配器吞吐量报告,且仅有 tty 和 CPU 报告(没有磁盘报告),请输入:
iostat -sat 要打印带有 hdisk0 和 hdisk7 的磁盘使用率报告的系统和适配器吞吐量报告,请输入: iostat -sad hdisk0 hdisk7 要显示 iostat 输出的每行的下一行的时间戳记,请输入: iostat -T
文件
/usr/bin/iostat 包含 iostat 命令。
With today’s big disks and extremely powerful CPUs out there, memory is probably the more expensive resource. When it comes to Virtual Hosts, also CPU power could be a problem, I have been running this site at Linode for a year now with no big problems, the only ones were because of some lack of memory, which caused the CPU usage to scale up, until I needed to restart the server.
Because of that experience I was investigating about vmstat
. you can read vmstat man page at any time.
First let’s check how memory works in Linux, memory is made of two components, the physical memory and swap memory (Disk), as you may know the disk is slower than the physical memory of your server so we all want to avoid memory paging or memory swapping.
So, we will use vmstat to check the status of the memory in our server and determine if we need to add more memory to it.
Syntax
vmstat [-a] [-n] [delay [ count]] vmstat [-f] [-s] [-m] vmstat [-S unit] vmstat [-d] vmstat [-D] vmstat [-p disk partition] vmstat [-V]
We will focus on memory and cpu, so the syntax we will use more is.
vmstat [delay [count]]
Where delay is the time between samples, (if you want more than one) and count is the number of samples (if you omit it, it will run continuously).
This is a sample of the the output of this command:
vmstat 5 10
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 1 0 986900 103532 492096 0 0 24 31 320 943 10 2 87 1
0 0 0 986760 103536 492212 0 0 21 0 714 1706 7 2 91 0
0 0 0 986760 103544 492212 0 0 0 4 877 3647 13 3 85 0
0 0 0 985884 103552 492232 0 0 0 32 913 3435 11 3 86 0
0 0 0 984644 103564 492232 0 0 0 16 752 2002 12 3 85 0
1 0 0 983528 103576 492232 0 0 0 64 667 1816 6 1 93 0
1 0 0 984768 103588 492232 0 0 0 9 767 1979 12 3 85 0
1 0 0 984892 103592 492236 0 0 0 34 773 2670 8 3 89 0
1 0 0 979064 103604 492392 0 0 32 10 901 3133 14 4 82 0
0 0 0 984396 103612 492388 0 0 0 90 942 3215 19 3 77 0
Now let’s describe the fields under memory and under cpu.
Memory
- swpd: the amount of virtual memory used.
That is the portion of swap partition or swap file in use, to have this used is not bad at all, and it is kind of normal.
- free: the amount of idle memory.
Your physical free memory, the same that the
free
-
command will output, if you constantly have this in a number close to 0 is bad.
- buff: the amount of memory used as buffers.
Is the memory that is being used as a “virtual” disk, when data is read from the disk, the kernel tries to keep it in the memory in case of future need, this will be done only in physical memory is available, to speed up processes. (That is why if you open OpenOffice and close it and open it again the second time it goes faster.
- cache: the amount of memory used as cache.
The cache is where Linux puts the data that is intended to go to the disk, this is once again to speed up processes, as memory is faste that the disk the data is first put in this cache, and the flushed to the disk in the background, this is also useful if the process that wrote this info to the disk (cache) needs it again, it will be read from the cache.
- inact: the amount of inactive memory. (-a option)
- active: the amount of active memory. (-a option)
Swap
- si: Amount of memory swapped in from disk (/s).
- so: Amount of memory swapped to disk (/s).
So, where do you need to look at to detect if you have memory issues in your Linux system.
A good place to start is free memory, but consider that also buffered and cached memory is somehow “free” memory, so do not run so fast to buy more memory yet, maybe the most important place to check is si and so, they show the data transferred in and out from the memory to the disk, if you find these counters always changing mean that you system is constantly paging the memory to the disk, which of course mean that you need more memory, or less processes running at a time.
You can find the culprit of this paging using, this command.
ps -eo pmem,pcpu,args | sort -k 1 -r | less
This will have an output like this.
%MEM %CPU COMMAND
4.2 2.8 X :0
3.8 3.6 kwin -session 1061726368000124320935300000029140000_1264439629_828678
2.8 0.1 kdeinit4: plasma-desktop [kdeinit]
2.2 0.4 kdeinit4: konsole [kdeinit]
2.0 0.1 /usr/lib/chromium/chromium
2.0 0.0 kdeinit4: krunner [kdeinit]
1.8 2.9 /usr/bin/knotify4
1.7 0.0 kdeinit4: kmix [kdeinit] -session 1061726368000124011843900000031580010_126443962
1.6 0.0 /usr/bin/virtuoso-t +foreground +config /tmp/virtuoso_hX1798.ini +wait
1.6 0.0 kdeinit4: kded4 [kdeinit]
1.5 0.0 /usr/lib/chromium/chromium --channel=13877.b2454868.257731315 --type=renderer --lang=en-US --force-fieldtest=AsyncSlowStart/_AsyncSlowStart/CacheSize/CacheSizeGroup_0/DnsImpact/_disabled_prefetch/GlobalSdch/_global_enable_sdch/SocketLateBinding/_disable_late_binding/
1.4 0.0 okular file:///tmp/redp4285.pdf -caption
1.4 0.0 kdeinit4: ksmserver [kdeinit]
1.4 0.0 kdeinit4: klipper [kdeinit]
1.4 0.0 kdeinit4: kglobalaccel [kdeinit]
1.3 0.0 kdeinit4: kdeinit4 Running...
1.3 0.0 kdeinit4: kaccess [kdeinit]
1.2 0.0 kdeinit4: nepomukserver [kdeinit]
1.1 0.0 /usr/bin/nepomukservicestub nepomukstrigiservice
1.1 0.0 kdeinit4: klauncher [kdeinit] --fd=10
10.3 9.9 /usr/bin/firefox
0.8 0.0 /usr/bin/nepomukservicestub nepomukontologyloader
0.7 0.0 /usr/bin/nepomukservicestub nepomukstorage
0.7 0.0 /usr/bin/korgac -icon korgac
0.6 0.0 /usr/lib/chromium/chromium --type=zygote
0.6 0.0 /usr/bin/nepomukservicestub nepomukremovablestorageservice
0.6 0.0 /usr/bin/nepomukservicestub nepomukqueryservice
0.6 0.0 /usr/bin/nepomukservicestub nepomukfilewatch
0.3 0.0 /usr/bin/kwrited
0.2 0.0 /usr/sbin/hald
0.1 0.0 /usr/sbin/console-kit-daemon --no-daemon
0.1 0.0 /usr/lib/polkit-1/polkitd
0.1 0.0 /usr/lib/gvfs/gvfs-gphoto2-volume-monitor
0.1 0.0 /usr/lib/gvfs/gvfs-gdu-volume-monitor
0.1 0.0 /usr/lib/gvfs//gvfs-fuse-daemon /home/ggarron/.gvfs
0.1 0.0 /usr/lib/GConf/gconfd-2
0.1 0.0 /usr/lib/DeviceKit/devkit-disks-daemon
0.1 0.0 /usr/lib/chromium/chromium
0.1 0.0 ssh -l root linux.alketech.com
0.1 0.0 ssh 69.164.209.24 -l root
0.0 0.0 xinit
0.0 0.0 xclip
0.0 0.0 [watchdog/1]
0.0 0.0 [watchdog/0]
0.0 0.0 /usr/sbin/syslog-ng
0.0 0.0 /usr/sbin/sshd
0.0 0.0 /usr/sbin/crond -S -l info
0.0 0.0 /usr/lib/kde4/libexec/start_kdeinit +kcminit_startup
0.0 0.0 /usr/lib/gvfs/gvfsd
0.0 0.0 /usr/bin/ssh-agent -s
0.0 0.0 /usr/bin/gpg-agent --daemon --pinentry-program /usr/bin/pinentry-qt4
0.0 0.0 /usr/bin/dbus-daemon --system
0.0 0.0 /usr/bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
0.0 0.0 [sync_supers]
0.0 0.0 supervising syslog-ng
0.0 0.0 sort -k 1 -r
0.0 0.0 [scsi_eh_7]
0.0 0.0 [scsi_eh_6]
0.0 0.0 [scsi_eh_5]
0.0 0.0 [scsi_eh_4]
0.0 0.0 [scsi_eh_3]
0.0 0.0 [scsi_eh_2]
0.0 0.0 [scsi_eh_1]
0.0 0.0 [scsi_eh_0]
0.0 0.0 /sbin/udevd --daemon
0.0 0.0 /sbin/udevd --daemon
0.0 0.0 /sbin/udevd --daemon
0.0 0.0 /sbin/agetty -8 38400 tty6 linux
0.0 0.0 /sbin/agetty -8 38400 tty5 linux
0.0 0.0 /sbin/agetty -8 38400 tty4 linux
0.0 0.0 /sbin/agetty -8 38400 tty3 linux
0.0 0.0 /sbin/agetty -8 38400 tty2 linux
0.0 0.0 ps -eo pmem,pcpu,args
0.0 0.0 [pm]
0.0 0.0 [migration/1]
0.0 0.0 [migration/0]
0.0 0.0 kwrapper4 ksmserver
0.0 0.0 [kthreadd]
0.0 0.0 [kswapd0]
0.0 0.0 [ksuspend_usbd]
0.0 0.0 [ksoftirqd/1]
0.0 0.0 [ksoftirqd/0]
0.0 0.0 [ksmd]
0.0 0.0 [kseriod]
0.0 0.0 [kpsmoused]
0.0 0.0 [kjournald]
0.0 0.0 [khungtaskd]
0.0 0.0 [khubd]
0.0 0.0 [khelper]
0.0 0.0 [kblockd/1]
0.0 0.0 [kblockd/0]
0.0 0.0 [kacpi_notify]
0.0 0.0 [kacpi_hotplug]
0.0 0.0 [kacpid]
0.0 0.0 ini
0.0 0.0 [hd-audio0]
0.0 0.0 hald-runner
0.0 0.0 hald-addon-storage: polling /dev/sr0 (every 2 sec)
0.0 0.0 hald-addon-input: Listening on /dev/input/event1 /dev/input/event3 /dev/input/event4 /dev/input/event13
0.0 0.0 hald-addon-acpi: listening on acpi kernel interface /proc/acpi/event
0.0 0.0 [flush-8:32]
0.0 0.0 [events/1]
0.0 0.0 [events/0]
0.0 0.0 devkit-disks-daemon: polling /dev/sr0
0.0 0.0 dbus-launch --sh-syntax --exit-with-session
0.0 0.0 [crypto/1]
0.0 0.0 [crypto/0]
0.0 0.0 /bin/sh /usr/bin/startkde
0.0 0.0 /bin/login --
0.0 0.0 /bin/bash
0.0 0.0 /bin/bash
0.0 0.0 /bin/bash
0.0 0.0 /bin/bash
0.0 0.0 /bin/bash
0.0 0.0 /bin/bash
0.0 0.0 /bin/bash
0.0 0.0 [bdi-default]
0.0 0.0 -bash
0.0 0.0 [ata_aux]
0.0 0.0 [ata/1]
0.0 0.0 [ata/0]
0.0 0.0 [async/mgr]
0.0 0.0 [aio/1]
0.0 0.0 [aio/0]
So you can see which process is “eating” your memory, you can also use htop once in your screen press F6 and select MEM% as the sort column, if you prefer top once it is running press “F” or “O” (with no quotes) and next “n” to sort by the Memory column, will have an output like this.
Tasks: 127 total, 2 running, 125 sleeping, 0 stopped, 0 zombie
Cpu(s): 7.9%us, 1.9%sy, 0.7%ni, 89.1%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 2064156k total, 1408676k used, 655480k free, 122628k buffers
Swap: 2650684k total, 0k used, 2650684k free, 681644k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1916 ggarron 20 0 540m 211m 40m R 22 10.5 37:48.52 firefox
1773 ggarron 20 0 351m 78m 32m S 8 3.9 14:05.72 kwin
1695 root 19 -1 246m 85m 9752 S 6 4.2 11:15.78 X
1 root 20 0 1688 612 544 S 0 0.0 0:00.78 init
2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0 0.0 0:00.36 ksoftirqd/0
5 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/0
6 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/1
7 root 20 0 0 0 0 S 0 0.0 0:00.06 ksoftirqd/1
8 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/1
9 root 20 0 0 0 0 S 0 0.0 0:00.20 events/0
10 root 20 0 0 0 0 S 0 0.0 0:00.04 events/1
11 root 20 0 0 0 0 S 0 0.0 0:00.00 khelper
12 root 20 0 0 0 0 S 0 0.0 0:00.00 async/mgr
13 root 20 0 0 0 0 S 0 0.0 0:00.00 pm
14 root 20 0 0 0 0 S 0 0.0 0:00.00 sync_supers
15 root 20 0 0 0 0 S 0 0.0 0:00.00 bdi-default
16 root 20 0 0 0 0 S 0 0.0 0:00.00 kblockd/0
17 root 20 0 0 0 0 S 0 0.0 0:00.00 kblockd/1
18 root 20 0 0 0 0 S 0 0.0 0:00.00 kacpid
19 root 20 0 0 0 0 S 0 0.0 0:00.00 kacpi_notify
20 root 20 0 0 0 0 S 0 0.0 0:00.00 kacpi_hotplug
21 root 20 0 0 0 0 S 0 0.0 0:00.02 kseriod
24 root 20 0 0 0 0 S 0 0.0 0:00.00 khungtaskd
25 root 20 0 0 0 0 S 0 0.0 0:00.00 kswapd0
26 root 25 5 0 0 0 S 0 0.0 0:00.00 ksmd
27 root 20 0 0 0 0 S 0 0.0 0:00.00 aio/0
28 root 20 0 0 0 0 S 0 0.0 0:00.00 aio/1
29 root 20 0 0 0 0 S 0 0.0 0:00.00 crypto/0
30 root 20 0 0 0 0 S 0 0.0 0:00.00 crypto/1
504 root 20 0 0 0 0 S 0 0.0 0:03.27 ata/0
510 root 20 0 0 0 0 S 0 0.0 0:00.06 ata/1
519 root 20 0 0 0 0 S 0 0.0 0:00.00 ata_aux
543 root 20 0 0 0 0 S 0 0.0 0:04.38 scsi_eh_0
560 root 20 0 0 0 0 S 0 0.0 0:00.00 scsi_eh_1
Now por the CPU these are the fields in vmstat
All fields are percentages of total CPU time.
- us: Time spent running non-kernel code. (user time, including nice time)
- sy: Time spent running kernel code. (system time)
- id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
- wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
If while running vmstat
with a delay option, you see that constantly the idle time is near to 0, you may have problems, usually this means a program is eating too much memory and the CPU is busy paging from memory to swap, and back again. But you can once again use ps (as shown above) or htop or top, (remember to sort by CPU time), the ps
is like this to sort by CPU
ps -eo pcpu,pmem,args | sort -k 1 -r | less