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.
33 * @author Alexandre Montplaisir
36 public class HistoryTreeBackend
implements IStateHistoryBackend
{
38 /** The history tree that sits underneath */
39 protected final HistoryTree sht
;
41 /** Indicates if the history tree construction is done */
42 protected boolean isFinishedBuilding
= false;
45 * Constructor for new history files. Use this when creating a new history
49 * The filename/location where to store the state history (Should
52 * The size of the blocks in the history file. This should be a
55 * The maximum number of children each core node can have
56 * @param providerVersion
57 * Version of of the state provider. We will only try to reopen
58 * existing files if this version matches the one in the
61 * The earliest time stamp that will be stored in the history
63 * Thrown if we can't create the file for some reason
65 public HistoryTreeBackend(File newStateFile
, int blockSize
,
66 int maxChildren
, int providerVersion
, long startTime
) throws IOException
{
67 final HTConfig conf
= new HTConfig(newStateFile
, blockSize
, maxChildren
,
68 providerVersion
, startTime
);
69 sht
= new HistoryTree(conf
);
73 * Constructor for new history files. Use this when creating a new history
74 * from scratch. This version supplies sane defaults for the configuration
78 * The filename/location where to store the state history (Should
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
85 * The earliest time stamp that will be stored in the history
87 * Thrown if we can't create the file for some reason
89 public HistoryTreeBackend(File newStateFile
, int providerVersion
, long startTime
)
91 this(newStateFile
, 64 * 1024, 50, providerVersion
, startTime
);
95 * Existing history constructor. Use this to open an existing state-file.
97 * @param existingStateFile
98 * Filename/location of the history we want to load
99 * @param providerVersion
100 * Expected version of of the state provider plugin.
101 * @throws IOException
102 * If we can't read the file, if it doesn't exist, is not
103 * recognized, or if the version of the file does not match the
104 * expected providerVersion.
106 public HistoryTreeBackend(File existingStateFile
, int providerVersion
)
108 sht
= new HistoryTree(existingStateFile
, providerVersion
);
109 isFinishedBuilding
= true;
113 public long getStartTime() {
114 return sht
.getTreeStart();
118 public long getEndTime() {
119 return sht
.getTreeEnd();
123 public void insertPastState(long stateStartTime
, long stateEndTime
,
124 int quark
, ITmfStateValue value
) throws TimeRangeException
{
125 HTInterval interval
= new HTInterval(stateStartTime
, stateEndTime
,
126 quark
, (TmfStateValue
) value
);
128 /* Start insertions at the "latest leaf" */
129 sht
.insertInterval(interval
);
133 public void finishedBuilding(long endTime
) {
134 sht
.closeTree(endTime
);
135 isFinishedBuilding
= true;
139 public FileInputStream
supplyAttributeTreeReader() {
140 return sht
.supplyATReader();
144 public File
supplyAttributeTreeWriterFile() {
145 return sht
.supplyATWriterFile();
149 public long supplyAttributeTreeWriterFilePosition() {
150 return sht
.supplyATWriterFilePos();
154 public void removeFiles() {
159 public void dispose() {
160 if (isFinishedBuilding
) {
164 * The build is being interrupted, delete the file we partially
165 * built since it won't be complete, so shouldn't be re-used in the
166 * future (.deleteFile() will close the file first)
173 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
)
174 throws TimeRangeException
, StateSystemDisposedException
{
175 if (!checkValidTime(t
)) {
176 /* We can't possibly have information about this query */
177 throw new TimeRangeException();
180 /* We start by reading the information in the root node */
181 // FIXME using CoreNode for now, we'll have to redo this part to handle
182 // different node types
183 CoreNode currentNode
= sht
.getLatestBranch().get(0);
184 currentNode
.writeInfoFromNode(stateInfo
, t
);
186 /* Then we follow the branch down in the relevant children */
188 while (currentNode
.getNbChildren() > 0) {
189 currentNode
= (CoreNode
) sht
.selectNextChild(currentNode
, t
);
190 currentNode
.writeInfoFromNode(stateInfo
, t
);
192 } catch (ClosedChannelException e
) {
193 throw new StateSystemDisposedException(e
);
197 * The stateInfo should now be filled with everything needed, we pass
198 * the control back to the State System.
204 public ITmfStateInterval
doSingularQuery(long t
, int attributeQuark
)
205 throws TimeRangeException
, StateSystemDisposedException
{
206 return getRelevantInterval(t
, attributeQuark
);
210 public boolean checkValidTime(long t
) {
211 return (t
>= sht
.getTreeStart() && t
<= sht
.getTreeEnd());
215 * Inner method to find the interval in the tree containing the requested
216 * key/timestamp pair, wherever in which node it is.
220 * @return The node containing the information we want
222 private HTInterval
getRelevantInterval(long t
, int key
)
223 throws TimeRangeException
, StateSystemDisposedException
{
224 if (!checkValidTime(t
)) {
225 throw new TimeRangeException();
228 // FIXME using CoreNode for now, we'll have to redo this part to handle
229 // different node types
230 CoreNode currentNode
= sht
.getLatestBranch().get(0);
231 HTInterval interval
= currentNode
.getRelevantInterval(key
, t
);
234 while (interval
== null && currentNode
.getNbChildren() > 0) {
235 currentNode
= (CoreNode
) sht
.selectNextChild(currentNode
, t
);
236 interval
= currentNode
.getRelevantInterval(key
, t
);
238 } catch (ClosedChannelException e
) {
239 throw new StateSystemDisposedException(e
);
242 * Since we should now have intervals at every attribute/timestamp
243 * combination, it should NOT be null here.
245 assert (interval
!= null);
250 * Return the size of the tree history file
252 * @return The current size of the history file in bytes
254 public long getFileSize() {
255 return sht
.getFileSize();
259 * Return the current depth of the tree, ie the number of node levels.
261 * @return The tree depth
263 public int getTreeDepth() {
264 return sht
.getLatestBranch().size();
268 * Return the average node usage as a percentage (between 0 and 100)
270 * @return Average node usage %
272 public int getAverageNodeUsage() {
278 for (int seq
= 0; seq
< sht
.getNodeCount(); seq
++) {
279 node
= sht
.readNode(seq
);
280 total
+= node
.getNodeUsagePRC();
282 } catch (ClosedChannelException e
) {
286 ret
= total
/ sht
.getNodeCount();
287 assert (ret
>= 0 && ret
<= 100);
292 public void debugPrint(PrintWriter writer
) {
293 /* By default don't print out all the intervals */
294 this.debugPrint(writer
, false);
298 * The basic debugPrint method will print the tree structure, but not their
301 * This method here print the contents (the intervals) as well.
304 * The PrintWriter to which the debug info will be written
305 * @param printIntervals
306 * Should we also print every contained interval individually?
308 public void debugPrint(PrintWriter writer
, boolean printIntervals
) {
309 /* Only used for debugging, shouldn't be externalized */
310 writer
.println("------------------------------"); //$NON-NLS-1$
311 writer
.println("State History Tree:\n"); //$NON-NLS-1$
312 writer
.println(sht
.toString());
313 writer
.println("Average node utilization: " //$NON-NLS-1$
314 + this.getAverageNodeUsage());
315 writer
.println(""); //$NON-NLS-1$
317 sht
.debugPrintFullTree(writer
, printIntervals
);