Linux常用命令之--sar

sar(system activity reporter 系统活动情况报告),用于提取在一段时间内系统的性能分析报告,包括:文件的读写情况,系统调用的使用情况,磁盘I/O,CPU效率,内存使用状况,进程活动及IPC有关的活动。

安装使用

  1. yum install sysstat/apt-get install sysstat
  2. 修改文件/etc/default/sysstat中的内容为ENANBLED="true"
  3. 重启服务systemctl restart sysstat

    命令参数

    sar -h的输出见下,使用方式及参数说明都挺直观。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    Usage: sar [ options ] [ <interval> [ <count> ] ]
    Main options and reports:
    -b I/O and transfer rate statistics
    -B Paging statistics
    -d Block device statistics
    -F [ MOUNT ]
    Filesystems statistics
    -H Hugepages utilization statistics
    -I { <int> | SUM | ALL | XALL }
    Interrupts statistics
    -m { <keyword> [,...] | ALL }
    Power management statistics
    Keywords are:
    CPU CPU instantaneous clock frequency
    FAN Fans speed
    FREQ CPU average clock frequency
    IN Voltage inputs
    TEMP Devices temperature
    USB USB devices plugged into the system
    -n { <keyword> [,...] | ALL }
    Network statistics
    Keywords are:
    DEV Network interfaces
    EDEV Network interfaces (errors)
    NFS NFS client
    NFSD NFS server
    SOCK Sockets (v4)
    IP IP traffic (v4)
    EIP IP traffic (v4) (errors)
    ICMP ICMP traffic (v4)
    EICMP ICMP traffic (v4) (errors)
    TCP TCP traffic (v4)
    ETCP TCP traffic (v4) (errors)
    UDP UDP traffic (v4)
    SOCK6 Sockets (v6)
    IP6 IP traffic (v6)
    EIP6 IP traffic (v6) (errors)
    ICMP6 ICMP traffic (v6)
    EICMP6 ICMP traffic (v6) (errors)
    UDP6 UDP traffic (v6)
    -q Queue length and load average statistics
    -r Memory utilization statistics
    -R Memory statistics
    -S Swap space utilization statistics
    -u [ ALL ]
    CPU utilization statistics
    -v Kernel table statistics
    -w Task creation and system switching statistics
    -W Swapping statistics
    -y TTY device statistics

    常用命令

    查看CPU使用率

    sar 1 3默认显示的是cpu使用率信息,等同于sar -u 1 3
    1
    2
    3
    4
    5
    6
    7
    Linux 4.15.0-173-generic (zw)   10/03/2022      _x86_64_        (16 CPU)

    08:07:19 AM CPU %user %nice %system %iowait %steal %idle
    08:07:20 AM all 17.87 0.00 2.05 0.00 0.00 80.07
    08:07:21 AM all 15.10 0.00 0.94 0.00 0.00 83.96
    08:07:22 AM all 16.56 0.00 1.69 0.00 0.00 81.74
    Average: all 16.51 0.00 1.56 0.00 0.00 81.92
  • %user:用户模式下消耗的CPU时间比例
  • %nice:通过nice改变了进程调度优先级的进程,在用户模式下消耗的CPU时间比例
  • %system:系统模式下消耗的CPU时间比例
  • %iowait:CPU等待磁盘I/O导致空闲状态消耗的时间比例
  • %steal:利用xen等操作系统虚拟化技术,等待其他虚拟CPU计算占用的时间比例
  • %idle:CPU空闲时间比例
    1. 若%iowait的值过高,表示磁盘存在I/O瓶颈
    2. 若%idle的值高但系统响应慢时,可能是CPU等待分配内存,此时应加大内存容量
    3. 若%idle的值持续低于1,则系统CPU的处理能力相对较低,考虑CPU的资源

I/O和传输速率统计

sar -b 1 3

1
2
3
4
5
6
7
Linux 4.15.0-173-generic (zw)   10/03/2022      _x86_64_        (16 CPU)

08:20:24 AM tps rtps wtps bread/s bwrtn/s
08:20:25 AM 6.00 0.00 6.00 0.00 80.00
08:20:26 AM 0.00 0.00 0.00 0.00 0.00
08:20:27 AM 0.00 0.00 0.00 0.00 0.00
Average: 2.00 0.00 2.00 0.00 26.67
  • tps:每秒钟物理设备的I/O传输总量

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

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

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

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

    内存利用率

    sar -r 1 3

    1
    2
    3
    4
    5
    6
    7
    Linux 4.15.0-173-generic (zw)   10/03/2022      _x86_64_        (16 CPU)

    08:27:46 AM kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
    08:27:47 AM 3897560 65082240 94987480 96.06 11224640 47914160 120160988 120.74 40025904 50646580 1808
    08:27:48 AM 3404600 64589496 95480440 96.56 11224640 47914440 120226200 120.81 40517296 50646796 2084
    08:27:49 AM 3465888 64650784 95419152 96.50 11224640 47914440 120163888 120.74 40456772 50646796 2084
    Average: 3589349 64774173 95295691 96.37 11224640 47914347 120183692 120.76 40333324 50646724 1992
  • kbmemfree:基本等同于free中的值free,不包括buffer和cache的空间

  • kbmemused:基本等同于free中的值used,包括buffer和cache的空间

  • %memused:物理内存总量,是kbmemused和内存总量的(不包括swap)的百分比

  • kbbuffers和kbcached:就是free中的buffer和cache

  • kbcommit:保证当前系统所需的内存,即为了确保不溢出而需要的内存(RAM + swap)

  • %commit:是kbcommit与内存总量(包括swap)的百分比

    排查技巧

  • 怀疑CPU存在瓶颈,可用sar -usar -q等查看

  • 怀疑内存存在瓶颈,可用sar -Bsar -rsar -W等查看

  • 怀疑I/O存在瓶颈,可用sar -bsar -usar -d等查看