vmstat

vmstat即虚拟内存统计,但用它不仅仅是可以获取系统的虚拟内存性能信息,还能获取整个系统性能的大概信息。

当我们用vmstat获取系统的CPU性能信息时命令格式如下:

vmstat [-n] [-s] [delay [count]]

  • -n表示当按时间间隔采样显示的数据在初始显示行标题后,后边的数据行不再重复显示标题
  • -s汇总显示统计数据
  • delay采样的时间间隔,单位秒
  • count采样次数

例如:

1vmstat 2 3
2procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
3 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
4 0  0      0 2511564    948 13848780  0    0     0    16    3    7  0  0 99  0  0
5 0  0      0 2509360    948 13848844  0    0     0     0 1671 3471  1  0 99  0  0
6 0  0      0 2510648    948 13848816  0    0     0    49 1424 3042  1  1 99  0  0

上面例子vmstat的输出中和CPU相关的列:

  • r当前可运行的进程数。这些进程没有等待IO并且已经准备好运行
  • b等待IO完成的被阻塞的进程
  • in系统发生中断的次数
  • cs系统发生上下文切换的次数
  • us用户进程消耗的总CPU时间百分比
  • sy系统代码消耗的总CPU时间百分比
  • id系统空闲的总CPU时间百分比
  • wa等待I/O消耗的总CPU时间百分比
 1vmstat -s
 2     32531016 K total memory
 3     16181704 K used memory
 4     20755452 K active memory
 5      8064936 K inactive memory
 6      2495896 K free memory
 7          948 K buffer memory
 8     13852468 K swap cache
 9     16383996 K total swap
10            0 K used swap
11     16383996 K free swap
12      2588894 non-nice user cpu ticks
13          253 nice user cpu ticks
14       753566 system cpu ticks
15    573325623 idle cpu ticks
16       482381 IO-wait cpu ticks
17            0 IRQ cpu ticks
18        10593 softirq cpu ticks
19            0 stolen cpu ticks
20       302611 pages paged in
21     94284222 pages paged out
22            0 pages swapped in
23            0 pages swapped out
24    532872694 interrupts
25   1116037916 CPU context switches
26   1494388740 boot time
27       736194 forks
  • forks表示创建的新进程的数量

top

top命令可以打印系统整体性能信息。

top的命令行选项:top -[d delay] [-n iter] [-i] [-b]

  • -d统计信息的更新间隔,默认3秒
  • -n退出前更新显示统计信息的次数
  • -i是否显示空闲进程
  • -b批处理显示形式,top默认只显示单屏信息超出屏幕的将不显示,如果需要将top命令的输出保存到另一个地方,这选项将很有用

top命令的运行时选项:

  • 在top命令界面输入数字1将切换CPU使用率是按独立CPU显示还是按总量显示
  • 输入字母l,将切换是否显示系统负载load信息
  • 输入f(或F)将会弹出显示配置界面,在这个配置界面中可以设置显示字段和排序字段。
  • top命令界面中输入A,将分类显示统计信息

top命令显示内容中除了提供特定进程信息外,还会提供系统的整体信息,例如:

1top - 15:35:09 up 13 days,  3:36,  3 users,  load average: 0.02, 0.05, 0.11
2Tasks: 275 total,   1 running, 274 sleeping,   0 stopped,   0 zombie
3%Cpu(s):  0.9 us,  0.4 sy,  0.0 ni, 98.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
4KiB Mem : 32531016 total,   541852 free, 15598928 used, 16390236 buff/cache
5KiB Swap: 16383996 total, 16383996 free,        0 used. 16156524 avail Mem
  • us用户应用程序消耗的CPU时间
  • sy内核消耗的CPU时间
  • ni修改过nice值的进程消耗的CPU时间
  • id空闲的CPU时间
  • wa等待I/O的CPU时间
  • hi 中断请求(irq)处理程序消耗的CPU时间
  • si softirq处理程序消耗的CPU时间

procinfo

procinfo命令从/proc文件系统显示系统的一些整体信息。 CentOS上安装procinfo:

1yum install ncurses-devel ncurses
2wget https://jaist.dl.sourceforge.net/project/procinfo-ng/procinfo-ng/2.0.304/procinfo-ng-2.0.304.tar.bz2
3bzip2 -d procinfo-ng-2.0.304.tar.bz2
4tar -zxvf procinfo-ng-2.0.304.tar
5./configure
6make
7make install

procinfo的命令格式为procinfo [-d] [-D] [-n sec]

  • -n sec统计间隔
  • -d For memory, CPU times, paging, swapping, disk, context and interrupt stats, display values per second rather than totals. This option implies -f.
  • -D Same as -d, except that memory stats are displayed as totals.
 1procinfo
 2Memory:        Total        Used        Free     Buffers
 3RAM:        32531016    32141696      389320         188
 4Swap:       16383996       12756    16371240
 5
 6Load average: 0.13 0.10 0.11 1/1395 182649
 7
 8user  :        1d 00:32:09.38   0.6%  page in :           443415
 9nice  :           00:00:04.62   0.0%  page out:        414517142
10system:           08:56:58.52   0.2%  page act:          7940993
11IOwait:           05:55:26.97   0.1%  page dea:          1729013
12hw irq:           00:00:00.00   0.0%  page flt:       1122837199
13sw irq:           00:10:20.71   0.0%  swap in :              330
14idle  :    24w 4d 18:16:48.69  99.1%  swap out:             3408
15uptime:        3w 20:07:24.53         context :       4685239938
16
17irq   0:        159  timer               irq  42:    4102662  em1-tx-0
18irq   8:         48  rtc0                irq  43:    1661923  em1-rx-1
19irq   9:          0  acpi                irq  44:    1286104  em1-rx-2
20irq  10:        179  ipmi_si             irq  45:     948437  em1-rx-3
21irq  18:         38  ehci_hcd:usb1, eh   irq  46:    1888365  em1-rx-4
  • 上面的命令没有使用-d,则显示统计信息为系统从开机以来的总和。显示各项的形式为天、小时、分钟。我们看到idle为24周,系统运行了3周,是因为这台机器有8个CPU。
  • irq no.显示了iraq的编号,已经启动的次数,以及对应负责的内核驱动程序。通过这个我们可以看出哪个设备引起了中断。

mpstat

mpstat命令可以显示随着时间变化的CPU数据,方便我们找出CPU使用率与时间的关系。

mpstat命令包含在安装系统状态查看工具集sysstat中,我们先安装sysstat:

1yum install sysstat

sysstat工具集包含sar, iostat, mpstat, sar等工具

mpstat命令格式:mpstat [-P {cpu | ALL}] [delay [count]]

  • -P指定监控那个CPU,取值0~cpunum-1
  • delay为采样间隔
  • count为采样次数
1mpstat 1 5
2
309:36:35 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
409:36:36 AM  all    0.63    0.00    0.38    0.00    0.00    0.00    0.00    0.00    0.00   99.00
509:36:37 AM  all    0.75    0.00    0.25    0.13    0.00    0.00    0.00    0.00    0.00   98.87
609:36:38 AM  all    1.13    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   98.62
709:36:39 AM  all    1.00    0.00    0.75    0.00    0.00    0.00    0.00    0.00    0.00   98.25
809:36:40 AM  all    0.38    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.37
9Average:     all    0.78    0.00    0.38    0.03    0.00    0.00    0.00    0.00    0.00   98.82

sar

sar即系统活动报告,可以将收据到的系统性能数据记录到二进制文件,之后可以重放二进制文件。 也就是说使用sar命令可以实时显示、记录和重放系统性能信息。

sar命令包含在安装系统状态查看工具集sysstat中,我们先安装sysstat:

1yum install sysstat

sar命令和CPU统计相关的命令格式:sar [ -q ] [ -u [ ALL ] ] [ -w ] [ -I { int [,...] | SUM | ALL | XALL } ] [ -P { cpu [,...] | ALL } ] [ -f [ filename ] | -o [ filename ] [ -s [ hh:mm:ss ] ] [ -e [ hh:mm:ss ] ] [ interval [ count ] ]

  • -q报告系统运行队列长度和平均负载
  • -u报告CPU使用率,这个也是sar命令的默认输出
  • -w报告系统中已经发生的上下文切换次数
  • -I报告系统中已经发生的中断的速率
  • -P指定从哪个CPU收集统计信息
  • -f指定用于重放的统计信息的文件名
  • -o指定用于记录的统计信息的文件名
  • -s-e指定重放统计信息时的开始时间和结束时间
  • interval指定采样的间隔时间
  • count指定采样次数

sar -q

  • runq-sz采样时的运行队列的长度
  • plist-sz采样时的进程的进程数,包括运行、睡眠和等待I/O的进程
  • ldavg-1采样时前1分钟的系统负载
  • ldavg-5采样时前5分钟的系统负载
  • ldavg-15采样时前15分钟的系统负载
  • blocked Number of tasks currently blocked, waiting for I/O to complete.

sar -u:

  • %user:上一个采样中CPU消耗在用户时间上的的百分比
  • %nice: 上一个采样中CPU执行低优先级(或nice)进程消耗时间的百分比
  • %system:上一个采样中CPU消耗在系统时间上的百分比
  • %steal:当 hypervisor 服务另一个虚拟处理器的时候,虚拟 CPU 等待实际 CPU 的时间的百分比。低 steal 值意味着你的应用程序在目前的虚拟机上运作良好。因为你的虚拟机不会经常地为了 CPU 时间与其它虚拟机激烈竞争
  • %idle: 上一个采样中CPU的空闲时间百分比

sar -w

  • proc/s每秒新建进程数据
  • cswch/s每秒上下文切换次数