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
.tmf
.core
.statesystem
;
16 import java
.io
.IOException
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.core
.runtime
.jobs
.Job
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.HistoryBuilder
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.IStateHistoryBackend
;
24 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.historytree
.HistoryTreeBackend
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.historytree
.ThreadedHistoryTreeBackend
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStateSystemBuildCompleted
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
33 * This abstract manager class handles loading or creating state history files
34 * for use in TMF's generic state system.
39 public abstract class StateSystemManager
extends TmfComponent
{
41 /** Size of the blocking queue to use when building a state history */
42 private final static int QUEUE_SIZE
= 10000;
45 * Load the history file matching the target trace. If the file already
46 * exists, it will be opened directly. If not, it will be created from
47 * scratch. In the case the history has to be built, it's possible to block
48 * the calling thread until construction is complete.
51 * The target name of the history file we want to use. If it
52 * exists it will be opened. If it doesn't, a new file will be
53 * created with this name/path.
55 * The IStateChangeInput to use for building the history file. It
56 * may be required even if we are opening an already-existing
57 * history (ie, for partial histories).
58 * @param waitForCompletion
59 * Should we block the calling thread until the construction is
60 * complete? It has no effect if the file already exists.
61 * @return A IStateSystemQuerier handler to the state system, with which you
62 * can then run queries on the history.
63 * @throws TmfTraceException
65 public static IStateSystemQuerier
loadStateHistory(File htFile
,
66 IStateChangeInput htInput
, boolean waitForCompletion
)
67 throws TmfTraceException
{
68 IStateSystemQuerier ss
;
69 IStateHistoryBackend htBackend
;
71 /* If the target file already exists, do not rebuild it uselessly */
72 // TODO for now we assume it's complete. Might be a good idea to check
73 // at least if its range matches the trace's range.
74 if (htFile
.exists()) {
75 /* Load an existing history */
77 htBackend
= new HistoryTreeBackend(htFile
);
78 ss
= HistoryBuilder
.openExistingHistory(htBackend
);
80 } catch (IOException e
) {
82 * There was an error opening the existing file. Perhaps it was
83 * corrupted, perhaps it's an old version? We'll just
84 * fall-through and try to build a new one from scratch instead.
89 /* Create a new state history from scratch */
90 HistoryBuilder builder
;
92 if (htInput
== null) {
96 htBackend
= new ThreadedHistoryTreeBackend(htFile
,
97 htInput
.getStartTime(), QUEUE_SIZE
);
98 builder
= new HistoryBuilder(htInput
, htBackend
);
99 } catch (IOException e
) {
101 * If it fails here however, it means there was a problem writing to
102 * the disk, so throw a real exception this time.
104 throw new TmfTraceException(e
.toString(), e
);
106 startBuilderJob(builder
, htInput
.getTrace(), waitForCompletion
);
107 return builder
.getStateSystemQuerier();
110 private static void startBuilderJob(final HistoryBuilder builder
,
111 final ITmfTrace
<?
> trace
, boolean waitForCompletion
) {
113 final Job job
= new Job("Building state history for " +
114 trace
.getName() + "...") { //$NON-NLS-1$
116 protected IStatus
run(final IProgressMonitor monitor
) {
120 /* Broadcast the signal saying the history is done building */
121 TmfSignalManager
.dispatchSignal(new TmfStateSystemBuildCompleted(
124 return Status
.OK_STATUS
;
129 if (waitForCompletion
) {
132 } catch (InterruptedException e
) {