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
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.CoreNode
;
24 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTConfig
;
25 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTInterval
;
26 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTNode
;
27 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HistoryTree
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
36 * History Tree backend for storing a state history. This is the basic version
37 * that runs in the same thread as the class creating it.
39 * @author Alexandre Montplaisir
42 public class HistoryTreeBackend
implements IStateHistoryBackend
{
44 private final @NonNull String ssid
;
47 * The history tree that sits underneath.
49 private final HistoryTree sht
;
51 /** Indicates if the history tree construction is done */
52 private volatile boolean fFinishedBuilding
= false;
55 * Indicates if the history tree construction is done
57 * @return if the history tree construction is done
59 protected boolean isFinishedBuilding() {
60 return fFinishedBuilding
;
64 * Sets if the history tree is finished building
66 * @param isFinishedBuilding
67 * is the history tree finished building
69 protected void setFinishedBuilding(boolean isFinishedBuilding
) {
70 this.fFinishedBuilding
= isFinishedBuilding
;
74 * Constructor for new history files. Use this when creating a new history
78 * The state system's ID
80 * The filename/location where to store the state history (Should
83 * The size of the blocks in the history file. This should be a
86 * The maximum number of children each core node can have
87 * @param providerVersion
88 * Version of of the state provider. We will only try to reopen
89 * existing files if this version matches the one in the
92 * The earliest time stamp that will be stored in the history
94 * Thrown if we can't create the file for some reason
96 public HistoryTreeBackend(@NonNull String ssid
, File newStateFile
, int blockSize
,
97 int maxChildren
, int providerVersion
, long startTime
) throws IOException
{
99 final HTConfig conf
= new HTConfig(newStateFile
, blockSize
, maxChildren
,
100 providerVersion
, startTime
);
101 sht
= new HistoryTree(conf
);
105 * Constructor for new history files. Use this when creating a new history
106 * from scratch. This version supplies sane defaults for the configuration
110 * The state system's id
111 * @param newStateFile
112 * The filename/location where to store the state history (Should
114 * @param providerVersion
115 * Version of of the state provider. We will only try to reopen
116 * existing files if this version matches the one in the
119 * The earliest time stamp that will be stored in the history
120 * @throws IOException
121 * Thrown if we can't create the file for some reason
123 public HistoryTreeBackend(@NonNull String ssid
, File newStateFile
, int providerVersion
, long startTime
)
125 this(ssid
, newStateFile
, 64 * 1024, 50, providerVersion
, startTime
);
129 * Existing history constructor. Use this to open an existing state-file.
132 * The state system's id
133 * @param existingStateFile
134 * Filename/location of the history we want to load
135 * @param providerVersion
136 * Expected version of of the state provider plugin.
137 * @throws IOException
138 * If we can't read the file, if it doesn't exist, is not
139 * recognized, or if the version of the file does not match the
140 * expected providerVersion.
142 public HistoryTreeBackend(@NonNull String ssid
, File existingStateFile
, int providerVersion
)
145 sht
= new HistoryTree(existingStateFile
, providerVersion
);
146 fFinishedBuilding
= true;
150 * Get the History Tree built by this backend.
152 * @return The history tree
154 protected HistoryTree
getSHT() {
159 public String
getSSID() {
164 public long getStartTime() {
165 return sht
.getTreeStart();
169 public long getEndTime() {
170 return sht
.getTreeEnd();
174 public void insertPastState(long stateStartTime
, long stateEndTime
,
175 int quark
, ITmfStateValue value
) throws TimeRangeException
{
176 HTInterval interval
= new HTInterval(stateStartTime
, stateEndTime
,
177 quark
, (TmfStateValue
) value
);
179 /* Start insertions at the "latest leaf" */
180 sht
.insertInterval(interval
);
184 public void finishedBuilding(long endTime
) {
185 sht
.closeTree(endTime
);
186 fFinishedBuilding
= true;
190 public FileInputStream
supplyAttributeTreeReader() {
191 return sht
.supplyATReader();
195 public File
supplyAttributeTreeWriterFile() {
196 return sht
.supplyATWriterFile();
200 public long supplyAttributeTreeWriterFilePosition() {
201 return sht
.supplyATWriterFilePos();
205 public void removeFiles() {
210 public void dispose() {
211 if (fFinishedBuilding
) {
215 * The build is being interrupted, delete the file we partially
216 * built since it won't be complete, so shouldn't be re-used in the
217 * future (.deleteFile() will close the file first)
224 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
)
225 throws TimeRangeException
, StateSystemDisposedException
{
226 if (!checkValidTime(t
)) {
227 /* We can't possibly have information about this query */
228 throw new TimeRangeException();
231 /* We start by reading the information in the root node */
232 HTNode currentNode
= sht
.getRootNode();
233 currentNode
.writeInfoFromNode(stateInfo
, t
);
235 /* Then we follow the branch down in the relevant children */
237 while (currentNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
238 currentNode
= sht
.selectNextChild((CoreNode
) currentNode
, t
);
239 currentNode
.writeInfoFromNode(stateInfo
, t
);
241 } catch (ClosedChannelException e
) {
242 throw new StateSystemDisposedException(e
);
246 * The stateInfo should now be filled with everything needed, we pass
247 * the control back to the State System.
253 public ITmfStateInterval
doSingularQuery(long t
, int attributeQuark
)
254 throws TimeRangeException
, StateSystemDisposedException
{
255 return getRelevantInterval(t
, attributeQuark
);
258 private boolean checkValidTime(long t
) {
259 return (t
>= sht
.getTreeStart() && t
<= sht
.getTreeEnd());
263 * Inner method to find the interval in the tree containing the requested
264 * key/timestamp pair, wherever in which node it is.
268 * @return The node containing the information we want
270 private HTInterval
getRelevantInterval(long t
, int key
)
271 throws TimeRangeException
, StateSystemDisposedException
{
272 if (!checkValidTime(t
)) {
273 throw new TimeRangeException();
276 HTNode currentNode
= sht
.getRootNode();
277 HTInterval interval
= currentNode
.getRelevantInterval(key
, t
);
280 while (interval
== null && currentNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
281 currentNode
= sht
.selectNextChild((CoreNode
) currentNode
, t
);
282 interval
= currentNode
.getRelevantInterval(key
, t
);
284 } catch (ClosedChannelException e
) {
285 throw new StateSystemDisposedException(e
);
291 * Return the size of the tree history file
293 * @return The current size of the history file in bytes
295 public long getFileSize() {
296 return sht
.getFileSize();
300 * Return the average node usage as a percentage (between 0 and 100)
302 * @return Average node usage %
304 public int getAverageNodeUsage() {
310 for (int seq
= 0; seq
< sht
.getNodeCount(); seq
++) {
311 node
= sht
.readNode(seq
);
312 total
+= node
.getNodeUsagePercent();
314 } catch (ClosedChannelException e
) {
318 ret
= total
/ sht
.getNodeCount();
319 assert (ret
>= 0 && ret
<= 100);
324 public void debugPrint(PrintWriter writer
) {
325 /* By default don't print out all the intervals */
326 this.debugPrint(writer
, false);
330 * The basic debugPrint method will print the tree structure, but not their
333 * This method here print the contents (the intervals) as well.
336 * The PrintWriter to which the debug info will be written
337 * @param printIntervals
338 * Should we also print every contained interval individually?
340 public void debugPrint(PrintWriter writer
, boolean printIntervals
) {
341 /* Only used for debugging, shouldn't be externalized */
342 writer
.println("------------------------------"); //$NON-NLS-1$
343 writer
.println("State History Tree:\n"); //$NON-NLS-1$
344 writer
.println(sht
.toString());
345 writer
.println("Average node utilization: " //$NON-NLS-1$
346 + this.getAverageNodeUsage());
347 writer
.println(""); //$NON-NLS-1$
349 sht
.debugPrintFullTree(writer
, printIntervals
);