Unix系统性能监控工具之--SAR

系统环境:

操作系统: AIX 5.3.9

Oracle:   Oracle 10gR2

    SAR可用于监控Unix系统性能,帮助我们分析性能瓶颈。sar工具的使用方式为”sar [选项] intervar [count]”,其中interval为统计信息采样时间,count为采样次数。

下文将说明如何使用sar获取以下性能分析数据:

  1. 整体CPU使用统计

  2. 各个CPU使用统计

  3. 内存使用情况统计

  4. 整体I/O情况

  5. 各个I/O设备情况

  6. 网络统计

常用的选项包括:

-b:报告I/O使用情况以及传输速率。(只适用于2.5及之前的内核,所以新内核有可能不支持这个选项)-B:报告“页”使用情况-c:报告进程创建情况-d:报告每一个块设备的使用情况(当你使用时,你会发现在DEV列有类似dev1-7格式的字符串,其中1代表设备的主序号,n代表设备的从序号,而且rd_sec/s列和wr_sec/s列的单位都是512bytes,也就是512B,也就是0.5KB)-I:汇报中断情况-n:汇报网络情况-P:设定CPU-q:汇报队列长度和负载信息-r:汇报内存和交换区使用情况-R:汇报内存情况-u:汇报CPU使用情况-v:汇报i节点、文件和其他内核表信息-w:汇报系统上下文切换情况-x:可以针对某个特定PID给出统计信息,可以直接指定进程ID号;也可以指定为SELF,这样就是检测sar进程本身;如果设定为ALL,则表示汇报所有系统进程信息。-X:汇报特定PID的子进程的信息-y:设定TTY设备的信息。

案例分析:

1、系统环境

[root@aix211 /]#lspv 

hdisk0          00040f8aaa29d28e                    rootvg          activehdisk1          000e1a6ce663b4af                    asmvg           active

[root@aix211 /]#lsvg -l asmvg

asmvg:LV NAME             TYPE       LPs     PPs     PVs  LV STATE      MOUNT POINTlv_asm1             jfs        64      64      1    open/syncd    N/Alv_asm2             jfs        64      64      1    open/syncd    N/Alv_asm3             jfs        64      64      1    open/syncd    N/Alv_asm4             jfs        64      64      1    open/syncd    N/A

SQL> set linesize 120

SQL> select group_number,name,state from v$asm_diskgroup;

GROUP_NUMBER NAME                           STATE------------ ------------------------------ -----------           1 DG1                            MOUNTED           2 RCY1                           MOUNTED

SQL> select GROUP_NUMBER,DISK_NUMBER,STATE,NAME,PATH from v$asm_disk;

GROUP_NUMBER DISK_NUMBER STATE    NAME                           PATH------------ ----------- -------- ------------------------------ ------------------------           1           0 NORMAL   DG1_0000                       /dev/rlv_asm1           1           1 NORMAL   DG1_0001                       /dev/rlv_asm2           2           0 NORMAL   RCY1_0000                      /dev/rlv_asm3           2           1 NORMAL   RCY1_0001                      /dev/rlv_asm4

在hdisk1磁盘上建立ASMVG磁盘组,并建立逻辑卷;在逻辑卷的基础上,建立ASM disk;

2、数据库环境

SQL> set linesize 120

SQL> select file_id,file_name,tablespace_name from dba_data_files

 FILE_ID FILE_NAME                                          TABLESPACE_NAME---------- -------------------------------------------------- ---------------------------         6 +DG1/prod/datafile/tbs2.265.853498689              TBS2         4 +DG1/prod/datafile/users.261.848857443             USERS         3 +DG1/prod/datafile/sysaux.258.848857287            SYSAUX         2 +DG1/prod/datafile/undotbs1.260.848857407          UNDOTBS1         1 +DG1/prod/datafile/system.257.848857161            SYSTEM         5 +DG1/prod/datafile/example.259.848857363           EXAMPLE

SQL> select file_id,file_name,tablespace_name from dba_temp_files;

 FILE_ID FILE_NAME                                          TABLESPACE_NAME---------- -------------------------------------------------- ---------------------------         2 +DG1/prod/tempfile/temp.262.848858041              TEMP

SQL> col member for a50

SQL> select group#,member from v$logfile order by 1;

GROUP# MEMBER---------- --------------------------------------------------         5 +DG1/prod/onlinelog/group_5.263.848858213         5 +RCY1/prod/onlinelog/group_5.256.848858217         6 +DG1/prod/onlinelog/group_6.264.848858253         6 +RCY1/prod/onlinelog/group_6.257.848858259

SQL> show parameter log

NAME                                 TYPE        VALUE------------------------------------ ----------- ------------------------------log_archive_dest_1                   string      location=/arch/arch_prod

2、案例(执行数据插入)

SQL> select * from tab;TNAME                          TABTYPE  CLUSTERID------------------------------ ------- ----------DEPT                           TABLEEMP                            TABLEBONUS                          TABLESALGRADE                       TABLEEMP1                           TABLEDEPT1                          TABLE6 rows selected.SQL> create table t1 (id int);Table created.SQL> begin   2  for i in 1..100000 loop  3  execute immediate 'insert into t1 values ('||i||')';  4  end loop;  5  end;  6  /     PL/SQL procedure successfully completed.

3、监控系统性能

1)topas监控系统整体性能

[root@aix211 /]#topas

Topas Monitor for host:    aix211               EVENTS/QUEUES    FILE/TTYMon Jul 21 15:36:07 2014   Interval:  2         Cswitch     137  Readch     1250                                                Syscall    5825  Writech    8473CPU  User%  Kern%  Wait%  Idle%                 Reads         9  Rawin         0ALL   50.2    0.2    3.2   46.2                 Writes        3  Ttyout      281                                                Forks         0  Igets         0Network  KBPS   I-Pack  O-Pack   KB-In  KB-Out  Execs         0  Namei        20en0       0.6      4.5     1.0     0.3     0.4  Runqueue    3.5  Dirblk        0lo0       0.1      1.0     1.0     0.0     0.0  Waitqueue   0.0en1       0.0      0.0     0.0     0.0     0.0                                                PAGING           MEMORYDisk    Busy%     KBPS     TPS KB-Read KB-Writ  Faults       11  Real,MB    4096hdisk1   24.0      1.1K   13.0     0.0     1.1K Steals        0  % Comp     37.7hdisk0    0.0      0.0     0.0     0.0     0.0  PgspIn        0  % Noncomp  24.4hdisk3    0.0      0.0     0.0     0.0     0.0  PgspOut       0  % Client   21.1hdisk2    0.0      0.0     0.0     0.0     0.0  PageIn        0cd0       0.0      0.0     0.0     0.0     0.0  PageOut       0  PAGING SPACE                                                Sios          0  Size,MB    4096Name            PID  CPU%  PgSp Owner                            % Used      0.0oracle       544852  49.9  10.6 oracle          NFS (calls/sec)  % Free    100.0java         331954   0.1  20.3 root            ServerV2       0topas        344118   0.1   1.5 root            ClientV2       0   Press:ocssd.bi     364724   0.0  27.6 oracle          ServerV3       0   "h" for helporacle       418002   0.0   9.4 oracle          ClientV3       0   "q" to quitoracle       389330   0.0  13.9 oraclepnsd         311512   0.0   0.5 root  gil           49176   0.0   0.1 root  oracle       196688   0.0  10.5 oraclenfsd         151682   0.0   0.3 root  oracle       327908   0.0   7.5 oraclejava         295068   0.0  36.6 root  rpc.lock     237718   0.0   0.2 root  oracle       377064   0.0   7.4 oracleoracle       503854   0.0  12.4 oraclexntpd        192620   0.0   0.4 rootoracle       446498   0.0  12.3 oracleoracle       356372   0.0   8.0 oracleoracle       409802   0.0   7.3 oracleoracle       380952   0.0   8.0 oracle

可以看到,oracle用户消耗了大部分资源

2)分别查看cpu的资源使用(2个cpu)

[root@aix211 /]#sar -P 0 2 2

AIX aix211 3 5 00040F8A4C00    07/21/14System configuration: lcpu=2  mode=Capped 15:37:09 cpu    %usr    %sys    %wio   %idle15:37:11  0        0       1       9      9015:37:13  0        0       1      21      78Average   0        1       1      15      84

[root@aix211 /]#sar -P 1 2 2

AIX aix211 3 5 00040F8A4C00    07/21/14System configuration: lcpu=2  mode=Capped 15:37:20 cpu    %usr    %sys    %wio   %idle15:37:22  1       99       1       0       015:37:24  1       99       1       0       0Average   1      100       0       0       0以上主要的统计项的解析如下:%user: 用户态下CPU使用时间比率%system: 内核态下CPU使用时间比率%iowait: CPU等待I/O占用时间比率%idle: CPU空闲时间比率

3)查看磁盘I/O

[root@aix211 /]#sar -d 2 2

AIX aix211 3 5 00040F8A4C00    07/21/14System configuration: lcpu=2 drives=5  mode=Capped 15:37:49     device    %busy    avque    r+w/s    Kbs/s   avwait   avserv15:37:51     hdisk0      0      0.0        0        0      0.0      0.0             hdisk2      0      0.0        0        0      0.0      0.0             hdisk3      0      0.0        0        0      0.0      0.0             hdisk1      7      0.0        4       47     12.2     17.0                cd0      0      0.0        0        0      0.0      0.015:37:53     hdisk0      0      0.0        0        0      0.0      0.0             hdisk2      0      0.0        0        0      0.0      0.0             hdisk3      0      0.0        0        0      0.0      0.0             hdisk1     24      1.1       13     1033    169.4     18.3                cd0      0      0.0        0        0      0.0      0.0Average      hdisk0      0      0.0        0        0      0.0      0.0             hdisk2      0      0.0        0        0      0.0      0.0             hdisk3      0      0.0        0        0      0.0      0.0             hdisk1     15      0.6        8      540     90.8     17.7                cd0      0      0.0        0        0      0.0      0.0

[root@aix211 /]#sar -b 2 2

AIX aix211 3 5 00040F8A4C00    07/21/14System configuration: lcpu=2  mode=Capped 15:18:49 bread/s lread/s %rcache bwrit/s lwrit/s %wcache pread/s pwrit/s15:18:51       0       0       0       0       0       0       0      1315:18:53       0       0       0       0       0       0       0       0Average        0       0       0       0       0       0       0       6以上各列的含义为:tps: 每秒向磁盘设备请求数据的次数,包括读、写请求,为rtps与wtps的和。出于效率考虑,每一次IO下发后并不是立即处理请求,而是将请求合并(merge),这里tps指请求合并后的请求计数。rtps: 每秒向磁盘设备的读请求次数wtps: 每秒向磁盘设备的写请求次数bread: 每秒从磁盘读的bytes数量bwrtn: 每秒向磁盘写的bytes数量

[root@aix211 /]#iostat 2 2

System configuration: lcpu=2 drives=5 paths=1 vdisks=0tty:      tin         tout    avg-cpu: % user % sys % idle % iowait          0.0         29.0               49.7   1.0   35.8     13.5Disks:        % tm_act     Kbps      tps    Kb_read   Kb_wrtnhdisk0           0.0       0.0       0.0          0         0hdisk2           0.0       0.0       0.0          0         0hdisk3           0.0       0.0       0.0          0         0hdisk1          29.0     1214.0      15.0          0      2428cd0              0.0       0.0       0.0          0         0tty:      tin         tout    avg-cpu: % user % sys % idle % iowait          0.0        260.5               49.7   0.8   49.5      0.0Disks:        % tm_act     Kbps      tps    Kb_read   Kb_wrtnhdisk0           0.0       0.0       0.0          0         0hdisk2           0.0       0.0       0.0          0         0hdisk3           0.0       0.0       0.0          0         0hdisk1           0.0       0.0       0.0          0         0cd0              0.0       0.0       0.0          0         0

[root@aix211 /]#iostat -D

System configuration: lcpu=2 drives=5 paths=1 vdisks=0hdisk0         xfer:  %tm_act      bps      tps      bread      bwrtn                             1.0     34.1K     2.6        7.4K      26.7K               read:      rps  avgserv  minserv  maxserv   timeouts      fails                         1.1      4.1      0.1     95.6           0          0              write:      wps  avgserv  minserv  maxserv   timeouts      fails                         1.5      8.8      0.4     83.4           0          0              queue:  avgtime  mintime  maxtime  avgwqsz    avgsqsz     sqfull                        18.9      0.0    679.0      0.0        0.0         0.9hdisk2         xfer:  %tm_act      bps      tps      bread      bwrtn                             0.0      0.0      0.0        0.0        0.0                read:      rps  avgserv  minserv  maxserv   timeouts      fails                         0.0      0.0      0.0      0.0           0          0              write:      wps  avgserv  minserv  maxserv   timeouts      fails                         0.0      0.0      0.0      0.0           0          0              queue:  avgtime  mintime  maxtime  avgwqsz    avgsqsz     sqfull                         0.0      0.0      0.0      0.0        0.0         0.0hdisk3         xfer:  %tm_act      bps      tps      bread      bwrtn                             0.0      0.0      0.0        0.0        0.0                read:      rps  avgserv  minserv  maxserv   timeouts      fails                         0.0      0.0      0.0      0.0           0          0              write:      wps  avgserv  minserv  maxserv   timeouts      fails                         0.0      0.0      0.0      0.0           0          0              queue:  avgtime  mintime  maxtime  avgwqsz    avgsqsz     sqfull                         0.0      0.0      0.0      0.0        0.0         0.0hdisk1         xfer:  %tm_act      bps      tps      bread      bwrtn                             9.1      1.1M    49.6        1.0M      66.5K               read:      rps  avgserv  minserv  maxserv   timeouts      fails                        46.1      0.6      0.2     39.9           0          0              write:      wps  avgserv  minserv  maxserv   timeouts      fails                         3.4     18.1      3.8     85.5           0          0              queue:  avgtime  mintime  maxtime  avgwqsz    avgsqsz     sqfull                        12.1      0.0      5.8S     0.3        0.0        49.6cd0            xfer:  %tm_act      bps      tps      bread      bwrtn                             0.0      0.0      0.0        0.0        0.0 --------------------------------------------------------------------------------

4)查看内存使用情况

[root@aix211 /]#sar -r 2 2

AIX aix211 3 5 00040F8A4C00    07/21/14System configuration: lcpu=2 mem=4096MB  mode=Capped 15:38:12   slots cycle/s fault/s  odio/s15:38:14 1046571    0.00   44.00    0.0015:38:16 1046571    0.00   13.00    0.00Average  1046571       0      28       0上面输出的内存单位为”kb”

@有以上案例可以看出,I/O的资源都在hdisk1上,hdisk1为datafile和redolog的存储磁盘(ASM磁盘组),所以在做DML操作时,产生了大量的I/O.

备注:

SAR怎么控制信息输出的时间间隔和次数?

有两个参数非常非常常用,就是“时间间隔”和“输出次数”。

时间间隔表示两次信息输出之间的时间间隔,单位是秒。如果这个值被设置为0,则表示所输出的信息是从开机到现在为止的信息平均值。如果不是0,sar计算就都是从当前开始的信息的平均值。

输出次数表示输出系统信息的次数,默认是1次。如果这个值被设置为0,则会永远的输出下去。

比如:sar 60 5 表示每60秒输出一次,共输出5次。

 怎么把sar输出的这些有用信息存到文件中去呢?

sar专门提供了一个选项-o filename,你只要使用它,就可以喽!

比如 sar 5 6 -o sys_info

不过,不幸的是,当你用cat看sys_info时发现全都是乱码,呵呵,别着急,sar为你准备了-f filename选项,你只要用-f设定要读取的信息存储文件,就可以清晰地读出信息了。

比如sar -f sys_info。

如果你觉得这种格式有些不利于处理,试试加上-h选项,你将获得不一样的体验。