Commit | Line | Data |
---|---|---|
6e71ce46 AM |
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> | |
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 | ||
13 | package org.eclipse.linuxtools.tmf.core.statesystem; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.IOException; | |
17 | ||
2ab9afbc | 18 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.HistoryBuilder; |
7e634be6 | 19 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateSystem; |
f9a76cac AM |
20 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend; |
21 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.InMemoryBackend; | |
22 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.NullBackend; | |
23 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.HistoryTreeBackend; | |
24 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.ThreadedHistoryTreeBackend; | |
6e71ce46 AM |
25 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent; |
26 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
6e71ce46 AM |
27 | |
28 | /** | |
29 | * This abstract manager class handles loading or creating state history files | |
30 | * for use in TMF's generic state system. | |
83134537 | 31 | * |
2cb26548 AM |
32 | * @version 1.0 |
33 | * @author Alexandre Montplaisir | |
6e71ce46 AM |
34 | */ |
35 | public abstract class StateSystemManager extends TmfComponent { | |
36 | ||
37 | /** Size of the blocking queue to use when building a state history */ | |
38 | private final static int QUEUE_SIZE = 10000; | |
39 | ||
40 | /** | |
41 | * Load the history file matching the target trace. If the file already | |
42 | * exists, it will be opened directly. If not, it will be created from | |
43 | * scratch. In the case the history has to be built, it's possible to block | |
44 | * the calling thread until construction is complete. | |
83134537 | 45 | * |
6e71ce46 AM |
46 | * @param htFile |
47 | * The target name of the history file we want to use. If it | |
48 | * exists it will be opened. If it doesn't, a new file will be | |
49 | * created with this name/path. | |
50 | * @param htInput | |
51 | * The IStateChangeInput to use for building the history file. It | |
52 | * may be required even if we are opening an already-existing | |
53 | * history (ie, for partial histories). | |
e34f7caa AM |
54 | * @param buildManually |
55 | * If false, the construction will wait for a signal before | |
56 | * starting. If true, it will build everything right now and | |
57 | * block the caller. It has no effect if the file already exists. | |
6e71ce46 AM |
58 | * @return A IStateSystemQuerier handler to the state system, with which you |
59 | * can then run queries on the history. | |
60 | * @throws TmfTraceException | |
5df842b3 AM |
61 | * If there was a problem reading or writing one of the files. |
62 | * See the contents of this exception for more info. | |
f1f86dfb | 63 | * @since 2.0 |
6e71ce46 | 64 | */ |
f1f86dfb | 65 | public static ITmfStateSystem loadStateHistory(File htFile, |
e45de797 | 66 | IStateChangeInput htInput, boolean buildManually) |
51e216bd | 67 | throws TmfTraceException { |
6e71ce46 | 68 | IStateHistoryBackend htBackend; |
51e216bd | 69 | |
6e71ce46 AM |
70 | /* If the target file already exists, do not rebuild it uselessly */ |
71 | // TODO for now we assume it's complete. Might be a good idea to check | |
72 | // at least if its range matches the trace's range. | |
73 | if (htFile.exists()) { | |
74 | /* Load an existing history */ | |
a96cc6be AM |
75 | final int version = (htInput == null) ? |
76 | IStateChangeInput.IGNORE_PROVIDER_VERSION : | |
77 | htInput.getVersion(); | |
6e71ce46 | 78 | try { |
a96cc6be | 79 | htBackend = new HistoryTreeBackend(htFile, version); |
7e634be6 | 80 | ITmfStateSystem ss = HistoryBuilder.openExistingHistory(htBackend); |
6e71ce46 AM |
81 | return ss; |
82 | } catch (IOException e) { | |
83 | /* | |
84 | * There was an error opening the existing file. Perhaps it was | |
85 | * corrupted, perhaps it's an old version? We'll just | |
86 | * fall-through and try to build a new one from scratch instead. | |
87 | */ | |
88 | } | |
89 | } | |
90 | ||
91 | /* Create a new state history from scratch */ | |
92 | HistoryBuilder builder; | |
51e216bd | 93 | |
6e71ce46 AM |
94 | if (htInput == null) { |
95 | return null; | |
96 | } | |
97 | try { | |
a96cc6be AM |
98 | htBackend = new ThreadedHistoryTreeBackend(htFile, |
99 | htInput.getStartTime(), htInput.getVersion(), QUEUE_SIZE); | |
7e634be6 AM |
100 | StateSystem ss = new StateSystem(htBackend); |
101 | htInput.assignTargetStateSystem(ss); | |
102 | builder = new HistoryBuilder(htInput, ss, htBackend, buildManually); | |
6e71ce46 | 103 | } catch (IOException e) { |
51e216bd AM |
104 | /* |
105 | * If it fails here however, it means there was a problem writing to | |
106 | * the disk, so throw a real exception this time. | |
6e71ce46 AM |
107 | */ |
108 | throw new TmfTraceException(e.toString(), e); | |
109 | } | |
110 | return builder.getStateSystemQuerier(); | |
111 | } | |
f9a76cac AM |
112 | |
113 | /** | |
114 | * Create a new state system using a null history back-end. This means that | |
115 | * no history intervals will be saved anywhere, and as such only | |
116 | * {@link ITmfStateSystem#queryOngoingState} will be available. | |
117 | * | |
118 | * This has to be built "manually" (which means you should call | |
119 | * input.processEvent() to update the ongoing state of the state system). | |
120 | * | |
121 | * @param input | |
122 | * The input plugin to build the history | |
123 | * @return Reference to the history-less state system that got built | |
124 | * @since 2.0 | |
125 | */ | |
126 | public static ITmfStateSystem newNullHistory(IStateChangeInput input) { | |
127 | IStateHistoryBackend backend = new NullBackend(); | |
7e634be6 AM |
128 | StateSystem ss = new StateSystem(backend); |
129 | input.assignTargetStateSystem(ss); | |
130 | ||
131 | HistoryBuilder builder = new HistoryBuilder(input, ss, backend, true); | |
132 | return builder.getStateSystemQuerier(); | |
f9a76cac AM |
133 | } |
134 | ||
135 | /** | |
136 | * Create a new state system using in-memory interval storage. This should | |
137 | * only be done for very small state system, and will be naturally limited | |
138 | * to 2^31 intervals. | |
139 | * | |
140 | * This will block the caller while the construction is ongoing. | |
141 | * | |
142 | * @param input | |
143 | * The state change input to use | |
144 | * @param buildManually | |
145 | * Set to true to block the caller and build without using TMF | |
146 | * signals (for test programs most of the time). Use false if you | |
147 | * are using the TMF facilities (experiments, etc.) | |
148 | * @return Reference to the state system that just got built | |
149 | * @since 2.0 | |
150 | */ | |
151 | public static ITmfStateSystem newInMemHistory(IStateChangeInput input, | |
152 | boolean buildManually) { | |
153 | IStateHistoryBackend backend = new InMemoryBackend(input.getStartTime()); | |
7e634be6 AM |
154 | StateSystem ss = new StateSystem(backend); |
155 | input.assignTargetStateSystem(ss); | |
156 | ||
157 | HistoryBuilder builder = new HistoryBuilder(input, ss, backend, buildManually); | |
f9a76cac AM |
158 | return builder.getStateSystemQuerier(); |
159 | } | |
6e71ce46 | 160 | } |