1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.management.mbeans; |
12 | |
|
13 | |
import org.mule.management.i18n.ManagementMessages; |
14 | |
|
15 | |
import com.yourkit.api.Controller; |
16 | |
|
17 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
18 | |
import org.apache.commons.logging.Log; |
19 | |
import org.apache.commons.logging.LogFactory; |
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 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 | controller.startAllocationRecording(mode); |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public void stopAllocationRecording() throws Exception |
80 | |
{ |
81 | 0 | controller.stopAllocationRecording(); |
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void startCPUProfiling(long mode, String filters) throws Exception |
88 | |
{ |
89 | 0 | controller.startCPUProfiling(mode, filters); |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public void stopCPUProfiling() throws Exception |
96 | |
{ |
97 | 0 | controller.stopCPUProfiling(); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public void startMonitorProfiling() throws Exception |
104 | |
{ |
105 | 0 | controller.startMonitorProfiling(); |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void stopMonitorProfiling() throws Exception |
112 | |
{ |
113 | 0 | controller.stopMonitorProfiling(); |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void advanceGeneration(String description) |
120 | |
{ |
121 | 0 | controller.advanceGeneration(description); |
122 | 0 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public String forceGC() throws Exception |
128 | |
{ |
129 | 0 | long[] heapSizes = controller.forceGC(); |
130 | 0 | return ManagementMessages.forceGC(heapSizes).getMessage(); |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void startCapturingMemSnapshot(final int seconds) |
137 | |
{ |
138 | 0 | if (!this.capturing.compareAndSet(false, true)) |
139 | |
{ |
140 | 0 | return; |
141 | |
} |
142 | |
|
143 | |
|
144 | 0 | final Thread thread = new Thread( |
145 | |
new Runnable() |
146 | |
{ |
147 | 0 | public void run() |
148 | |
{ |
149 | |
try |
150 | |
{ |
151 | 0 | while (capturing.get()) |
152 | |
{ |
153 | 0 | controller.captureMemorySnapshot(); |
154 | 0 | Thread.sleep(seconds * 1000 ); |
155 | |
} |
156 | |
} |
157 | 0 | catch (Exception e) |
158 | |
{ |
159 | 0 | logger.error("Failed to capture memory snapshot", e); |
160 | 0 | } |
161 | 0 | } |
162 | |
} |
163 | |
); |
164 | 0 | thread.setDaemon(true); |
165 | 0 | thread.start(); |
166 | 0 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public void stopCapturingMemSnapshot() |
172 | |
{ |
173 | 0 | this.capturing.set(false); |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public long getStatus() throws java.lang.Exception |
181 | |
{ |
182 | 0 | return (this.capturing.get()) ? (controller.getStatus() | SNAPSHOT_CAPTURING) : controller.getStatus(); |
183 | |
} |
184 | |
|
185 | |
} |