renjianwolai 2012-03-06
JSP 页面查找 CPU 100% 堆栈
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.io.*, java.util.*, java.net.*, java.lang.management.*" %> <html> <head> <title>DeProfile</title> <style type="text/css"> body{font-size:14px;font-family:Arial;} .stack{font-size:12px; font-family:Arial; margin:10px} </style> </head> <body> <% final long MAX_TIME=1000; final long THEAD_ID=1; final boolean IS_OUT_DETAIL=false; ThreadMXBean tm = ManagementFactory.getThreadMXBean(); tm.setThreadContentionMonitoringEnabled(true); long [] tid = tm.getAllThreadIds(); ThreadInfo [] tia = tm.getThreadInfo(tid, Integer.MAX_VALUE); for (int i = tia.length-1; i > 0; i--) { long threadId = tia[i].getThreadId(); long cpuTime = tm.getThreadCpuTime(tia[i].getThreadId())/(1000*1000*1000); out.println("<div>ThreadId:"+threadId+" CPU Time:"+cpuTime+"</div>"); if(cpuTime>=MAX_TIME || threadId ==THEAD_ID){ out.println("<div>"+"==================================================================="+"</div>"); out.println("<div class=stack>"); out.println("<div>Stack Info - Thread ID: "+threadId+"</div>"); StackTraceElement[] stackElements = tia[i].getStackTrace(); if (stackElements != null) { for (int s = 0; s < stackElements.length; s++) { out.println("<div>"+"" + stackElements[s]+"</div>"); if(IS_OUT_DETAIL){ out.println("<div>"+"Class:" + stackElements[s].getClassName()+"</div>"); out.println("<div>"+"File:" + stackElements[s].getFileName()+"</div>"); out.println("<div>"+"Line:" + stackElements[s].getLineNumber()+"</div>"); out.println("<div>"+"Method:" + stackElements[s].getMethodName()+"</div>"); out.println("<div>"+"-----------------------------------"+"</div>"); } } } out.println("</div>"); } } %> </body> </html>