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