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