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