1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.historytree
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.IOException
;
18 import java
.io
.PrintWriter
;
19 import java
.nio
.channels
.ClosedChannelException
;
20 import java
.util
.List
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.IStateHistoryBackend
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateSystemDisposedException
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
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.
36 public class HistoryTreeBackend
implements IStateHistoryBackend
{
38 /** The history tree that sits underneath */
39 protected final HistoryTree sht
;
41 /** Direct reference to the tree's IO object */
42 private final HT_IO treeIO
;
44 /** Indicates if the history tree construction is done */
45 protected boolean isFinishedBuilding
= false;
48 * Constructor for new history files. Use this when creating a new history
52 * The filename/location where to store the state history (Should
55 * The size of the blocks in the history file. This should be a
58 * The maximum number of children each core node can have
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
64 * The earliest time stamp that will be stored in the history
66 * Thrown if we can't create the file for some reason
68 public HistoryTreeBackend(File newStateFile
, int blockSize
,
69 int maxChildren
, int providerVersion
, long startTime
) throws IOException
{
70 final HTConfig conf
= new HTConfig(newStateFile
, blockSize
, maxChildren
,
71 providerVersion
, startTime
);
72 sht
= new HistoryTree(conf
);
73 treeIO
= sht
.getTreeIO();
77 * Constructor for new history files. Use this when creating a new history
78 * from scratch. This version supplies sane defaults for the configuration
82 * The filename/location where to store the state history (Should
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
89 * The earliest time stamp that will be stored in the history
91 * Thrown if we can't create the file for some reason
93 public HistoryTreeBackend(File newStateFile
, int providerVersion
, long startTime
)
95 this(newStateFile
, 64 * 1024, 50, providerVersion
, startTime
);
99 * Existing history constructor. Use this to open an existing state-file.
101 * @param existingStateFile
102 * Filename/location of the history we want to load
103 * @param providerVersion
104 * Expected version of of the state provider plugin.
105 * @throws IOException
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.
110 public HistoryTreeBackend(File existingStateFile
, int providerVersion
)
112 sht
= new HistoryTree(existingStateFile
, providerVersion
);
113 treeIO
= sht
.getTreeIO();
114 isFinishedBuilding
= true;
118 public long getStartTime() {
119 return sht
.getTreeStart();
123 public long getEndTime() {
124 return sht
.getTreeEnd();
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
);
133 /* Start insertions at the "latest leaf" */
134 sht
.insertInterval(interval
);
138 public void finishedBuilding(long endTime
) {
139 sht
.closeTree(endTime
);
140 isFinishedBuilding
= true;
144 public FileInputStream
supplyAttributeTreeReader() {
145 return treeIO
.supplyATReader();
149 public File
supplyAttributeTreeWriterFile() {
150 return treeIO
.supplyATWriterFile();
154 public long supplyAttributeTreeWriterFilePosition() {
155 return treeIO
.supplyATWriterFilePos();
159 public void removeFiles() {
164 public void dispose() {
165 if (isFinishedBuilding
) {
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)
178 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
)
179 throws TimeRangeException
, StateSystemDisposedException
{
180 if (!checkValidTime(t
)) {
181 /* We can't possibly have information about this query */
182 throw new TimeRangeException();
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
188 CoreNode currentNode
= sht
.latestBranch
.firstElement();
189 currentNode
.writeInfoFromNode(stateInfo
, t
);
191 /* Then we follow the branch down in the relevant children */
193 while (currentNode
.getNbChildren() > 0) {
194 currentNode
= (CoreNode
) sht
.selectNextChild(currentNode
, t
);
195 currentNode
.writeInfoFromNode(stateInfo
, t
);
197 } catch (ClosedChannelException e
) {
198 throw new StateSystemDisposedException();
202 * The stateInfo should now be filled with everything needed, we pass
203 * the control back to the State System.
209 public ITmfStateInterval
doSingularQuery(long t
, int attributeQuark
)
210 throws TimeRangeException
, StateSystemDisposedException
{
211 return getRelevantInterval(t
, attributeQuark
);
215 public boolean checkValidTime(long t
) {
216 return (t
>= sht
.getTreeStart() && t
<= sht
.getTreeEnd());
220 * Inner method to find the interval in the tree containing the requested
221 * key/timestamp pair, wherever in which node it is.
225 * @return The node containing the information we want
227 private HTInterval
getRelevantInterval(long t
, int key
)
228 throws TimeRangeException
, StateSystemDisposedException
{
229 if (!checkValidTime(t
)) {
230 throw new TimeRangeException();
233 // FIXME using CoreNode for now, we'll have to redo this part to handle
234 // different node types
235 CoreNode currentNode
= sht
.latestBranch
.firstElement();
236 HTInterval interval
= currentNode
.getRelevantInterval(key
, t
);
239 while (interval
== null && currentNode
.getNbChildren() > 0) {
240 currentNode
= (CoreNode
) sht
.selectNextChild(currentNode
, t
);
241 interval
= currentNode
.getRelevantInterval(key
, t
);
243 } catch (ClosedChannelException e
) {
244 throw new StateSystemDisposedException();
247 * Since we should now have intervals at every attribute/timestamp
248 * combination, it should NOT be null here.
250 assert (interval
!= null);
255 * Return the size of the tree history file
257 * @return The current size of the history file in bytes
259 public long getFileSize() {
260 return sht
.getFileSize();
264 * Return the current depth of the tree, ie the number of node levels.
266 * @return The tree depth
268 public int getTreeDepth() {
269 return sht
.latestBranch
.size();
273 * Return the average node usage as a percentage (between 0 and 100)
275 * @return Average node usage %
277 public int getAverageNodeUsage() {
283 for (int seq
= 0; seq
< sht
.getNodeCount(); seq
++) {
284 node
= treeIO
.readNode(seq
);
285 total
+= node
.getNodeUsagePRC();
287 } catch (ClosedChannelException e
) {
291 ret
= total
/ sht
.getNodeCount();
292 assert (ret
>= 0 && ret
<= 100);
297 public void debugPrint(PrintWriter writer
) {
298 /* By default don't print out all the intervals */
299 this.debugPrint(writer
, false);
303 * The basic debugPrint method will print the tree structure, but not their
306 * This method here print the contents (the intervals) as well.
309 * The PrintWriter to which the debug info will be written
310 * @param printIntervals
311 * Should we also print every contained interval individually?
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$
322 sht
.debugPrintFullTree(writer
, printIntervals
);