1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Geneviève Bastien - Initial API and implementation
11 * Bernd Hufmann - Integrated history builder functionality
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.statesystem
;
17 import java
.io
.IOException
;
18 import java
.util
.Collections
;
19 import java
.util
.concurrent
.CountDownLatch
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.StateSystem
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.IStateHistoryBackend
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.InMemoryBackend
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.NullBackend
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.historytree
.HistoryTreeBackend
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.historytree
.ThreadedHistoryTreeBackend
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.partial
.PartialHistoryBackend
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.partial
.PartialStateSystem
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
44 * Abstract analysis module to generate a state system. It is a base class that
45 * can be used as a shortcut by analysis who just need to build a single state
46 * system with a state provider.
48 * Analysis implementing this class should only need to provide a state system
49 * and optionally a backend (default to NULL) and, if required, a filename
50 * (defaults to the analysis'ID)
52 * @author Geneviève Bastien
56 public abstract class TmfStateSystemAnalysisModule
extends TmfAbstractAnalysisModule
57 implements ITmfAnalysisModuleWithStateSystems
{
59 private static final String EXTENSION
= ".ht"; //$NON-NLS-1$
61 private final CountDownLatch fInitialized
= new CountDownLatch(1);
63 @Nullable private ITmfStateSystemBuilder fStateSystem
;
64 @Nullable private ITmfStateProvider fStateProvider
;
65 @Nullable private IStateHistoryBackend fHtBackend
;
66 @Nullable private ITmfEventRequest fRequest
;
69 * State system backend types
71 * @author Geneviève Bastien
73 protected enum StateSystemBackendType
{
74 /** Full history in file */
76 /** In memory state system */
80 /** State system backed with partial history */
85 * Get the state provider for this analysis module
87 * @return the state provider
89 protected abstract ITmfStateProvider
createStateProvider();
92 * Get the state system backend type used by this module
94 * @return The {@link StateSystemBackendType}
96 protected StateSystemBackendType
getBackendType() {
97 /* Using full history by default, sub-classes can override */
98 return StateSystemBackendType
.FULL
;
102 * Get the supplementary file name where to save this state system. The
103 * default is the ID of the analysis followed by the extension.
105 * @return The supplementary file name
107 protected String
getSsFileName() {
108 return getId() + EXTENSION
;
112 * Get the state system generated by this analysis, or null if it is not yet
115 * @return The state system
118 public ITmfStateSystem
getStateSystem() {
123 * Block the calling thread until the analysis module has been initialized.
124 * After this method returns, {@link #getStateSystem()} should not return
127 public void waitForInitialization() {
129 fInitialized
.await();
130 } catch (InterruptedException e
) {}
133 // ------------------------------------------------------------------------
134 // TmfAbstractAnalysisModule
135 // ------------------------------------------------------------------------
138 protected boolean executeAnalysis(@Nullable final IProgressMonitor monitor
) {
139 IProgressMonitor mon
= (monitor
== null ?
new NullProgressMonitor() : monitor
);
140 final ITmfStateProvider provider
= createStateProvider();
144 /* FIXME: State systems should make use of the monitor, to be cancelled */
146 /* Get the state system according to backend */
147 StateSystemBackendType backend
= getBackendType();
152 directory
= TmfTraceManager
.getSupplementaryFileDir(getTrace());
153 htFile
= new File(directory
+ getSsFileName());
154 createFullHistory(id
, provider
, htFile
);
157 directory
= TmfTraceManager
.getSupplementaryFileDir(getTrace());
158 htFile
= new File(directory
+ getSsFileName());
159 createPartialHistory(id
, provider
, htFile
);
162 createInMemoryHistory(id
, provider
);
165 createNullHistory(id
, provider
);
170 } catch (TmfTraceException e
) {
173 return !mon
.isCanceled();
177 protected void canceling() {
178 ITmfEventRequest req
= fRequest
;
179 if ((req
!= null) && (!req
.isCompleted())) {
185 public void dispose() {
187 if (fStateSystem
!= null) {
188 fStateSystem
.dispose();
192 // ------------------------------------------------------------------------
193 // History creation methods
194 // ------------------------------------------------------------------------
197 * Load the history file matching the target trace. If the file already
198 * exists, it will be opened directly. If not, it will be created from
201 private void createFullHistory(String id
, ITmfStateProvider provider
, File htFile
) throws TmfTraceException
{
203 /* If the target file already exists, do not rebuild it uselessly */
204 // TODO for now we assume it's complete. Might be a good idea to check
205 // at least if its range matches the trace's range.
207 if (htFile
.exists()) {
208 /* Load an existing history */
209 final int version
= provider
.getVersion();
211 IStateHistoryBackend backend
= new HistoryTreeBackend(htFile
, version
);
212 fHtBackend
= backend
;
213 fStateSystem
= new StateSystem(id
, backend
, false);
214 fInitialized
.countDown();
216 } catch (IOException e
) {
218 * There was an error opening the existing file. Perhaps it was
219 * corrupted, perhaps it's an old version? We'll just
220 * fall-through and try to build a new one from scratch instead.
225 /* Size of the blocking queue to use when building a state history */
226 final int QUEUE_SIZE
= 10000;
229 IStateHistoryBackend backend
= new ThreadedHistoryTreeBackend(htFile
,
230 provider
.getStartTime(), provider
.getVersion(), QUEUE_SIZE
);
231 fHtBackend
= backend
;
232 fStateSystem
= new StateSystem(id
, backend
);
233 provider
.assignTargetStateSystem(fStateSystem
);
235 } catch (IOException e
) {
237 * If it fails here however, it means there was a problem writing to
238 * the disk, so throw a real exception this time.
240 throw new TmfTraceException(e
.toString(), e
);
245 * Create a new state system backed with a partial history. A partial
246 * history is similar to a "full" one (which you get with
247 * {@link #newFullHistory}), except that the file on disk is much smaller,
248 * but queries are a bit slower.
250 * Also note that single-queries are implemented using a full-query
251 * underneath, (which are much slower), so this might not be a good fit for
252 * a use case where you have to do lots of single queries.
254 private void createPartialHistory(String id
, ITmfStateProvider provider
, File htPartialFile
)
255 throws TmfTraceException
{
257 * The order of initializations is very tricky (but very important!)
258 * here. We need to follow this pattern:
259 * (1 is done before the call to this method)
261 * 1- Instantiate realStateProvider
262 * 2- Instantiate realBackend
263 * 3- Instantiate partialBackend, with prereqs:
264 * 3a- Instantiate partialProvider, via realProvider.getNew()
265 * 3b- Instantiate nullBackend (partialSS's backend)
266 * 3c- Instantiate partialSS
267 * 3d- partialProvider.assignSS(partialSS)
268 * 4- Instantiate realSS
269 * 5- partialSS.assignUpstream(realSS)
270 * 6- realProvider.assignSS(realSS)
271 * 7- Call HistoryBuilder(realProvider, realSS, partialBackend) to build the thing.
274 /* Size of the blocking queue to use when building a state history */
275 final int QUEUE_SIZE
= 10000;
277 final long granularity
= 50000;
280 IStateHistoryBackend realBackend
= null;
282 realBackend
= new ThreadedHistoryTreeBackend(htPartialFile
,
283 provider
.getStartTime(), provider
.getVersion(), QUEUE_SIZE
);
284 } catch (IOException e
) {
285 throw new TmfTraceException(e
.toString(), e
);
289 ITmfStateProvider partialProvider
= provider
.getNewInstance();
291 /* 3b-3c, constructor automatically uses a NullBackend */
292 PartialStateSystem pss
= new PartialStateSystem();
295 partialProvider
.assignTargetStateSystem(pss
);
298 IStateHistoryBackend partialBackend
=
299 new PartialHistoryBackend(partialProvider
, pss
, realBackend
, granularity
);
302 StateSystem realSS
= new StateSystem(id
, partialBackend
);
305 pss
.assignUpstream(realSS
);
308 provider
.assignTargetStateSystem(realSS
);
311 fHtBackend
= partialBackend
;
312 fStateSystem
= realSS
;
318 * Create a new state system using a null history back-end. This means that
319 * no history intervals will be saved anywhere, and as such only
320 * {@link ITmfStateSystem#queryOngoingState} will be available.
322 private void createNullHistory(String id
, ITmfStateProvider provider
) {
323 IStateHistoryBackend backend
= new NullBackend();
324 fHtBackend
= backend
;
325 fStateSystem
= new StateSystem(id
, backend
);
326 provider
.assignTargetStateSystem(fStateSystem
);
331 * Create a new state system using in-memory interval storage. This should
332 * only be done for very small state system, and will be naturally limited
335 private void createInMemoryHistory(String id
, ITmfStateProvider provider
) {
336 IStateHistoryBackend backend
= new InMemoryBackend(provider
.getStartTime());
337 fHtBackend
= backend
;
338 fStateSystem
= new StateSystem(id
, backend
);
339 provider
.assignTargetStateSystem(fStateSystem
);
343 private void disposeProvider(boolean deleteFiles
) {
344 ITmfStateProvider provider
= fStateProvider
;
345 if (provider
!= null) {
348 if (deleteFiles
&& (fHtBackend
!= null)) {
349 fHtBackend
.removeFiles();
353 private void build(ITmfStateProvider provider
) {
354 if ((fStateSystem
== null) || (fHtBackend
== null)) {
355 throw new IllegalArgumentException();
358 ITmfEventRequest request
= fRequest
;
359 if ((request
!= null) && (!request
.isCompleted())) {
363 request
= new StateSystemEventRequest(provider
);
364 provider
.getTrace().sendRequest(request
);
367 * Only now that we've actually started the build, we'll update the
368 * class fields, so that they become visible for other callers.
370 fStateProvider
= provider
;
374 * The state system object is now created, we can consider this module
375 * "initialized" (components can retrieve it and start doing queries).
377 fInitialized
.countDown();
380 * Block the executeAnalysis() construction is complete (so that the
381 * progress monitor displays that it is running).
384 request
.waitForCompletion();
385 } catch (InterruptedException e
) {
390 private class StateSystemEventRequest
extends TmfEventRequest
{
391 private final ITmfStateProvider sci
;
392 private final ITmfTrace trace
;
394 public StateSystemEventRequest(ITmfStateProvider sp
) {
395 super(sp
.getExpectedEventType(),
396 TmfTimeRange
.ETERNITY
,
398 ITmfEventRequest
.ALL_DATA
,
399 ITmfEventRequest
.ExecutionType
.BACKGROUND
);
402 // sci.getTrace() will eventually return a @NonNull
403 @SuppressWarnings("null")
404 @NonNull ITmfTrace tr
= sci
.getTrace();
410 public void handleData(final @Nullable ITmfEvent event
) {
411 super.handleData(event
);
412 if (event
!= null && event
.getTrace() == trace
) {
413 sci
.processEvent(event
);
418 public void handleSuccess() {
419 super.handleSuccess();
420 disposeProvider(false);
424 public void handleCancel() {
425 super.handleCancel();
426 disposeProvider(true);
430 public void handleFailure() {
431 super.handleFailure();
432 disposeProvider(true);
436 // ------------------------------------------------------------------------
437 // ITmfAnalysisModuleWithStateSystems
438 // ------------------------------------------------------------------------
442 public ITmfStateSystem
getStateSystem(String id
) {
443 if (id
.equals(getId())) {
450 public Iterable
<ITmfStateSystem
> getStateSystems() {
451 @SuppressWarnings("null")
452 @NonNull Iterable
<ITmfStateSystem
> ret
= Collections
.singleton((ITmfStateSystem
) fStateSystem
);