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