top的用法

top

The top command is probably the most useful one for an Oracle DBA managing a database on Linux. Say the system is slow and you want to find out who is gobbling up all the CPU and/or memory. To display the top processes, you use the command top.

Note that unlike other commands, top does not produce an output and sits still. It refreshes the screen to display new information. So, if you just issue top and leave the screen up, the most current information is always up. To stop and exit to shell, you can press Control-C.

$ top

18:46:13  up 11 days, 21:50,  5 users,  load average: 0.11, 0.19, 0.18 
151 processes: 147 sleeping, 4 running, 0 zombie, 0 stopped 
CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle 
           total   12.5%    0.0%    6.7%   0.0%     0.0%    5.3%   75.2% 
Mem:  1026912k av,  999548k used,   27364k free,       0k shrd,  116104k buff 
                    758312k actv,  145904k in_d,   16192k in_c 
Swap: 2041192k av,  122224k used, 1918968k free                  590140k cached 
 
  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME CPU COMMAND 
  451 oracle    15   0  6044 4928  4216 S     0.1  0.4   0:20   0 tnslsnr 
 8991 oracle    15   0  1248 1248   896 R     0.1  0.1   0:00   0 top 
    1 root      19   0   440  400   372 S     0.0  0.0   0:04   0 init 
    2 root      15   0     0    0     0 SW    0.0  0.0   0:00   0 keventd 
    3 root      15   0     0    0     0 SW    0.0  0.0   0:00   0 kapmd 
    4 root      34  19     0    0     0 SWN   0.0  0.0   0:00   0 ksoftirqd/0 
    7 root      15   0     0    0     0 SW    0.0  0.0   0:01   0 bdflush 
    5 root      15   0     0    0     0 SW    0.0  0.0   0:33   0 kswapd 
    6 root      15   0     0    0     0 SW    0.0  0.0   0:14   0 kscand 
    8 root      15   0     0    0     0 SW    0.0  0.0   0:00   0 kupdated 
    9 root      25   0     0    0     0 SW    0.0  0.0   0:00   0 mdrecoveryd
... output snipped ...

Let's examine the different types of information produced. The first line:

18:46:13  up 11 days, 21:50,  5 users,  load average: 0.11, 0.19, 0.18

shows the current time (18:46:13), that system has been up for 11 days; that the system has been working for 21 hours 50 seconds. The load average of the system is shown (0.11, 0.19, 0.18) for the last 1, 5 and 15 minutes respectively. (By the way, you can also get this information by issuing the uptime command.)

If the load average is not required, press the letter "l" (lowercase L); it will turn it off. To turn it back on press l again. The second line:

151 processes: 147 sleeping, 4 running, 0 zombie, 0 stopped

shows the number of processes, running, sleeping, etc. The third and fourth lines:

CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle 
           total   12.5%    0.0%    6.7%   0.0%     0.0%    5.3%   75.2%

show the CPU utilization details. The above line shows that user processes consume 12.5% and system consumes 6.7%. The user processes include the Oracle processes. Press "t" to turn these three lines off and on. If there are more than one CPU, you will see one line per CPU.

The next two lines:

Mem:  1026912k av, 1000688k used,  26224k free,    0k shrd,  113624k buff 
                    758668k actv,  146872k in_d,  14460k in_c
Swap: 2041192k av, 122476k used,   1918716k free             591776k cached

show the memory available and utilized. Total memory is "1026912k av", approximately 1GB, of which only 26224k or 26MB is free. The swap space is 2GB; but it's almost not used. To turn it off and on, press "m".

The rest of the display shows the processes in a tabular format. Here is the explanation of the columns:

Column

Description

PIDThe process ID of the process
USER

The user running the process
PRI

The priority of the process
NIThe nice value: The higher the value, the lower the priority of the task
SIZE

Memory used by this process (code+data+stack)
RSS

The physical memory used by this process
SHARE

The shared memory used by this process
STAT

The status of this process, shown in code. Some major status codes are:
R – Running
S –Sleeping
Z – Zombie
T – Stopped

You can also see second and third characters, which indicate:
W – Swapped out process
N – positive nice value
%CPU

The percentage of CPU used by this process
%MEM

The percentage of memory used by this process
TIME

The total CPU time used by this process
CPU

If this is a multi-processor system, this column indicates the ID of the CPU this process is running on.
COMMANDThe command issued by this process

While the top is being displayed, you can press a few keys to format the display as you like. Pressing the uppercase M key sorts the output by memory usage. (Note that using lowercase m will turn the memory summary lines on or off at the top of the display.) This is very useful when you want to find out who is consuming the memory. Here is sample output:

PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME CPU COMMAND 
31903 oracle    15   0 75760  72M 72508 S     0.0  7.2   0:01   0 ora_smon_PRODB2 
31909 oracle    15   0 68944  66M 64572 S     0.0  6.6   0:03   0 ora_mmon_PRODB2 
31897 oracle    15   0 53788  49M 48652 S     0.0  4.9   0:00   0 ora_dbw0_PRODB2

Now that you learned how to interpret the output, let's see how to use command line parameters.

The most useful is -d, which indicates the delay between the screen refreshes. To refresh every second, use top -d 1.

The other useful option is -p. If you want to monitor only a few processes, not all, you can specify only those after the -p option. To monitor processes 13609, 13608 and 13554, issue:

top -p 13609 -p 13608 -p 13554

This will show results in the same format as the top command, but only those specific processes.

Tip for Oracle Users

It's probably needless to say that the top utility comes in very handy for analyzing the performance of database servers. Here is a partial top output.

20:51:14  up 11 days, 23:55,  4 users,  load average: 0.88, 0.39, 0.27 
113 processes: 110 sleeping, 2 running, 1 zombie, 0 stopped 
CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle 
           total    1.0%    0.0%    5.6%   2.2%     0.0%   91.2%    0.0% 
Mem:  1026912k av, 1008832k used,   18080k free,       0k shrd,   30064k buff 
                    771512k actv,  141348k in_d,   13308k in_c 
Swap: 2041192k av,   66776k used, 1974416k free                  812652k cached 
 
  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME CPU COMMAND 
16143 oracle    15   0 39280  32M 26608 D     4.0  3.2   0:02   0 oraclePRODB2...
    5 root      15   0     0    0     0 SW    1.6  0.0   0:33   0 kswapd
... output snipped ...

Let's analyze the output carefully. The first thing you should notice is the "idle" column under CPU states; it's 0.0%—meaning, the CPU is completely occupied doing something. The question is, doing what? Move your attention to the column "system", just slightly left; it shows 5.6%. So the system itself is not doing much. Go even more left to the column marked "user", which shows 1.0%. Since user processes include Oracle as well, Oracle is not consuming the CPU cycles. So, what's eating up all the CPU?

The answer lies in the same line, just to the right under the column "iowait", which indicates 91.2%. This explains it all: the CPU is waiting for IO 91.2% of the time.

So why so much IO wait? The answer lies in the display. Note the PID of the highest consuming process: 16143. You can use the following query to determine what the process is doing:

select s.sid, s.username, s.program
from v$session s, v$process p
where spid = 16143
and p.addr = s.paddr
/

       SID USERNAME PROGRAM
------------------- -----------------------------
       159 SYS      rman@prolin2 (TNS V1-V3)	

The rman process is taking up the IO waits related CPU cycles. This information helps you determine the next course of action.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/37724/viewspace-152569/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/37724/viewspace-152569/

`top` 命令是 Linux 系统中一个非常常用的性能监控工具,能够实时显示系统中各个进程的资源占用情况(如 CPU、内存等)。它类似于 Windows 系统中的任务管理器,适合用来快速查看系统整体运行状态和资源使用情况。 ### 基本用法 启动 `top` 命令非常简单,只需在终端输入: ```bash top ``` 运行后,会进入交互式界面,显示系统中各个进程的动态信息。 ### 主要信息区域 1. **系统概况信息**: - 第一行显示系统当前时间、运行时长、登录用户数以及系统负载(load average)。 - 第二行显示任务总数、运行状态的任务数、睡眠状态的任务数、停止的任务数以及僵尸进程数。 - 第三行显示 CPU 使用情况,包括用户态(us)、系统态(sy)、空闲(id)、I/O 等待(wa)等指标。 - 第四行和第五行分别显示物理内存和交换分区(swap)的使用情况。 2. **进程列表**: - 默认按 CPU 使用率排序,显示进程的 PID、用户、优先级、内存占用、CPU 占用、启动时间、命令等信息。 ### 常用操作 - **排序**: - 按 CPU 使用率排序:按下 `P`。 - 按内存使用率排序:按下 `M`。 - 按运行时间排序:按下 `T`。 - **切换多核视图**: - 在 `top` 界面中按下 `1`,可以查看每个 CPU 核心的详细使用情况,适合多核系统分析负载是否均衡。 - **退出**: - 按下 `q` 键即可退出 `top` 界面。 - **其他功能**: - 杀死进程:按下 `k` 键,输入进程 PID 即可终止进程。 - 更改刷新间隔:按下 `s` 键,输入新的刷新间隔时间(单位为秒)。 ### 高级功能 - **批处理模式**: - 使用 `-b` 参数可以让 `top` 进入批处理模式,适用于将输出保存到文件或日志中: ```bash top -b ``` - **指定刷新次数**: - 使用 `-n` 参数指定刷新次数后自动退出: ```bash top -n 3 ``` - **结合 `htop` 增强可视化**: - `htop` 是 `top` 的增强版本,提供更友好的交互界面和颜色高亮,支持鼠标操作和垂直/水平滚动。可以通过以下命令安装并运行: ```bash sudo apt install htop # Debian/Ubuntu htop ``` ### 性能瓶颈分析 通过 `top` 命令可以初步判断系统是否存在性能瓶颈: - **CPU 瓶颈**: - 如果 `us`(用户态 CPU 使用率)持续偏高,说明应用程序负载高,可能需要优化代码或增加资源。 - 如果 `sy`(系统态 CPU 使用率)持续偏高,说明内核操作频繁,可能存在系统调用过多或驱动问题。 - 如果 `wa`(I/O 等待时间)持续偏高,说明磁盘或网络性能存在问题。 - 如果 `st`(虚拟机偷取时间)持续偏高,说明宿主机资源不足,可能需要迁移或扩容虚拟机。 - **内存瓶颈**: - 如果 `free`(空闲内存)持续偏低,同时 `swap` 使用率上升,说明系统可能面临内存不足问题。 - **进程问题**: - 如果发现某个进程的 CPU 或内存使用率异常高,可以结合 `ps` 或 `pstack` 命令进一步分析具体进程。 ### 示例输出 ```bash top - 14:30:00 up 2 days, 3:15, 1 user, load average: 0.15, 0.10, 0.05 Tasks: 150 total, 1 running, 149 sleeping, 0 stopped, 0 zombie %Cpu(s): 2.3 us, 1.2 sy, 0.0 ni, 96.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 7983.1 total, 234.5 free, 5120.3 used, 2628.3 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2500.0 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1234 root 20 0 527872 48532 9876 S 5.6 0.6 1:23.45 /usr/bin/python3 ``` ### 总结 `top` 命令是一个非常实用的实时监控工具,能够帮助用户快速了解系统的运行状态和资源使用情况。通过掌握其基本用法和常用操作,可以有效辅助系统性能调优和故障排查。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值