本文介绍通过ps和top查看进程的cpu、内存等使用情况。
1.ps命令
1.1 概览
ps命令相关参数定义:
- -e 或者-A,选择所有的进程;
- -L 显示线程;
- -o 自定义输出格式;
输出格式:
C cpu使用百分比,为”使用的CPU时间/进程启动到现在的时间”
STIME 启动时间,小于24小时,格式为”HH:MM”,否则为“mmm dd”为 month day
TIME 累计使用CPU时间(user+system)
LWP 线程ID;
NLWP 当前进程中的线程数;
$ ps -ef |grep orange
UID PID PPID C STIME TTY TIME CMD
root 18666 1 0 Sep13 ? 00:00:00 /usr/local/orange_service/orange /usr/local/orange_service/orange_log.conf
root 18667 18666 13 Sep13 ? 7-12:15:51 /usr/local/lorange_service/orange /usr/local/orange_service/orange_log.conf
1.2 查看线程
$ ps -eLf | grep orange
UID PID PPID LWP C NLWP STIME TTY TIME CMD
root 18666 1 18666 0 1 Sep13 ? 00:00:00 /usr/local /orange /usr/local/orange_log.conf
root 18667 18666 18667 0 6 Sep13 ? 00:00:01 /usr/local /orange /usr/local/orange_log.conf
root 18667 18666 18668 0 6 Sep13 ? 00:00:00 /usr/local /orange /usr/local/orange_log.conf
root 18667 18666 18669 12 6 Sep13 ? 7-01:27:27 /usr/local /orange /usr/local/orange_log.conf
root 18667 18666 18671 0 6 Sep13 ? 00:00:02 /usr/local /orange /usr/local/orange_log.conf
root 18667 18666 18672 0 6 Sep13 ? 10:45:49 /usr/local /orange /usr/local/orange_log.conf
root 18667 18666 18673 0 6 Sep13 ? 00:02:30 /usr/local /orange /usr/local/orange_log.conf
1.3 自定义参数的使用
-p 指定进程ID
-o 自定义输出格式;
$ ps -p 1813 –o pid,ppid,lstart,etime,args
PID PPID STARTED ELAPSED COMMAND
1813 1 Mon Aug 29 15:01:33 2016 71-02:34:45 /usr//sbin/zabbix_agentd
lstart:详细的启动时间;
etime: 从进程启动到现在消逝的时间;
time: 累计消耗的cpu时间;
tid或者lwp:线程ID;
nlwp: 当前进程中的线程数;
%cpu: 该进程使用的CPU百分比;含义参考ps输出格式中的”C”字段; CPU定义:进程占用CPU时间除以进程启动以来执行的时间;这个值不可能达到100%。与top命令的CPU不一样。
%mem: 当前进程的常驻内存(resident set size)占机器物理内存的百分比;即top命令显示的%MEM字段;
rss: 当前进程常驻内存大小(resident set size),单位kiloBytes;
vsize: 或者vsz,当前进程占用的VM虚拟内存大小,单位kB;
注意自定义参数之前使用逗号分隔,没有空格;
2.top命令
再来看一下top命令:
$ top
Tasks: 248 total, 1 running, 247 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16079044k total, 2654840k used, 13424204k free, 207208k buffers
Swap: 8110076k total, 0k used, 8110076k free, 2021428k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1677 mysql 20 0 2298m 59m 7376 S 0.3 0.4 82:05.45 mysqld
VIRT:当前进程使用的VM虚拟内存大小;
%CPU: 当前进程占用的CPU百分比;
%MEM: 当前进程占用的物理内存百分比;
TIME+: 累计占用CPU时间,单位分钟,82分5.45秒;
2.1查看线程
查看整个进程
$ top | grep orange
10151 root 20 0 1644m 112m 1268 S 77.5 0.7 69:35.22 orange
10151 root 20 0 1644m 114m 1268 S 49.2 0.7 69:36.70 orange
10151 root 20 0 1644m 115m 1268 S 69.8 0.7 69:38.80 orange
10151 root 20 0 1644m 123m 1268 S 63.5 0.8 69:40.71 orange
查看各个线程
$ top -H | grep orange
10158 root 20 0 1644m 111m 1268 S 37.6 0.7 13:51.40 orange
10169 root 20 0 1644m 111m 1268 S 31.7 0.7 9:23.18 orange
10160 root 20 0 1644m 111m 1268 S 23.8 0.7 9:14.69 orange
10173 root 20 0 1644m 111m 1268 S 2.0 0.7 6:39.96 orange
10156 root 20 0 1644m 124m 1268 S 19.3 0.8 13:59.24 orange
10160 root 20 0 1644m 124m 1268 S 15.0 0.8 9:15.14 orange
10169 root 20 0 1644m 124m 1268 S 14.0 0.8 9:23.60 orange
10158 root 20 0 1644m 124m 1268 S 12.6 0.8 13:51.78 orange
10173 root 20 0 1644m 124m 1268 S 6.7 0.8 6:40.16 orange
查看某个进程的线程
$ top –H –p 20675
top - 21:49:06 up 79 days, 6:47, 7 users, load average: 0.35, 0.39, 0.19
Tasks: 23 total, 0 running, 23 sleeping, 0 stopped, 0 zombie
Cpu(s): 5.5%us, 0.2%sy, 0.0%ni, 94.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16079044k total, 15925868k used, 153176k free, 120784k buffers
Swap: 8110076k total, 0k used, 8110076k free, 15093952k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
20693 root 20 0 1580m 84m 1288 S 9.3 0.5 0:07.96 cl_promotionTra
20692 root 20 0 1580m 84m 1288 S 7.0 0.5 0:09.03 cl_app_catalina
20686 root 20 0 1580m 84m 1288 S 5.7 0.5 0:15.61 cl_cartAction.l
20684 root 20 0 1580m 84m 1288 S 5.3 0.5 0:08.79 cl_shoppingActi
20681 root 20 0 1580m 84m 1288 S 5.0 0.5 0:16.67 cl_cartAction1.
20697 root 20 0 1580m 84m 1288 S 5.0 0.5 0:19.74 curl thread
20688 root 20 0 1580m 84m 1288 S 4.3 0.5 0:08.15 cl_app_catalina
20694 root 20 0 1580m 84m 1288 S 2.7 0.5 0:08.88 cl_central1.log
20698 root 20 0 1580m 84m 1288 S 0.3 0.5 0:00.01 timeDataFunc
20675 root 20 0 1580m 84m 1288 S 0.0 0.5 0:00.00 orange
20677 root 20 0 1580m 84m 1288 S 0.0 0.5 0:00.00 FileCheck
20678 root 20 0 1580m 84m 1288 S 0.0 0.5 0:00.00 cl_promotionErr
20679 root 20 0 1580m 84m 1288 S 0.0 0.5 0:05.73 cl_shoppingBigL
20680 root 20 0 1580m 84m 1288 S 0.0 0.5 0:21.06 cl_pis-taskmgr2
20682 root 20 0 1580m 84m 1288 S 0.0 0.5 0:19.70 cl_pis-taskmgr1
20683 root 20 0 1580m 84m 1288 S 0.0 0.5 0:13.52 cl_checkoutMobi
20685 root 20 0 1580m 84m 1288 S 0.0 0.5 0:05.04 cl_shoppingBigL
20687 root 20 0 1580m 84m 1288 S 0.0 0.5 0:18.48 cl_checkoutLogg
20689 root 20 0 1580m 84m 1288 S 0.0 0.5 0:07.90 cl_central.log
20690 root 20 0 1580m 84m 1288 S 0.0 0.5 0:05.47 cl_shoppingBigL
20691 root 20 0 1580m 84m 1288 S 0.0 0.5 0:18.05 cl_shoppingBigL
20695 root 20 0 1580m 84m 1288 S 0.0 0.5 0:08.30 cl_pis-taskmgr.
20696 root 20 0 1580m 84m 1288 S 0.0 0.5 0:00.25 cl_agent.log