1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 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
12 * Alexandre Montplaisir - Initial API and implementation
13 * Patrick Tasse - Add message to exceptions
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
21 import java
.nio
.channels
.ClosedChannelException
;
22 import java
.util
.Deque
;
23 import java
.util
.LinkedList
;
24 import java
.util
.List
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.Activator
;
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
;
35 import com
.google
.common
.annotations
.VisibleForTesting
;
38 * History Tree backend for storing a state history. This is the basic version
39 * that runs in the same thread as the class creating it.
41 * @author Alexandre Montplaisir
43 public class HistoryTreeBackend
implements IStateHistoryBackend
{
45 private final @NonNull String fSsid
;
48 * The history tree that sits underneath.
50 private final @NonNull IHistoryTree fSht
;
52 /** Indicates if the history tree construction is done */
53 private volatile boolean fFinishedBuilding
= false;
56 * Indicates if the history tree construction is done
58 * @return if the history tree construction is done
60 protected boolean isFinishedBuilding() {
61 return fFinishedBuilding
;
65 * Sets if the history tree is finished building
67 * @param isFinishedBuilding
68 * is the history tree finished building
70 protected void setFinishedBuilding(boolean isFinishedBuilding
) {
71 fFinishedBuilding
= isFinishedBuilding
;
75 * Constructor for new history files. Use this when creating a new history
79 * The state system's ID
81 * The filename/location where to store the state history (Should
83 * @param providerVersion
84 * Version of of the state provider. We will only try to reopen
85 * existing files if this version matches the one in the
88 * The earliest time stamp that will be stored in the history
90 * The size of the blocks in the history file. This should be a
93 * The maximum number of children each core node can have
95 * Thrown if we can't create the file for some reason
97 public HistoryTreeBackend(@NonNull String ssid
,
102 int maxChildren
) throws IOException
{
104 final HTConfig conf
= new HTConfig(newStateFile
, blockSize
, maxChildren
,
105 providerVersion
, startTime
);
106 fSht
= initializeSHT(conf
);
110 * Constructor for new history files. Use this when creating a new history
111 * from scratch. This version supplies sane defaults for the configuration
115 * The state system's id
116 * @param newStateFile
117 * The filename/location where to store the state history (Should
119 * @param providerVersion
120 * Version of of the state provider. We will only try to reopen
121 * existing files if this version matches the one in the
124 * The earliest time stamp that will be stored in the history
125 * @throws IOException
126 * Thrown if we can't create the file for some reason
129 public HistoryTreeBackend(@NonNull String ssid
, File newStateFile
, int providerVersion
, long startTime
)
131 this(ssid
, newStateFile
, providerVersion
, startTime
, 64 * 1024, 50);
135 * Existing history constructor. Use this to open an existing state-file.
138 * The state system's id
139 * @param existingStateFile
140 * Filename/location of the history we want to load
141 * @param providerVersion
142 * Expected version of of the state provider plugin.
143 * @throws IOException
144 * If we can't read the file, if it doesn't exist, is not
145 * recognized, or if the version of the file does not match the
146 * expected providerVersion.
148 public HistoryTreeBackend(@NonNull String ssid
, @NonNull File existingStateFile
, int providerVersion
)
151 fSht
= initializeSHT(existingStateFile
, providerVersion
);
152 fFinishedBuilding
= true;
156 * New-tree initializer for the History Tree wrapped by this backend. Can be
157 * overriden to use different implementations.
160 * The HTConfig configuration object
161 * @return The new history tree
162 * @throws IOException
163 * If there was a problem during creation
166 protected @NonNull IHistoryTree
initializeSHT(@NonNull HTConfig conf
) throws IOException
{
167 return HistoryTreeFactory
.createHistoryTree(conf
);
171 * Existing-tree initializer for the History Tree wrapped by this backend.
172 * Can be overriden to use different implementations.
174 * @param existingStateFile
176 * @param providerVersion
177 * The expected state provider version
178 * @return The history tree opened from the given file
179 * @throws IOException
180 * If there was a problem during creation
183 protected @NonNull IHistoryTree
initializeSHT(@NonNull File existingStateFile
, int providerVersion
) throws IOException
{
184 return HistoryTreeFactory
.createFromFile(existingStateFile
.toPath(), providerVersion
);
188 * Get the History Tree built by this backend.
190 * Note: Do not override this method. If you want to extend the class to use
191 * a different History Tree implementation, override both variants of
192 * {@link #initializeSHT} instead.
194 * @return The history tree
196 protected final @NonNull IHistoryTree
getSHT() {
201 public String
getSSID() {
206 public long getStartTime() {
207 return getSHT().getTreeStart();
211 public long getEndTime() {
212 return getSHT().getTreeEnd();
216 public void insertPastState(long stateStartTime
, long stateEndTime
,
217 int quark
, ITmfStateValue value
) throws TimeRangeException
{
218 HTInterval interval
= new HTInterval(stateStartTime
, stateEndTime
,
219 quark
, (TmfStateValue
) value
);
221 /* Start insertions at the "latest leaf" */
222 getSHT().insertInterval(interval
);
226 public void finishedBuilding(long endTime
) {
227 getSHT().closeTree(endTime
);
228 fFinishedBuilding
= true;
232 public FileInputStream
supplyAttributeTreeReader() {
233 return getSHT().supplyATReader();
237 public File
supplyAttributeTreeWriterFile() {
238 return getSHT().supplyATWriterFile();
242 public long supplyAttributeTreeWriterFilePosition() {
243 return getSHT().supplyATWriterFilePos();
247 public void removeFiles() {
248 getSHT().deleteFile();
252 public void dispose() {
253 if (fFinishedBuilding
) {
254 getSHT().closeFile();
257 * The build is being interrupted, delete the file we partially
258 * built since it won't be complete, so shouldn't be re-used in the
259 * future (.deleteFile() will close the file first)
261 getSHT().deleteFile();
266 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
)
267 throws TimeRangeException
, StateSystemDisposedException
{
270 /* Queue is a stack of nodes containing nodes intersecting t */
271 Deque
<HTNode
> queue
= new LinkedList
<>();
273 /* We start by reading the information in the root node */
274 queue
.add(getSHT().getRootNode());
276 /* Then we follow the down in the relevant children */
278 while (!queue
.isEmpty()) {
279 HTNode currentNode
= queue
.pop();
280 if (currentNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
281 /* Here we add the relevant children nodes for BFS */
282 queue
.addAll(getSHT().selectNextChildren((CoreNode
) currentNode
, t
));
284 currentNode
.writeInfoFromNode(stateInfo
, t
);
286 } catch (ClosedChannelException e
) {
287 throw new StateSystemDisposedException(e
);
291 * The stateInfo should now be filled with everything needed, we pass
292 * the control back to the State System.
297 public ITmfStateInterval
doSingularQuery(long t
, int attributeQuark
)
298 throws TimeRangeException
, StateSystemDisposedException
{
300 return getRelevantInterval(t
, attributeQuark
);
301 } catch (ClosedChannelException e
) {
302 throw new StateSystemDisposedException(e
);
306 private void checkValidTime(long t
) {
307 long startTime
= getStartTime();
308 long endTime
= getEndTime();
309 if (t
< startTime
|| t
> endTime
) {
310 throw new TimeRangeException(String
.format("%s Time:%d, Start:%d, End:%d", //$NON-NLS-1$
311 fSsid
, t
, startTime
, endTime
));
316 * Inner method to find the interval in the tree containing the requested
317 * key/timestamp pair, wherever in which node it is.
319 private HTInterval
getRelevantInterval(long t
, int key
)
320 throws TimeRangeException
, ClosedChannelException
{
323 Deque
<HTNode
> queue
= new LinkedList
<>();
324 queue
.add(getSHT().getRootNode());
325 HTInterval interval
= null;
326 while (interval
== null && !queue
.isEmpty()) {
327 HTNode currentNode
= queue
.pop();
328 if (currentNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
329 queue
.addAll(getSHT().selectNextChildren((CoreNode
) currentNode
, t
));
331 interval
= currentNode
.getRelevantInterval(key
, t
);
337 * Return the size of the tree history file
339 * @return The current size of the history file in bytes
341 public long getFileSize() {
342 return getSHT().getFileSize();
346 * Return the average node usage as a percentage (between 0 and 100)
348 * @return Average node usage %
350 public int getAverageNodeUsage() {
356 for (int seq
= 0; seq
< getSHT().getNodeCount(); seq
++) {
357 node
= getSHT().readNode(seq
);
358 total
+= node
.getNodeUsagePercent();
360 } catch (ClosedChannelException e
) {
361 Activator
.getDefault().logError(e
.getMessage(), e
);
364 ret
= total
/ getSHT().getNodeCount();
365 /* The return value should be a percentage */
366 if (ret
< 0 || ret
> 100) {
367 throw new IllegalStateException("Average node usage is not a percentage: " + ret
); //$NON-NLS-1$