0e119a95c70cf85fc03808d5989ade07d5f44240
1 /*******************************************************************************
2 * Copyright (c) 2012 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
.historytree
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.IOException
;
18 import java
.io
.PrintWriter
;
19 import java
.util
.List
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.IStateHistoryBackend
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
28 * History Tree backend for storing a state history. This is the basic version
29 * that runs in the same thread as the class creating it.
34 public class HistoryTreeBackend
implements IStateHistoryBackend
{
36 protected final HistoryTree sht
;
37 private final HT_IO treeIO
;
40 * Construtor for new history files. Use this when creating a new history
44 * The filename/location where to store the state history (Should
47 * The size of the blocks in the history file. This should be a
50 * The maximum number of children each core node can have
52 * The earliest time stamp that will be stored in the history
54 * Thrown if we can't create the file for some reason
56 public HistoryTreeBackend(File newStateFile
, int blockSize
,
57 int maxChildren
, long startTime
) throws IOException
{
58 sht
= new HistoryTree(newStateFile
, blockSize
, maxChildren
, startTime
);
59 treeIO
= sht
.getTreeIO();
63 * Construtor for new history files. Use this when creating a new history
64 * from scratch. This version supplies sane defaults for the configuration
68 * The filename/location where to store the state history (Should
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
, long startTime
)
77 this(newStateFile
, 64 * 1024, 50, startTime
);
81 * Existing history constructor. Use this to open an existing state-file.
83 * @param existingStateFile
84 * Filename/location of the history we want to load
86 * If we can't read the file, if it doesn't exist or is not
89 public HistoryTreeBackend(File existingStateFile
) throws IOException
{
90 sht
= new HistoryTree(existingStateFile
);
91 treeIO
= sht
.getTreeIO();
95 public long getStartTime() {
96 return sht
.getTreeStart();
100 public long getEndTime() {
101 return sht
.getTreeEnd();
105 public void insertPastState(long stateStartTime
, long stateEndTime
,
106 int quark
, ITmfStateValue value
) throws TimeRangeException
{
107 HTInterval interval
= new HTInterval(stateStartTime
, stateEndTime
,
108 quark
, (TmfStateValue
) value
);
110 /* Start insertions at the "latest leaf" */
111 sht
.insertInterval(interval
);
115 public void finishedBuilding(long endTime
) {
116 sht
.closeTree(endTime
);
120 public FileInputStream
supplyAttributeTreeReader() {
121 return treeIO
.supplyATReader();
125 public File
supplyAttributeTreeWriterFile() {
126 return treeIO
.supplyATWriterFile();
130 public long supplyAttributeTreeWriterFilePosition() {
131 return treeIO
.supplyATWriterFilePos();
135 public void removeFiles() {
140 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
)
141 throws TimeRangeException
{
142 if (!checkValidTime(t
)) {
143 /* We can't possibly have information about this query */
144 throw new TimeRangeException();
147 /* We start by reading the information in the root node */
148 // FIXME using CoreNode for now, we'll have to redo this part to handle
149 // different node types
150 CoreNode currentNode
= sht
.latestBranch
.firstElement();
151 currentNode
.writeInfoFromNode(stateInfo
, t
);
153 /* Then we follow the branch down in the relevant children */
154 while (currentNode
.getNbChildren() > 0) {
155 currentNode
= (CoreNode
) sht
.selectNextChild(currentNode
, t
);
156 currentNode
.writeInfoFromNode(stateInfo
, t
);
160 * The stateInfo should now be filled with everything needed, we pass
161 * the control back to the State System.
167 public ITmfStateInterval
doSingularQuery(long t
, int attributeQuark
)
168 throws TimeRangeException
{
169 return getRelevantInterval(t
, attributeQuark
);
173 public boolean checkValidTime(long t
) {
174 return (t
>= sht
.getTreeStart() && t
<= sht
.getTreeEnd());
178 * Inner method to find the interval in the tree containing the requested
179 * key/timestamp pair, wherever in which node it is.
183 * @return The node containing the information we want
185 private HTInterval
getRelevantInterval(long t
, int key
)
186 throws TimeRangeException
{
187 if (!checkValidTime(t
)) {
188 throw new TimeRangeException();
191 // FIXME using CoreNode for now, we'll have to redo this part to handle
192 // different node types
193 CoreNode currentNode
= sht
.latestBranch
.firstElement();
194 HTInterval interval
= currentNode
.getRelevantInterval(key
, t
);
196 while (interval
== null && currentNode
.getNbChildren() > 0) {
197 currentNode
= (CoreNode
) sht
.selectNextChild(currentNode
, t
);
198 interval
= currentNode
.getRelevantInterval(key
, t
);
201 * Since we should now have intervals at every attribute/timestamp
202 * combination, it should NOT be null here.
204 assert (interval
!= null);
209 * Return the size of the tree history file
211 * @return The current size of the history file in bytes
213 public long getFileSize() {
214 return sht
.getFileSize();
218 * Return the current depth of the tree, ie the number of node levels.
220 * @return The tree depth
222 public int getTreeDepth() {
223 return sht
.latestBranch
.size();
227 * Return the average node usage as a percentage (between 0 and 100)
229 * @return Average node usage %
231 public int getAverageNodeUsage() {
236 for (int seq
= 0; seq
< sht
.getNodeCount(); seq
++) {
237 node
= treeIO
.readNode(seq
);
238 total
+= node
.getNodeUsagePRC();
241 ret
= total
/ sht
.getNodeCount();
242 assert (ret
>= 0 && ret
<= 100);
247 public void debugPrint(PrintWriter writer
) {
248 /* By default don't print out all the intervals */
249 this.debugPrint(writer
, false);
253 * The basic debugPrint method will print the tree structure, but not their
256 * This method here print the contents (the intervals) as well.
259 * The PrintWriter to which the debug info will be written
260 * @param printIntervals
261 * Should we also print every contained interval individually?
263 public void debugPrint(PrintWriter writer
, boolean printIntervals
) {
264 /* Only used for debugging, shouldn't be externalized */
265 writer
.println("------------------------------"); //$NON-NLS-1$
266 writer
.println("State History Tree:\n"); //$NON-NLS-1$
267 writer
.println(sht
.toString());
268 writer
.println("Average node utilization: " //$NON-NLS-1$
269 + this.getAverageNodeUsage());
270 writer
.println(""); //$NON-NLS-1$
272 sht
.debugPrintFullTree(writer
, printIntervals
);
This page took 0.039588 seconds and 5 git commands to generate.