1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
.backends
.historytree
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.IOException
;
18 import java
.io
.PrintWriter
;
19 import java
.nio
.ByteBuffer
;
20 import java
.nio
.ByteOrder
;
21 import java
.nio
.channels
.ClosedChannelException
;
22 import java
.nio
.channels
.FileChannel
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Collections
;
25 import java
.util
.List
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateProvider
;
31 * Meta-container for the History Tree. This structure contains all the
32 * high-level data relevant to the tree.
34 * @author Alexandre Montplaisir
40 * Size of the "tree header" in the tree-file The nodes will use this offset
41 * to know where they should be in the file. This should always be a
44 public static final int TREE_HEADER_SIZE
= 4096;
46 private static final int HISTORY_FILE_MAGIC_NUMBER
= 0x05FFA900;
48 /** File format version. Increment when breaking compatibility. */
49 private static final int FILE_VERSION
= 3;
51 // ------------------------------------------------------------------------
52 // Tree-specific configuration
53 // ------------------------------------------------------------------------
55 /** Container for all the configuration constants */
56 private final HTConfig config
;
58 /** Reader/writer object */
59 private final HT_IO treeIO
;
61 // ------------------------------------------------------------------------
62 // Variable Fields (will change throughout the existence of the SHT)
63 // ------------------------------------------------------------------------
65 /** Latest timestamp found in the tree (at any given moment) */
68 /** The total number of nodes that exists in this tree */
69 private int nodeCount
;
71 /** "Cache" to keep the active nodes in memory */
72 private List
<CoreNode
> latestBranch
;
74 // ------------------------------------------------------------------------
75 // Constructors/"Destructors"
76 // ------------------------------------------------------------------------
79 * Create a new State History from scratch, using a SHTConfig object for
82 HistoryTree(HTConfig conf
) throws IOException
{
84 * Simple check to make sure we have enough place in the 0th block
85 * for the tree configuration
87 if (conf
.getBlockSize() < TREE_HEADER_SIZE
) {
88 throw new IllegalArgumentException();
92 treeEnd
= conf
.getTreeStart();
94 latestBranch
= new ArrayList
<CoreNode
>();
96 /* Prepare the IO object */
97 treeIO
= new HT_IO(config
, true);
99 /* Add the first node to the tree */
100 CoreNode firstNode
= initNewCoreNode(-1, conf
.getTreeStart());
101 latestBranch
.add(firstNode
);
105 * "Reader" constructor : instantiate a SHTree from an existing tree file on
108 * @param existingFileName
109 * Path/filename of the history-file we are to open
110 * @param expProviderVersion
111 * The expected version of the state provider
112 * @throws IOException
114 HistoryTree(File existingStateFile
, int expProviderVersion
) throws IOException
{
116 * Open the file ourselves, get the tree header information we need,
117 * then pass on the descriptor to the TreeIO object.
119 int rootNodeSeqNb
, res
;
123 /* Java I/O mumbo jumbo... */
124 if (!existingStateFile
.exists()) {
125 throw new IOException("Selected state file does not exist"); //$NON-NLS-1$
127 if (existingStateFile
.length() <= 0) {
128 throw new IOException("Empty target file"); //$NON-NLS-1$
131 FileInputStream fis
= new FileInputStream(existingStateFile
);
132 ByteBuffer buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
133 FileChannel fc
= fis
.getChannel();
134 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
140 * Check the magic number to make sure we're opening the right type of
143 res
= buffer
.getInt();
144 if (res
!= HISTORY_FILE_MAGIC_NUMBER
) {
147 throw new IOException("Wrong magic number"); //$NON-NLS-1$
150 res
= buffer
.getInt(); /* File format version number */
151 if (res
!= FILE_VERSION
) {
154 throw new IOException("Mismatching History Tree file format versions"); //$NON-NLS-1$
157 res
= buffer
.getInt(); /* Event handler's version number */
158 if (res
!= expProviderVersion
&&
159 expProviderVersion
!= ITmfStateProvider
.IGNORE_PROVIDER_VERSION
) {
161 * The existing history was built using an event handler that doesn't
162 * match the current one in the framework.
164 * Information could be all wrong. Instead of keeping an incorrect
165 * history file, a rebuild is done.
169 throw new IOException("Mismatching event handler versions"); //$NON-NLS-1$
172 bs
= buffer
.getInt(); /* Block Size */
173 maxc
= buffer
.getInt(); /* Max nb of children per node */
175 this.nodeCount
= buffer
.getInt();
176 rootNodeSeqNb
= buffer
.getInt();
177 startTime
= buffer
.getLong();
179 this.config
= new HTConfig(existingStateFile
, bs
, maxc
, expProviderVersion
, startTime
);
183 * FIXME We close fis here and the TreeIO will then reopen the same
184 * file, not extremely elegant. But how to pass the information here to
187 this.treeIO
= new HT_IO(config
, false);
189 rebuildLatestBranch(rootNodeSeqNb
);
190 this.treeEnd
= latestBranch
.get(0).getNodeEnd();
193 * Make sure the history start time we read previously is consistent
194 * with was is actually in the root node.
196 if (startTime
!= latestBranch
.get(0).getNodeStart()) {
199 throw new IOException("Inconsistent start times in the" + //$NON-NLS-1$
200 "history file, it might be corrupted."); //$NON-NLS-1$
205 * "Save" the tree to disk. This method will cause the treeIO object to
206 * commit all nodes to disk and then return the RandomAccessFile descriptor
207 * so the Tree object can save its configuration into the header of the
210 * @param requestedEndTime
211 * The greatest timestamp present in the history tree
213 void closeTree(long requestedEndTime
) {
219 * Work-around the "empty branches" that get created when the root node
220 * becomes full. Overwrite the tree's end time with the original wanted
221 * end-time, to ensure no queries are sent into those empty nodes.
223 * This won't be needed once extended nodes are implemented.
225 this.treeEnd
= requestedEndTime
;
227 /* Close off the latest branch of the tree */
228 for (i
= 0; i
< latestBranch
.size(); i
++) {
229 latestBranch
.get(i
).closeThisNode(treeEnd
);
230 treeIO
.writeNode(latestBranch
.get(i
));
233 fc
= treeIO
.getFcOut();
234 buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
235 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
238 /* Save the config of the tree to the header of the file */
242 buffer
.putInt(HISTORY_FILE_MAGIC_NUMBER
);
244 buffer
.putInt(FILE_VERSION
);
245 buffer
.putInt(config
.getProviderVersion());
247 buffer
.putInt(config
.getBlockSize());
248 buffer
.putInt(config
.getMaxChildren());
250 buffer
.putInt(nodeCount
);
252 /* root node seq. nb */
253 buffer
.putInt(latestBranch
.get(0).getSequenceNumber());
255 /* start time of this history */
256 buffer
.putLong(latestBranch
.get(0).getNodeStart());
259 res
= fc
.write(buffer
);
260 assert (res
<= TREE_HEADER_SIZE
);
261 /* done writing the file header */
263 } catch (IOException e
) {
264 /* We should not have any problems at this point... */
268 } catch (IOException e
) {
274 // ------------------------------------------------------------------------
276 // ------------------------------------------------------------------------
278 HTConfig
getConfig() {
282 long getTreeStart() {
283 return config
.getTreeStart();
294 List
<CoreNode
> getLatestBranch() {
295 return Collections
.unmodifiableList(latestBranch
);
298 // ------------------------------------------------------------------------
300 // ------------------------------------------------------------------------
302 File
supplyATWriterFile() {
303 return config
.getStateFile();
306 FileInputStream
supplyATReader() {
307 return treeIO
.supplyATReader(getNodeCount());
310 long supplyATWriterFilePos() {
311 return HistoryTree
.TREE_HEADER_SIZE
312 + ((long) getNodeCount() * config
.getBlockSize());
315 HTNode
readNode(int seqNumber
) throws ClosedChannelException
{
316 /* Try to read the node from memory */
317 for (HTNode node
: getLatestBranch()) {
318 if (node
.getSequenceNumber() == seqNumber
) {
323 /* Read the node from disk */
324 return treeIO
.readNode(seqNumber
);
327 void writeNode(HTNode node
) {
328 treeIO
.writeNode(node
);
339 // ------------------------------------------------------------------------
341 // ------------------------------------------------------------------------
344 * Rebuild the latestBranch "cache" object by reading the nodes from disk
345 * (When we are opening an existing file on disk and want to append to it,
348 * @param rootNodeSeqNb
349 * The sequence number of the root node, so we know where to
351 * @throws ClosedChannelException
353 private void rebuildLatestBranch(int rootNodeSeqNb
) throws ClosedChannelException
{
354 HTNode nextChildNode
;
356 this.latestBranch
= new ArrayList
<CoreNode
>();
358 nextChildNode
= treeIO
.readNode(rootNodeSeqNb
);
359 latestBranch
.add((CoreNode
) nextChildNode
);
360 while (latestBranch
.get(latestBranch
.size() - 1).getNbChildren() > 0) {
361 nextChildNode
= treeIO
.readNode(latestBranch
.get(latestBranch
.size() - 1).getLatestChild());
362 latestBranch
.add((CoreNode
) nextChildNode
);
367 * Insert an interval in the tree
370 * The interval to be inserted
372 void insertInterval(HTInterval interval
) throws TimeRangeException
{
373 if (interval
.getStartTime() < config
.getTreeStart()) {
374 throw new TimeRangeException();
376 tryInsertAtNode(interval
, latestBranch
.size() - 1);
380 * Inner method to find in which node we should add the interval.
383 * The interval to add to the tree
385 * The index *in the latestBranch* where we are trying the
388 private void tryInsertAtNode(HTInterval interval
, int indexOfNode
) {
389 HTNode targetNode
= latestBranch
.get(indexOfNode
);
391 /* Verify if there is enough room in this node to store this interval */
392 if (interval
.getIntervalSize() > targetNode
.getNodeFreeSpace()) {
393 /* Nope, not enough room. Insert in a new sibling instead. */
394 addSiblingNode(indexOfNode
);
395 tryInsertAtNode(interval
, latestBranch
.size() - 1);
399 /* Make sure the interval time range fits this node */
400 if (interval
.getStartTime() < targetNode
.getNodeStart()) {
402 * No, this interval starts before the startTime of this node. We
403 * need to check recursively in parents if it can fit.
405 assert (indexOfNode
>= 1);
406 tryInsertAtNode(interval
, indexOfNode
- 1);
411 * Ok, there is room, and the interval fits in this time slot. Let's add
414 targetNode
.addInterval(interval
);
416 /* Update treeEnd if needed */
417 if (interval
.getEndTime() > this.treeEnd
) {
418 this.treeEnd
= interval
.getEndTime();
423 * Method to add a sibling to any node in the latest branch. This will add
424 * children back down to the leaf level, if needed.
427 * The index in latestBranch where we start adding
429 private void addSiblingNode(int indexOfNode
) {
431 CoreNode newNode
, prevNode
;
432 long splitTime
= treeEnd
;
434 assert (indexOfNode
< latestBranch
.size());
436 /* Check if we need to add a new root node */
437 if (indexOfNode
== 0) {
442 /* Check if we can indeed add a child to the target parent */
443 if (latestBranch
.get(indexOfNode
- 1).getNbChildren() == config
.getMaxChildren()) {
444 /* If not, add a branch starting one level higher instead */
445 addSiblingNode(indexOfNode
- 1);
449 /* Split off the new branch from the old one */
450 for (i
= indexOfNode
; i
< latestBranch
.size(); i
++) {
451 latestBranch
.get(i
).closeThisNode(splitTime
);
452 treeIO
.writeNode(latestBranch
.get(i
));
454 prevNode
= latestBranch
.get(i
- 1);
455 newNode
= initNewCoreNode(prevNode
.getSequenceNumber(),
457 prevNode
.linkNewChild(newNode
);
459 latestBranch
.set(i
, newNode
);
464 * Similar to the previous method, except here we rebuild a completely new
467 private void addNewRootNode() {
469 CoreNode oldRootNode
, newRootNode
, newNode
, prevNode
;
470 long splitTime
= this.treeEnd
;
472 oldRootNode
= latestBranch
.get(0);
473 newRootNode
= initNewCoreNode(-1, config
.getTreeStart());
475 /* Tell the old root node that it isn't root anymore */
476 oldRootNode
.setParentSequenceNumber(newRootNode
.getSequenceNumber());
478 /* Close off the whole current latestBranch */
479 for (i
= 0; i
< latestBranch
.size(); i
++) {
480 latestBranch
.get(i
).closeThisNode(splitTime
);
481 treeIO
.writeNode(latestBranch
.get(i
));
484 /* Link the new root to its first child (the previous root node) */
485 newRootNode
.linkNewChild(oldRootNode
);
487 /* Rebuild a new latestBranch */
488 depth
= latestBranch
.size();
489 latestBranch
= new ArrayList
<CoreNode
>();
490 latestBranch
.add(newRootNode
);
491 for (i
= 1; i
< depth
+ 1; i
++) {
492 prevNode
= latestBranch
.get(i
- 1);
493 newNode
= initNewCoreNode(prevNode
.getParentSequenceNumber(),
495 prevNode
.linkNewChild(newNode
);
496 latestBranch
.add(newNode
);
501 * Add a new empty node to the tree.
503 * @param parentSeqNumber
504 * Sequence number of this node's parent
506 * Start time of the new node
507 * @return The newly created node
509 private CoreNode
initNewCoreNode(int parentSeqNumber
, long startTime
) {
510 CoreNode newNode
= new CoreNode(config
, this.nodeCount
, parentSeqNumber
,
514 /* Update the treeEnd if needed */
515 if (startTime
>= this.treeEnd
) {
516 this.treeEnd
= startTime
+ 1;
522 * Inner method to select the next child of the current node intersecting
523 * the given timestamp. Useful for moving down the tree following one
527 * The node on which the request is made
529 * The timestamp to choose which child is the next one
530 * @return The child node intersecting t
531 * @throws ClosedChannelException
532 * If the file channel was closed while we were reading the tree
534 HTNode
selectNextChild(CoreNode currentNode
, long t
) throws ClosedChannelException
{
535 assert (currentNode
.getNbChildren() > 0);
536 int potentialNextSeqNb
= currentNode
.getSequenceNumber();
538 for (int i
= 0; i
< currentNode
.getNbChildren(); i
++) {
539 if (t
>= currentNode
.getChildStart(i
)) {
540 potentialNextSeqNb
= currentNode
.getChild(i
);
547 * Once we exit this loop, we should have found a children to follow. If
548 * we didn't, there's a problem.
550 assert (potentialNextSeqNb
!= currentNode
.getSequenceNumber());
553 * Since this code path is quite performance-critical, avoid iterating
554 * through the whole latestBranch array if we know for sure the next
555 * node has to be on disk
557 if (currentNode
.isDone()) {
558 return treeIO
.readNode(potentialNextSeqNb
);
560 return treeIO
.readNode(potentialNextSeqNb
);
564 return config
.getStateFile().length();
567 // ------------------------------------------------------------------------
568 // Test/debugging methods
569 // ------------------------------------------------------------------------
571 /* Only used for debugging, shouldn't be externalized */
572 @SuppressWarnings("nls")
573 boolean checkNodeIntegrity(HTNode zenode
) {
577 StringBuffer buf
= new StringBuffer();
580 // FIXME /* Only testing Core Nodes for now */
581 if (!(zenode
instanceof CoreNode
)) {
585 node
= (CoreNode
) zenode
;
589 * Test that this node's start and end times match the start of the
590 * first child and the end of the last child, respectively
592 if (node
.getNbChildren() > 0) {
593 otherNode
= treeIO
.readNode(node
.getChild(0));
594 if (node
.getNodeStart() != otherNode
.getNodeStart()) {
595 buf
.append("Start time of node (" + node
.getNodeStart() + ") "
596 + "does not match start time of first child " + "("
597 + otherNode
.getNodeStart() + "), " + "node #"
598 + otherNode
.getSequenceNumber() + ")\n");
602 otherNode
= treeIO
.readNode(node
.getLatestChild());
603 if (node
.getNodeEnd() != otherNode
.getNodeEnd()) {
604 buf
.append("End time of node (" + node
.getNodeEnd()
605 + ") does not match end time of last child ("
606 + otherNode
.getNodeEnd() + ", node #"
607 + otherNode
.getSequenceNumber() + ")\n");
614 * Test that the childStartTimes[] array matches the real nodes' start
617 for (int i
= 0; i
< node
.getNbChildren(); i
++) {
618 otherNode
= treeIO
.readNode(node
.getChild(i
));
619 if (otherNode
.getNodeStart() != node
.getChildStart(i
)) {
620 buf
.append(" Expected start time of child node #"
621 + node
.getChild(i
) + ": " + node
.getChildStart(i
)
622 + "\n" + " Actual start time of node #"
623 + otherNode
.getSequenceNumber() + ": "
624 + otherNode
.getNodeStart() + "\n");
629 } catch (ClosedChannelException e
) {
634 System
.out
.println("");
635 System
.out
.println("SHT: Integrity check failed for node #"
636 + node
.getSequenceNumber() + ":");
637 System
.out
.println(buf
.toString());
642 void checkIntegrity() {
644 for (int i
= 0; i
< nodeCount
; i
++) {
645 checkNodeIntegrity(treeIO
.readNode(i
));
647 } catch (ClosedChannelException e
) {
651 /* Only used for debugging, shouldn't be externalized */
652 @SuppressWarnings("nls")
654 public String
toString() {
655 return "Information on the current tree:\n\n" + "Blocksize: "
656 + config
.getBlockSize() + "\n" + "Max nb. of children per node: "
657 + config
.getMaxChildren() + "\n" + "Number of nodes: " + nodeCount
658 + "\n" + "Depth of the tree: " + latestBranch
.size() + "\n"
659 + "Size of the treefile: " + this.getFileSize() + "\n"
660 + "Root node has sequence number: "
661 + latestBranch
.get(0).getSequenceNumber() + "\n"
662 + "'Latest leaf' has sequence number: "
663 + latestBranch
.get(latestBranch
.size() - 1).getSequenceNumber();
667 * Start at currentNode and print the contents of all its children, in
668 * pre-order. Give the root node in parameter to visit the whole tree, and
669 * have a nice overview.
671 /* Only used for debugging, shouldn't be externalized */
672 @SuppressWarnings("nls")
673 private void preOrderPrint(PrintWriter writer
, boolean printIntervals
,
674 CoreNode currentNode
, int curDepth
) {
676 writer
.println(currentNode
.toString());
677 if (printIntervals
) {
678 currentNode
.debugPrintIntervals(writer
);
682 for (int i
= 0; i
< currentNode
.getNbChildren(); i
++) {
683 HTNode nextNode
= treeIO
.readNode(currentNode
.getChild(i
));
684 assert (nextNode
instanceof CoreNode
); // TODO temporary
685 for (int j
= 0; j
< curDepth
; j
++) {
689 preOrderPrint(writer
, printIntervals
, (CoreNode
) nextNode
,
692 } catch (ClosedChannelException e
) {
698 * Print out the full tree for debugging purposes
701 * PrintWriter in which to write the output
702 * @param printIntervals
703 * Flag to enable full output of the interval information
705 void debugPrintFullTree(PrintWriter writer
, boolean printIntervals
) {
706 /* Only used for debugging, shouldn't be externalized */
708 this.preOrderPrint(writer
, false, latestBranch
.get(0), 0);
710 if (printIntervals
) {
711 writer
.println("\nDetails of intervals:"); //$NON-NLS-1$
712 this.preOrderPrint(writer
, true, latestBranch
.get(0), 0);
714 writer
.println('\n');