1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
;
15 import java
.io
.IOException
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
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
.IStateChangeInput
;
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
.trace
.ITmfTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
34 * This is the high-level wrapper around the State History and its input and
35 * storage plugins. Just create the object using the constructor then .run()
37 * You can use one HistoryBuilder and it will instantiate everything underneath.
38 * If you need more fine-grained control you can still ignore this and
39 * instantiate everything manually.
44 public class HistoryBuilder
extends TmfComponent
{
46 private final IStateChangeInput sci
;
47 private final StateSystem ss
;
48 private final IStateHistoryBackend hb
;
49 private boolean started
= true; /* Don't handle signals until we're ready */
52 * Instantiate a new HistoryBuilder helper.
54 * @param stateChangeInput
55 * The input plugin to use. This is required.
57 * The backend storage to use.
58 * @param buildManually
59 * Should we build this history in-band or not. True means we
60 * will start the building ourselves and block the caller until
61 * construction is done. False (out-of-band) means we will start
62 * listening for the signal and return immediately. Another
63 * signal will be sent when finished.
65 * Is thrown if anything went wrong (usually with the storage
68 public HistoryBuilder(IStateChangeInput stateChangeInput
,
69 IStateHistoryBackend backend
, boolean buildManually
)
71 if (stateChangeInput
== null || backend
== null) {
72 throw new IllegalArgumentException();
74 sci
= stateChangeInput
;
76 ss
= new StateSystem(hb
, true);
78 sci
.assignTargetStateSystem(ss
);
81 TmfSignalManager
.deregister(this);
85 /* We'll now wait for the signal to start building */
90 * Factory-style method to open an existing history, you only have to
91 * provide the already-instantiated IStateHistoryBackend object.
94 * The history-backend object
95 * @return A IStateSystemBuilder reference to the new state system. If you
96 * will only run queries on this history, you should *definitely*
97 * cast it to IStateSystemQuerier.
99 * If there was something wrong.
101 public static ITmfStateSystemBuilder
openExistingHistory(
102 IStateHistoryBackend hb
) throws IOException
{
103 return new StateSystem(hb
, false);
107 * Return a read/write reference to the state system object that was
110 * @return Reference to the state system, with access to everything.
112 public ITmfStateSystemBuilder
getStateSystemBuilder() {
117 * Return a read-only reference to the state system object that was created.
119 * @return Reference to the state system, but only with the query methods
122 public ITmfStateSystem
getStateSystemQuerier() {
127 * Build the state history without waiting for signals or anything
129 private void buildManually() {
130 StateSystemBuildRequest request
= new StateSystemBuildRequest(this);
132 /* Send the request to the trace here, since there is probably no
134 sci
.getTrace().sendRequest(request
);
136 request
.waitForCompletion();
137 } catch (InterruptedException e
) {
143 // ------------------------------------------------------------------------
145 // ------------------------------------------------------------------------
148 * Listen to the "trace range updated" signal to start the state history
152 * The "trace range updated" signal. Listening to this
153 * signal will coalesce this request with the one from the
154 * indexer and histogram.
157 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
158 ITmfTrace trace
= signal
.getTrace();
159 if (signal
.getTrace() instanceof TmfExperiment
) {
160 TmfExperiment experiment
= (TmfExperiment
) signal
.getTrace();
161 for (ITmfTrace expTrace
: experiment
.getTraces()) {
162 if (expTrace
== sci
.getTrace()) {
168 if (trace
!= sci
.getTrace()) {
171 /* the signal is for this trace or for an experiment containing this trace */
175 StateSystemBuildRequest request
= new StateSystemBuildRequest(this);
176 trace
= signal
.getTrace();
177 trace
.sendRequest(request
);
182 * Listen to the "trace closed" signal to clean up if necessary.
185 * The "trace closed" signal.
188 public void traceClosed(TmfTraceClosedSignal signal
) {
189 ITmfTrace trace
= signal
.getTrace();
190 if (signal
.getTrace() instanceof TmfExperiment
) {
191 TmfExperiment experiment
= (TmfExperiment
) signal
.getTrace();
192 for (ITmfTrace expTrace
: experiment
.getTraces()) {
193 if (expTrace
== sci
.getTrace()) {
199 if (trace
!= sci
.getTrace()) {
202 /* the signal is for this trace or for an experiment containing this trace */
209 // ------------------------------------------------------------------------
210 // Methods reserved for the request object below
211 // ------------------------------------------------------------------------
213 /** Get the input plugin object */
214 IStateChangeInput
getInputPlugin() {
218 void close(boolean deleteFiles
) {
227 class StateSystemBuildRequest
extends TmfEventRequest
{
229 /** The amount of events queried at a time through the requests */
230 private final static int chunkSize
= 50000;
232 private final HistoryBuilder builder
;
233 private final IStateChangeInput sci
;
234 private final ITmfTrace trace
;
236 StateSystemBuildRequest(HistoryBuilder builder
) {
237 super(builder
.getInputPlugin().getExpectedEventType(),
238 TmfTimeRange
.ETERNITY
,
239 TmfDataRequest
.ALL_DATA
,
241 ITmfDataRequest
.ExecutionType
.BACKGROUND
);
242 this.builder
= builder
;
243 this.sci
= builder
.getInputPlugin();
244 this.trace
= sci
.getTrace();
248 public void handleData(final ITmfEvent event
) {
249 super.handleData(event
);
251 if (event
.getTrace() == trace
) {
252 sci
.processEvent(event
);
258 public void handleSuccess() {
259 super.handleSuccess();
260 builder
.close(false);
264 public void handleCancel() {
265 super.handleCancel();
270 public void handleFailure() {
271 super.handleFailure();