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
.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
.Vector
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
28 * Meta-container for the History Tree. This structure contains all the
29 * high-level data relevant to the tree.
36 private static final int HISTORY_FILE_MAGIC_NUMBER
= 0x05FFA900;
39 * File format version. Increment minor on backwards-compatible changes.
40 * Increment major + set minor back to 0 when breaking compatibility.
42 private static final int MAJOR_VERSION
= 3;
43 private static final byte MINOR_VERSION
= 0;
45 // ------------------------------------------------------------------------
46 // Tree-specific configuration
47 // ------------------------------------------------------------------------
49 /** Container for all the configuration constants */
50 protected final HTConfig config
;
52 /** Reader/writer object */
53 private final HT_IO treeIO
;
55 // ------------------------------------------------------------------------
56 // Variable Fields (will change throughout the existance of the SHT)
57 // ------------------------------------------------------------------------
59 /** Latest timestamp found in the tree (at any given moment) */
62 /** How many nodes exist in this tree, total */
63 private int nodeCount
;
65 /** "Cache" to keep the active nodes in memory */
66 protected Vector
<CoreNode
> latestBranch
;
68 // ------------------------------------------------------------------------
69 // Constructors/"Destructors"
70 // ------------------------------------------------------------------------
73 * Create a new State History from scratch, using a SHTConfig object for
76 private HistoryTree(HTConfig conf
) throws IOException
{
78 * Simple check to make sure we have enough place in the 0th block
79 * for the tree configuration
81 if (conf
.blockSize
< getTreeHeaderSize()) {
82 throw new IllegalArgumentException();
86 treeEnd
= conf
.treeStart
;
88 latestBranch
= new Vector
<CoreNode
>();
90 /* Prepare the IO object */
91 treeIO
= new HT_IO(this, true);
93 /* Add the first node to the tree */
94 CoreNode firstNode
= initNewCoreNode(-1, conf
.treeStart
);
95 latestBranch
.add(firstNode
);
99 * "New State History" constructor, which doesn't use HTConfig but the
100 * individual values separately. Kept for now for backwards compatibility,
101 * but you should definitely consider using SHTConfig instead (since its
102 * contents can then change without directly affecting SHT's API).
105 HistoryTree(File newStateFile
, int blockSize
, int maxChildren
,
106 long startTime
) throws IOException
{
107 this(new HTConfig(newStateFile
, blockSize
, maxChildren
, startTime
));
111 * "Reader" constructor : instantiate a SHTree from an existing tree file on
114 * @param existingFileName
115 * Path/filename of the history-file we are to open
116 * @throws IOException
118 HistoryTree(File existingStateFile
) throws IOException
{
120 * Open the file ourselves, get the tree header information we need,
121 * then pass on the descriptor to the TreeIO object.
123 int rootNodeSeqNb
, res
;
127 /* Java I/O mumbo jumbo... */
128 if (!existingStateFile
.exists()) {
129 throw new IOException("Selected state file does not exist"); //$NON-NLS-1$
131 if (existingStateFile
.length() <= 0) {
132 throw new IOException("Invalid state file selected, " + //$NON-NLS-1$
133 "target file is empty"); //$NON-NLS-1$
136 FileInputStream fis
= new FileInputStream(existingStateFile
);
137 ByteBuffer buffer
= ByteBuffer
.allocate(getTreeHeaderSize());
138 FileChannel fc
= fis
.getChannel();
139 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
145 * Check the magic number,to make sure we're opening the right type of
148 res
= buffer
.getInt();
149 if (res
!= HISTORY_FILE_MAGIC_NUMBER
) {
152 throw new IOException("Selected file does not" + //$NON-NLS-1$
153 "look like a History Tree file"); //$NON-NLS-1$
156 res
= buffer
.getInt(); /* Major version number */
157 if (res
!= MAJOR_VERSION
) {
160 throw new IOException("Select History Tree file is of an older " //$NON-NLS-1$
161 + "format. Please use a previous version of " //$NON-NLS-1$
162 + "the parser to open it."); //$NON-NLS-1$
165 res
= buffer
.getInt(); /* Minor version number */
167 bs
= buffer
.getInt(); /* Block Size */
168 maxc
= buffer
.getInt(); /* Max nb of children per node */
170 this.nodeCount
= buffer
.getInt();
171 rootNodeSeqNb
= buffer
.getInt();
172 startTime
= buffer
.getLong();
174 this.config
= new HTConfig(existingStateFile
, bs
, maxc
, startTime
);
178 * FIXME We close fis here and the TreeIO will then reopen the same
179 * file, not extremely elegant. But how to pass the information here to
182 this.treeIO
= new HT_IO(this, false);
184 rebuildLatestBranch(rootNodeSeqNb
);
185 this.treeEnd
= latestBranch
.firstElement().getNodeEnd();
188 * Make sure the history start time we read previously is consistent
189 * with was is actually in the root node.
191 if (startTime
!= latestBranch
.firstElement().getNodeStart()) {
194 throw new IOException("Inconsistent start times in the" + //$NON-NLS-1$
195 "history file, it might be corrupted."); //$NON-NLS-1$
200 * "Save" the tree to disk. This method will cause the treeIO object to
201 * commit all nodes to disk and then return the RandomAccessFile descriptor
202 * so the Tree object can save its configuration into the header of the
205 * @param requestedEndTime
207 void closeTree(long requestedEndTime
) {
213 * Work-around the "empty branches" that get created when the root node
214 * becomes full. Overwrite the tree's end time with the original wanted
215 * end-time, to ensure no queries are sent into those empty nodes.
217 * This won't be needed once extended nodes are implemented.
219 this.treeEnd
= requestedEndTime
;
221 /* Close off the latest branch of the tree */
222 for (i
= 0; i
< latestBranch
.size(); i
++) {
223 latestBranch
.get(i
).closeThisNode(treeEnd
);
224 treeIO
.writeNode(latestBranch
.get(i
));
227 /* Only use this for debugging purposes, it's VERY slow! */
228 // this.checkIntegrity();
230 fc
= treeIO
.getFcOut();
231 buffer
= ByteBuffer
.allocate(getTreeHeaderSize());
232 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
235 /* Save the config of the tree to the header of the file */
239 buffer
.putInt(HISTORY_FILE_MAGIC_NUMBER
);
241 buffer
.putInt(MAJOR_VERSION
);
242 buffer
.putInt(MINOR_VERSION
);
244 buffer
.putInt(config
.blockSize
);
245 buffer
.putInt(config
.maxChildren
);
247 buffer
.putInt(nodeCount
);
249 /* root node seq. nb */
250 buffer
.putInt(latestBranch
.firstElement().getSequenceNumber());
252 /* start time of this history */
253 buffer
.putLong(latestBranch
.firstElement().getNodeStart());
256 res
= fc
.write(buffer
);
257 assert (res
<= getTreeHeaderSize());
258 /* done writing the file header */
260 } catch (IOException e
) {
261 /* We should not have any problems at this point... */
266 } catch (IOException e
) {
273 // ------------------------------------------------------------------------
275 // ------------------------------------------------------------------------
277 long getTreeStart() {
278 return config
.treeStart
;
293 // ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
298 * Rebuild the latestBranch "cache" object by reading the nodes from disk
299 * (When we are opening an existing file on disk and want to append to it,
302 * @param rootNodeSeqNb
303 * The sequence number of the root node, so we know where to
305 * @throws ClosedChannelException
307 private void rebuildLatestBranch(int rootNodeSeqNb
) throws ClosedChannelException
{
308 HTNode nextChildNode
;
310 this.latestBranch
= new Vector
<CoreNode
>();
312 nextChildNode
= treeIO
.readNodeFromDisk(rootNodeSeqNb
);
313 latestBranch
.add((CoreNode
) nextChildNode
);
314 while (latestBranch
.lastElement().getNbChildren() > 0) {
315 nextChildNode
= treeIO
.readNodeFromDisk(latestBranch
.lastElement().getLatestChild());
316 latestBranch
.add((CoreNode
) nextChildNode
);
321 * Insert an interval in the tree
325 void insertInterval(HTInterval interval
) throws TimeRangeException
{
326 if (interval
.getStartTime() < config
.treeStart
) {
327 throw new TimeRangeException();
329 tryInsertAtNode(interval
, latestBranch
.size() - 1);
333 * Inner method to find in which node we should add the interval.
336 * The interval to add to the tree
338 * The index *in the latestBranch* where we are trying the
341 private void tryInsertAtNode(HTInterval interval
, int indexOfNode
) {
342 HTNode targetNode
= latestBranch
.get(indexOfNode
);
344 /* Verify if there is enough room in this node to store this interval */
345 if (interval
.getIntervalSize() > targetNode
.getNodeFreeSpace()) {
346 /* Nope, not enough room. Insert in a new sibling instead. */
347 addSiblingNode(indexOfNode
);
348 tryInsertAtNode(interval
, latestBranch
.size() - 1);
352 /* Make sure the interval time range fits this node */
353 if (interval
.getStartTime() < targetNode
.getNodeStart()) {
355 * No, this interval starts before the startTime of this node. We
356 * need to check recursively in parents if it can fit.
358 assert (indexOfNode
>= 1);
359 tryInsertAtNode(interval
, indexOfNode
- 1);
364 * Ok, there is room, and the interval fits in this time slot. Let's add
367 targetNode
.addInterval(interval
);
369 /* Update treeEnd if needed */
370 if (interval
.getEndTime() > this.treeEnd
) {
371 this.treeEnd
= interval
.getEndTime();
377 * Method to add a sibling to any node in the latest branch. This will add
378 * children back down to the leaf level, if needed.
381 * The index in latestBranch where we start adding
383 private void addSiblingNode(int indexOfNode
) {
385 CoreNode newNode
, prevNode
;
386 long splitTime
= treeEnd
;
388 assert (indexOfNode
< latestBranch
.size());
390 /* Check if we need to add a new root node */
391 if (indexOfNode
== 0) {
396 /* Check if we can indeed add a child to the target parent */
397 if (latestBranch
.get(indexOfNode
- 1).getNbChildren() == config
.maxChildren
) {
398 /* If not, add a branch starting one level higher instead */
399 addSiblingNode(indexOfNode
- 1);
403 /* Split off the new branch from the old one */
404 for (i
= indexOfNode
; i
< latestBranch
.size(); i
++) {
405 latestBranch
.get(i
).closeThisNode(splitTime
);
406 treeIO
.writeNode(latestBranch
.get(i
));
408 prevNode
= latestBranch
.get(i
- 1);
409 newNode
= initNewCoreNode(prevNode
.getSequenceNumber(),
411 prevNode
.linkNewChild(newNode
);
413 latestBranch
.set(i
, newNode
);
419 * Similar to the previous method, except here we rebuild a completely new
422 private void addNewRootNode() {
424 CoreNode oldRootNode
, newRootNode
, newNode
, prevNode
;
425 long splitTime
= this.treeEnd
;
427 oldRootNode
= latestBranch
.firstElement();
428 newRootNode
= initNewCoreNode(-1, config
.treeStart
);
430 /* Tell the old root node that it isn't root anymore */
431 oldRootNode
.setParentSequenceNumber(newRootNode
.getSequenceNumber());
433 /* Close off the whole current latestBranch */
434 for (i
= 0; i
< latestBranch
.size(); i
++) {
435 latestBranch
.get(i
).closeThisNode(splitTime
);
436 treeIO
.writeNode(latestBranch
.get(i
));
439 /* Link the new root to its first child (the previous root node) */
440 newRootNode
.linkNewChild(oldRootNode
);
442 /* Rebuild a new latestBranch */
443 depth
= latestBranch
.size();
444 latestBranch
= new Vector
<CoreNode
>();
445 latestBranch
.add(newRootNode
);
446 for (i
= 1; i
< depth
+ 1; i
++) {
447 prevNode
= latestBranch
.get(i
- 1);
448 newNode
= initNewCoreNode(prevNode
.getParentSequenceNumber(),
450 prevNode
.linkNewChild(newNode
);
451 latestBranch
.add(newNode
);
456 * Add a new empty node to the tree.
458 * @param parentSeqNumber
459 * Sequence number of this node's parent
461 * Start time of the new node
462 * @return The newly created node
464 private CoreNode
initNewCoreNode(int parentSeqNumber
, long startTime
) {
465 CoreNode newNode
= new CoreNode(this, this.nodeCount
, parentSeqNumber
,
469 /* Update the treeEnd if needed */
470 if (startTime
>= this.treeEnd
) {
471 this.treeEnd
= startTime
+ 1;
477 * Inner method to select the next child of the current node intersecting
478 * the given timestamp. Useful for moving down the tree following one
483 * @return The child node intersecting t
484 * @throws ClosedChannelException
485 * If the file channel was closed while we were reading the tree
487 HTNode
selectNextChild(CoreNode currentNode
, long t
) throws ClosedChannelException
{
488 assert (currentNode
.getNbChildren() > 0);
489 int potentialNextSeqNb
= currentNode
.getSequenceNumber();
491 for (int i
= 0; i
< currentNode
.getNbChildren(); i
++) {
492 if (t
>= currentNode
.getChildStart(i
)) {
493 potentialNextSeqNb
= currentNode
.getChild(i
);
499 * Once we exit this loop, we should have found a children to follow. If
500 * we didn't, there's a problem.
502 assert (potentialNextSeqNb
!= currentNode
.getSequenceNumber());
505 * Since this code path is quite performance-critical, avoid iterating
506 * through the whole latestBranch array if we know for sure the next
507 * node has to be on disk
509 if (currentNode
.isDone()) {
510 return treeIO
.readNodeFromDisk(potentialNextSeqNb
);
512 return treeIO
.readNode(potentialNextSeqNb
);
516 * Helper function to get the size of the "tree header" in the tree-file The
517 * nodes will use this offset to know where they should be in the file. This
518 * should always be a multiple of 4K.
520 static int getTreeHeaderSize() {
525 return config
.stateFile
.length();
528 // ------------------------------------------------------------------------
529 // Test/debugging methods
530 // ------------------------------------------------------------------------
532 /* Only used for debugging, shouldn't be externalized */
533 @SuppressWarnings("nls")
534 boolean checkNodeIntegrity(HTNode zenode
) {
538 StringBuffer buf
= new StringBuffer();
541 // FIXME /* Only testing Core Nodes for now */
542 if (!(zenode
instanceof CoreNode
)) {
546 node
= (CoreNode
) zenode
;
550 * Test that this node's start and end times match the start of the
551 * first child and the end of the last child, respectively
553 if (node
.getNbChildren() > 0) {
554 otherNode
= treeIO
.readNode(node
.getChild(0));
555 if (node
.getNodeStart() != otherNode
.getNodeStart()) {
556 buf
.append("Start time of node (" + node
.getNodeStart() + ") "
557 + "does not match start time of first child " + "("
558 + otherNode
.getNodeStart() + "), " + "node #"
559 + otherNode
.getSequenceNumber() + ")\n");
563 otherNode
= treeIO
.readNode(node
.getLatestChild());
564 if (node
.getNodeEnd() != otherNode
.getNodeEnd()) {
565 buf
.append("End time of node (" + node
.getNodeEnd()
566 + ") does not match end time of last child ("
567 + otherNode
.getNodeEnd() + ", node #"
568 + otherNode
.getSequenceNumber() + ")\n");
575 * Test that the childStartTimes[] array matches the real nodes' start
578 for (int i
= 0; i
< node
.getNbChildren(); i
++) {
579 otherNode
= treeIO
.readNode(node
.getChild(i
));
580 if (otherNode
.getNodeStart() != node
.getChildStart(i
)) {
581 buf
.append(" Expected start time of child node #"
582 + node
.getChild(i
) + ": " + node
.getChildStart(i
)
583 + "\n" + " Actual start time of node #"
584 + otherNode
.getSequenceNumber() + ": "
585 + otherNode
.getNodeStart() + "\n");
590 } catch (ClosedChannelException e
) {
595 System
.out
.println("");
596 System
.out
.println("SHT: Integrity check failed for node #"
597 + node
.getSequenceNumber() + ":");
598 System
.out
.println(buf
.toString());
603 void checkIntegrity() {
605 for (int i
= 0; i
< nodeCount
; i
++) {
606 checkNodeIntegrity(treeIO
.readNode(i
));
608 } catch (ClosedChannelException e
) {
613 /* Only used for debugging, shouldn't be externalized */
614 @SuppressWarnings("nls")
616 public String
toString() {
617 return "Information on the current tree:\n\n" + "Blocksize: "
618 + config
.blockSize
+ "\n" + "Max nb. of children per node: "
619 + config
.maxChildren
+ "\n" + "Number of nodes: " + nodeCount
620 + "\n" + "Depth of the tree: " + latestBranch
.size() + "\n"
621 + "Size of the treefile: " + this.getFileSize() + "\n"
622 + "Root node has sequence number: "
623 + latestBranch
.firstElement().getSequenceNumber() + "\n"
624 + "'Latest leaf' has sequence number: "
625 + latestBranch
.lastElement().getSequenceNumber();
628 private int curDepth
;
631 * Start at currentNode and print the contents of all its children, in
632 * pre-order. Give the root node in parameter to visit the whole tree, and
633 * have a nice overview.
635 @SuppressWarnings("nls")
636 private void preOrderPrint(PrintWriter writer
, boolean printIntervals
,
637 CoreNode currentNode
) {
638 /* Only used for debugging, shouldn't be externalized */
642 writer
.println(currentNode
.toString());
643 if (printIntervals
) {
644 currentNode
.debugPrintIntervals(writer
);
649 for (i
= 0; i
< currentNode
.getNbChildren(); i
++) {
650 nextNode
= treeIO
.readNode(currentNode
.getChild(i
));
651 assert (nextNode
instanceof CoreNode
); // TODO temporary
652 for (j
= 0; j
< curDepth
- 1; j
++) {
656 preOrderPrint(writer
, printIntervals
, (CoreNode
) nextNode
);
658 } catch (ClosedChannelException e
) {
666 * Print out the full tree for debugging purposes
669 * PrintWriter in which to write the output
670 * @param printIntervals
671 * Says if you want to output the full interval information
673 void debugPrintFullTree(PrintWriter writer
, boolean printIntervals
) {
674 /* Only used for debugging, shouldn't be externalized */
676 this.preOrderPrint(writer
, false, latestBranch
.firstElement());
678 if (printIntervals
) {
679 writer
.println("\nDetails of intervals:"); //$NON-NLS-1$
681 this.preOrderPrint(writer
, true, latestBranch
.firstElement());
683 writer
.println('\n');