在linux下,中間件如weblogic,tomcat會進程資源飆高,如CPU,IO,memory。你可以用top,vmstat,iostat命令分別看,也可以用一個工具搞定就是pidstat,它不僅可以監控進程的性能情況,也可以監控線程的性能情況。
安裝:CentOS/Fedora/RHEL版本的linux中則使用下面的命令 # yum install sysstat在Debian/Ubuntu系統中可以使用下面的命令來安裝 # sudo apt-get install sysstat當然,也可以從這里下載 https://www.icewalkers.com/Linux/Software/59040/Sysstat.html寫一個demo模擬CPU飆高的情況:
public class PIDTestCPU { public static class CPUTask implements Runnable{ public void run() { while(true){ double a=Math.random()*Math.random(); } } } public static class IdleTask implements Runnable{ public void run(){ try{ while(true){ Thread.sleep(2000); } }catch(Exception e){ } } } public static void main(String[] args){ new Thread(new CPUTask()).start(); new Thread(new IdleTask()).start(); new Thread(new IdleTask()).start(); new Thread(new IdleTask()).start(); }}[root@localhost ~]# java PIDTestCPU[root@localhost ~]# jps11161 PIDTestCPU11239 Jps[root@localhost ~]# pidstat -p 11161 1 3 -u -tLinux 2.6.32-431.el6.x86_64 (localhost.localdomain) 02/09/2017 _x86_64_ (1 CPU)06:11:03 PM TGID TID %usr %system %guest %CPU CPU Command06:11:04 PM 11161 - 100.00 0.00 0.00 100.00 0 java06:11:04 PM - 11161 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11162 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11163 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11164 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11165 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11166 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11167 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11168 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11169 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11170 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11171 99.00 0.00 0.00 99.00 0 |__java06:11:04 PM - 11172 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11173 0.00 0.00 0.00 0.00 0 |__java06:11:04 PM - 11174 0.00 0.00 0.00 0.00 0 |__java十進制11171轉換為十六進制2ba32017-02-09 18:11:54Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.45-b01 mixed mode):"DestroyJavaVM" PRio=10 tid=0x00007f7078006800 nid=0x2b9a waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Thread-3" prio=10 tid=0x00007f70780a7800 nid=0x2ba6 waiting on condition [0x00007f7069a64000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at PIDTestCPU$IdleTask.run(PIDTestCPU.java:13) at java.lang.Thread.run(Thread.java:662)"Thread-2" prio=10 tid=0x00007f70780a5800 nid=0x2ba5 waiting on condition [0x00007f7069b65000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at PIDTestCPU$IdleTask.run(PIDTestCPU.java:13) at java.lang.Thread.run(Thread.java:662)"Thread-1" prio=10 tid=0x00007f70780a3800 nid=0x2ba4 waiting on condition [0x00007f7069c66000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at PIDTestCPU$IdleTask.run(PIDTestCPU.java:13) at java.lang.Thread.run(Thread.java:662)"Thread-0" prio=10 tid=0x00007f70780a1800 nid=0x2ba3 runnable [0x00007f7069d67000] java.lang.Thread.State: RUNNABLE at PIDTestCPU$CPUTask.run(PIDTestCPU.java:6) at java.lang.Thread.run(Thread.java:662)"Low Memory Detector" daemon prio=10 tid=0x00007f7078088000 nid=0x2ba1 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread1" daemon prio=10 tid=0x00007f7078085800 nid=0x2ba0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread0" daemon prio=10 tid=0x00007f7078083000 nid=0x2b9f waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Signal Dispatcher" daemon prio=10 tid=0x00007f7078081000 nid=0x2b9e waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Finalizer" daemon prio=10 tid=0x00007f7078064000 nid=0x2b9d in Object.wait() [0x00007f707c22b000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000eb601300> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118) - locked <0x00000000eb601300> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:171)"Reference Handler" daemon prio=10 tid=0x00007f7078062000 nid=0x2b9c in Object.wait() [0x00007f707c32c000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000eb6011d8> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:485) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116) - locked <0x00000000eb6011d8> (a java.lang.ref.Reference$Lock)"VM Thread" prio=10 tid=0x00007f707805b800 nid=0x2b9b runnable "VM Periodic Task Thread" prio=10 tid=0x00007f7078093000 nid=0x2ba2 waiting on condition JNI global references: 888Heap def new generation total 4736K, used 446K [0x00000000eb600000, 0x00000000ebb20000, 0x00000000f08a0000) eden space 4224K, 10% used [0x00000000eb600000, 0x00000000eb66fb30, 0x00000000eba20000) from space 512K, 0% used [0x00000000eba20000, 0x00000000eba20000, 0x00000000ebaa0000) to space 512K, 0% used [0x00000000ebaa0000, 0x00000000ebaa0000, 0x00000000ebb20000) tenured generation total 10624K, used 0K [0x00000000f08a0000, 0x00000000f1300000, 0x00000000fae00000) the space 10624K, 0% used [0x00000000f08a0000, 0x00000000f08a0000, 0x00000000f08a0200, 0x00000000f1300000) compacting perm gen total 21248K, used 2590K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000) the space 21248K, 12% used [0x00000000fae00000, 0x00000000fb0879f0, 0x00000000fb087a00, 0x00000000fc2c0000)pidstat -p 11161 1 3 -u -tCPU監控:pidstat -p指定進程ID,-u表示對CPU使用率監控,參數1 3 表示每秒采集1次,共采集三次%usr: 進程在用戶態運行所占cpu時間比率%system: 進程在內核態運行所占cpu時間比率%CPU: 進程運行所占cpu時間比率CPU: 指示進程在哪個核運行pidstat -p 11161 1 3 -d -tIO監控:pidstat -p指定進程ID,-d表示對磁盤IO使用率監控,參數1 3 表示每秒采集1次,共采集三次kB_rd/s: 每秒進程從磁盤讀取的數據量(以kB為單位)kB_wr/s: 每秒進程向磁盤寫的數據量(以kB為單位)pidstat -p 11161 1 3 -r -t內存監控:pidstat -p指定進程ID,-r表示對內存監控,參數1 3 表示每秒采集1次,共采集三次minflt/s:進程每秒不需要從磁盤中調出內存頁的總數majflt/s:進程每秒需要從磁盤中調出內存頁的總數,一般在內存使用緊張時產生VSZ:進程使用虛擬內存大小,單位KBrss:占用物理內存大小,單位KB%MEM:占用內存比率Average: TGID TID minflt/s majflt/s VSZ RSS %MEM CommandAverage: 11161 - 0.00 0.00 1059588 20552 2.03 javaAverage: - 11161 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11340 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11341 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11342 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11343 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11344 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11345 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11346 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11347 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11348 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11349 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11350 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11351 0.00 0.00 1059588 20552 2.03 |__javaAverage: - 11352 0.00 0.00 1059588 20552 2.03 |__java
|
新聞熱點
疑難解答