Integrate Babeltrace CTF tests and fix parsing problems
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statesystem / StateSystemManager.java
CommitLineData
6e71ce46 1/*******************************************************************************
61759503 2 * Copyright (c) 2012, 2013 Ericsson
6e71ce46
AM
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
83134537 5 *
6e71ce46
AM
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
83134537 10 *
6e71ce46
AM
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.statesystem;
14
15import java.io.File;
16import java.io.IOException;
17
2ab9afbc 18import org.eclipse.linuxtools.internal.tmf.core.statesystem.HistoryBuilder;
7e634be6 19import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateSystem;
f9a76cac
AM
20import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend;
21import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.InMemoryBackend;
22import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.NullBackend;
23import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.HistoryTreeBackend;
24import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.ThreadedHistoryTreeBackend;
1b9d3765
AM
25import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.partial.PartialHistoryBackend;
26import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.partial.PartialStateSystem;
6e71ce46
AM
27import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
28import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
6e71ce46
AM
29
30/**
31 * This abstract manager class handles loading or creating state history files
32 * for use in TMF's generic state system.
83134537 33 *
2cb26548
AM
34 * @version 1.0
35 * @author Alexandre Montplaisir
6e71ce46
AM
36 */
37public abstract class StateSystemManager extends TmfComponent {
38
39 /** Size of the blocking queue to use when building a state history */
40 private final static int QUEUE_SIZE = 10000;
41
42 /**
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.
83134537 47 *
6e71ce46
AM
48 * @param htFile
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.
52 * @param htInput
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).
e34f7caa
AM
56 * @param buildManually
57 * If false, the construction will wait for a signal before
58 * starting. If true, it will build everything right now and
59 * block the caller. It has no effect if the file already exists.
6e71ce46
AM
60 * @return A IStateSystemQuerier handler to the state system, with which you
61 * can then run queries on the history.
62 * @throws TmfTraceException
5df842b3
AM
63 * If there was a problem reading or writing one of the files.
64 * See the contents of this exception for more info.
f1f86dfb 65 * @since 2.0
6e71ce46 66 */
f1f86dfb 67 public static ITmfStateSystem loadStateHistory(File htFile,
e45de797 68 IStateChangeInput htInput, boolean buildManually)
51e216bd 69 throws TmfTraceException {
6e71ce46 70 IStateHistoryBackend htBackend;
51e216bd 71
6e71ce46
AM
72 /* If the target file already exists, do not rebuild it uselessly */
73 // TODO for now we assume it's complete. Might be a good idea to check
74 // at least if its range matches the trace's range.
75 if (htFile.exists()) {
76 /* Load an existing history */
a96cc6be
AM
77 final int version = (htInput == null) ?
78 IStateChangeInput.IGNORE_PROVIDER_VERSION :
79 htInput.getVersion();
6e71ce46 80 try {
a96cc6be 81 htBackend = new HistoryTreeBackend(htFile, version);
7e634be6 82 ITmfStateSystem ss = HistoryBuilder.openExistingHistory(htBackend);
6e71ce46
AM
83 return ss;
84 } catch (IOException e) {
85 /*
86 * There was an error opening the existing file. Perhaps it was
87 * corrupted, perhaps it's an old version? We'll just
88 * fall-through and try to build a new one from scratch instead.
89 */
90 }
91 }
92
93 /* Create a new state history from scratch */
94 HistoryBuilder builder;
51e216bd 95
6e71ce46
AM
96 if (htInput == null) {
97 return null;
98 }
99 try {
a96cc6be
AM
100 htBackend = new ThreadedHistoryTreeBackend(htFile,
101 htInput.getStartTime(), htInput.getVersion(), QUEUE_SIZE);
7e634be6
AM
102 StateSystem ss = new StateSystem(htBackend);
103 htInput.assignTargetStateSystem(ss);
104 builder = new HistoryBuilder(htInput, ss, htBackend, buildManually);
6e71ce46 105 } catch (IOException e) {
51e216bd
AM
106 /*
107 * If it fails here however, it means there was a problem writing to
108 * the disk, so throw a real exception this time.
6e71ce46
AM
109 */
110 throw new TmfTraceException(e.toString(), e);
111 }
112 return builder.getStateSystemQuerier();
113 }
f9a76cac
AM
114
115 /**
116 * Create a new state system using a null history back-end. This means that
117 * no history intervals will be saved anywhere, and as such only
118 * {@link ITmfStateSystem#queryOngoingState} will be available.
119 *
120 * This has to be built "manually" (which means you should call
121 * input.processEvent() to update the ongoing state of the state system).
122 *
123 * @param input
124 * The input plugin to build the history
125 * @return Reference to the history-less state system that got built
126 * @since 2.0
127 */
128 public static ITmfStateSystem newNullHistory(IStateChangeInput input) {
129 IStateHistoryBackend backend = new NullBackend();
7e634be6
AM
130 StateSystem ss = new StateSystem(backend);
131 input.assignTargetStateSystem(ss);
132
133 HistoryBuilder builder = new HistoryBuilder(input, ss, backend, true);
134 return builder.getStateSystemQuerier();
f9a76cac
AM
135 }
136
137 /**
138 * Create a new state system using in-memory interval storage. This should
139 * only be done for very small state system, and will be naturally limited
140 * to 2^31 intervals.
141 *
142 * This will block the caller while the construction is ongoing.
143 *
144 * @param input
145 * The state change input to use
146 * @param buildManually
147 * Set to true to block the caller and build without using TMF
148 * signals (for test programs most of the time). Use false if you
149 * are using the TMF facilities (experiments, etc.)
150 * @return Reference to the state system that just got built
151 * @since 2.0
152 */
153 public static ITmfStateSystem newInMemHistory(IStateChangeInput input,
154 boolean buildManually) {
155 IStateHistoryBackend backend = new InMemoryBackend(input.getStartTime());
7e634be6
AM
156 StateSystem ss = new StateSystem(backend);
157 input.assignTargetStateSystem(ss);
158
159 HistoryBuilder builder = new HistoryBuilder(input, ss, backend, buildManually);
f9a76cac
AM
160 return builder.getStateSystemQuerier();
161 }
1b9d3765
AM
162
163 /**
164 * Create a new state system backed with a partial history. A partial
165 * history is similar to a "full" one (which you get with
166 * {@link #loadStateHistory}), except that the file on disk is much smaller,
167 * but queries are a bit slower.
168 *
169 * Also note that single-queries are implemented using a full-query
170 * underneath, (which are much slower), so this might not be a good fit for
171 * a use case where you have to do lots of single queries.
172 *
173 * @param htFile
174 * The target file of the history. Since they are usually quick
175 * to build, it will overwrite any existing file, without trying
176 * to re-open it.
177 * @param realInput
178 * The state provider input to use to build this history.
179 * @param buildManually
180 * Indicates if you want to build the state system in-band
181 * ('true', for unit tests for example), or to not block the
182 * caller and start the build once the RangeUpdated signal.
183 * @return Reference to the newly constructed state system
184 * @throws TmfTraceException
185 * If the history file could not be created
186 * @since 2.0
187 */
188 public static ITmfStateSystem newPartialHistory(File htFile,
189 IStateChangeInput realInput, boolean buildManually) throws TmfTraceException {
190 /*
191 * The order of initializations is very tricky (but very important!)
192 * here. We need to follow this pattern:
193 * (1 is done before the call to this method)
194 *
195 * 1- Instantiate realInput
196 * 2- Instantiate realBackend
197 * 3- Instantiate partialBackend, whith prereqs:
198 * 3a- Instantiate partialInput, via realInput.getNew()
199 * 3b- Instantiate nullBackend (partialSS's backend)
200 * 3c- Instantiate partialSS
201 * 3d- partialInput.assignSS(partialSS)
202 * 4- Instantiate realSS
203 * 5- partialSS.assignUpstream(realSS)
204 * 6- realInput.assignSS(realSS)
205 * 7- Call HistoryBuilder(realInput, realSS, partialBackend) to build the thing.
206 */
207
208 final long granularity = 50000;
209
210 /* 2 */
211 IStateHistoryBackend realBackend = null;
212 try {
213 realBackend = new ThreadedHistoryTreeBackend(htFile,
214 realInput.getStartTime(), realInput.getVersion(), QUEUE_SIZE);
215 } catch (IOException e) {
216 throw new TmfTraceException(e.toString(), e);
217 }
218
219 /* 3a */
220 IStateChangeInput partialInput = realInput.getNewInstance();
221
222 /* 3b-3c, constructor automatically uses a NullBackend */
223 PartialStateSystem pss = new PartialStateSystem();
224
225 /* 3d */
226 partialInput.assignTargetStateSystem(pss);
227
228 /* 3 */
229 IStateHistoryBackend partialBackend =
230 new PartialHistoryBackend(partialInput, pss, realBackend, granularity);
231
232 /* 4 */
233 StateSystem realSS = new StateSystem(partialBackend);
234
235 /* 5 */
236 pss.assignUpstream(realSS);
237
238 /* 6 */
239 realInput.assignTargetStateSystem(realSS);
240
241 /* 7 */
242 HistoryBuilder builder = new HistoryBuilder(realInput, realSS, partialBackend, buildManually);
243 return builder.getStateSystemQuerier();
244 }
6e71ce46 245}
This page took 0.045977 seconds and 5 git commands to generate.