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
.backends
.partial
.PartialHistoryBackend
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.partial
.PartialStateSystem
;
28 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
29 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystemBuilder
;
30 import org
.eclipse
.linuxtools
.statesystem
.core
.StateSystemFactory
;
31 import org
.eclipse
.linuxtools
.statesystem
.core
.backend
.IStateHistoryBackend
;
32 import org
.eclipse
.linuxtools
.statesystem
.core
.backend
.InMemoryBackend
;
33 import org
.eclipse
.linuxtools
.statesystem
.core
.backend
.NullBackend
;
34 import org
.eclipse
.linuxtools
.statesystem
.core
.backend
.historytree
.HistoryTreeBackend
;
35 import org
.eclipse
.linuxtools
.statesystem
.core
.backend
.historytree
.ThreadedHistoryTreeBackend
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
46 * Abstract analysis module to generate a state system. It is a base class that
47 * can be used as a shortcut by analysis who just need to build a single state
48 * system with a state provider.
50 * Analysis implementing this class should only need to provide a state system
51 * and optionally a backend (default to NULL) and, if required, a filename
52 * (defaults to the analysis'ID)
54 * @author Geneviève Bastien
58 public abstract class TmfStateSystemAnalysisModule
extends TmfAbstractAnalysisModule
59 implements ITmfAnalysisModuleWithStateSystems
{
61 private static final String EXTENSION
= ".ht"; //$NON-NLS-1$
63 private final CountDownLatch fInitialized
= new CountDownLatch(1);
65 @Nullable private ITmfStateSystemBuilder fStateSystem
;
66 @Nullable private ITmfStateProvider fStateProvider
;
67 @Nullable private IStateHistoryBackend fHtBackend
;
68 @Nullable private ITmfEventRequest fRequest
;
71 * State system backend types
73 * @author Geneviève Bastien
75 protected enum StateSystemBackendType
{
76 /** Full history in file */
78 /** In memory state system */
82 /** State system backed with partial history */
87 * Get the state provider for this analysis module
89 * @return the state provider
91 protected abstract ITmfStateProvider
createStateProvider();
94 * Get the state system backend type used by this module
96 * @return The {@link StateSystemBackendType}
98 protected StateSystemBackendType
getBackendType() {
99 /* Using full history by default, sub-classes can override */
100 return StateSystemBackendType
.FULL
;
104 * Get the supplementary file name where to save this state system. The
105 * default is the ID of the analysis followed by the extension.
107 * @return The supplementary file name
109 protected String
getSsFileName() {
110 return getId() + EXTENSION
;
114 * Get the state system generated by this analysis, or null if it is not yet
117 * @return The state system
120 public ITmfStateSystem
getStateSystem() {
125 * Block the calling thread until the analysis module has been initialized.
126 * After this method returns, {@link #getStateSystem()} should not return
129 public void waitForInitialization() {
131 fInitialized
.await();
132 } catch (InterruptedException e
) {}
135 // ------------------------------------------------------------------------
136 // TmfAbstractAnalysisModule
137 // ------------------------------------------------------------------------
140 protected boolean executeAnalysis(@Nullable final IProgressMonitor monitor
) {
141 IProgressMonitor mon
= (monitor
== null ?
new NullProgressMonitor() : monitor
);
142 final ITmfStateProvider provider
= createStateProvider();
146 /* FIXME: State systems should make use of the monitor, to be cancelled */
148 /* Get the state system according to backend */
149 StateSystemBackendType backend
= getBackendType();
154 directory
= TmfTraceManager
.getSupplementaryFileDir(getTrace());
155 htFile
= new File(directory
+ getSsFileName());
156 createFullHistory(id
, provider
, htFile
);
159 directory
= TmfTraceManager
.getSupplementaryFileDir(getTrace());
160 htFile
= new File(directory
+ getSsFileName());
161 createPartialHistory(id
, provider
, htFile
);
164 createInMemoryHistory(id
, provider
);
167 createNullHistory(id
, provider
);
172 } catch (TmfTraceException e
) {
175 return !mon
.isCanceled();
179 protected void canceling() {
180 ITmfEventRequest req
= fRequest
;
181 if ((req
!= null) && (!req
.isCompleted())) {
187 public void dispose() {
189 if (fStateSystem
!= null) {
190 fStateSystem
.dispose();
194 // ------------------------------------------------------------------------
195 // History creation methods
196 // ------------------------------------------------------------------------
199 * Load the history file matching the target trace. If the file already
200 * exists, it will be opened directly. If not, it will be created from
203 private void createFullHistory(String id
, ITmfStateProvider provider
, File htFile
) throws TmfTraceException
{
205 /* If the target file already exists, do not rebuild it uselessly */
206 // TODO for now we assume it's complete. Might be a good idea to check
207 // at least if its range matches the trace's range.
209 if (htFile
.exists()) {
210 /* Load an existing history */
211 final int version
= provider
.getVersion();
213 IStateHistoryBackend backend
= new HistoryTreeBackend(htFile
, version
);
214 fHtBackend
= backend
;
215 fStateSystem
= StateSystemFactory
.newStateSystem(id
, backend
, false);
216 fInitialized
.countDown();
218 } catch (IOException e
) {
220 * There was an error opening the existing file. Perhaps it was
221 * corrupted, perhaps it's an old version? We'll just
222 * fall-through and try to build a new one from scratch instead.
227 /* Size of the blocking queue to use when building a state history */
228 final int QUEUE_SIZE
= 10000;
231 IStateHistoryBackend backend
= new ThreadedHistoryTreeBackend(htFile
,
232 provider
.getStartTime(), provider
.getVersion(), QUEUE_SIZE
);
233 fHtBackend
= backend
;
234 fStateSystem
= StateSystemFactory
.newStateSystem(id
, backend
);
235 provider
.assignTargetStateSystem(fStateSystem
);
237 } catch (IOException e
) {
239 * If it fails here however, it means there was a problem writing to
240 * the disk, so throw a real exception this time.
242 throw new TmfTraceException(e
.toString(), e
);
247 * Create a new state system backed with a partial history. A partial
248 * history is similar to a "full" one (which you get with
249 * {@link #newFullHistory}), except that the file on disk is much smaller,
250 * but queries are a bit slower.
252 * Also note that single-queries are implemented using a full-query
253 * underneath, (which are much slower), so this might not be a good fit for
254 * a use case where you have to do lots of single queries.
256 private void createPartialHistory(String id
, ITmfStateProvider provider
, File htPartialFile
)
257 throws TmfTraceException
{
259 * The order of initializations is very tricky (but very important!)
260 * here. We need to follow this pattern:
261 * (1 is done before the call to this method)
263 * 1- Instantiate realStateProvider
264 * 2- Instantiate realBackend
265 * 3- Instantiate partialBackend, with prereqs:
266 * 3a- Instantiate partialProvider, via realProvider.getNew()
267 * 3b- Instantiate nullBackend (partialSS's backend)
268 * 3c- Instantiate partialSS
269 * 3d- partialProvider.assignSS(partialSS)
270 * 4- Instantiate realSS
271 * 5- partialSS.assignUpstream(realSS)
272 * 6- realProvider.assignSS(realSS)
273 * 7- Call HistoryBuilder(realProvider, realSS, partialBackend) to build the thing.
276 /* Size of the blocking queue to use when building a state history */
277 final int QUEUE_SIZE
= 10000;
279 final long granularity
= 50000;
282 IStateHistoryBackend realBackend
= null;
284 realBackend
= new ThreadedHistoryTreeBackend(htPartialFile
,
285 provider
.getStartTime(), provider
.getVersion(), QUEUE_SIZE
);
286 } catch (IOException e
) {
287 throw new TmfTraceException(e
.toString(), e
);
291 ITmfStateProvider partialProvider
= provider
.getNewInstance();
293 /* 3b-3c, constructor automatically uses a NullBackend */
294 PartialStateSystem pss
= new PartialStateSystem();
297 partialProvider
.assignTargetStateSystem(pss
);
300 IStateHistoryBackend partialBackend
=
301 new PartialHistoryBackend(partialProvider
, pss
, realBackend
, granularity
);
304 @SuppressWarnings("restriction")
305 org
.eclipse
.linuxtools
.internal
.statesystem
.core
.StateSystem realSS
=
306 (org
.eclipse
.linuxtools
.internal
.statesystem
.core
.StateSystem
) StateSystemFactory
.newStateSystem(id
, partialBackend
);
309 pss
.assignUpstream(realSS
);
312 provider
.assignTargetStateSystem(realSS
);
315 fHtBackend
= partialBackend
;
316 fStateSystem
= realSS
;
322 * Create a new state system using a null history back-end. This means that
323 * no history intervals will be saved anywhere, and as such only
324 * {@link ITmfStateSystem#queryOngoingState} will be available.
326 private void createNullHistory(String id
, ITmfStateProvider provider
) {
327 IStateHistoryBackend backend
= new NullBackend();
328 fHtBackend
= backend
;
329 fStateSystem
= StateSystemFactory
.newStateSystem(id
, backend
);
330 provider
.assignTargetStateSystem(fStateSystem
);
335 * Create a new state system using in-memory interval storage. This should
336 * only be done for very small state system, and will be naturally limited
339 private void createInMemoryHistory(String id
, ITmfStateProvider provider
) {
340 IStateHistoryBackend backend
= new InMemoryBackend(provider
.getStartTime());
341 fHtBackend
= backend
;
342 fStateSystem
= StateSystemFactory
.newStateSystem(id
, backend
);
343 provider
.assignTargetStateSystem(fStateSystem
);
347 private void disposeProvider(boolean deleteFiles
) {
348 ITmfStateProvider provider
= fStateProvider
;
349 if (provider
!= null) {
352 if (deleteFiles
&& (fHtBackend
!= null)) {
353 fHtBackend
.removeFiles();
357 private void build(ITmfStateProvider provider
) {
358 if ((fStateSystem
== null) || (fHtBackend
== null)) {
359 throw new IllegalArgumentException();
362 ITmfEventRequest request
= fRequest
;
363 if ((request
!= null) && (!request
.isCompleted())) {
367 request
= new StateSystemEventRequest(provider
);
368 provider
.getTrace().sendRequest(request
);
371 * Only now that we've actually started the build, we'll update the
372 * class fields, so that they become visible for other callers.
374 fStateProvider
= provider
;
378 * The state system object is now created, we can consider this module
379 * "initialized" (components can retrieve it and start doing queries).
381 fInitialized
.countDown();
384 * Block the executeAnalysis() construction is complete (so that the
385 * progress monitor displays that it is running).
388 request
.waitForCompletion();
389 } catch (InterruptedException e
) {
394 private class StateSystemEventRequest
extends TmfEventRequest
{
395 private final ITmfStateProvider sci
;
396 private final ITmfTrace trace
;
398 public StateSystemEventRequest(ITmfStateProvider sp
) {
399 super(sp
.getExpectedEventType(),
400 TmfTimeRange
.ETERNITY
,
402 ITmfEventRequest
.ALL_DATA
,
403 ITmfEventRequest
.ExecutionType
.BACKGROUND
);
406 // sci.getTrace() will eventually return a @NonNull
407 @SuppressWarnings("null")
408 @NonNull ITmfTrace tr
= sci
.getTrace();
414 public void handleData(final ITmfEvent event
) {
415 super.handleData(event
);
416 if (event
.getTrace() == trace
) {
417 sci
.processEvent(event
);
422 public void handleSuccess() {
423 super.handleSuccess();
424 disposeProvider(false);
428 public void handleCancel() {
429 super.handleCancel();
430 disposeProvider(true);
434 public void handleFailure() {
435 super.handleFailure();
436 disposeProvider(true);
440 // ------------------------------------------------------------------------
441 // ITmfAnalysisModuleWithStateSystems
442 // ------------------------------------------------------------------------
446 public ITmfStateSystem
getStateSystem(String id
) {
447 if (id
.equals(getId())) {
454 public Iterable
<ITmfStateSystem
> getStateSystems() {
455 @SuppressWarnings("null")
456 @NonNull Iterable
<ITmfStateSystem
> ret
= Collections
.singleton((ITmfStateSystem
) fStateSystem
);