安装sysstat rpm包就有iostat、mpstat、sar、sa的功能,rpm –Uvh sysstat*

mpstat

另一个用于获取 CPU 相关统计信息的有用的命令是 mpstat。下面是一个示例输出:

# mpstat -P ALL 5 2

Linux 2.6.9-67.ELsmp (oraclerac1) 12/20/2008

10:42:38 PM CPU%user %nice %system %iowait %irq%soft %idle intr/s

10:42:43 PM all6.89 0.00 44.760.10 0.10 0.1048.05 1121.60

10:42:43 PM 09.20 0.00 49.000.00 0.00 0.2041.60 413.00

10:42:43 PM 14.60 0.00 40.600.00 0.20 0.2054.60 708.40

10:42:43 PM CPU%user %nice %system %iowait %irq%soft %idle intr/s

10:42:48 PM all7.60 0.00 45.300.30 0.00 0.1046.70 1195.01

10:42:48 PM 04.19 0.00 2.200.40 0.00 0.0093.21 1034.53

10:42:48 PM 110.78 0.00 88.220.40 0.00 0.000.20 160.48

Average: CPU%user %nice %system %iowait %irq%soft %idle intr/s

Average: all7.25 0.00 45.030.20 0.05 0.1047.38 1158.34

Average: 06.69 0.00 25.570.20 0.00 0.1067.43 724.08

Average: 17.69 0.00 64.440.20 0.10 0.1027.37 434.17

它显示了系统中 CPU 的各种统计信息。–P ALL 选项指示该命令显示所有 CPU 的统计信息,而不只是特定 CPU 的统计信息。参数 5 2 指示该命令每隔 5 秒运行一次,共运行 2次。以上输出首先显示了所有 CPU 的合计指标,然后显示了每个 CPU 各自的指标。最后,在结尾处显示所有 CPU 的平均值。

让我们看一看这些列值的含义:

%user表示处理用户进程所使用 CPU 的百分比。用户进程是用于应用程序(如 Oracle 数据库)的非内核进程。在本示例输出中,用户 CPU 百分比非常低。

%nice表示使用 nice 命令对进程进行降级时 CPU 的百分比。在之前的部分中已经对 nice 命令进行了介绍。简单来说,nice 命令更改进程的优先级。

%system表示内核进程使用的 CPU 百分比

%iowait表示等待进行 I/O 所使用的 CPU 时间百分比

%irq表示用于处理系统中断的 CPU 百分比

%soft表示用于软件中断的 CPU 百分比

%idle显示 CPU 的空闲时间

%intr/s显示每秒 CPU 接收的中断总数

当您拥有前面所述的 vmstat 时,您可能想知道 mpstat 命令的作用。差别很大:mpstat 可以显示每个处理器的统计,而vmstat 显示所有处理器的统计。因此,编写糟糕的应用程序(不使用多线程体系结构)可能会运行在一个多处理器机器上,而不使用所有处理器。从而导致一个 CPU 过载,而其他 CPU 却很空闲。通过 mpstat 可以轻松诊断这些类型的问题。

针对 Oracle 用户的用法 与 vmstat 相似,mpstat 命令还产生与 CPU 有关的统计信息,因此所有与CPU 问题有关的讨论也都适用于 mpstat。

当您看到较低的 %idle 数字时,您知道出现了 CPU 不足的问题。当您看到较高的 %iowait 数字时,您知道在当前负载下 I/O 子系统出现了某些问题。该信息对于解决 Oracle 数据库性能问题非常方便。

sar

Sar以下命令都可以interval count参数

[root@daidai ~]# sar -u 2 2

Linux 2.6.18-194.el5 (daidai.com) 06/20/2016

11:14:10 AM CPU%user %nice %system%iowait %steal %idle

11:14:12 AM all0.25 0.00 0.000.25 0.00 99.49

11:14:14 AM all0.00 0.50 0.760.00 0.00 98.74

Average: all 0.130.25 0.38 0.130.00 99.12

CPUall 表示统计信息为所有 CPU 的平均值。

%user显示在用户级别(application)运行使用 CPU 总时间的百分比。

%nice显示在用户级别,用于nice操作,所占用 CPU 总时间的百分比。

%system 在核心级别(kernel)运行所使用 CPU 总时间的百分比。

%iowait 显示用于等待I/O操作占用 CPU 总时间的百分比。

%steal管理程序(hypervisor)为另一个虚拟进程提供服务而等待虚拟 CPU 的百分比。

%idle显示 CPU 空闲时间占用 CPU 总时间的百分比。

tips:

若 %iowait 的值过高,表示硬盘存在I/O瓶颈

若 %idle 的值高但系统响应慢时,有可能是 CPU 等待分配内存,此时应加大内存容量

若 %idle 的值持续低于 10,则系统的 CPU 处理能力相对较低,表明系统中最需要解决的资源是 CPU。

[root@daidai ~]# sar -b 2 3

Linux 2.6.18-194.el5 (daidai.com) 06/20/2016

11:31:08 AM tpsrtps wtps bread/sbwrtn/s

11:31:10 AM 3.030.00 3.03 0.00129.29

11:31:12 AM 3.030.00 3.03 0.0064.65

11:31:14 AM 3.050.00 3.05 0.0064.97

Average: 3.04 0.003.04 0.00 86.34

tps每秒钟物理设备的 I/O 传输总量

rtps每秒钟从物理设备读入的数据总量

wtps每秒钟向物理设备写入的数据总量

bread/s 每秒钟从物理设备读入的数据量,单位为 块/s

bwrtn/s 每秒钟向物理设备写入的数据量,单位为 块/s

[root@daidai ~]# sar -c

Linux 2.6.18-194.el5 (daidai.com) 06/20/2016

09:50:01 AM proc/s

10:00:01 AM 0.38

10:10:01 AM 0.30

10:20:01 AM 0.30

10:30:01 AM 0.45

10:40:01 AM 0.30

11:20:01 AM 0.79

11:30:01 AM 0.28

Average: 0.43

每秒钟创建的进程数

[root@daidai ~]# sar -n DEV 1 2

Linux 2.6.18-194.el5 (daidai.com) 06/20/2016

11:39:34 AM IFACErxpck/s txpck/s rxbyt/stxbyt/s rxcmp/s txcmp/srxmcst/s

11:39:35 AM lo0.00 0.00 0.000.00 0.00 0.000.00

11:39:35 AM eth00.00 0.00 0.000.00 0.00 0.000.00

11:39:35 AM sit00.00 0.00 0.000.00 0.00 0.000.00

11:39:35 AM IFACErxpck/s txpck/s rxbyt/stxbyt/s rxcmp/s txcmp/srxmcst/s

11:39:36 AM lo2.04 2.04 102.04102.04 0.00 0.000.00

11:39:36 AM eth03.06 5.10 183.67867.35 0.00 0.000.00

11:39:36 AM sit00.00 0.00 0.000.00 0.00 0.000.00

Average: IFACErxpck/s txpck/s rxbyt/stxbyt/s rxcmp/s txcmp/srxmcst/s

Average: lo 1.021.02 50.76 50.760.00 0.00 0.00

Average: eth0 1.522.54 91.37 431.470.00 0.00 0.00

Average: sit0 0.000.00 0.00 0.000.00 0.00 0.00

IFACE 网络设备名

rxpck/s 每秒接收的包总数

txpck/s 每秒传输的包总数

rxbyt/s 每秒接收的字节(byte)总数

txbyt/s 每秒传输的字节(byte)总数

rxcmp/s 每秒接收压缩包的总数

txcmp/s每秒传输压缩包的总数

rxmcst/s 每秒接收的多播(multicast)包的总数

[root@daidai ~]# sar -q 1 3

Linux 2.6.18-194.el5 (daidai.com) 06/20/2016

11:42:54 AM runq-szplist-sz ldavg-1 ldavg-5ldavg-15

11:42:55 AM 0184 0.02 0.020.00

11:42:56 AM 0184 0.02 0.020.00

11:42:57 AM 0184 0.02 0.020.00

Average: 0 1840.02 0.02 0.00

runq-sz运行队列的长度(等待运行的进程数)

plist-sz 进程列表中进程(processes)和线程(threads)的数量

ldavg-1最后1分钟的系统平均负载(System load average)

ldavg-5过去5分钟的系统平均负载

ldavg-15 过去15分钟的系统平均负载

[root@daidai ~]# sar -r

Linux 2.6.18-194.el5 (daidai.com) 06/20/2016

09:50:01 AM kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpusedkbswpcad

10:00:01 AM 10676003869580 78.38 1956163249408 10482404 00.00 0

10:10:01 AM 10686243868556 78.36 1956163249500 10482404 00.00 0

10:20:01 AM 10694923867688 78.34 1956163249572 10482404 00.00 0

10:30:01 AM 10790243858156 78.14 1956163249588 10482404 00.00 0

10:40:01 AM 10796763857504 78.13 1956163249616 10482404 00.00 0

11:20:01 AM 10837923853388 78.05 1956163249952 10482404 00.00 0

11:30:01 AM 10834203853760 78.06 1956163249984 10482404 00.00 0

11:40:01 AM 10841643853016 78.04 1956163250008 10482404 00.00 0

Average: 10769743860206 78.19 1956163249704 10482404 00.00 0

kbmemfree Amountof free memory available in kilobytes.

kbmemused Amount of used memory in kilobytes. This doesnot take into account memory used by the kernel itself.

%memused Percentage of used memory.

kbbuffers Amount of memory used as buffers by thekernel in kilobytes.

kbcached Amount of memory used to cache data by thekernel in kilobytes.

kbswpfree Amount of free swap space in kilobytes.

kbswpused Amount of used swap space in kilobytes.

%swpused Percentage of used swap space.

kbswpcad Amount of cached swap memory inkilobytes. This is memory that once was swappedout, is swapped back in but still also is in the swap area (if memory is neededit doesn’t need to be swapped out again because it is already in the swap area.This saves I/O).

iostat

[root@daidai ~]# iostat 1 3

Linux 2.6.18-194.el5 (daidai.com) 06/20/2016

avg-cpu:%user %nice %system %iowait %steal%idle

0.23 0.020.43 0.11 0.0099.22

Device: tps Blk_read/sBlk_wrtn/s Blk_read Blk_wrtn

sda 3.15 15.25 60.923462295 13828032

sda1 3.15 15.24 60.923460258 13828032

sda2 0.00 0.01 0.00 1613 0

sr0 0.00 0.01 0.00 1644 0

avg-cpu:%user %nice %system %iowait %steal%idle

0.51 0.000.51 0.00 0.0098.98

Device: tps Blk_read/sBlk_wrtn/s Blk_read Blk_wrtn

sda 0.00 0.00 0.00 0 0

sda1 0.00 0.000.00 0 0

sda2 0.00 0.00 0.00 0 0

sr0 0.00 0.00 0.00 0 0

avg-cpu:%user %nice %system %iowait %steal%idle

0.00 0.000.51 0.00 0.0099.49

Device: tps Blk_read/sBlk_wrtn/s Blk_read Blk_wrtn

sda 3.00 0.00 64.00 0 64

sda1 3.00 0.00 64.00 0 64

sda2 0.00 0.00 0.00 0 0

sr0 0.00 0.00 0.00 0 0

vmstat

[root@daidai ~]# vmstat 1 5

procs -----------memory-------------swap-- -----io---- --system-- -----cpu------

rb swpd freebuff cache siso bi boin cs us sy id wa st

00 0 1083368 1957003250136 0 04 16 2441 0 0 990 0

00 0 1083368 1957003250136 0 00 40 998205 1 1 990 0

0 0 0 1083368 195700 3250136 00 0 32998 221 0 298 10

00 0 1089088 1957003250136 0 00 0 998218 0 1 990 0

00 0 1089088 1957003250136 0 00 0 1004 1910 0 100 0 0

Procs

r: The number of processes waiting forrun time.

b: The number of processes inuninterruptible sleep.

Memory

swpd: the amount of virtual memory used.

free: the amount of idle memory.

buff: the amount of memory used asbuffers.

cache: the amount of memory used ascache.

inact: the amount of inactive memory.(-a option)

active: the amount of active memory. (-aoption)

Swap

si: Amount of memory swapped in fromdisk (/s).

so: Amount of memory swapped to disk(/s).

IO

bi: Blocks received from a block device (blocks/s).

bo: Blocks sent to a block device(blocks/s).

System

in: The number of interrupts per second,including the clock.

cs: The number of context switches persecond.

CPU

These 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 Linux2.5.41, this includes IO-wait time.

wa: Time spent waiting for IO. Prior toLinux 2.5.41, included in idle.

st: Time stolen from a virtual machine.Prior to Linux 2.6.11, unknown.