sar (System Activity Reporter system activity report) is one of the most comprehensive system performance analysis tools on Linux. It can
report system activities from various aspects, including: file read and write, system call usage, disk I/O, CPU efficiency, memory usage,
process activity, and IPC-related activities. This article mainly takes the CentOS 6.3 x64 system as an example to introduce the sar command.
Format
sar [options] [-A] [-o file] t [n]
parameter | illustrate |
---|---|
t | is the sampling interval, the default is seconds |
n | is the number of samples, the default is 1 |
-O | file means to store the command result in a file in binary format, file is the file name |
-A | sum of all reports |
-u | Output statistics of CPU usage |
-v | Output statistics for inodes, files and other kernel tables |
-d | Output activity information for each block device |
-r | Output statistics of memory and swap space |
-b | Display I/O and transfer rate statistics |
-a | file read and write |
-c | Output process statistics, the number of processes created per second |
-R | Output statistics of memory pages |
-and | Terminal equipment activity |
-w | Output system exchange activity information |
CPU resource monitoring
sar -u -o test 8 4
Linux 2.6.32-358.el6.x86_64 (nagios_s1) 11/12/15 _x86_64_ (1 CPU)
16:07:18 CPU %user %nice %system %iowait %steal %idle
16:07:26 all 0.00 0.00 0.00 0.00 0.00 100.00
16:07:34 all 0.00 0.00 0.00 0.00 0.00 100.00
16:07:42 all 0.00 0.00 0.00 0.00 0.00 100.00
16:07:50 all 0.00 0.00 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.00 0.00 0.00 100.00
%user : 显示在用户级别(application)运行使用CPU总时间的百分比。
%nice : 显示在用户级别,用于nice操作(进程优先级切换),所占用CPU总时间的百分比
%system : 在核心级别(kernel) 运行所使用CPU总时间的百分比。
%iowait : 显示用于等待I/O操作占用CPU总时间的百分比。
%steal : 管理程序(hypervisor)为另一虚拟进程提供服务而等待虚拟CPU的百分比。
%idle : 显示CPU空闲时间占用CPU总时间的百分比。
1. If the value of %iowait is too high, it means that the hard disk has an I/O bottleneck
. 2. If the value of %idle is high but the system response is slow, it may be that the CPU is waiting to allocate memory, and the memory capacity should be increased
. 3. If the value of %idle is high If it is continuously lower than 1, the CPU processing power of the system is relatively low, indicating that the resource that needs to be solved most in the system is the CPU.
To view the contents of the binary file test, type the following sar command:
sar -u -f test
Monitor inodes, files, and other kernel tables
sar -v 8 4
Linux 2.6.32-358.el6.x86_64 (nagios_s1) 11/12/15 _x86_64_ (1 CPU)
16:32:40 dentunusd file-nr inode-nr pty-nr
16:32:48 5296 288 7672 2
16:32:56 5296 288 7672 2
16:33:04 5296 288 7672 2
16:33:12 5296 288 7672 2
Average: 5296 288 7672 2
dentunusd : 目录高速缓存中未被使用的条目数量
file-nr : 文件句柄(file handle) 的使用数量
inode-nr : 索引节点句柄(inode handle) 的使用数量
pty-nr : 使用的pty数量
Monitor memory and swap space
sar -r 8 4
Linux 2.6.32-358.el6.x86_64 (nagios_s1) 11/12/15 _x86_64_ (1 CPU)
16:41:15 kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit
16:41:23 301116 200320 39.95 10688 147824 56648 2.77
16:41:31 301116 200320 39.95 10688 147824 56648 2.77
16:41:39 301116 200320 39.95 10688 147824 56648 2.77
16:41:47 301116 200320 39.95 10688 147824 56648 2.77
Average: 301116 200320 39.95 10688 147824 56648 2.77
-r Report memory utilization statistics. The following values are displayed:
kbmemfree : 这个值和free命令中的free值基本一致,所以它不包括buffer和cache的空间.
kbmemused : 这个值和free命令中的free值基本一致,所以它不包括buffer和cache的空间.
%memused : 这个值是kbmemused和内存总量(不包括swap)的一个百分比.
kbbuffers :这个值就是free命令中的buffer.
kbcached :这个值就是free命令中的cache.
kbcommit :保证当前系统所需要的内存,即为了确保不溢出而需要的内存(RAM+swap).
%commit :这个值是kbcommit与内存总量(包括swap)的一个百分比.
Memory paging monitoring
sar -B 8 4
Linux 2.6.32-358.el6.x86_64 (nagios_s1) 11/13/15 _x86_64_ (1 CPU)
11:08:48 pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff
11:08:56 0.00 0.00 4.25 0.00 9.25 0.00 0.00 0.00 0.00
11:09:04 0.00 0.00 5.25 0.00 10.75 0.00 0.00 0.00 0.00
11:09:12 0.00 0.00 3.75 0.00 8.89 0.00 0.00 0.00 0.00
11:09:20 0.00 0.00 3.75 0.00 10.11 0.00 0.00 0.00 0.00
Average: 0.00 0.00 4.25 0.00 9.75 0.00 0.00 0.00 0.00
Report paging statistics. The following values are displayed:
pgpgin/s: 表示每秒从磁盘或SWAP置换到内存的字节数(KB)
pgpgout/s: 表示每秒从内存置换到磁盘或SWAP的字节数(KB)
fault/s: 每秒钟系统产生的缺页数,即主缺页与次缺页之和(major + minor)
majflts/s: 每秒钟产生的主缺页数
pgfree/s: 每秒被放入空闲队列中的页个数
pgscank/s: 每秒被kswapd扫描的页个数
pgscand/s: 每秒直接被扫描的页个数
pgsteal/s: 每秒钟从cache中被清除来满足内存需要的页个数
%vmeff: 每秒清除的页(pgsteal)占总扫描页(pgscank+pgscand)的百分比
What is a page fault?
When the CPU requests a memory page that is not in RAM, a page fault will occur. For example, when we read/write data from memory, but the data is not in the memory, a page fault will occur.
A major means a major page fault, a major page fault is a page fault that requires input/output to disk. A
minor means a minor page fault, and a minor page fault is any other page fault.
I/O and transfer rate monitoring
sar -b 8 4
Linux 2.6.18-308.el5xen (tlsgzsit01) 11/13/2015
12:04:30 PM tps rtps wtps bread/s bwrtn/s
12:04:38 PM 10.92 0.00 10.92 0.00 116.44
12:04:46 PM 1.75 0.00 1.75 0.00 53.13
12:04:54 PM 2.23 0.00 2.23 0.00 61.46
12:05:02 PM 1.25 0.00 1.25 0.00 24.94
Average: 4.03 0.00 4.03 0.00 63.92
tps : 每秒钟物理设备的 I/O 传输总量
rtps : 每秒钟从物理设备读入的数据总量
wtps : 每秒钟向物理设备写入的数据总量
bread/s : 每秒中从物理设备读入的数据量,单位为 块/s
bwrtn/s : 每秒中从物理设备写入的数据量,单位为 块/s
View the number of processes in the run queue, process size on the system, load average, etc.
Compared with other commands, sar can view the changes of various indicators over time;
sar -q
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain) 11/02/2017 _x86_64_ (2 CPU)
12:00:01 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
12:10:01 AM 0 150 0.00 0.01 0.05 0
12:20:01 AM 0 150 0.00 0.01 0.05 0
12:30:01 AM 2 150 0.00 0.01 0.05 0
12:40:01 AM 0 150 0.00 0.01 0.05 0
12:50:01 AM 0 150 0.00 0.01 0.05 0
01:00:01 AM 1 150 0.00 0.01 0.05 0
01:10:01 AM 1 150 0.00 0.01 0.05 0
01:20:01 AM 0 150 0.00 0.01 0.05 0
01:30:01 AM 0 150 0.00 0.01 0.05 0
01:40:01 AM 1 150 0.00 0.01 0.05 0
01:50:01 AM 1 150 0.00 0.01 0.05 0
02:00:01 AM 0 150 0.00 0.01 0.05 0
02:10:01 AM 0 150 0.00 0.01 0.05 0
02:20:01 AM 0 150 0.00 0.01 0.05 0
02:30:01 AM 0 150 0.00 0.01 0.05 0
runq-sz:运行队列的长度(等待运行的进程数)
plist-sz:进程列表中进程(processes)和线程(threads)的数量
ldavg-1:最后1分钟的系统平均负载
ldavg-5:过去5分钟的系统平均负载
ldavg-15:过去15分钟的系统平均负载