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