1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.management.mbean; |
12 | |
|
13 | |
import org.apache.commons.logging.Log; |
14 | |
import org.apache.commons.logging.LogFactory; |
15 | |
import org.mule.module.management.i18n.ManagementMessages; |
16 | |
|
17 | |
import com.yourkit.api.Controller; |
18 | |
|
19 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
20 | |
|
21 | 0 | public class YourKitProfilerService implements YourKitProfilerServiceMBean |
22 | |
{ |
23 | |
|
24 | |
|
25 | |
|
26 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
27 | |
|
28 | |
private final Controller controller; |
29 | 0 | private final AtomicBoolean capturing = new AtomicBoolean(false); |
30 | |
|
31 | |
public YourKitProfilerService() throws Exception |
32 | 0 | { |
33 | 0 | controller = new Controller(); |
34 | 0 | } |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public String getHost() |
40 | |
{ |
41 | 0 | return controller.getHost(); |
42 | |
} |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public int getPort() |
48 | |
{ |
49 | 0 | return controller.getPort(); |
50 | |
} |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public String captureMemorySnapshot() throws Exception |
56 | |
{ |
57 | 0 | return controller.captureMemorySnapshot(); |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public String captureSnapshot(long snapshotFlags) throws Exception |
64 | |
{ |
65 | 0 | return controller.captureSnapshot(snapshotFlags); |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public void startAllocationRecording(long mode) throws Exception |
72 | |
{ |
73 | 0 | if (ALLOCATION_RECORDING_ADAPTIVE != mode && ALLOCATION_RECORDING_ALL != mode) |
74 | |
{ |
75 | 0 | throw new IllegalArgumentException("Invalid allocation recording mode requested: " + mode); |
76 | |
} |
77 | 0 | if (mode == ALLOCATION_RECORDING_ALL) |
78 | |
{ |
79 | 0 | controller.startAllocationRecording(true, 1, false, 0); |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | |
|
84 | 0 | controller.startAllocationRecording(true, 10, true, 1048576); |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void stopAllocationRecording() throws Exception |
92 | |
{ |
93 | 0 | controller.stopAllocationRecording(); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public void startCPUProfiling(long mode, String filters) throws Exception |
100 | |
{ |
101 | 0 | controller.startCPUProfiling(mode, filters); |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void stopCPUProfiling() throws Exception |
108 | |
{ |
109 | 0 | controller.stopCPUProfiling(); |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void startMonitorProfiling() throws Exception |
116 | |
{ |
117 | 0 | controller.startMonitorProfiling(); |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public void stopMonitorProfiling() throws Exception |
124 | |
{ |
125 | 0 | controller.stopMonitorProfiling(); |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void advanceGeneration(String description) throws Exception |
132 | |
{ |
133 | 0 | controller.advanceGeneration(description); |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public String forceGC() throws Exception |
140 | |
{ |
141 | 0 | final long[] heapSizes = controller.forceGC(); |
142 | 0 | return ManagementMessages.forceGC(heapSizes).getMessage(); |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public void startCapturingMemSnapshot(final int seconds) |
149 | |
{ |
150 | 0 | if (!this.capturing.compareAndSet(false, true)) |
151 | |
{ |
152 | 0 | return; |
153 | |
} |
154 | |
|
155 | 0 | final Thread thread = new Thread(new Runnable() |
156 | 0 | { |
157 | |
public void run() |
158 | |
{ |
159 | |
try |
160 | |
{ |
161 | 0 | while (capturing.get()) |
162 | |
{ |
163 | 0 | controller.captureMemorySnapshot(); |
164 | 0 | Thread.sleep(seconds * 1000 ); |
165 | |
} |
166 | |
} |
167 | 0 | catch (final Exception e) |
168 | |
{ |
169 | 0 | logger.error("Failed to capture memory snapshot", e); |
170 | 0 | } |
171 | 0 | } |
172 | |
}); |
173 | 0 | thread.setDaemon(true); |
174 | 0 | thread.start(); |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public void stopCapturingMemSnapshot() |
181 | |
{ |
182 | 0 | this.capturing.set(false); |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public long getStatus() throws java.lang.Exception |
189 | |
{ |
190 | 0 | return (this.capturing.get()) |
191 | |
? (controller.getStatus() | SNAPSHOT_CAPTURING) |
192 | |
: controller.getStatus(); |
193 | |
} |
194 | |
|
195 | |
} |