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