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
.TmfExperimentRangeUpdatedSignal
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignal
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStateSystemBuildCompleted
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateChangeInput
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemBuilder
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
35 * This is the high-level wrapper around the State History and its input and
36 * storage plugins. Just create the object using the constructor then .run()
38 * You can use one HistoryBuilder and it will instantiate everything underneath.
39 * If you need more fine-grained control you can still ignore this and
40 * instantiate everything manually.
45 public class HistoryBuilder
extends TmfComponent
{
47 private final IStateChangeInput sci
;
48 private final StateSystem ss
;
49 private final IStateHistoryBackend hb
;
50 private boolean started
= true; /* Don't handle signals until we're ready */
53 * Instantiate a new HistoryBuilder helper.
55 * @param stateChangeInput
56 * The input plugin to use. This is required.
58 * The backend storage to use.
59 * @param buildManually
60 * Should we build this history in-band or not. True means we
61 * will start the building ourselves and block the caller until
62 * construction is done. False (out-of-band) means we will
63 * start listening for the signal and return immediately. Another
64 * signal will be sent when finished.
66 * Is thrown if anything went wrong (usually with the storage
69 public HistoryBuilder(IStateChangeInput stateChangeInput
,
70 IStateHistoryBackend backend
, boolean buildManually
)
72 if (stateChangeInput
== null || backend
== null) {
73 throw new IllegalArgumentException();
75 sci
= stateChangeInput
;
77 ss
= new StateSystem(hb
, true);
79 sci
.assignTargetStateSystem(ss
);
82 TmfSignalManager
.deregister(this);
86 /* We'll now wait for the signal to start building */
91 * Factory-style method to open an existing history, you only have to
92 * provide the already-instantiated IStateHistoryBackend object.
95 * The history-backend object
96 * @return A IStateSystemBuilder reference to the new state system. If you
97 * will only run queries on this history, you should *definitely*
98 * cast it to IStateSystemQuerier.
100 * If there was something wrong.
102 public static IStateSystemBuilder
openExistingHistory(
103 IStateHistoryBackend hb
) throws IOException
{
104 return new StateSystem(hb
, false);
108 * Return a read/write reference to the state system object that was
111 * @return Reference to the state system, with access to everything.
113 public IStateSystemBuilder
getStateSystemBuilder() {
118 * Return a read-only reference to the state system object that was created.
120 * @return Reference to the state system, but only with the query methods
123 public IStateSystemQuerier
getStateSystemQuerier() {
128 * Build the state history without waiting for signals or anything
130 private void buildManually() {
131 StateSystemBuildRequest request
= new StateSystemBuildRequest(this);
133 /* Send the request to the trace here, since there is probably no
135 sci
.getTrace().sendRequest(request
);
137 request
.waitForCompletion();
138 } catch (InterruptedException e
) {
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 * Listen to the "experiment selected" signal to start the state history
153 * The "experiment range updated" signal. Listening to this
154 * signal will coalesce this request with the one from the
155 * indexer and histogram.
158 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal
) {
159 StateSystemBuildRequest request
;
160 TmfExperiment
<ITmfEvent
> exp
;
164 request
= new StateSystemBuildRequest(this);
165 exp
= (TmfExperiment
<ITmfEvent
>) TmfExperiment
.getCurrentExperiment();
169 exp
.sendRequest(request
);
174 // ------------------------------------------------------------------------
175 // Methods reserved for the request object below
176 // ------------------------------------------------------------------------
178 /** Get the input plugin object */
179 IStateChangeInput
getInputPlugin() {
183 void close(boolean deleteFiles
) {
189 /* We won't broadcast the signal if the request was cancelled */
191 /* Broadcast the signal saying the history is done building */
192 doneSig
= new TmfStateSystemBuildCompleted(this, sci
.getTrace());
193 TmfSignalManager
.dispatchSignal(doneSig
);
196 TmfSignalManager
.deregister(this);
200 class StateSystemBuildRequest
extends TmfEventRequest
<ITmfEvent
> {
202 /** The amount of events queried at a time through the requests */
203 private final static int chunkSize
= 50000;
205 private final HistoryBuilder builder
;
206 private final IStateChangeInput sci
;
207 private final ITmfTrace
<ITmfEvent
> trace
;
209 StateSystemBuildRequest(HistoryBuilder builder
) {
210 super((Class
<ITmfEvent
>) builder
.getInputPlugin().getExpectedEventType().getClass(),
211 TmfTimeRange
.ETERNITY
,
212 TmfDataRequest
.ALL_DATA
,
214 ITmfDataRequest
.ExecutionType
.BACKGROUND
);
215 this.builder
= builder
;
216 this.sci
= builder
.getInputPlugin();
217 this.trace
= sci
.getTrace();
221 public void handleData(final ITmfEvent event
) {
222 super.handleData(event
);
224 if (event
.getTrace() == trace
) {
225 sci
.processEvent(event
);
231 public void handleSuccess() {
232 super.handleSuccess();
233 builder
.close(false);
237 public void handleCancel() {
238 super.handleCancel();
243 public void handleFailure() {
244 super.handleFailure();