1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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
.tracecompass
.statesystem
.core
.backend
.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
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.CoreNode
;
23 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTConfig
;
24 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTInterval
;
25 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTNode
;
26 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HistoryTree
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
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.
38 * @author Alexandre Montplaisir
41 public class HistoryTreeBackend
implements IStateHistoryBackend
{
44 * The history tree that sits underneath.
46 * Using default visibility to only allow {@link ThreadedHistoryTreeBackend}
49 final HistoryTree sht
;
51 /** Indicates if the history tree construction is done */
52 protected boolean isFinishedBuilding
= false;
55 * Constructor for new history files. Use this when creating a new history
59 * The filename/location where to store the state history (Should
62 * The size of the blocks in the history file. This should be a
65 * The maximum number of children each core node can have
66 * @param providerVersion
67 * Version of of the state provider. We will only try to reopen
68 * existing files if this version matches the one in the
71 * The earliest time stamp that will be stored in the history
73 * Thrown if we can't create the file for some reason
75 public HistoryTreeBackend(File newStateFile
, int blockSize
,
76 int maxChildren
, int providerVersion
, long startTime
) throws IOException
{
77 final HTConfig conf
= new HTConfig(newStateFile
, blockSize
, maxChildren
,
78 providerVersion
, startTime
);
79 sht
= new HistoryTree(conf
);
83 * Constructor for new history files. Use this when creating a new history
84 * from scratch. This version supplies sane defaults for the configuration
88 * The filename/location where to store the state history (Should
90 * @param providerVersion
91 * Version of of the state provider. We will only try to reopen
92 * existing files if this version matches the one in the
95 * The earliest time stamp that will be stored in the history
97 * Thrown if we can't create the file for some reason
99 public HistoryTreeBackend(File newStateFile
, int providerVersion
, long startTime
)
101 this(newStateFile
, 64 * 1024, 50, providerVersion
, startTime
);
105 * Existing history constructor. Use this to open an existing state-file.
107 * @param existingStateFile
108 * Filename/location of the history we want to load
109 * @param providerVersion
110 * Expected version of of the state provider plugin.
111 * @throws IOException
112 * If we can't read the file, if it doesn't exist, is not
113 * recognized, or if the version of the file does not match the
114 * expected providerVersion.
116 public HistoryTreeBackend(File existingStateFile
, int providerVersion
)
118 sht
= new HistoryTree(existingStateFile
, providerVersion
);
119 isFinishedBuilding
= true;
123 public long getStartTime() {
124 return sht
.getTreeStart();
128 public long getEndTime() {
129 return sht
.getTreeEnd();
133 public void insertPastState(long stateStartTime
, long stateEndTime
,
134 int quark
, ITmfStateValue value
) throws TimeRangeException
{
135 HTInterval interval
= new HTInterval(stateStartTime
, stateEndTime
,
136 quark
, (TmfStateValue
) value
);
138 /* Start insertions at the "latest leaf" */
139 sht
.insertInterval(interval
);
143 public void finishedBuilding(long endTime
) {
144 sht
.closeTree(endTime
);
145 isFinishedBuilding
= true;
149 public FileInputStream
supplyAttributeTreeReader() {
150 return sht
.supplyATReader();
154 public File
supplyAttributeTreeWriterFile() {
155 return sht
.supplyATWriterFile();
159 public long supplyAttributeTreeWriterFilePosition() {
160 return sht
.supplyATWriterFilePos();
164 public void removeFiles() {
169 public void dispose() {
170 if (isFinishedBuilding
) {
174 * The build is being interrupted, delete the file we partially
175 * built since it won't be complete, so shouldn't be re-used in the
176 * future (.deleteFile() will close the file first)
183 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
)
184 throws TimeRangeException
, StateSystemDisposedException
{
185 if (!checkValidTime(t
)) {
186 /* We can't possibly have information about this query */
187 throw new TimeRangeException();
190 /* We start by reading the information in the root node */
191 HTNode currentNode
= sht
.getRootNode();
192 currentNode
.writeInfoFromNode(stateInfo
, t
);
194 /* Then we follow the branch down in the relevant children */
196 while (currentNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
197 currentNode
= sht
.selectNextChild((CoreNode
) currentNode
, t
);
198 currentNode
.writeInfoFromNode(stateInfo
, t
);
200 } catch (ClosedChannelException e
) {
201 throw new StateSystemDisposedException(e
);
205 * The stateInfo should now be filled with everything needed, we pass
206 * the control back to the State System.
212 public ITmfStateInterval
doSingularQuery(long t
, int attributeQuark
)
213 throws TimeRangeException
, StateSystemDisposedException
{
214 return getRelevantInterval(t
, attributeQuark
);
217 private boolean checkValidTime(long t
) {
218 return (t
>= sht
.getTreeStart() && t
<= sht
.getTreeEnd());
222 * Inner method to find the interval in the tree containing the requested
223 * key/timestamp pair, wherever in which node it is.
227 * @return The node containing the information we want
229 private HTInterval
getRelevantInterval(long t
, int key
)
230 throws TimeRangeException
, StateSystemDisposedException
{
231 if (!checkValidTime(t
)) {
232 throw new TimeRangeException();
235 HTNode currentNode
= sht
.getRootNode();
236 HTInterval interval
= currentNode
.getRelevantInterval(key
, t
);
239 while (interval
== null && currentNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
240 currentNode
= sht
.selectNextChild((CoreNode
)currentNode
, t
);
241 interval
= currentNode
.getRelevantInterval(key
, t
);
243 } catch (ClosedChannelException e
) {
244 throw new StateSystemDisposedException(e
);
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 average node usage as a percentage (between 0 and 100)
266 * @return Average node usage %
268 public int getAverageNodeUsage() {
274 for (int seq
= 0; seq
< sht
.getNodeCount(); seq
++) {
275 node
= sht
.readNode(seq
);
276 total
+= node
.getNodeUsagePercent();
278 } catch (ClosedChannelException e
) {
282 ret
= total
/ sht
.getNodeCount();
283 assert (ret
>= 0 && ret
<= 100);
288 public void debugPrint(PrintWriter writer
) {
289 /* By default don't print out all the intervals */
290 this.debugPrint(writer
, false);
294 * The basic debugPrint method will print the tree structure, but not their
297 * This method here print the contents (the intervals) as well.
300 * The PrintWriter to which the debug info will be written
301 * @param printIntervals
302 * Should we also print every contained interval individually?
304 public void debugPrint(PrintWriter writer
, boolean printIntervals
) {
305 /* Only used for debugging, shouldn't be externalized */
306 writer
.println("------------------------------"); //$NON-NLS-1$
307 writer
.println("State History Tree:\n"); //$NON-NLS-1$
308 writer
.println(sht
.toString());
309 writer
.println("Average node utilization: " //$NON-NLS-1$
310 + this.getAverageNodeUsage());
311 writer
.println(""); //$NON-NLS-1$
313 sht
.debugPrintFullTree(writer
, printIntervals
);