tmf: Fix incorrect TimeGraphFilterDialog result when dialog shell closed
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / HistoryTreeBackend.java
CommitLineData
a52fde77 1/*******************************************************************************
e13bd4cd 2 * Copyright (c) 2012, 2015 Ericsson
a52fde77
AM
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5df842b3 5 *
a52fde77
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
5df842b3 10 *
e13bd4cd
PT
11 * Contributors:
12 * Alexandre Montplaisir - Initial API and implementation
13 * Patrick Tasse - Add message to exceptions
a52fde77
AM
14 *******************************************************************************/
15
0306a843 16package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree;
a52fde77
AM
17
18import java.io.File;
19import java.io.FileInputStream;
20import java.io.IOException;
21import java.io.PrintWriter;
3b7f5abe 22import java.nio.channels.ClosedChannelException;
a52fde77
AM
23import java.util.List;
24
b2f62cb5 25import org.eclipse.jdt.annotation.NonNull;
e894a508
AM
26import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.CoreNode;
27import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTConfig;
28import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTInterval;
29import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTNode;
30import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTree;
31import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
32import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
33import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
34import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
35import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
36import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
a52fde77
AM
37
38/**
39 * History Tree backend for storing a state history. This is the basic version
40 * that runs in the same thread as the class creating it.
5df842b3 41 *
360f899e 42 * @author Alexandre Montplaisir
a52fde77
AM
43 */
44public class HistoryTreeBackend implements IStateHistoryBackend {
45
b2f62cb5
AM
46 private final @NonNull String ssid;
47
bcec0116
AM
48 /**
49 * The history tree that sits underneath.
bcec0116 50 */
e62a23a9 51 private final HistoryTree sht;
6f4e8ec0 52
1a4205d9 53 /** Indicates if the history tree construction is done */
d740e018
MK
54 private volatile boolean fFinishedBuilding = false;
55
56 /**
57 * Indicates if the history tree construction is done
58 *
59 * @return if the history tree construction is done
60 */
61 protected boolean isFinishedBuilding() {
62 return fFinishedBuilding;
63 }
64
65 /**
66 * Sets if the history tree is finished building
67 *
68 * @param isFinishedBuilding
69 * is the history tree finished building
70 */
71 protected void setFinishedBuilding(boolean isFinishedBuilding) {
72 this.fFinishedBuilding = isFinishedBuilding;
73 }
1a4205d9 74
a52fde77 75 /**
7453b40e 76 * Constructor for new history files. Use this when creating a new history
a52fde77 77 * from scratch.
5df842b3 78 *
b2f62cb5
AM
79 * @param ssid
80 * The state system's ID
a52fde77
AM
81 * @param newStateFile
82 * The filename/location where to store the state history (Should
83 * end in .ht)
a96cc6be
AM
84 * @param providerVersion
85 * Version of of the state provider. We will only try to reopen
86 * existing files if this version matches the one in the
87 * framework.
a52fde77
AM
88 * @param startTime
89 * The earliest time stamp that will be stored in the history
f6d24a71
AM
90 * @param blockSize
91 * The size of the blocks in the history file. This should be a
92 * multiple of 4096.
93 * @param maxChildren
94 * The maximum number of children each core node can have
a52fde77
AM
95 * @throws IOException
96 * Thrown if we can't create the file for some reason
97 */
f6d24a71
AM
98 public HistoryTreeBackend(@NonNull String ssid,
99 File newStateFile,
100 int providerVersion,
101 long startTime,
102 int blockSize,
103 int maxChildren) throws IOException {
b2f62cb5 104 this.ssid = ssid;
a96cc6be
AM
105 final HTConfig conf = new HTConfig(newStateFile, blockSize, maxChildren,
106 providerVersion, startTime);
7453b40e 107 sht = new HistoryTree(conf);
a52fde77
AM
108 }
109
110 /**
7453b40e 111 * Constructor for new history files. Use this when creating a new history
a52fde77
AM
112 * from scratch. This version supplies sane defaults for the configuration
113 * parameters.
5df842b3 114 *
b2f62cb5
AM
115 * @param ssid
116 * The state system's id
a52fde77
AM
117 * @param newStateFile
118 * The filename/location where to store the state history (Should
119 * end in .ht)
a96cc6be
AM
120 * @param providerVersion
121 * Version of of the state provider. We will only try to reopen
122 * existing files if this version matches the one in the
123 * framework.
a52fde77
AM
124 * @param startTime
125 * The earliest time stamp that will be stored in the history
126 * @throws IOException
127 * Thrown if we can't create the file for some reason
dbc7991d 128 * @since 1.0
a52fde77 129 */
b2f62cb5 130 public HistoryTreeBackend(@NonNull String ssid, File newStateFile, int providerVersion, long startTime)
a52fde77 131 throws IOException {
f6d24a71 132 this(ssid, newStateFile, providerVersion, startTime, 64 * 1024, 50);
a52fde77
AM
133 }
134
135 /**
136 * Existing history constructor. Use this to open an existing state-file.
5df842b3 137 *
b2f62cb5
AM
138 * @param ssid
139 * The state system's id
0d9a6d76 140 * @param existingStateFile
b255ae4b 141 * Filename/location of the history we want to load
a96cc6be
AM
142 * @param providerVersion
143 * Expected version of of the state provider plugin.
b255ae4b 144 * @throws IOException
a96cc6be
AM
145 * If we can't read the file, if it doesn't exist, is not
146 * recognized, or if the version of the file does not match the
147 * expected providerVersion.
a52fde77 148 */
b2f62cb5 149 public HistoryTreeBackend(@NonNull String ssid, File existingStateFile, int providerVersion)
a96cc6be 150 throws IOException {
b2f62cb5 151 this.ssid = ssid;
a96cc6be 152 sht = new HistoryTree(existingStateFile, providerVersion);
d740e018 153 fFinishedBuilding = true;
a52fde77
AM
154 }
155
e62a23a9
AM
156 /**
157 * Get the History Tree built by this backend.
158 *
159 * @return The history tree
160 */
161 protected HistoryTree getSHT() {
162 return sht;
163 }
164
b2f62cb5
AM
165 @Override
166 public String getSSID() {
167 return ssid;
168 }
169
a52fde77
AM
170 @Override
171 public long getStartTime() {
172 return sht.getTreeStart();
173 }
174
175 @Override
176 public long getEndTime() {
177 return sht.getTreeEnd();
178 }
179
180 @Override
181 public void insertPastState(long stateStartTime, long stateEndTime,
182 int quark, ITmfStateValue value) throws TimeRangeException {
183 HTInterval interval = new HTInterval(stateStartTime, stateEndTime,
184 quark, (TmfStateValue) value);
185
186 /* Start insertions at the "latest leaf" */
187 sht.insertInterval(interval);
188 }
189
190 @Override
b33c7369 191 public void finishedBuilding(long endTime) {
6a1074ce 192 sht.closeTree(endTime);
d740e018 193 fFinishedBuilding = true;
a52fde77
AM
194 }
195
196 @Override
197 public FileInputStream supplyAttributeTreeReader() {
360f899e 198 return sht.supplyATReader();
a52fde77
AM
199 }
200
201 @Override
202 public File supplyAttributeTreeWriterFile() {
360f899e 203 return sht.supplyATWriterFile();
a52fde77
AM
204 }
205
206 @Override
207 public long supplyAttributeTreeWriterFilePosition() {
360f899e 208 return sht.supplyATWriterFilePos();
a52fde77
AM
209 }
210
36bf82a2
AM
211 @Override
212 public void removeFiles() {
360f899e 213 sht.deleteFile();
36bf82a2
AM
214 }
215
1a4205d9
AM
216 @Override
217 public void dispose() {
d740e018 218 if (fFinishedBuilding) {
360f899e 219 sht.closeFile();
1a4205d9
AM
220 } else {
221 /*
222 * The build is being interrupted, delete the file we partially
223 * built since it won't be complete, so shouldn't be re-used in the
224 * future (.deleteFile() will close the file first)
225 */
360f899e 226 sht.deleteFile();
1a4205d9
AM
227 }
228 }
229
a52fde77
AM
230 @Override
231 public void doQuery(List<ITmfStateInterval> stateInfo, long t)
3b7f5abe 232 throws TimeRangeException, StateSystemDisposedException {
e13bd4cd 233 checkValidTime(t);
a52fde77
AM
234
235 /* We start by reading the information in the root node */
bb7f92ce 236 HTNode currentNode = sht.getRootNode();
a52fde77
AM
237 currentNode.writeInfoFromNode(stateInfo, t);
238
239 /* Then we follow the branch down in the relevant children */
3b7f5abe 240 try {
bb7f92ce
FW
241 while (currentNode.getNodeType() == HTNode.NodeType.CORE) {
242 currentNode = sht.selectNextChild((CoreNode) currentNode, t);
3b7f5abe
AM
243 currentNode.writeInfoFromNode(stateInfo, t);
244 }
245 } catch (ClosedChannelException e) {
cb42195c 246 throw new StateSystemDisposedException(e);
a52fde77
AM
247 }
248
249 /*
250 * The stateInfo should now be filled with everything needed, we pass
251 * the control back to the State System.
252 */
253 return;
254 }
255
256 @Override
257 public ITmfStateInterval doSingularQuery(long t, int attributeQuark)
3b7f5abe 258 throws TimeRangeException, StateSystemDisposedException {
a52fde77
AM
259 return getRelevantInterval(t, attributeQuark);
260 }
261
e13bd4cd
PT
262 private void checkValidTime(long t) {
263 long treeStart = sht.getTreeStart();
264 long treeEnd = sht.getTreeEnd();
265 if (t < treeStart || t > treeEnd) {
266 throw new TimeRangeException(ssid + " Time:" + t + ", Start:" + treeStart + ", End:" + treeEnd); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
267 }
a52fde77
AM
268 }
269
270 /**
271 * Inner method to find the interval in the tree containing the requested
272 * key/timestamp pair, wherever in which node it is.
5df842b3 273 *
a52fde77
AM
274 * @param t
275 * @param key
276 * @return The node containing the information we want
277 */
278 private HTInterval getRelevantInterval(long t, int key)
3b7f5abe 279 throws TimeRangeException, StateSystemDisposedException {
e13bd4cd 280 checkValidTime(t);
a52fde77 281
bb7f92ce 282 HTNode currentNode = sht.getRootNode();
a52fde77
AM
283 HTInterval interval = currentNode.getRelevantInterval(key, t);
284
3b7f5abe 285 try {
bb7f92ce 286 while (interval == null && currentNode.getNodeType() == HTNode.NodeType.CORE) {
d740e018 287 currentNode = sht.selectNextChild((CoreNode) currentNode, t);
3b7f5abe
AM
288 interval = currentNode.getRelevantInterval(key, t);
289 }
290 } catch (ClosedChannelException e) {
cb42195c 291 throw new StateSystemDisposedException(e);
a52fde77 292 }
a52fde77
AM
293 return interval;
294 }
295
296 /**
297 * Return the size of the tree history file
5df842b3 298 *
a52fde77
AM
299 * @return The current size of the history file in bytes
300 */
301 public long getFileSize() {
302 return sht.getFileSize();
303 }
304
a52fde77
AM
305 /**
306 * Return the average node usage as a percentage (between 0 and 100)
5df842b3 307 *
a52fde77
AM
308 * @return Average node usage %
309 */
310 public int getAverageNodeUsage() {
311 HTNode node;
312 long total = 0;
313 long ret;
314
3b7f5abe
AM
315 try {
316 for (int seq = 0; seq < sht.getNodeCount(); seq++) {
360f899e 317 node = sht.readNode(seq);
8d47cc34 318 total += node.getNodeUsagePercent();
3b7f5abe
AM
319 }
320 } catch (ClosedChannelException e) {
321 e.printStackTrace();
a52fde77
AM
322 }
323
324 ret = total / sht.getNodeCount();
325 assert (ret >= 0 && ret <= 100);
326 return (int) ret;
327 }
328
329 @Override
330 public void debugPrint(PrintWriter writer) {
331 /* By default don't print out all the intervals */
332 this.debugPrint(writer, false);
333 }
334
335 /**
b255ae4b
AM
336 * The basic debugPrint method will print the tree structure, but not their
337 * contents.
5df842b3 338 *
a52fde77 339 * This method here print the contents (the intervals) as well.
5df842b3 340 *
a52fde77 341 * @param writer
5df842b3 342 * The PrintWriter to which the debug info will be written
a52fde77 343 * @param printIntervals
5df842b3 344 * Should we also print every contained interval individually?
a52fde77
AM
345 */
346 public void debugPrint(PrintWriter writer, boolean printIntervals) {
347 /* Only used for debugging, shouldn't be externalized */
348 writer.println("------------------------------"); //$NON-NLS-1$
349 writer.println("State History Tree:\n"); //$NON-NLS-1$
350 writer.println(sht.toString());
351 writer.println("Average node utilization: " //$NON-NLS-1$
352 + this.getAverageNodeUsage());
353 writer.println(""); //$NON-NLS-1$
354
355 sht.debugPrintFullTree(writer, printIntervals);
356 }
357}
This page took 0.089591 seconds and 5 git commands to generate.