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 <html>
|
|
71 <head>
|
|
72 <title>Profiler</title>
|
|
73 <style type="text/css">
|
|
74 body {
|
|
75 font-family:Verdana, Arial, Serif;
|
|
76 font-size:.74em;
|
|
77 }
|
|
78 .gray {
|
|
79 background-color: #eeeeee;
|
|
80 padding: .5em;
|
|
81 margin: .5em;
|
|
82 font-size:100%;
|
|
83 }
|
|
84 </style>
|
|
85 </head>
|
|
86 <body>
|
|
87 <div>
|
|
88 <a href="/tools/">Tools</a>
|
|
89 </div>
|
|
90 <%
|
|
91 String file = request.getParameter("file");
|
|
92 if( file != null ) {
|
|
93 %>
|
|
94 <div style="margin:1em .5em .5em;padding:.3em">
|
|
95 <b>Snapshot File</b>: <%=file%>
|
|
96 </div>
|
|
97 <%
|
|
98 }
|
|
99 %>
|
|
100 <table class="gray">
|
|
101 <tr>
|
|
102 <td class="row-label">Free Memory</td>
|
|
103 <td>
|
|
104 <%=String.format("%.2f",free)%> Mb
|
|
105 </td>
|
|
106 </tr>
|
|
107 <tr>
|
|
108 <td class="row-label">Used Memory</td>
|
|
109 <td><%=String.format("%.2f",used)%> Mb</td>
|
|
110 </tr>
|
|
111 <tr>
|
|
112 <td class="row-label">Total Memory</td>
|
|
113 <td><%=String.format("%.2f",total)%> Mb</td>
|
|
114 </tr>
|
|
115 </table>
|
|
116 <input type="button" onclick="location='/tools/Profile.jtp?action=gc'" value="Run GC"/>
|
|
117 <% if (profiler == null) { %>
|
|
118 <div style="margin:1em 0">
|
|
119 Yourkit profiler is disabled on this server.
|
|
120 </div>
|
|
121 <% } else { %>
|
|
122 <div style="margin:1em 0">
|
|
123 <input type="button" onclick="location='/tools/Profile.jtp?action=memory'" value="Capture Memory Snapshot"/>
|
|
124 </div>
|
|
125 <div style="margin:1em 0">
|
|
126 <form>
|
|
127 <input type="hidden" name="action" value="cpu">
|
|
128 <input type="text" name="seconds" value="5" size="2"> seconds
|
|
129 <input type="submit" value="CPU Profiling">
|
|
130 </form>
|
|
131 </div>
|
|
132 <% } %>
|
|
133 </body>
|
|
134 </html>
|
|
135 <%
|
|
136 }
|
|
137
|
|
138 }
|
|
139 %> |