linux's docs

17.7.0: 实例-线程资源占用


1. 查看进程的pid

ps aux |grep java
root     21662  0.1  7.7 3670236 1269156 ?     Sl    2015 274:57 /opt/jdk1.7.0_67/bin/java -Dnop -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms1G -Xmx1G -XX:PermSize=128M -XX:MaxNewSize=512m -XX:MaxPermSize=256m -Djava.awt.headless=true -Djava.endorsed.dirs=/opt/apache-tomcat-7055-to8ptclient/endorsed -classpath /opt/apache-tomcat-7055-to8ptclient/bin/bootstrap.jar:/opt/apache-tomcat-7055-to8ptclient/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-7055-to8ptclient -Dcatalina.home=/opt/apache-tomcat-7055-to8ptclient -Djava.io.tmpdir=/opt/apache-tomcat-7055-to8ptclient/temp org.apache.catalina.startup.Bootstrap start
root     21963  0.0  0.0 103244   840 pts/0    S+   22:27   0:00 grep java
root     22223  0.3  7.5 4797448 1235008 ?     Sl   May06  20:04 /opt/jdk1.7.0_67/bin/java -Dnop -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms2G -Xmx2G -XX:PermSize=128M -XX:MaxNewSize=512m -XX:MaxPermSize=256m -Djava.awt.headless=true -Djava.endorsed.dirs=/opt/apache-tomcat-7055-web/endorsed -classpath /opt/apache-tomcat-7055-web/bin/bootstrap.jar:/opt/apache-tomcat-7055-web/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-7055-web -Dcatalina.home=/opt/apache-tomcat-7055-web -Djava.io.tmpdir=/opt/apache-tomcat-7055-web/temp org.apache.catalina.startup.Bootstrap start
root     22589  0.2  5.8 4021188 957512 ?      Sl   May06  10:12 /opt/jdk1.7.0_67/bin/java -Dnop -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms1G -Xmx1G -XX:PermSize=128M -XX:MaxNewSize=512m -XX:MaxPermSize=256m -Djava.awt.headless=true -Djava.endorsed.dirs=/opt/apache-tomcat-7055-bom/endorsed -classpath /opt/apache-tomcat-7055-bom/bin/bootstrap.jar:/opt/apache-tomcat-7055-bom/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-7055-bom -Dcatalina.home=/opt/apache-tomcat-7055-bom -Djava.io.tmpdir=/opt/apache-tomcat-7055-bom/temp org.apache.catalina.startup.Bootstrap start
root     47891  0.0  4.4 3669412 717848 ?      Sl   Apr21  19:06 /opt/jdk1.7.0_67/bin/java -Dnop -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms1G -Xmx1G -XX:PermSize=128M -XX:MaxNewSize=512m -XX:MaxPermSize=256m -Djava.awt.headless=true -Djava.endorsed.dirs=/opt/apache-tomcat-7055-agent/endorsed -classpath /opt/apache-tomcat-7055-agent/bin/bootstrap.jar:/opt/apache-tomcat-7055-agent/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-7055-agent -Dcatalina.home=/opt/apache-tomcat-7055-agent -Djava.io.tmpdir=/opt/apache-tomcat-7055-agent/temp org.apache.catalina.startup.Bootstrap start

2. 用ps查看具体线程使用的累计cpu时间

-T 参数增加SPID列,就是thread id列

ps -T -p 21662
  PID  SPID TTY          TIME CMD
21662 21662 ?        00:00:00 java
21662 21663 ?        00:00:01 java
21662 21664 ?        00:00:08 java
21662 21665 ?        00:00:08 java
21662 21666 ?        00:00:08 java
21662 21667 ?        00:00:08 java
21662 21668 ?        00:06:20 java
21662 21669 ?        00:00:03 java
21662 21670 ?        00:00:06 java
21662 21671 ?        00:00:00 java
21662 21672 ?        00:00:33 java
21662 21673 ?        00:00:27 java
21662 21674 ?        00:00:00 java
21662 21675 ?        01:26:12 java
21662 21677 ?        00:00:00 java
21662 21684 ?        00:01:05 java
21662 21696 ?        00:00:02 java
21662 21699 ?        00:12:57 java
21662 21700 ?        00:02:55 java
21662 21701 ?        00:04:23 java
21662 21703 ?        00:00:00 java
21662 21704 ?        00:04:23 java
21662 21712 ?        00:06:51 java
21662 48981 ?        00:00:00 java
21662 10189 ?        00:00:48 java
21662 10190 ?        00:00:47 java
21662 10191 ?        00:00:48 java
21662 10192 ?        00:00:47 java
21662 10193 ?        00:00:47 java
21662 10194 ?        00:00:47 java
21662 10195 ?        00:00:47 java
21662 10196 ?        00:00:48 java
21662 10197 ?        00:00:47 java
21662 10198 ?        00:00:48 java
21662 36693 ?        00:01:06 java
21662  8913 ?        00:01:01 java
21662 21314 ?        00:00:56 java
21662 40593 ?        00:00:04 java
21662 40842 ?        00:00:03 java
21662 25602 ?        00:00:02 java
21662 21732 ?        00:00:00 java
21662 21845 ?        00:00:00 java

3. 用top来查看具体线程的cpu、内存使用率等情况

-H 参数来打开thread

top -H -p 21662
top - 22:34:58 up 315 days,  9:00,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  42 total,   0 running,  42 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni, 99.9%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16282932k total, 13527960k used,  2754972k free,   231676k buffers
Swap:  8208380k total,    78152k used,  8130228k free,  8351040k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                                                         
21675 root      20   0 3584m 1.2g 9544 S  0.3  7.8  86:12.40 java                                                                                                                                                                                                             
21662 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.00 java                                                                                                                                                                                                             
21663 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:01.31 java                                                                                                                                                                                                             
21664 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:08.62 java                                                                                                                                                                                                             
21665 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:08.42 java                                                                                                                                                                                                             
21666 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:08.63 java                                                                                                                                                                                                             
21667 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:08.61 java                                                                                                                                                                                                             
21668 root      20   0 3584m 1.2g 9544 S  0.0  7.8   6:20.73 java                                                                                                                                                                                                             
21669 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:03.89 java                                                                                                                                                                                                             
21670 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:06.64 java                                                                                                                                                                                                             
21671 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.00 java                                                                                                                                                                                                             
21672 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:33.28 java                                                                                                                                                                                                             
21673 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:27.35 java                                                                                                                                                                                                             
21674 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.00 java                                                                                                                                                                                                             
21677 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.00 java                                                                                                                                                                                                             
21684 root      20   0 3584m 1.2g 9544 S  0.0  7.8   1:05.85 java                                                                                                                                                                                                             
21696 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:02.30 java                                                                                                                                                                                                             
21699 root      20   0 3584m 1.2g 9544 S  0.0  7.8  12:57.35 java                                                                                                                                                                                                             
21700 root      20   0 3584m 1.2g 9544 S  0.0  7.8   2:55.99 java                                                                                                                                                                                                             
21701 root      20   0 3584m 1.2g 9544 S  0.0  7.8   4:23.48 java                                                                                                                                                                                                             
21703 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.00 java                                                                                                                                                                                                             
21704 root      20   0 3584m 1.2g 9544 S  0.0  7.8   4:23.17 java                                                                                                                                                                                                             
21712 root      20   0 3584m 1.2g 9544 S  0.0  7.8   6:51.52 java                                                                                                                                                                                                             
48981 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.20 java                                                                                                                                                                                                             
10189 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:48.05 java                                                                                                                                                                                                             
10190 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:47.88 java                                                                                                                                                                                                             
10191 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:48.11 java                                                                                                                                                                                                             
10192 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:47.82 java                                                                                                                                                                                                             
10193 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:47.75 java                                                                                                                                                                                                             
10194 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:47.69 java                                                                                                                                                                                                             
10195 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:47.72 java                                                                                                                                                                                                             
10196 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:48.26 java                                                                                                                                                                                                             
10197 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:47.58 java                                                                                                                                                                                                             
10198 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:48.22 java                                                                                                                                                                                                             
36693 root      20   0 3584m 1.2g 9544 S  0.0  7.8   1:06.29 java                                                                                                                                                                                                             
 8913 root      20   0 3584m 1.2g 9544 S  0.0  7.8   1:01.80 java                                                                                                                                                                                                             
21314 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:56.88 java                                                                                                                                                                                                             
40593 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:04.16 java                                                                                                                                                                                                             
40842 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:03.39 java                                                                                                                                                                                                             
25602 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:02.41 java                                                                                                                                                                                                             
21732 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.17 java                                                                                                                                                                                                             
21845 root      20   0 3584m 1.2g 9544 S  0.0  7.8   0:00.05 java

Contents