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 - Keep interval list sorted on insert
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
;
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
.FileChannel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collections
;
24 import java
.util
.List
;
25 import java
.util
.concurrent
.locks
.ReentrantReadWriteLock
;
27 import org
.eclipse
.jdt
.annotation
.NonNull
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
32 import com
.google
.common
.collect
.Iterables
;
35 * The base class for all the types of nodes that go in the History Tree.
37 * @author Alexandre Montplaisir
39 public abstract class HTNode
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
48 public static enum NodeType
{
50 * Core node, which is a "front" node, at any level of the tree except
51 * the bottom-most one. It has children, and may have extensions.
55 * Leaf node, which is a node at the last bottom level of the tree. It
56 * cannot have any children or extensions.
61 * Determine a node type by reading a serialized byte.
64 * The byte representation of the node type
65 * @return The corresponding NodeType
67 * If the NodeType is unrecognized
69 public static NodeType
fromByte(byte rep
) throws IOException
{
76 throw new IOException();
81 * Get the byte representation of this node type. It can then be read
82 * with {@link #fromByte}.
84 * @return The byte matching this node type
86 public byte toByte() {
93 throw new IllegalStateException();
101 * 16 - 2x long (start time, end time)
102 * 16 - 3x int (seq number, parent seq number, intervalcount)
103 * 1 - byte (done or not)
106 private static final int COMMON_HEADER_SIZE
= Byte
.BYTES
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
115 /* Configuration of the History Tree to which belongs this node */
116 private final HTConfig fConfig
;
118 /* Time range of this node */
119 private final long fNodeStart
;
120 private long fNodeEnd
;
122 /* Sequence number = position in the node section of the file */
123 private final int fSequenceNumber
;
124 private int fParentSequenceNumber
; /* = -1 if this node is the root node */
126 /* Sum of bytes of all intervals in the node */
127 private int fSizeOfIntervalSection
;
129 /* True if this node was read from disk (meaning its end time is now fixed) */
130 private volatile boolean fIsOnDisk
;
132 /* Vector containing all the intervals contained in this node */
133 private final List
<HTInterval
> fIntervals
;
135 /* Lock used to protect the accesses to intervals, nodeEnd and such */
136 private final ReentrantReadWriteLock fRwl
= new ReentrantReadWriteLock(false);
142 * Configuration of the History Tree
144 * The (unique) sequence number assigned to this particular node
145 * @param parentSeqNumber
146 * The sequence number of this node's parent node
148 * The earliest timestamp stored in this node
150 protected HTNode(HTConfig config
, int seqNumber
, int parentSeqNumber
, long start
) {
153 fSequenceNumber
= seqNumber
;
154 fParentSequenceNumber
= parentSeqNumber
;
156 fSizeOfIntervalSection
= 0;
158 fIntervals
= new ArrayList
<>();
162 * Reader factory method. Build a Node object (of the right type) by reading
163 * a block in the file.
166 * Configuration of the History Tree
168 * FileChannel to the history file, ALREADY SEEKED at the start
171 * The factory to create the nodes for this tree
172 * @return The node object
173 * @throws IOException
174 * If there was an error reading from the file channel
176 public static final @NonNull HTNode
readNode(HTConfig config
, FileChannel fc
, IHistoryTree
.IHTNodeFactory nodeFactory
)
178 HTNode newNode
= null;
181 ByteBuffer buffer
= ByteBuffer
.allocate(config
.getBlockSize());
182 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
184 res
= fc
.read(buffer
);
185 assert (res
== config
.getBlockSize());
188 /* Read the common header part */
189 byte typeByte
= buffer
.get();
190 NodeType type
= NodeType
.fromByte(typeByte
);
191 long start
= buffer
.getLong();
192 long end
= buffer
.getLong();
193 int seqNb
= buffer
.getInt();
194 int parentSeqNb
= buffer
.getInt();
195 int intervalCount
= buffer
.getInt();
196 buffer
.get(); // TODO Used to be "isDone", to be removed from the header
198 /* Now the rest of the header depends on the node type */
202 newNode
= nodeFactory
.createCoreNode(config
, seqNb
, parentSeqNb
, start
);
203 newNode
.readSpecificHeader(buffer
);
208 newNode
= nodeFactory
.createLeafNode(config
, seqNb
, parentSeqNb
, start
);
209 newNode
.readSpecificHeader(buffer
);
213 /* Unrecognized node type */
214 throw new IOException();
218 * At this point, we should be done reading the header and 'buffer'
219 * should only have the intervals left
221 for (i
= 0; i
< intervalCount
; i
++) {
222 HTInterval interval
= HTInterval
.readFrom(buffer
);
223 newNode
.fIntervals
.add(interval
);
224 newNode
.fSizeOfIntervalSection
+= interval
.getSizeOnDisk();
227 /* Assign the node's other information we have read previously */
228 newNode
.fNodeEnd
= end
;
229 newNode
.fIsOnDisk
= true;
235 * Write this node to the given file channel.
238 * The file channel to write to (should be sought to be correct
240 * @throws IOException
241 * If there was an error writing
243 public final void writeSelf(FileChannel fc
) throws IOException
{
245 * Yes, we are taking the *read* lock here, because we are reading the
246 * information in the node to write it to disk.
248 fRwl
.readLock().lock();
250 final int blockSize
= fConfig
.getBlockSize();
252 ByteBuffer buffer
= ByteBuffer
.allocate(blockSize
);
253 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
256 /* Write the common header part */
257 buffer
.put(getNodeType().toByte());
258 buffer
.putLong(fNodeStart
);
259 buffer
.putLong(fNodeEnd
);
260 buffer
.putInt(fSequenceNumber
);
261 buffer
.putInt(fParentSequenceNumber
);
262 buffer
.putInt(fIntervals
.size());
263 buffer
.put((byte) 1); // TODO Used to be "isDone", to be removed from header
265 /* Now call the inner method to write the specific header part */
266 writeSpecificHeader(buffer
);
268 /* Back to us, we write the intervals */
269 fIntervals
.forEach(i
-> i
.writeInterval(buffer
));
270 if (blockSize
- buffer
.position() != getNodeFreeSpace()) {
271 throw new IllegalStateException("Wrong free space: Actual: " + (blockSize
- buffer
.position()) + ", Expected: " + getNodeFreeSpace()); //$NON-NLS-1$ //$NON-NLS-2$
274 * Fill the rest with zeros
276 while (buffer
.position() < blockSize
) {
277 buffer
.put((byte) 0);
280 /* Finally, write everything in the Buffer to disk */
282 int res
= fc
.write(buffer
);
283 if (res
!= blockSize
) {
284 throw new IllegalStateException("Wrong size of block written: Actual: " + res
+ ", Expected: " + blockSize
); //$NON-NLS-1$ //$NON-NLS-2$
288 fRwl
.readLock().unlock();
293 // ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
298 * Retrieve the history tree configuration used for this node.
300 * @return The history tree config
302 protected HTConfig
getConfig() {
307 * Get the start time of this node.
309 * @return The start time of this node
311 public long getNodeStart() {
316 * Get the end time of this node.
318 * @return The end time of this node
320 public long getNodeEnd() {
328 * Get the sequence number of this node.
330 * @return The sequence number of this node
332 public int getSequenceNumber() {
333 return fSequenceNumber
;
337 * Get the sequence number of this node's parent.
339 * @return The parent sequence number
341 public int getParentSequenceNumber() {
342 return fParentSequenceNumber
;
346 * Change this node's parent. Used when we create a new root node for
350 * The sequence number of the node that is the new parent
352 public void setParentSequenceNumber(int newParent
) {
353 fParentSequenceNumber
= newParent
;
357 * Return if this node is "done" (full and written to disk).
359 * @return If this node is done or not
361 public boolean isOnDisk() {
366 * Add an interval to this node
369 * Interval to add to this node
371 public void addInterval(HTInterval newInterval
) {
372 fRwl
.writeLock().lock();
374 /* Just in case, should be checked before even calling this function */
375 assert (newInterval
.getSizeOnDisk() <= getNodeFreeSpace());
377 /* Find the insert position to keep the list sorted */
378 int index
= fIntervals
.size();
379 while (index
> 0 && newInterval
.compareTo(fIntervals
.get(index
- 1)) < 0) {
383 fIntervals
.add(index
, newInterval
);
384 fSizeOfIntervalSection
+= newInterval
.getSizeOnDisk();
387 fRwl
.writeLock().unlock();
392 * We've received word from the containerTree that newest nodes now exist to
393 * our right. (Puts isDone = true and sets the endtime)
396 * The nodeEnd time that the node will have
398 public void closeThisNode(long endtime
) {
399 fRwl
.writeLock().lock();
402 * FIXME: was assert (endtime >= fNodeStart); but that exception
403 * is reached with an empty node that has start time endtime + 1
405 // if (endtime < fNodeStart) {
406 // throw new IllegalArgumentException("Endtime " + endtime + " cannot be lower than start time " + fNodeStart);
409 if (!fIntervals
.isEmpty()) {
411 * Make sure there are no intervals in this node with their
412 * EndTime > the one requested. Only need to check the last one
413 * since they are sorted
415 if (endtime
< Iterables
.getLast(fIntervals
).getEndTime()) {
416 throw new IllegalArgumentException("Closing end time should be greater than or equal to the end time of the intervals of this node"); //$NON-NLS-1$
422 fRwl
.writeLock().unlock();
427 * The method to fill up the stateInfo (passed on from the Current State
428 * Tree when it does a query on the SHT). We'll replace the data in that
429 * vector with whatever relevant we can find from this node
432 * The same stateInfo that comes from SHT's doQuery()
434 * The timestamp for which the query is for. Only return
435 * intervals that intersect t.
436 * @throws TimeRangeException
439 public void writeInfoFromNode(List
<ITmfStateInterval
> stateInfo
, long t
)
440 throws TimeRangeException
{
441 /* This is from a state system query, we are "reading" this node */
442 fRwl
.readLock().lock();
444 for (int i
= getStartIndexFor(t
); i
< fIntervals
.size(); i
++) {
446 * Now we only have to compare the Start times, since we now the
447 * End times necessarily fit.
449 * Second condition is to ignore new attributes that might have
450 * been created after stateInfo was instantiated (they would be
453 ITmfStateInterval interval
= fIntervals
.get(i
);
454 if (t
>= interval
.getStartTime() &&
455 interval
.getAttribute() < stateInfo
.size()) {
456 stateInfo
.set(interval
.getAttribute(), interval
);
460 fRwl
.readLock().unlock();
465 * Get a single Interval from the information in this node If the
466 * key/timestamp pair cannot be found, we return null.
469 * The attribute quark to look for
472 * @return The Interval containing the information we want, or null if it
474 * @throws TimeRangeException
477 public HTInterval
getRelevantInterval(int key
, long t
) throws TimeRangeException
{
478 fRwl
.readLock().lock();
480 for (int i
= getStartIndexFor(t
); i
< fIntervals
.size(); i
++) {
481 HTInterval curInterval
= fIntervals
.get(i
);
482 if (curInterval
.getAttribute() == key
483 && curInterval
.getStartTime() <= t
484 && curInterval
.getEndTime() >= t
) {
489 /* We didn't find the relevant information in this node */
493 fRwl
.readLock().unlock();
497 private int getStartIndexFor(long t
) throws TimeRangeException
{
498 /* Should only be called by methods with the readLock taken */
500 if (fIntervals
.isEmpty()) {
504 * Since the intervals are sorted by end time, we can skip all the ones
505 * at the beginning whose end times are smaller than 't'. Java does
506 * provides a .binarySearch method, but its API is quite weird...
508 HTInterval dummy
= new HTInterval(0, t
, 0, TmfStateValue
.nullValue());
509 int index
= Collections
.binarySearch(fIntervals
, dummy
);
513 * .binarySearch returns a negative number if the exact value was
514 * not found. Here we just want to know where to start searching, we
515 * don't care if the value is exact or not.
521 * Another API quirkiness, the returned index is the one of the *last*
522 * element of a series of equal endtimes, which happens sometimes. We
523 * want the *first* element of such a series, to read through them
527 && fIntervals
.get(index
- 1).compareTo(fIntervals
.get(index
)) == 0) {
536 * Return the total header size of this node (will depend on the node type).
538 * @return The total header size
540 public final int getTotalHeaderSize() {
541 return COMMON_HEADER_SIZE
+ getSpecificHeaderSize();
545 * @return The offset, within the node, where the Data section ends
547 private int getDataSectionEndOffset() {
548 return getTotalHeaderSize() + fSizeOfIntervalSection
;
552 * Returns the free space in the node, which is simply put, the
553 * stringSectionOffset - dataSectionOffset
555 * @return The amount of free space in the node (in bytes)
557 public int getNodeFreeSpace() {
558 fRwl
.readLock().lock();
559 int ret
= fConfig
.getBlockSize() - getDataSectionEndOffset();
560 fRwl
.readLock().unlock();
566 * Returns the current space utilization of this node, as a percentage.
567 * (used space / total usable space, which excludes the header)
569 * @return The percentage (value between 0 and 100) of space utilization in
572 public long getNodeUsagePercent() {
573 fRwl
.readLock().lock();
575 final int blockSize
= fConfig
.getBlockSize();
576 float freePercent
= (float) getNodeFreeSpace()
577 / (float) (blockSize
- getTotalHeaderSize())
579 return (long) (100L - freePercent
);
582 fRwl
.readLock().unlock();
587 * @name Debugging functions
590 @SuppressWarnings("nls")
592 public String
toString() {
593 /* Only used for debugging, shouldn't be externalized */
594 return String
.format("Node #%d, %s, %s, %d intervals (%d%% used), [%d - %s]",
596 (fParentSequenceNumber
== -1) ?
"Root" : "Parent #" + fParentSequenceNumber
,
599 getNodeUsagePercent(),
601 (fIsOnDisk
|| fNodeEnd
!= 0) ? fNodeEnd
: "...");
605 * Debugging function that prints out the contents of this node
608 * PrintWriter in which we will print the debug output
610 @SuppressWarnings("nls")
611 public void debugPrintIntervals(PrintWriter writer
) {
612 /* Only used for debugging, shouldn't be externalized */
613 writer
.println("Intervals for node #" + fSequenceNumber
+ ":");
615 /* Array of children */
616 if (getNodeType() != NodeType
.LEAF
) { /* Only Core Nodes can have children */
617 ParentNode thisNode
= (ParentNode
) this;
618 writer
.print(" " + thisNode
.getNbChildren() + " children");
619 if (thisNode
.getNbChildren() >= 1) {
620 writer
.print(": [ " + thisNode
.getChild(0));
621 for (int i
= 1; i
< thisNode
.getNbChildren(); i
++) {
622 writer
.print(", " + thisNode
.getChild(i
));
629 /* List of intervals in the node */
630 writer
.println(" Intervals contained:");
631 for (int i
= 0; i
< fIntervals
.size(); i
++) {
632 writer
.println(fIntervals
.get(i
).toString());
634 writer
.println('\n');
637 // ------------------------------------------------------------------------
639 // ------------------------------------------------------------------------
642 * Get the byte value representing the node type.
644 * @return The node type
646 public abstract NodeType
getNodeType();
649 * Return the specific header size of this node. This means the size
650 * occupied by the type-specific section of the header (not counting the
653 * @return The specific header size
655 protected abstract int getSpecificHeaderSize();
658 * Read the type-specific part of the node header from a byte buffer.
661 * The byte buffer to read from. It should be already positioned
664 protected abstract void readSpecificHeader(ByteBuffer buffer
);
667 * Write the type-specific part of the header in a byte buffer.
670 * The buffer to write to. It should already be at the correct
673 protected abstract void writeSpecificHeader(ByteBuffer buffer
);
676 * Node-type-specific toString method. Used for debugging.
678 * @return A string representing the node
680 protected abstract String
toStringSpecific();