0
|
1
|
|
2 package nabble.view.web.tools;
|
|
3
|
|
4 import com.yourkit.api.Controller;
|
|
5 import com.yourkit.api.ProfilingModes;
|
|
6 import fschmidt.util.java.HtmlUtils;
|
|
7 import org.slf4j.Logger;
|
|
8 import org.slf4j.LoggerFactory;
|
|
9
|
|
10 import javax.servlet.ServletException;
|
|
11 import javax.servlet.http.HttpServlet;
|
|
12 import javax.servlet.http.HttpServletRequest;
|
|
13 import javax.servlet.http.HttpServletResponse;
|
|
14 import java.io.IOException;
|
|
15 import java.io.PrintWriter;
|
|
16
|
|
17
|
|
18 public final class Profile extends HttpServlet {
|
|
19
|
|
20 private static final Logger logger = LoggerFactory.getLogger(Profile.class);
|
|
21
|
|
22 private static Controller profiler;
|
|
23 static {
|
|
24 try {
|
|
25 profiler = new com.yourkit.api.Controller();
|
|
26 } catch (Exception e) {
|
|
27 logger.info("Profiler error: " + e);
|
|
28 }
|
|
29 }
|
|
30
|
|
31 protected void service(HttpServletRequest request,HttpServletResponse response)
|
|
32 throws ServletException, IOException
|
|
33 {
|
|
34 PrintWriter out = response.getWriter();
|
|
35
|
|
36 float oneMega = 1024f * 1024f;
|
|
37 float free = Runtime.getRuntime().freeMemory() / oneMega;
|
|
38 float total = Runtime.getRuntime().totalMemory() / oneMega;
|
|
39 float used = total - free;
|
|
40
|
|
41 String action = request.getParameter("action");
|
|
42 if ("gc".equals(action)) {
|
|
43 System.gc();
|
|
44 response.sendRedirect("/tools/Profile.jtp");
|
|
45 return;
|
|
46 } else if ("memory".equals(action)) {
|
|
47 String file;
|
|
48 try {
|
|
49 file = profiler.captureSnapshot(ProfilingModes.SNAPSHOT_WITH_HEAP);
|
|
50 } catch(Exception e) {
|
|
51 throw new RuntimeException(e);
|
|
52 }
|
|
53 response.sendRedirect("/tools/Profile.jtp?file="+HtmlUtils.urlEncode(file));
|
|
54 return;
|
|
55 } else if ("cpu".equals(action)) {
|
|
56 long seconds = Long.valueOf( request.getParameter("seconds") );
|
|
57 String file;
|
|
58 try {
|
|
59 profiler.startCPUProfiling(ProfilingModes.CPU_SAMPLING, null);
|
|
60 Thread.sleep(seconds*1000L);
|
|
61 profiler.stopCPUProfiling();
|
|
62 file = profiler.captureSnapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP);
|
|
63 } catch(Exception e) {
|
|
64 throw new RuntimeException(e);
|
|
65 }
|
|
66 response.sendRedirect("/tools/Profile.jtp?file="+HtmlUtils.urlEncode(file));
|
|
67 return;
|
|
68 }
|
|
69
|
|
70 out.print( "\n<html>\n <head>\n <title>Profiler</title>\n <style type=\"text/css\">\n body {\n font-family:Verdana, Arial, Serif;\n font-size:.74em;\n }\n .gray {\n background-color: #eeeeee;\n padding: .5em;\n margin: .5em;\n font-size:100%;\n }\n </style>\n </head>\n <body>\n <div>\n <a href=\"/tools/\">Tools</a>\n </div>\n " );
|
|
71
|
|
72 String file = request.getParameter("file");
|
|
73 if( file != null ) {
|
|
74
|
|
75 out.print( "\n<div style=\"margin:1em .5em .5em;padding:.3em\">\n <b>Snapshot File</b>: " );
|
|
76 out.print( (file) );
|
|
77 out.print( "\n</div>\n" );
|
|
78
|
|
79 }
|
|
80
|
|
81 out.print( "\n<table class=\"gray\">\n <tr>\n <td class=\"row-label\">Free Memory</td>\n <td>\n " );
|
|
82 out.print( (String.format("%.2f",free)) );
|
|
83 out.print( " Mb\n </td>\n </tr>\n <tr>\n <td class=\"row-label\">Used Memory</td>\n <td>" );
|
|
84 out.print( (String.format("%.2f",used)) );
|
|
85 out.print( " Mb</td>\n </tr>\n <tr>\n <td class=\"row-label\">Total Memory</td>\n <td>" );
|
|
86 out.print( (String.format("%.2f",total)) );
|
|
87 out.print( " Mb</td>\n </tr>\n</table>\n<input type=\"button\" onclick=\"location='/tools/Profile.jtp?action=gc'\" value=\"Run GC\"/>\n" );
|
|
88 if (profiler == null) {
|
|
89 out.print( "\n <div style=\"margin:1em 0\">\n Yourkit profiler is disabled on this server.\n </div>\n" );
|
|
90 } else {
|
|
91 out.print( "\n <div style=\"margin:1em 0\">\n <input type=\"button\" onclick=\"location='/tools/Profile.jtp?action=memory'\" value=\"Capture Memory Snapshot\"/>\n </div>\n <div style=\"margin:1em 0\">\n <form>\n <input type=\"hidden\" name=\"action\" value=\"cpu\">\n <input type=\"text\" name=\"seconds\" value=\"5\" size=\"2\"> seconds\n <input type=\"submit\" value=\"CPU Profiling\">\n </form>\n </div>\n" );
|
|
92 }
|
|
93 out.print( "\n</body>\n</html>\n" );
|
|
94
|
|
95 }
|
|
96
|
|
97 }
|
|
98
|