3、iostat

  第三个可以用来使用的命令是iostat.

 

$ iostat -t 2 4
tty: tin tout avg-cpu: % user % sys % idle % iowait
0.0 0.0 0.0 0.1 99.8 0.1
0.0 81.0 0.0 0.1 99.9 0.0
0.0 40.5 0.0 0.0 100.0 0.0
0.0 40.5 0.0 0.1 99.1 0.8

 

  TTY 的两列信息(tin 和 tou)显示了由所有 TTY 设备读写的字符数

  CPU 统计信息列(% user、% sys、% idle 和 % iowait)提供了 CPU 的使用情况。

  注意:第一份报告为系统启动以来的一个累积值。

4、tprof

  使用tprof命令用于统计每个进程的CPU使用情况

  # tprof -x sleep 30

  该命令的输出结果可查看 __prof.all文件。

  此命令运行30秒钟,在当前目录下创建一个名为_prof.all 的文件。30秒钟内, CPU被调度次数约为3000次。__prof.all 文件中的字段Total 为此进程调度到的CPU次数。如果进程所对应的 Total字 段的值为1500,即表示该进程在3000次 CPU调度中占用了1500次,或理解为使用了一半的CPU时间。tprof的输出准确地显示出哪个进程在使用CPU 时间。

  在我下面的这一份示例中,可以看到,大部分的cpu时间都是被wait所占用的。这里的wait实际上是idle进程,可以表明这个系统是一个完全空闲的系统。

 

$ more __prof.all
Process PID TID Total Kernel User Shared Other
======= === === ===== ====== ==== ====== =====
wait 40970 40971 2998 2998 0 0 0
wait 32776 32777 2994 2994 0 0 0
wait 24582 24583 2985 2985 0 0 0
wait 16388 16389 2980 2980 0 0 0
syncd 221254 155707 31 31 0 0 0
caiUxOs 524540 2294015 3 0 0 3 0
netm 73746 73747 1 1 0 0 0
hats_nim 1671242 1220665 1 0 0 1 0
snmpd64 598258 1245291 1 1 0 0 0
rpc.lockd 639212 1728679 1 1 0 0 0
tprof 704622 2277437 1 0 0 1 0
trclogio 360524 2408625 1 1 0 0 0
trace 1523820 2523145 1 0 0 1 0
clinfo 1958102 2760945 1 1 0 0 0
sh 1572938 2285709 1 1 0 0 0
======= === === ===== ====== ==== ====== =====
Total 12000 11994 0 6 0


Process FREQ Total Kernel User Shared Other
======= === ===== ====== ==== ====== =====
wait 4 11957 11957 0 0 0
syncd 1 31 31 0 0 0
caiUxOs 1 3 0 0 3 0
netm 1 1 1 0 0 0
hats_nim 1 1 0 0 1 0
snmpd64 1 1 1 0 0 0
rpc.lockd 1 1 1 0 0 0
tprof 1 1 0 0 1 0
trclogio 1 1 1 0 0 0
trace 1 1 0 0 1 0
clinfo 1 1 1 0 0 0
sh 1 1 1 0 0 0
======= === ===== ====== ==== ====== =====
Total 15 12000 11994 0 6 0

 

  在这里,对wait进程作一点补充说明。

  在AIX 5L下,你用ps aux会发现有一些root的wait进程

 

在AIX 5L下,你用ps aux会发现有一些root的wait进程
#ps aux |head -20
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
oracle 266354 5.7 0.0 50136 27524 - A 15:40:35 0:32 oracleora92 (LOC
root 17214 3.1 0.0 40 40 - A Jul 04 24793:53 wait
root 16946 3.1 0.0 40 40 - A Jul 04 24633:59 wait
root 16678 3.1 0.0 40 40 - A Jul 04 24600:21 wait
root 53274 3.1 0.0 40 40 - A Jul 04 24397:54 wait
root 286 3.1 0.0 40 40 - A Jul 04 24371:55 wait
root 8196 3.0 0.0 40 40 - A Jul 04 24312:40 wait
root 822 3.0 0.0 40 40 - A Jul 04 24303:36 wait
root 554 3.0 0.0 40 40 - A Jul 04 24261:50 wait
root 20776 2.7 0.0 40 40 - A Jul 04 21502:46 wait
root 57372 2.7 0.0 40 40 - A Jul 04 21439:31 wait
root 49176 2.7 0.0 40 40 - A Jul 04 21423:47 wait
root 21044 2.7 0.0 40 40 - A Jul 04 21398:24 wait
root 12848 2.7 0.0 40 40 - A Jul 04 21357:07 wait
root 21312 2.7 0.0 40 40 - A Jul 04 21324:26 wait
root 12580 2.7 0.0 40 40 - A Jul 04 21293:06 wait
root 13116 2.7 0.0 40 40 - A Jul 04 21195:47 wait
oracle 344612 0.3 0.0 57588 34976 - A Jul 04 2663:08 ora_j000_ora92
oracle 430408 0.3 0.0 55908 33296 - A Jul 04 2220:57 ora_j001_ora92

 

  wait就是CPU空闲的时候运行的空闲进程,AIX4上叫kproc。所以这个进程占用越大,表示机器越空闲。Wait进程的数量是由机器上的逻辑CPU的个数决定的,有几个逻辑CPU,就有几个wait进程.

  5、ps

  这个命令使用本身也比较复杂,在这里只介绍如何查看cpu占用最高的进程。使用举例如下:

 

#ps aux | head -25
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
root 17214 3.1 0.0 40 40 - A Jul 04 25578:42 wait
root 16946 3.1 0.0 40 40 - A Jul 04 25415:54 wait
root 16678 3.1 0.0 40 40 - A Jul 04 25377:03 wait
root 53274 3.1 0.0 40 40 - A Jul 04 25170:12 wait
root 286 3.1 0.0 40 40 - A Jul 04 25144:00 wait
root 8196 3.0 0.0 40 40 - A Jul 04 25082:32 wait
root 822 3.0 0.0 40 40 - A Jul 04 25072:25 wait
root 554 3.0 0.0 40 40 - A Jul 04 25034:14 wait
root 20776 2.7 0.0 40 40 - A Jul 04 22181:27 wait
root 57372 2.7 0.0 40 40 - A Jul 04 22118:00 wait
root 49176 2.7 0.0 40 40 - A Jul 04 22102:02 wait
root 21044 2.7 0.0 40 40 - A Jul 04 22077:18 wait
root 12848 2.7 0.0 40 40 - A Jul 04 22036:44 wait
root 21312 2.7 0.0 40 40 - A Jul 04 21998:53 wait
root 12580 2.7 0.0 40 40 - A Jul 04 21967:17 wait
root 13116 2.7 0.0 40 40 - A Jul 04 21865:51 wait
oracle 344612 0.3 0.0 56372 33852 - A Jul 04 2707:30 ora_j000_ora92
oracle 430408 0.3 0.0 55916 33396 - A Jul 04 2266:20 ora_j001_ora92
oracle 365092 0.2 0.0 56184 33664 - A Jul 04 1765:58 ora_j002_ora92
oracle 442430 0.2 0.0 56092 33572 - A Jul 04 1426:40 ora_j003_ora92
oracle 385606 0.1 0.0 55984 33464 - A Jul 05 1159:17 ora_j004_ora92
oracle 413856 0.1 0.0 50520 28000 - A Jul 23 543:31 oracleora92 (LOC
oracle 143668 0.1 0.0 50528 28008 - A Jul 13 833:21 oracleora92 (LOC
oracle 369230 0.1 0.0 56600 34080 - A Jul 05 806:36 ora_j005_ora92

 

  在这个输出结果中,排在前面的是16个root用户的wait进程,这其实是CPU空闲的时候运行的空闲进程,之前已作说明。

  所以CPU最高的几个进程其实是下面的ORACLE用户的ora_j00*进程,这是ORACLE的job进程。在这里,这些进程的开销很小。如果ORACLE的进程开销比较大,我们可以用如下的方法来查询具体的进程在干什么事情,例如我们要查询进程ora_j000_ora92,PID=344612,可以使用下面的方法:

 

$su – oracle
SQL>sqlplus “/as sysdba”
SQL>oradebug setospid 344612
SQL>oradebug event 10046 trace name context forever, level 8
SQL>oradebug tracefile_name –这个命令我们获得输出文件的绝对路径和文件名
SQL>oradebug event 10046 trace name context off
$tkprof /opt/oracle/app/oracle/admin/ora92/bdump/ora92_j000_344612.trc tracepid.txt
$more tracepid.txt

 

  在tracepid.txt中,我们就可以看到这个进程中具体运行的语句、过程等,以及所有的SQL的cpu消耗、物理读、逻辑读、执行计划等信息。

  另外,我们也可以执行下面的语句查看进程具体运行的SQL语句的文本:

 

SELECT /*+ ORDERED */ sql_text FROM v$sqltext a
WHERE (a.hash_value, a.address) IN (
SELECT DECODE (sql_hash_value,0, prev_hash_value,sql_hash_value),
DECODE (sql_hash_value,0, prev_sql_addr, sql_address)
FROM v$session b
WHERE b.paddr = (SELECT addr
FROM v$process c
WHERE c.spid = '&pid'))
ORDER BY piece ASC