lttng: Support live updating of Control Flow view and Resources view
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / statesystem / HistoryBuilder.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.core.statesystem;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend;
19 import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
22 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
23 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
24 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
25 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal;
26 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
27 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateProvider;
28 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
29 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder;
30 import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
31 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
32 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
34
35 /**
36 * This is the high-level wrapper around the State History and its provider and
37 * storage plugins. Just create the object using the constructor then .run()
38 *
39 * You can use one HistoryBuilder and it will instantiate everything underneath.
40 * If you need more fine-grained control you can still ignore this and
41 * instantiate everything manually.
42 *
43 * @author alexmont
44 *
45 * @deprecated Building state systems should now be done via
46 * {@link TmfStateSystemAnalysisModule}
47 */
48 @Deprecated
49 public class HistoryBuilder extends TmfComponent {
50
51 private final ITmfStateProvider sp;
52 private final StateSystem ss;
53 private final IStateHistoryBackend hb;
54 private boolean started = true; /* Don't handle signals until we're ready */
55
56 /**
57 * Instantiate a new HistoryBuilder helper. The provider -> ss -> backend
58 * relationships should have been set up already.
59 *
60 * @param stateProvider
61 * The state provider plugin to use
62 * @param ss
63 * The state system object that will receive the state changes
64 * from the provider
65 * @param backend
66 * The back-end storage to use, which will receive the intervals
67 * from the ss
68 * @param buildManually
69 * Should we build this history in-band or not. True means we
70 * will start the building ourselves and block the caller until
71 * construction is done. False (out-of-band) means we will start
72 * listening for the signal and return immediately.
73 */
74 public HistoryBuilder(ITmfStateProvider stateProvider, StateSystem ss,
75 IStateHistoryBackend backend, boolean buildManually) {
76 if (stateProvider == null || backend == null || ss == null) {
77 throw new IllegalArgumentException();
78 }
79 if (stateProvider.getAssignedStateSystem() != ss) {
80 /* Logic check to make sure the provider is setup properly */
81 throw new IllegalArgumentException();
82 }
83
84 sp = stateProvider;
85 hb = backend;
86 this.ss = ss;
87
88 if (buildManually) {
89 TmfSignalManager.deregister(this);
90 this.buildManually();
91 } else {
92 started = false;
93 /* We'll now wait for the signal to start building */
94 }
95 }
96
97 /**
98 * Factory-style method to open an existing history, you only have to
99 * provide the already-instantiated IStateHistoryBackend object.
100 *
101 * @param hb
102 * The history-backend object
103 * @return A IStateSystemBuilder reference to the new state system. If you
104 * will only run queries on this history, you should *definitely*
105 * cast it to IStateSystemQuerier.
106 * @throws IOException
107 * If there was something wrong.
108 */
109 public static ITmfStateSystemBuilder openExistingHistory(
110 @NonNull IStateHistoryBackend hb) throws IOException {
111 return new StateSystem(hb, false);
112 }
113
114 /**
115 * Return a read/write reference to the state system object that was
116 * created.
117 *
118 * @return Reference to the state system, with access to everything.
119 */
120 public ITmfStateSystemBuilder getStateSystemBuilder() {
121 return ss;
122 }
123
124 /**
125 * Return a read-only reference to the state system object that was created.
126 *
127 * @return Reference to the state system, but only with the query methods
128 * available.
129 */
130 public ITmfStateSystem getStateSystemQuerier() {
131 return ss;
132 }
133
134 /**
135 * Build the state history without waiting for signals or anything
136 */
137 private void buildManually() {
138 StateSystemBuildRequest request = new StateSystemBuildRequest(this);
139
140 /* Send the request to the trace here, since there is probably no
141 * experiment. */
142 sp.getTrace().sendRequest(request);
143 try {
144 request.waitForCompletion();
145 } catch (InterruptedException e) {
146 e.printStackTrace();
147 }
148 }
149
150
151 // ------------------------------------------------------------------------
152 // Signal handlers
153 // ------------------------------------------------------------------------
154
155 /**
156 * Listen to the "trace range updated" signal to start the state history
157 * construction.
158 *
159 * @param signal
160 * The "trace range updated" signal. Listening to this
161 * signal will coalesce this request with the one from the
162 * indexer and histogram.
163 */
164 @TmfSignalHandler
165 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
166 ITmfTrace sender = signal.getTrace();
167
168 if (!signalIsForUs(sender)) {
169 return;
170 }
171
172 if (!started) {
173 started = true;
174 StateSystemBuildRequest request = new StateSystemBuildRequest(this);
175 sender.sendRequest(request);
176 }
177 }
178
179 /**
180 * Listen to the "trace closed" signal to clean up if necessary.
181 *
182 * @param signal
183 * The "trace closed" signal.
184 */
185 @TmfSignalHandler
186 public void traceClosed(TmfTraceClosedSignal signal) {
187 ITmfTrace sender = signal.getTrace();
188
189 if (signalIsForUs(sender) && !started) {
190 close(true);
191 }
192 }
193
194 /**
195 * Check if this signal is for this trace, or for an experiment containing
196 * this trace.
197 */
198 private boolean signalIsForUs(ITmfTrace sender) {
199 if (sender instanceof TmfExperiment) {
200 /* Yeah doing a lazy instanceof check here, but it's a special case! */
201 TmfExperiment exp = (TmfExperiment) sender;
202 for (ITmfTrace childTrace : exp.getTraces()) {
203 if (childTrace == sp.getTrace()) {
204 return true;
205 }
206 }
207 } else if (sender == sp.getTrace()) {
208 return true;
209 }
210 return false;
211 }
212
213 // ------------------------------------------------------------------------
214 // Methods reserved for the request object below
215 // ------------------------------------------------------------------------
216
217 /** Get the state provider object */
218 ITmfStateProvider getStateProvider() {
219 return sp;
220 }
221
222 void close(boolean deleteFiles) {
223 sp.dispose();
224 if (deleteFiles) {
225 hb.removeFiles();
226 }
227 dispose();
228 }
229 }
230
231 class StateSystemBuildRequest extends TmfEventRequest {
232 private final HistoryBuilder builder;
233 private final ITmfStateProvider sci;
234 private final ITmfTrace trace;
235
236 StateSystemBuildRequest(HistoryBuilder builder) {
237 super(builder.getStateProvider().getExpectedEventType(),
238 TmfTimeRange.ETERNITY,
239 0,
240 ITmfEventRequest.ALL_DATA,
241 ITmfEventRequest.ExecutionType.BACKGROUND);
242 this.builder = builder;
243 this.sci = builder.getStateProvider();
244 this.trace = sci.getTrace();
245 }
246
247 @Override
248 public void handleData(final ITmfEvent event) {
249 super.handleData(event);
250 if (event != null && event.getTrace() == trace) {
251 sci.processEvent(event);
252 }
253 }
254
255 @Override
256 public void handleSuccess() {
257 super.handleSuccess();
258 builder.close(false);
259 }
260
261 @Override
262 public void handleCancel() {
263 super.handleCancel();
264 builder.close(true);
265 }
266
267 @Override
268 public void handleFailure() {
269 super.handleFailure();
270 builder.close(true);
271 }
272 }
This page took 0.036675 seconds and 5 git commands to generate.