ss: History trees can define their own node types
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / classic / HistoryTreeClassic.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson, École Polytechnique de Montréal, and others
3 *
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
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Florian Wininger - Add Extension and Leaf Node
12 * Patrick Tasse - Add message to exceptions
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.classic;
16
17 import java.io.File;
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;
28
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;
39
40 import com.google.common.annotations.VisibleForTesting;
41 import com.google.common.collect.ImmutableList;
42
43 /**
44 * Meta-container for the History Tree. This structure contains all the
45 * high-level data relevant to the tree.
46 *
47 * @author Alexandre Montplaisir
48 */
49 public class HistoryTreeClassic implements IHistoryTree {
50
51 /**
52 * The magic number for this file format.
53 */
54 public static final int HISTORY_FILE_MAGIC_NUMBER = 0x05FFA900;
55
56 /** File format version. Increment when breaking compatibility. */
57 private static final int FILE_VERSION = 7;
58
59 private static final IHTNodeFactory CLASSIC_NODE_FACTORY = new IHTNodeFactory() {
60
61 @Override
62 public HTNode createCoreNode(HTConfig config, int seqNumber, int parentSeqNumber, long start) {
63 return new CoreNode(config, seqNumber, parentSeqNumber, start);
64 }
65
66 @Override
67 public HTNode createLeafNode(HTConfig config, int seqNumber, int parentSeqNumber, long start) {
68 return new LeafNode(config, seqNumber, parentSeqNumber, start);
69 }
70
71 };
72
73 // ------------------------------------------------------------------------
74 // Tree-specific configuration
75 // ------------------------------------------------------------------------
76
77 /** Container for all the configuration constants */
78 private final HTConfig fConfig;
79
80 /** Reader/writer object */
81 private final @NonNull HT_IO fTreeIO;
82
83 // ------------------------------------------------------------------------
84 // Variable Fields (will change throughout the existence of the SHT)
85 // ------------------------------------------------------------------------
86
87 /** Latest timestamp found in the tree (at any given moment) */
88 private long fTreeEnd;
89
90 /** The total number of nodes that exists in this tree */
91 private int fNodeCount;
92
93 /** "Cache" to keep the active nodes in memory */
94 private final @NonNull List<@NonNull HTNode> fLatestBranch;
95
96 // ------------------------------------------------------------------------
97 // Constructors/"Destructors"
98 // ------------------------------------------------------------------------
99
100 /**
101 * Create a new State History from scratch, using a {@link HTConfig} object
102 * for configuration.
103 *
104 * @param conf
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
109 */
110 public HistoryTreeClassic(HTConfig conf) throws IOException {
111 /*
112 * Simple check to make sure we have enough place in the 0th block for
113 * the tree configuration
114 */
115 if (conf.getBlockSize() < TREE_HEADER_SIZE) {
116 throw new IllegalArgumentException();
117 }
118
119 fConfig = conf;
120 fTreeEnd = conf.getTreeStart();
121 fNodeCount = 0;
122 fLatestBranch = Collections.synchronizedList(new ArrayList<>());
123
124 /* Prepare the IO object */
125 fTreeIO = new HT_IO(fConfig, true, CLASSIC_NODE_FACTORY);
126
127 /* Add the first node to the tree */
128 LeafNode firstNode = initNewLeafNode(-1, conf.getTreeStart());
129 fLatestBranch.add(firstNode);
130 }
131
132 /**
133 * "Reader" constructor : instantiate a SHTree from an existing tree file on
134 * disk
135 *
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
142 */
143 public HistoryTreeClassic(File existingStateFile, int expProviderVersion) throws IOException {
144 /*
145 * Open the file ourselves, get the tree header information we need,
146 * then pass on the descriptor to the TreeIO object.
147 */
148 int rootNodeSeqNb, res;
149 int bs, maxc;
150 long startTime;
151
152 /* Java I/O mumbo jumbo... */
153 if (!existingStateFile.exists()) {
154 throw new IOException("Selected state file does not exist"); //$NON-NLS-1$
155 }
156 if (existingStateFile.length() <= 0) {
157 throw new IOException("Empty target file"); //$NON-NLS-1$
158 }
159
160 try (FileInputStream fis = new FileInputStream(existingStateFile);
161 FileChannel fc = fis.getChannel();) {
162
163 ByteBuffer buffer = ByteBuffer.allocate(TREE_HEADER_SIZE);
164 buffer.order(ByteOrder.LITTLE_ENDIAN);
165 buffer.clear();
166
167 res = fc.read(buffer);
168 if (res != TREE_HEADER_SIZE) {
169 throw new IOException("Invalid header size"); //$NON-NLS-1$
170 }
171
172 buffer.flip();
173
174 /*
175 * Check the magic number to make sure we're opening the right type
176 * of file
177 */
178 res = buffer.getInt();
179 if (res != HISTORY_FILE_MAGIC_NUMBER) {
180 throw new IOException("Wrong magic number"); //$NON-NLS-1$
181 }
182
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$
186 }
187
188 res = buffer.getInt(); /* Event handler's version number */
189 if (res != expProviderVersion &&
190 expProviderVersion != ITmfStateSystemBuilder.IGNORE_PROVIDER_VERSION) {
191 /*
192 * The existing history was built using an event handler that
193 * doesn't match the current one in the framework.
194 *
195 * Information could be all wrong. Instead of keeping an
196 * incorrect history file, a rebuild is done.
197 */
198 throw new IOException("Mismatching event handler versions"); //$NON-NLS-1$
199 }
200
201 bs = buffer.getInt(); /* Block Size */
202 maxc = buffer.getInt(); /* Max nb of children per node */
203
204 fNodeCount = buffer.getInt();
205 rootNodeSeqNb = buffer.getInt();
206 startTime = buffer.getLong();
207
208 fConfig = new HTConfig(existingStateFile, bs, maxc, expProviderVersion, startTime);
209 }
210
211 /*
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
214 * the SHT otherwise?
215 */
216 fTreeIO = new HT_IO(fConfig, false, CLASSIC_NODE_FACTORY);
217
218 fLatestBranch = buildLatestBranch(rootNodeSeqNb);
219 fTreeEnd = getRootNode().getNodeEnd();
220
221 /*
222 * Make sure the history start time we read previously is consistent
223 * with was is actually in the root node.
224 */
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$
228 }
229 }
230
231 /**
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,
234 * for example).
235 *
236 * @param rootNodeSeqNb
237 * The sequence number of the root node, so we know where to
238 * start
239 * @throws ClosedChannelException
240 */
241 private @NonNull List<@NonNull HTNode> buildLatestBranch(int rootNodeSeqNb) throws ClosedChannelException {
242 List<@NonNull HTNode> list = new ArrayList<>();
243
244 HTNode nextChildNode = fTreeIO.readNode(rootNodeSeqNb);
245 list.add(nextChildNode);
246
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);
251 }
252 return Collections.synchronizedList(list);
253 }
254
255 @Override
256 public void closeTree(long requestedEndTime) {
257 /* This is an important operation, queries can wait */
258 synchronized (fLatestBranch) {
259 /*
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
263 * those empty nodes.
264 *
265 * This won't be needed once extended nodes are implemented.
266 */
267 fTreeEnd = requestedEndTime;
268
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));
273 }
274
275 try (FileChannel fc = fTreeIO.getFcOut();) {
276 ByteBuffer buffer = ByteBuffer.allocate(TREE_HEADER_SIZE);
277 buffer.order(ByteOrder.LITTLE_ENDIAN);
278 buffer.clear();
279
280 /* Save the config of the tree to the header of the file */
281 fc.position(0);
282
283 buffer.putInt(HISTORY_FILE_MAGIC_NUMBER);
284
285 buffer.putInt(FILE_VERSION);
286 buffer.putInt(fConfig.getProviderVersion());
287
288 buffer.putInt(fConfig.getBlockSize());
289 buffer.putInt(fConfig.getMaxChildren());
290
291 buffer.putInt(fNodeCount);
292
293 /* root node seq. nb */
294 buffer.putInt(fLatestBranch.get(0).getSequenceNumber());
295
296 /* start time of this history */
297 buffer.putLong(fLatestBranch.get(0).getNodeStart());
298
299 buffer.flip();
300 int res = fc.write(buffer);
301 assert (res <= TREE_HEADER_SIZE);
302 /* done writing the file header */
303
304 } catch (IOException e) {
305 /*
306 * If we were able to write so far, there should not be any
307 * problem at this point...
308 */
309 throw new RuntimeException("State system write error"); //$NON-NLS-1$
310 }
311 }
312 }
313
314 // ------------------------------------------------------------------------
315 // Accessors
316 // ------------------------------------------------------------------------
317
318 @Override
319 public long getTreeStart() {
320 return fConfig.getTreeStart();
321 }
322
323 @Override
324 public long getTreeEnd() {
325 return fTreeEnd;
326 }
327
328 @Override
329 public int getNodeCount() {
330 return fNodeCount;
331 }
332
333 @Override
334 public HTNode getRootNode() {
335 return fLatestBranch.get(0);
336 }
337
338 /**
339 * Return the latest branch of the tree. That branch is immutable. Used for
340 * unit testing and debugging.
341 *
342 * @return The immutable latest branch
343 */
344 @VisibleForTesting
345 protected List<@NonNull HTNode> getLatestBranch() {
346 return ImmutableList.copyOf(fLatestBranch);
347 }
348
349 /**
350 * Read a node at sequence number
351 *
352 * @param seqNum
353 * The sequence number of the node to read
354 * @return The HTNode object
355 * @throws ClosedChannelException
356 * Exception thrown when reading the node
357 */
358 @VisibleForTesting
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) {
363 return node;
364 }
365 }
366 return fTreeIO.readNode(seqNum);
367 }
368
369 /**
370 * Retrieve the TreeIO object. Should only be used for testing.
371 *
372 * @return The TreeIO
373 */
374 @VisibleForTesting
375 protected @NonNull HT_IO getTreeIO() {
376 return fTreeIO;
377 }
378
379 // ------------------------------------------------------------------------
380 // HT_IO interface
381 // ------------------------------------------------------------------------
382
383 @Override
384 public FileInputStream supplyATReader() {
385 return fTreeIO.supplyATReader(getNodeCount());
386 }
387
388 @Override
389 public File supplyATWriterFile() {
390 return fConfig.getStateFile();
391 }
392
393 @Override
394 public long supplyATWriterFilePos() {
395 return IHistoryTree.TREE_HEADER_SIZE
396 + ((long) getNodeCount() * fConfig.getBlockSize());
397 }
398
399 @Override
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) {
405 return node;
406 }
407 }
408 }
409
410 /* Read the node from disk */
411 return fTreeIO.readNode(seqNumber);
412 }
413
414 @Override
415 public void writeNode(HTNode node) {
416 fTreeIO.writeNode(node);
417 }
418
419 @Override
420 public void closeFile() {
421 fTreeIO.closeFile();
422 }
423
424 @Override
425 public void deleteFile() {
426 fTreeIO.deleteFile();
427 }
428
429 // ------------------------------------------------------------------------
430 // Operations
431 // ------------------------------------------------------------------------
432
433 @Override
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$
437 }
438 tryInsertAtNode(interval, fLatestBranch.size() - 1);
439 }
440
441 /**
442 * Inner method to find in which node we should add the interval.
443 *
444 * @param interval
445 * The interval to add to the tree
446 * @param indexOfNode
447 * The index *in the latestBranch* where we are trying the
448 * insertion
449 */
450 private void tryInsertAtNode(HTInterval interval, int indexOfNode) {
451 HTNode targetNode = fLatestBranch.get(indexOfNode);
452
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);
458 return;
459 }
460
461 /* Make sure the interval time range fits this node */
462 if (interval.getStartTime() < targetNode.getNodeStart()) {
463 /*
464 * No, this interval starts before the startTime of this node. We
465 * need to check recursively in parents if it can fit.
466 */
467 tryInsertAtNode(interval, indexOfNode - 1);
468 return;
469 }
470
471 /*
472 * Ok, there is room, and the interval fits in this time slot. Let's add
473 * it.
474 */
475 targetNode.addInterval(interval);
476
477 /* Update treeEnd if needed */
478 if (interval.getEndTime() > fTreeEnd) {
479 fTreeEnd = interval.getEndTime();
480 }
481 }
482
483 /**
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.
486 *
487 * @param indexOfNode
488 * The index in latestBranch where we start adding
489 */
490 private void addSiblingNode(int indexOfNode) {
491 synchronized (fLatestBranch) {
492 final long splitTime = fTreeEnd;
493
494 if (indexOfNode >= fLatestBranch.size()) {
495 /*
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.
498 */
499 throw new IllegalStateException();
500 }
501
502 /* Check if we need to add a new root node */
503 if (indexOfNode == 0) {
504 addNewRootNode();
505 return;
506 }
507
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);
512 return;
513 }
514
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));
519
520 ParentNode prevNode = (ParentNode) fLatestBranch.get(i - 1);
521 HTNode newNode;
522
523 switch (fLatestBranch.get(i).getNodeType()) {
524 case CORE:
525 newNode = initNewCoreNode(prevNode.getSequenceNumber(), splitTime + 1);
526 break;
527 case LEAF:
528 newNode = initNewLeafNode(prevNode.getSequenceNumber(), splitTime + 1);
529 break;
530 default:
531 throw new IllegalStateException();
532 }
533
534 prevNode.linkNewChild(newNode);
535 fLatestBranch.set(i, newNode);
536 }
537 }
538 }
539
540 /**
541 * Similar to the previous method, except here we rebuild a completely new
542 * latestBranch
543 */
544 private void addNewRootNode() {
545 final long splitTime = fTreeEnd;
546
547 HTNode oldRootNode = fLatestBranch.get(0);
548 ParentNode newRootNode = initNewCoreNode(-1, fConfig.getTreeStart());
549
550 /* Tell the old root node that it isn't root anymore */
551 oldRootNode.setParentSequenceNumber(newRootNode.getSequenceNumber());
552
553 /* Close off the whole current latestBranch */
554
555 for (int i = 0; i < fLatestBranch.size(); i++) {
556 fLatestBranch.get(i).closeThisNode(splitTime);
557 fTreeIO.writeNode(fLatestBranch.get(i));
558 }
559
560 /* Link the new root to its first child (the previous root node) */
561 newRootNode.linkNewChild(oldRootNode);
562
563 /* Rebuild a new latestBranch */
564 int depth = fLatestBranch.size();
565 fLatestBranch.clear();
566 fLatestBranch.add(newRootNode);
567
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(),
572 splitTime + 1);
573 prevNode.linkNewChild(newNode);
574 fLatestBranch.add(newNode);
575 }
576
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);
582 }
583
584 /**
585 * Add a new empty core node to the tree.
586 *
587 * @param parentSeqNumber
588 * Sequence number of this node's parent
589 * @param startTime
590 * Start time of the new node
591 * @return The newly created node
592 */
593 private @NonNull ParentNode initNewCoreNode(int parentSeqNumber, long startTime) {
594 ParentNode newNode = new CoreNode(fConfig, fNodeCount, parentSeqNumber,
595 startTime);
596 fNodeCount++;
597 return newNode;
598 }
599
600 /**
601 * Add a new empty leaf node to the tree.
602 *
603 * @param parentSeqNumber
604 * Sequence number of this node's parent
605 * @param startTime
606 * Start time of the new node
607 * @return The newly created node
608 */
609 private @NonNull LeafNode initNewLeafNode(int parentSeqNumber, long startTime) {
610 LeafNode newNode = new LeafNode(fConfig, fNodeCount, parentSeqNumber,
611 startTime);
612 fNodeCount++;
613 return newNode;
614 }
615
616 @Override
617 public Collection<HTNode> selectNextChildren(ParentNode currentNode, long t) throws ClosedChannelException {
618 assert (currentNode.getNbChildren() > 0);
619 int potentialNextSeqNb = currentNode.getSequenceNumber();
620
621 for (int i = 0; i < currentNode.getNbChildren(); i++) {
622 if (t >= currentNode.getChildStart(i)) {
623 potentialNextSeqNb = currentNode.getChild(i);
624 } else {
625 break;
626 }
627 }
628
629 /*
630 * Once we exit this loop, we should have found a children to follow. If
631 * we didn't, there's a problem.
632 */
633 if (potentialNextSeqNb == currentNode.getSequenceNumber()) {
634 throw new IllegalStateException("No next child node found"); //$NON-NLS-1$
635 }
636
637 /*
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
641 */
642 if (currentNode.isOnDisk()) {
643 return Collections.singleton(fTreeIO.readNode(potentialNextSeqNb));
644 }
645 return Collections.singleton(readNode(potentialNextSeqNb));
646 }
647
648 @Override
649 public long getFileSize() {
650 return fConfig.getStateFile().length();
651 }
652
653 // ------------------------------------------------------------------------
654 // Test/debugging methods
655 // ------------------------------------------------------------------------
656
657 /* Only used for debugging, shouldn't be externalized */
658 @SuppressWarnings("nls")
659 @Override
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();
670 }
671
672 }
This page took 0.048331 seconds and 5 git commands to generate.