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