1 package net.sourceforge.basher.yourkit;
2
3 import com.yourkit.Constants;
4 import com.yourkit.api.*;
5 import net.sourceforge.basher.*;
6 import net.sourceforge.basher.events.*;
7 import org.apache.commons.logging.Log;
8
9
10
11
12 public class YourKitController implements BasherEventListener
13 {
14 private Controller _controller;
15 private Log _log;
16
17 private String _host = "localhost";
18 private int _port = 10020;
19
20 private String _filters = "com.sun.*\\njava.*\\njavax.*\\nsun.*\\njrockit.*\\nnet.sourceforge.basher.*";
21
22 public void setHost(final String host)
23 {
24 _host = host;
25 }
26
27 public void setPort(final int port)
28 {
29 _port = port;
30 }
31
32 public void setLog(final Log log)
33 {
34 _log = log;
35 }
36
37 public void initializeService() throws Exception
38 {
39 _log.info("Initializing YourKit Controller");
40 _log.info("Hostname: " + _host);
41 _log.info("Port: " + _port);
42
43 connect();
44 }
45
46 public void basherEvent(final BasherEvent basherEvent)
47 {
48 if (basherEvent instanceof CollectionStartedEvent)
49 {
50
51 try
52 {
53 BasherContext basherContext = getBasherContext(basherEvent);
54
55 YourKitProfilerContext yourKitProfilerContext = extractYourkitProfilerContext(basherContext);
56 if (yourKitProfilerContext == null)
57 {
58 return;
59 }
60
61 if (_controller == null)
62 {
63 _log.warn("Not connected to profiler, will not perform any action");
64 return;
65 }
66
67 if (yourKitProfilerContext.isGarbageCollectBeforeStart())
68 {
69 _log.info("Forcing GC before starting profiling");
70
71 final long[] longs = _controller.forceGC();
72 _log.info("GC completed");
73 _log.info("Memory Before: " + longs[0]);
74 _log.info("Memory After: " + longs[1]);
75 _log.info("Total Reclaimed: " + (longs[0] - longs[1]));
76
77 }
78
79
80
81 _controller.startCPUProfiling(ProfilingModes.CPU_TRACING, _filters);
82
83 }
84 catch (Exception e)
85 {
86 e.printStackTrace();
87 }
88 }
89 if (basherEvent instanceof CollectionStoppedEvent)
90 {
91 BasherContext basherContext = getBasherContext(basherEvent);
92
93
94 try
95 {
96 YourKitProfilerContext yourKitProfilerContext = extractYourkitProfilerContext(basherContext);
97 if (yourKitProfilerContext == null)
98 {
99 return;
100 }
101
102 if (_controller == null)
103 {
104 _log.warn("Not connected to profiler, will not perform any action");
105 return;
106 }
107
108 if (yourKitProfilerContext.isGarbageCollectBeforeStart())
109 {
110 _log.info("Forcing GC before capturing snapshot");
111
112 final long[] longs = _controller.forceGC();
113 _log.info("GC completed");
114 _log.info("Memory Before: " + longs[0]);
115 _log.info("Memory After: " + longs[1]);
116 _log.info("Total Reclaimed: " + (longs[0] - longs[1]));
117
118 }
119
120
121 _log.info("Capturing snapshot");
122 final String s = _controller.captureSnapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP);
123 _log.info("Snapshot captured to: " + s);
124
125 _log.info("Stopping profiling");
126 _controller.stopCPUProfiling();
127 _log.info("Profiling stopped");
128
129 }
130 catch (Exception e)
131 {
132 e.printStackTrace();
133 }
134 }
135
136 }
137
138 private YourKitProfilerContext extractYourkitProfilerContext(final BasherContext basherContext)
139 {
140 final ProfilerContext profilerContext = basherContext.getProfilerContext();
141 if (profilerContext == null)
142 {
143 _log.info("No profiler context defined in Basher context, will do nothing");
144 return null;
145 }
146 if (!(profilerContext instanceof YourKitProfilerContext))
147 {
148 _log.info("Profiler context not applicable to YourKit, will do nothing");
149 return null;
150 }
151
152 return (YourKitProfilerContext) profilerContext;
153 }
154
155 private void connect()
156 {
157 try
158 {
159 _log.info("Connecting to profiling agent");
160 _controller = new Controller(_host, _port);
161 _log.info("Successfully connected to profiling agent");
162 }
163 catch (Exception e)
164 {
165 _log.error("Error while setting up YourKit controller: " + e.getMessage(), e);
166 }
167 }
168
169 private void disconnect(final BasherContext basherContext)
170 {
171 if (_controller == null)
172 {
173 _log.warn("No YourKit controller available, will do nothing");
174 }
175 _controller = null;
176 }
177
178 private BasherContext getBasherContext(final BasherEvent basherEvent)
179 {
180 return ((CollectionEvent) basherEvent).getBasherContext();
181 }
182 }