1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson, École Polytechnique de Montréal, and others
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Alexandre Montplaisir - Initial API and implementation
11 * Florian Wininger - Add Extension and Leaf Node
12 * Patrick Tasse - Add message to exceptions
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.classic
;
18 import java
.io
.FileInputStream
;
19 import java
.io
.IOException
;
20 import java
.nio
.ByteBuffer
;
21 import java
.nio
.ByteOrder
;
22 import java
.nio
.channels
.ClosedChannelException
;
23 import java
.nio
.channels
.FileChannel
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Collection
;
26 import java
.util
.Collections
;
27 import java
.util
.List
;
29 import org
.eclipse
.jdt
.annotation
.NonNull
;
30 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTConfig
;
31 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTInterval
;
32 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTNode
;
33 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HT_IO
;
34 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.IHistoryTree
;
35 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.LeafNode
;
36 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.ParentNode
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
40 import com
.google
.common
.annotations
.VisibleForTesting
;
41 import com
.google
.common
.collect
.ImmutableList
;
44 * Meta-container for the History Tree. This structure contains all the
45 * high-level data relevant to the tree.
47 * @author Alexandre Montplaisir
49 public class HistoryTreeClassic
implements IHistoryTree
{
52 * The magic number for this file format.
54 public static final int HISTORY_FILE_MAGIC_NUMBER
= 0x05FFA900;
56 /** File format version. Increment when breaking compatibility. */
57 private static final int FILE_VERSION
= 7;
59 private static final IHTNodeFactory CLASSIC_NODE_FACTORY
= new IHTNodeFactory() {
62 public HTNode
createCoreNode(HTConfig config
, int seqNumber
, int parentSeqNumber
, long start
) {
63 return new CoreNode(config
, seqNumber
, parentSeqNumber
, start
);
67 public HTNode
createLeafNode(HTConfig config
, int seqNumber
, int parentSeqNumber
, long start
) {
68 return new LeafNode(config
, seqNumber
, parentSeqNumber
, start
);
73 // ------------------------------------------------------------------------
74 // Tree-specific configuration
75 // ------------------------------------------------------------------------
77 /** Container for all the configuration constants */
78 private final HTConfig fConfig
;
80 /** Reader/writer object */
81 private final @NonNull HT_IO fTreeIO
;
83 // ------------------------------------------------------------------------
84 // Variable Fields (will change throughout the existence of the SHT)
85 // ------------------------------------------------------------------------
87 /** Latest timestamp found in the tree (at any given moment) */
88 private long fTreeEnd
;
90 /** The total number of nodes that exists in this tree */
91 private int fNodeCount
;
93 /** "Cache" to keep the active nodes in memory */
94 private final @NonNull List
<@NonNull HTNode
> fLatestBranch
;
96 // ------------------------------------------------------------------------
97 // Constructors/"Destructors"
98 // ------------------------------------------------------------------------
101 * Create a new State History from scratch, using a {@link HTConfig} object
105 * The config to use for this History Tree.
106 * @throws IOException
107 * If an error happens trying to open/write to the file
108 * specified in the config
110 public HistoryTreeClassic(HTConfig conf
) throws IOException
{
112 * Simple check to make sure we have enough place in the 0th block for
113 * the tree configuration
115 if (conf
.getBlockSize() < TREE_HEADER_SIZE
) {
116 throw new IllegalArgumentException();
120 fTreeEnd
= conf
.getTreeStart();
122 fLatestBranch
= Collections
.synchronizedList(new ArrayList
<>());
124 /* Prepare the IO object */
125 fTreeIO
= new HT_IO(fConfig
, true, CLASSIC_NODE_FACTORY
);
127 /* Add the first node to the tree */
128 LeafNode firstNode
= initNewLeafNode(-1, conf
.getTreeStart());
129 fLatestBranch
.add(firstNode
);
133 * "Reader" constructor : instantiate a SHTree from an existing tree file on
136 * @param existingStateFile
137 * Path/filename of the history-file we are to open
138 * @param expProviderVersion
139 * The expected version of the state provider
140 * @throws IOException
141 * If an error happens reading the file
143 public HistoryTreeClassic(File existingStateFile
, int expProviderVersion
) throws IOException
{
145 * Open the file ourselves, get the tree header information we need,
146 * then pass on the descriptor to the TreeIO object.
148 int rootNodeSeqNb
, res
;
152 /* Java I/O mumbo jumbo... */
153 if (!existingStateFile
.exists()) {
154 throw new IOException("Selected state file does not exist"); //$NON-NLS-1$
156 if (existingStateFile
.length() <= 0) {
157 throw new IOException("Empty target file"); //$NON-NLS-1$
160 try (FileInputStream fis
= new FileInputStream(existingStateFile
);
161 FileChannel fc
= fis
.getChannel();) {
163 ByteBuffer buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
164 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
167 res
= fc
.read(buffer
);
168 if (res
!= TREE_HEADER_SIZE
) {
169 throw new IOException("Invalid header size"); //$NON-NLS-1$
175 * Check the magic number to make sure we're opening the right type
178 res
= buffer
.getInt();
179 if (res
!= HISTORY_FILE_MAGIC_NUMBER
) {
180 throw new IOException("Wrong magic number"); //$NON-NLS-1$
183 res
= buffer
.getInt(); /* File format version number */
184 if (res
!= FILE_VERSION
) {
185 throw new IOException("Mismatching History Tree file format versions"); //$NON-NLS-1$
188 res
= buffer
.getInt(); /* Event handler's version number */
189 if (res
!= expProviderVersion
&&
190 expProviderVersion
!= ITmfStateSystemBuilder
.IGNORE_PROVIDER_VERSION
) {
192 * The existing history was built using an event handler that
193 * doesn't match the current one in the framework.
195 * Information could be all wrong. Instead of keeping an
196 * incorrect history file, a rebuild is done.
198 throw new IOException("Mismatching event handler versions"); //$NON-NLS-1$
201 bs
= buffer
.getInt(); /* Block Size */
202 maxc
= buffer
.getInt(); /* Max nb of children per node */
204 fNodeCount
= buffer
.getInt();
205 rootNodeSeqNb
= buffer
.getInt();
206 startTime
= buffer
.getLong();
208 fConfig
= new HTConfig(existingStateFile
, bs
, maxc
, expProviderVersion
, startTime
);
212 * FIXME We close fis here and the TreeIO will then reopen the same
213 * file, not extremely elegant. But how to pass the information here to
216 fTreeIO
= new HT_IO(fConfig
, false, CLASSIC_NODE_FACTORY
);
218 fLatestBranch
= buildLatestBranch(rootNodeSeqNb
);
219 fTreeEnd
= getRootNode().getNodeEnd();
222 * Make sure the history start time we read previously is consistent
223 * with was is actually in the root node.
225 if (startTime
!= getRootNode().getNodeStart()) {
226 throw new IOException("Inconsistent start times in the" + //$NON-NLS-1$
227 "history file, it might be corrupted."); //$NON-NLS-1$
232 * Rebuild the latestBranch "cache" object by reading the nodes from disk
233 * (When we are opening an existing file on disk and want to append to it,
236 * @param rootNodeSeqNb
237 * The sequence number of the root node, so we know where to
239 * @throws ClosedChannelException
241 private @NonNull List
<@NonNull HTNode
> buildLatestBranch(int rootNodeSeqNb
) throws ClosedChannelException
{
242 List
<@NonNull HTNode
> list
= new ArrayList
<>();
244 HTNode nextChildNode
= fTreeIO
.readNode(rootNodeSeqNb
);
245 list
.add(nextChildNode
);
247 /* Follow the last branch up to the leaf */
248 while (nextChildNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
249 nextChildNode
= fTreeIO
.readNode(((CoreNode
) nextChildNode
).getLatestChild());
250 list
.add(nextChildNode
);
252 return Collections
.synchronizedList(list
);
256 public void closeTree(long requestedEndTime
) {
257 /* This is an important operation, queries can wait */
258 synchronized (fLatestBranch
) {
260 * Work-around the "empty branches" that get created when the root
261 * node becomes full. Overwrite the tree's end time with the
262 * original wanted end-time, to ensure no queries are sent into
265 * This won't be needed once extended nodes are implemented.
267 fTreeEnd
= requestedEndTime
;
269 /* Close off the latest branch of the tree */
270 for (int i
= 0; i
< fLatestBranch
.size(); i
++) {
271 fLatestBranch
.get(i
).closeThisNode(fTreeEnd
);
272 fTreeIO
.writeNode(fLatestBranch
.get(i
));
275 try (FileChannel fc
= fTreeIO
.getFcOut();) {
276 ByteBuffer buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
277 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
280 /* Save the config of the tree to the header of the file */
283 buffer
.putInt(HISTORY_FILE_MAGIC_NUMBER
);
285 buffer
.putInt(FILE_VERSION
);
286 buffer
.putInt(fConfig
.getProviderVersion());
288 buffer
.putInt(fConfig
.getBlockSize());
289 buffer
.putInt(fConfig
.getMaxChildren());
291 buffer
.putInt(fNodeCount
);
293 /* root node seq. nb */
294 buffer
.putInt(fLatestBranch
.get(0).getSequenceNumber());
296 /* start time of this history */
297 buffer
.putLong(fLatestBranch
.get(0).getNodeStart());
300 int res
= fc
.write(buffer
);
301 assert (res
<= TREE_HEADER_SIZE
);
302 /* done writing the file header */
304 } catch (IOException e
) {
306 * If we were able to write so far, there should not be any
307 * problem at this point...
309 throw new RuntimeException("State system write error"); //$NON-NLS-1$
314 // ------------------------------------------------------------------------
316 // ------------------------------------------------------------------------
319 public long getTreeStart() {
320 return fConfig
.getTreeStart();
324 public long getTreeEnd() {
329 public int getNodeCount() {
334 public HTNode
getRootNode() {
335 return fLatestBranch
.get(0);
339 * Return the latest branch of the tree. That branch is immutable. Used for
340 * unit testing and debugging.
342 * @return The immutable latest branch
345 protected List
<@NonNull HTNode
> getLatestBranch() {
346 return ImmutableList
.copyOf(fLatestBranch
);
350 * Read a node at sequence number
353 * The sequence number of the node to read
354 * @return The HTNode object
355 * @throws ClosedChannelException
356 * Exception thrown when reading the node
359 protected @NonNull HTNode
getNode(int seqNum
) throws ClosedChannelException
{
360 // First, check in the latest branch if the node is there
361 for (HTNode node
: fLatestBranch
) {
362 if (node
.getSequenceNumber() == seqNum
) {
366 return fTreeIO
.readNode(seqNum
);
370 * Retrieve the TreeIO object. Should only be used for testing.
375 protected @NonNull HT_IO
getTreeIO() {
379 // ------------------------------------------------------------------------
381 // ------------------------------------------------------------------------
384 public FileInputStream
supplyATReader() {
385 return fTreeIO
.supplyATReader(getNodeCount());
389 public File
supplyATWriterFile() {
390 return fConfig
.getStateFile();
394 public long supplyATWriterFilePos() {
395 return IHistoryTree
.TREE_HEADER_SIZE
396 + ((long) getNodeCount() * fConfig
.getBlockSize());
400 public HTNode
readNode(int seqNumber
) throws ClosedChannelException
{
401 /* Try to read the node from memory */
402 synchronized (fLatestBranch
) {
403 for (HTNode node
: fLatestBranch
) {
404 if (node
.getSequenceNumber() == seqNumber
) {
410 /* Read the node from disk */
411 return fTreeIO
.readNode(seqNumber
);
415 public void writeNode(HTNode node
) {
416 fTreeIO
.writeNode(node
);
420 public void closeFile() {
425 public void deleteFile() {
426 fTreeIO
.deleteFile();
429 // ------------------------------------------------------------------------
431 // ------------------------------------------------------------------------
434 public void insertInterval(HTInterval interval
) throws TimeRangeException
{
435 if (interval
.getStartTime() < fConfig
.getTreeStart()) {
436 throw new TimeRangeException("Interval Start:" + interval
.getStartTime() + ", Config Start:" + fConfig
.getTreeStart()); //$NON-NLS-1$ //$NON-NLS-2$
438 tryInsertAtNode(interval
, fLatestBranch
.size() - 1);
442 * Inner method to find in which node we should add the interval.
445 * The interval to add to the tree
447 * The index *in the latestBranch* where we are trying the
450 private void tryInsertAtNode(HTInterval interval
, int indexOfNode
) {
451 HTNode targetNode
= fLatestBranch
.get(indexOfNode
);
453 /* Verify if there is enough room in this node to store this interval */
454 if (interval
.getSizeOnDisk() > targetNode
.getNodeFreeSpace()) {
455 /* Nope, not enough room. Insert in a new sibling instead. */
456 addSiblingNode(indexOfNode
);
457 tryInsertAtNode(interval
, fLatestBranch
.size() - 1);
461 /* Make sure the interval time range fits this node */
462 if (interval
.getStartTime() < targetNode
.getNodeStart()) {
464 * No, this interval starts before the startTime of this node. We
465 * need to check recursively in parents if it can fit.
467 tryInsertAtNode(interval
, indexOfNode
- 1);
472 * Ok, there is room, and the interval fits in this time slot. Let's add
475 targetNode
.addInterval(interval
);
477 /* Update treeEnd if needed */
478 if (interval
.getEndTime() > fTreeEnd
) {
479 fTreeEnd
= interval
.getEndTime();
484 * Method to add a sibling to any node in the latest branch. This will add
485 * children back down to the leaf level, if needed.
488 * The index in latestBranch where we start adding
490 private void addSiblingNode(int indexOfNode
) {
491 synchronized (fLatestBranch
) {
492 final long splitTime
= fTreeEnd
;
494 if (indexOfNode
>= fLatestBranch
.size()) {
496 * We need to make sure (indexOfNode - 1) doesn't get the last
497 * node in the branch, because that one is a Leaf Node.
499 throw new IllegalStateException();
502 /* Check if we need to add a new root node */
503 if (indexOfNode
== 0) {
508 /* Check if we can indeed add a child to the target parent */
509 if (((ParentNode
) fLatestBranch
.get(indexOfNode
- 1)).getNbChildren() == fConfig
.getMaxChildren()) {
510 /* If not, add a branch starting one level higher instead */
511 addSiblingNode(indexOfNode
- 1);
515 /* Split off the new branch from the old one */
516 for (int i
= indexOfNode
; i
< fLatestBranch
.size(); i
++) {
517 fLatestBranch
.get(i
).closeThisNode(splitTime
);
518 fTreeIO
.writeNode(fLatestBranch
.get(i
));
520 ParentNode prevNode
= (ParentNode
) fLatestBranch
.get(i
- 1);
523 switch (fLatestBranch
.get(i
).getNodeType()) {
525 newNode
= initNewCoreNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
528 newNode
= initNewLeafNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
531 throw new IllegalStateException();
534 prevNode
.linkNewChild(newNode
);
535 fLatestBranch
.set(i
, newNode
);
541 * Similar to the previous method, except here we rebuild a completely new
544 private void addNewRootNode() {
545 final long splitTime
= fTreeEnd
;
547 HTNode oldRootNode
= fLatestBranch
.get(0);
548 ParentNode newRootNode
= initNewCoreNode(-1, fConfig
.getTreeStart());
550 /* Tell the old root node that it isn't root anymore */
551 oldRootNode
.setParentSequenceNumber(newRootNode
.getSequenceNumber());
553 /* Close off the whole current latestBranch */
555 for (int i
= 0; i
< fLatestBranch
.size(); i
++) {
556 fLatestBranch
.get(i
).closeThisNode(splitTime
);
557 fTreeIO
.writeNode(fLatestBranch
.get(i
));
560 /* Link the new root to its first child (the previous root node) */
561 newRootNode
.linkNewChild(oldRootNode
);
563 /* Rebuild a new latestBranch */
564 int depth
= fLatestBranch
.size();
565 fLatestBranch
.clear();
566 fLatestBranch
.add(newRootNode
);
568 // Create new coreNode
569 for (int i
= 1; i
< depth
; i
++) {
570 ParentNode prevNode
= (ParentNode
) fLatestBranch
.get(i
- 1);
571 ParentNode newNode
= initNewCoreNode(prevNode
.getSequenceNumber(),
573 prevNode
.linkNewChild(newNode
);
574 fLatestBranch
.add(newNode
);
577 // Create the new leafNode
578 ParentNode prevNode
= (ParentNode
) fLatestBranch
.get(depth
- 1);
579 LeafNode newNode
= initNewLeafNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
580 prevNode
.linkNewChild(newNode
);
581 fLatestBranch
.add(newNode
);
585 * Add a new empty core node to the tree.
587 * @param parentSeqNumber
588 * Sequence number of this node's parent
590 * Start time of the new node
591 * @return The newly created node
593 private @NonNull ParentNode
initNewCoreNode(int parentSeqNumber
, long startTime
) {
594 ParentNode newNode
= new CoreNode(fConfig
, fNodeCount
, parentSeqNumber
,
601 * Add a new empty leaf node to the tree.
603 * @param parentSeqNumber
604 * Sequence number of this node's parent
606 * Start time of the new node
607 * @return The newly created node
609 private @NonNull LeafNode
initNewLeafNode(int parentSeqNumber
, long startTime
) {
610 LeafNode newNode
= new LeafNode(fConfig
, fNodeCount
, parentSeqNumber
,
617 public Collection
<HTNode
> selectNextChildren(ParentNode currentNode
, long t
) throws ClosedChannelException
{
618 assert (currentNode
.getNbChildren() > 0);
619 int potentialNextSeqNb
= currentNode
.getSequenceNumber();
621 for (int i
= 0; i
< currentNode
.getNbChildren(); i
++) {
622 if (t
>= currentNode
.getChildStart(i
)) {
623 potentialNextSeqNb
= currentNode
.getChild(i
);
630 * Once we exit this loop, we should have found a children to follow. If
631 * we didn't, there's a problem.
633 if (potentialNextSeqNb
== currentNode
.getSequenceNumber()) {
634 throw new IllegalStateException("No next child node found"); //$NON-NLS-1$
638 * Since this code path is quite performance-critical, avoid iterating
639 * through the whole latestBranch array if we know for sure the next
640 * node has to be on disk
642 if (currentNode
.isOnDisk()) {
643 return Collections
.singleton(fTreeIO
.readNode(potentialNextSeqNb
));
645 return Collections
.singleton(readNode(potentialNextSeqNb
));
649 public long getFileSize() {
650 return fConfig
.getStateFile().length();
653 // ------------------------------------------------------------------------
654 // Test/debugging methods
655 // ------------------------------------------------------------------------
657 /* Only used for debugging, shouldn't be externalized */
658 @SuppressWarnings("nls")
660 public String
toString() {
661 return "Information on the current tree:\n\n" + "Blocksize: "
662 + fConfig
.getBlockSize() + "\n" + "Max nb. of children per node: "
663 + fConfig
.getMaxChildren() + "\n" + "Number of nodes: " + fNodeCount
664 + "\n" + "Depth of the tree: " + fLatestBranch
.size() + "\n"
665 + "Size of the treefile: " + getFileSize() + "\n"
666 + "Root node has sequence number: "
667 + fLatestBranch
.get(0).getSequenceNumber() + "\n"
668 + "'Latest leaf' has sequence number: "
669 + fLatestBranch
.get(fLatestBranch
.size() - 1).getSequenceNumber();