1. top 命令找到占用cpu最高的进程

  1. top - 14:37:14 up 34 days, 13:27, 2 users, load average: 0.21, 0.29, 0.29
  2. Tasks: 151 total, 1 running, 150 sleeping, 0 stopped, 0 zombie
  3. Cpu(s): 4.4%us, 2.7%sy, 0.0%ni, 90.9%id, 0.5%wa, 0.0%hi, 0.2%si, 1.3%st
  4. Mem: 16334064k total, 16171240k used, 162824k free, 16716k buffers
  5. Swap: 16383996k total, 4470816k used, 11913180k free, 539788k cached
  6. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  7. 1818 tomcat 20 0 3643m 983m 7548 S 0.7 24.8 190:40.13 java

字段解释:
top - 时间 运行时间 用户 系统负载
Tasks: 进程相关信息
Cpu(s): cpu相关信息
Mem: 内存相关
Swap: 交换区相关信息

进程相关信息
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

2. 使用top -H -p 查看该进程内所有线程

top -H -p 1818

  1. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  2. 6656 tomcat 20 0 4193m 608m 11m R 21.0 3.8 1419:44 java

3. printf “%x\n” 将10进制线程号转换为16进制结果

  1. [liuzhihang@test08 ~]$ printf "%x\n" 1876
  2. 754
  3. [liuzhihang@test08 ~]$

4. jstack |grep

jstack 1818 | grep 754 -A 30

pid 为第一次执行top命令时的 pid
tid 为将第二次的pid进行十六进制转换后的结果

  1. "catalina-8180-89" #1842 daemon prio=5 os_prio=0 tid=0x00007f4ec4096000 nid=0x5d96 waiting on condition [0x00007f4e87545000]
  2. java.lang.Thread.State: WAITING (parking)
  3. at sun.misc.Unsafe.park(Native Method)
  4. - parking to wait for <0x00000000f418f898> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
  5. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
  6. at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
  7. at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
  8. at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)
  9. at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)
  10. at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
  11. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
  12. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  13. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  14. at java.lang.Thread.run(Thread.java:748)
  15. "catalina-8180-88" #1841 daemon prio=5 os_prio=0 tid=0x00007f4eb848e800 nid=0x5d94 waiting on condition [0x00007f4e8bd8b000]
  16. java.lang.Thread.State: WAITING (parking)
  17. at sun.misc.Unsafe.park(Native Method)