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