目录
背景
top命令是Linux下常用的性能分析工具,特别是应用性能报警的时候,我们经常会用到这个命令
参考资料
https://linuxaria.com/howto/understanding-the-top-command-on-linux
http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/top.html?highlight=top
先看懂top的输出吧
我们先运行一下top命令
top - 13:26:40 up 188 days, 20:28, 1 user, load average: 0.07, 0.05, 0.05
Tasks: 145 total, 1 running, 144 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.7%us, 0.3%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 8059416k total, 7374232k used, 685184k free, 288420k buffers
Swap: 2096440k total, 0k used, 2096440k free, 5570192k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8182 sankuai 20 0 981m 40m 3420 S 3.0 0.5 180:24.71 sg_agent
17939 sankuai 20 0 3612m 996m 17m S 1.7 12.7 639:16.22 java
8193 sankuai 20 0 1045m 29m 3152 S 1.3 0.4 84:28.97 sg_agent_worker
10107 sankuai 20 0 268m 6724 3616 S 0.3 0.1 7:05.65 log_agent_file
1 root 20 0 23500 1552 1212 S 0.0 0.0 0:05.93 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:54.01 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 3:28.67 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
- 第一行内容-top
- 13:26:40 系统当前时间
- 188 days, 20:28, 系统开机到现在经过了多少时间
- 1 user, 当前1个用户在线
- load average: 0.07, 0.05, 0.05 系统1分钟、5分钟、15分钟的CPU负载信息
- 第二行内容-task
- Tasks 任务,右边的数字也就是一个对任务的统计信息
- 145 total, 任务总数
- 1 running, 运行的任务总数
- 144 sleeping, 休眠的任务总数
- 0 stopped, 停止的任务总数
- 0 zombie, 等待父进程来停止的任务总数
- 第三行内容-cpu
- 0.7%us (user processes)用户进程的CPU百分比,不包含renice值为负的任务。
- 0.3%sy (system processes)系统进程的CPU百分比。
- 0.0%ni (processes with priority upgrade nice)改变过优先级的进程的CPU百分比。
- 99.0%id 没被利用的CPU百分比。
- 0.0%wa (processes waiting for I/O operations)等待I/O的CPU百分比。
- 0.0%hi (hardware interrupts)硬中断的CPU百分比。
- 0.0%si (software interrupts)软终端的CPU百分比。
- 0.0%st (Steal Time)通常我们服务器是一个虚拟机,这个指标就代表宿主机上其他虚拟机实例偷取的CPU时间
- 第四行内容-内存利用
- 8059416k total, 物理内存总量
- 7374232k used, 使用的物理内存量
- 685184k free, 空闲的物理内存量
- 288420k buffers, 用作内核缓存的物理内存量
- 第五行内容-交换空间
- 2096440k total, 交换区总量
- 0k used, 使用的交换区量
- 2096440k free, 空闲的交换区量
- 5570192k cached, 缓冲交换区总量
- 大于第六行内容-进程列表
- PID:进程的ID
- USER:进程所有者
- PR:进程的优先级别,越小越优先被执行
- NI:The “NICE” value of the process,关于nice value of process的解释
- VIRT:进程占用的虚拟内存
- RES:进程占用的物理内存
- SHR:进程使用的共享内存
- S:进程的状态。S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值为负数
- %CPU:进程占用CPU的使用率
- %MEM:进程使用的物理内存和总内存的百分比
- TIME+:该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值。
- COMMAND:进程启动命令名称
top常用的一些命令
- 命令行命令
- a 根据内存利用率排序
- b 在“批量模式”启动top,这种模式很利于把输出top的结果,这种模式下,top不再接受你输入的界面命令,同时它会运行至 -n 命令设置的时间或者手动kill掉
- H 线程开关(这个经常用于排查故障,找出有问题的线程pid),使用这个命令会把进程私有的线程也都输出出来。
- n 用法 -n (number),top每隔一段时间就会输出一次,这个-n就是指定top输出的次数
- p 指定pid输出对应的进程
- 界面命令 (输入top之后可以输入的命令)
- q 退出
- space 立即刷新
- s 设置刷新时间
- c COMMAND 显示完整的命令 等效于 top -c
- k kill 进程
- u 指定用户
- t 显示或隐藏进程和CPU状态信息
- m 显示或隐藏内存状态信息
- l 显示或隐藏uptime信息
- 1 监控每个逻辑CPU的状况
- P 按%CPU使用率排行
- T 按MITE+排行
- M 按%MEM排行
- 其他命令请使用 man top
nice value of process
http://askubuntu.com/questions/399357/what-does-the-nice-value-mean-in-cpu-utilization-statistics
The nice value for the processor is
the time the CPU has spent running users’ processes that have been “niced”.
(from man top) A “niced” process is one with a positive nice value. So if the processor’s nice value is high, that means it is working with some low priority processes. So this indicator is useful when you see high CPU utilization and you are afraid that this high load will have bad effect on your system:
- High CPU utilization with high nice value: Nothing to worry, not so important tasks doing their job, important processes will easily get CPU time if they need. This situation is not a real bottleneck.
- High CPU utilization with low nice value: Something to worry because the CPU is stressed with important processes so these or new processes will have to wait. This situation is a real bottleneck. I think this nice value is not too important for a simple user, this is more useful in a server environment.
结论:nice value值越高的时候,被cpu执行的机会越少,如果进程占用cpu比较高,同时nice value也比较高的话,你就需要担心了。说明此时cpu比较紧张