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
.trace
.ITmfTrace
;
31 * This abstract manager class handles loading or creating state history files
32 * for use in TMF's generic state system.
37 public abstract class StateSystemManager
extends TmfComponent
{
39 /** Size of the blocking queue to use when building a state history */
40 private final static int QUEUE_SIZE
= 10000;
43 * Load the history file matching the target trace. If the file already
44 * exists, it will be opened directly. If not, it will be created from
45 * scratch. In the case the history has to be built, it's possible to block
46 * the calling thread until construction is complete.
49 * The target name of the history file we want to use. If it
50 * exists it will be opened. If it doesn't, a new file will be
51 * created with this name/path.
53 * The IStateChangeInput to use for building the history file. It
54 * may be required even if we are opening an already-existing
55 * history (ie, for partial histories).
56 * @param waitForCompletion
57 * Should we block the calling thread until the construction is
58 * complete? It has no effect if the file already exists.
59 * @return A IStateSystemQuerier handler to the state system, with which you
60 * can then run queries on the history.
61 * @throws TmfTraceException
63 public static IStateSystemQuerier
loadStateHistory(File htFile
,
64 IStateChangeInput htInput
, boolean waitForCompletion
)
65 throws TmfTraceException
{
66 IStateSystemQuerier ss
;
67 IStateHistoryBackend htBackend
;
69 /* If the target file already exists, do not rebuild it uselessly */
70 // TODO for now we assume it's complete. Might be a good idea to check
71 // at least if its range matches the trace's range.
72 if (htFile
.exists()) {
73 /* Load an existing history */
75 htBackend
= new HistoryTreeBackend(htFile
);
76 ss
= HistoryBuilder
.openExistingHistory(htBackend
);
78 } catch (IOException e
) {
80 * There was an error opening the existing file. Perhaps it was
81 * corrupted, perhaps it's an old version? We'll just
82 * fall-through and try to build a new one from scratch instead.
87 /* Create a new state history from scratch */
88 HistoryBuilder builder
;
90 if (htInput
== null) {
94 htBackend
= new ThreadedHistoryTreeBackend(htFile
,
95 htInput
.getStartTime(), QUEUE_SIZE
);
96 builder
= new HistoryBuilder(htInput
, htBackend
);
97 } catch (IOException e
) {
99 * If it fails here however, it means there was a problem writing to
100 * the disk, so throw a real exception this time.
102 throw new TmfTraceException(e
.toString(), e
);
104 startBuilderJob(builder
, htInput
.getTrace(), waitForCompletion
);
105 return builder
.getStateSystemQuerier();
108 private static void startBuilderJob(final HistoryBuilder builder
,
109 ITmfTrace
<?
> trace
, boolean waitForCompletion
) {
111 final Job job
= new Job("Building state history for " +
112 trace
.getName() + "...") { //$NON-NLS-1$
114 protected IStatus
run(final IProgressMonitor monitor
) {
118 return Status
.OK_STATUS
;
123 if (waitForCompletion
) {
126 } catch (InterruptedException e
) {