| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are made |
| 5 | * 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 | * Matthew Khouzam - Initial API and implementation |
| 11 | * Alexandre Montplaisir - Initial API and implementation |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.ctf.core.trace; |
| 15 | |
| 16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collections; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.List; |
| 23 | import java.util.PriorityQueue; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.eclipse.tracecompass.ctf.core.CTFException; |
| 27 | import org.eclipse.tracecompass.ctf.core.event.EventDefinition; |
| 28 | import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration; |
| 29 | import org.eclipse.tracecompass.internal.ctf.core.Activator; |
| 30 | import org.eclipse.tracecompass.internal.ctf.core.trace.StreamInputReaderTimestampComparator; |
| 31 | |
| 32 | /** |
| 33 | * A CTF trace reader. Reads the events of a trace. |
| 34 | * |
| 35 | * @version 1.0 |
| 36 | * @author Matthew Khouzam |
| 37 | * @author Alexandre Montplaisir |
| 38 | */ |
| 39 | public class CTFTraceReader implements AutoCloseable { |
| 40 | |
| 41 | private static final int LINE_LENGTH = 60; |
| 42 | |
| 43 | private static final int MIN_PRIO_SIZE = 16; |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Attributes |
| 47 | // ------------------------------------------------------------------------ |
| 48 | |
| 49 | /** |
| 50 | * The trace to read from. |
| 51 | */ |
| 52 | private final CTFTrace fTrace; |
| 53 | |
| 54 | /** |
| 55 | * Vector of all the trace file readers. |
| 56 | */ |
| 57 | private final List<CTFStreamInputReader> fStreamInputReaders = Collections.synchronizedList(new ArrayList<CTFStreamInputReader>()); |
| 58 | |
| 59 | /** |
| 60 | * Priority queue to order the trace file readers by timestamp. |
| 61 | */ |
| 62 | private PriorityQueue<CTFStreamInputReader> fPrio; |
| 63 | |
| 64 | /** |
| 65 | * Array to count the number of event per trace file. |
| 66 | */ |
| 67 | private long[] fEventCountPerTraceFile; |
| 68 | |
| 69 | /** |
| 70 | * Timestamp of the first event in the trace |
| 71 | */ |
| 72 | private long fStartTime; |
| 73 | |
| 74 | /** |
| 75 | * Timestamp of the last event read so far |
| 76 | */ |
| 77 | private long fEndTime; |
| 78 | |
| 79 | /** |
| 80 | * Boolean to indicate if the CTFTraceReader has been closed |
| 81 | */ |
| 82 | private boolean fClosed = false; |
| 83 | |
| 84 | // ------------------------------------------------------------------------ |
| 85 | // Constructors |
| 86 | // ------------------------------------------------------------------------ |
| 87 | |
| 88 | /** |
| 89 | * Constructs a TraceReader to read a trace. |
| 90 | * |
| 91 | * @param trace |
| 92 | * The trace to read from. |
| 93 | * @throws CTFException |
| 94 | * if an error occurs |
| 95 | */ |
| 96 | public CTFTraceReader(CTFTrace trace) throws CTFException { |
| 97 | fTrace = trace; |
| 98 | fStreamInputReaders.clear(); |
| 99 | |
| 100 | /** |
| 101 | * Create the trace file readers. |
| 102 | */ |
| 103 | createStreamInputReaders(); |
| 104 | |
| 105 | /** |
| 106 | * Populate the timestamp-based priority queue. |
| 107 | */ |
| 108 | populateStreamInputReaderHeap(); |
| 109 | |
| 110 | /** |
| 111 | * Get the start Time of this trace bear in mind that the trace could be |
| 112 | * empty. |
| 113 | */ |
| 114 | fStartTime = 0; |
| 115 | if (hasMoreEvents()) { |
| 116 | fStartTime = checkNotNull(getTopStream().getCurrentEvent()).getTimestamp(); |
| 117 | setEndTime(fStartTime); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Copy constructor |
| 123 | * |
| 124 | * @return The new CTFTraceReader |
| 125 | * @throws CTFException |
| 126 | * if an error occurs |
| 127 | */ |
| 128 | public CTFTraceReader copyFrom() throws CTFException { |
| 129 | CTFTraceReader newReader = null; |
| 130 | |
| 131 | newReader = new CTFTraceReader(fTrace); |
| 132 | newReader.fStartTime = fStartTime; |
| 133 | newReader.setEndTime(fEndTime); |
| 134 | return newReader; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Dispose the CTFTraceReader |
| 139 | */ |
| 140 | @Override |
| 141 | public void close() { |
| 142 | synchronized (fStreamInputReaders) { |
| 143 | for (CTFStreamInputReader reader : fStreamInputReaders) { |
| 144 | if (reader != null) { |
| 145 | try { |
| 146 | reader.close(); |
| 147 | } catch (IOException e) { |
| 148 | Activator.logError(e.getMessage(), e); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | fStreamInputReaders.clear(); |
| 153 | } |
| 154 | fPrio.clear(); |
| 155 | fClosed = true; |
| 156 | } |
| 157 | |
| 158 | // ------------------------------------------------------------------------ |
| 159 | // Getters/Setters/Predicates |
| 160 | // ------------------------------------------------------------------------ |
| 161 | |
| 162 | /** |
| 163 | * Return the start time of this trace (== timestamp of the first event) |
| 164 | * |
| 165 | * @return the trace start time |
| 166 | */ |
| 167 | public long getStartTime() { |
| 168 | return fStartTime; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Set the trace's end time |
| 173 | * |
| 174 | * @param endTime |
| 175 | * The end time to use |
| 176 | */ |
| 177 | protected final void setEndTime(long endTime) { |
| 178 | fEndTime = endTime; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get the priority queue of this trace reader. |
| 183 | * |
| 184 | * @return The priority queue of input readers |
| 185 | */ |
| 186 | protected PriorityQueue<CTFStreamInputReader> getPrio() { |
| 187 | return fPrio; |
| 188 | } |
| 189 | |
| 190 | // ------------------------------------------------------------------------ |
| 191 | // Operations |
| 192 | // ------------------------------------------------------------------------ |
| 193 | |
| 194 | /** |
| 195 | * Creates one trace file reader per trace file contained in the trace. |
| 196 | * |
| 197 | * @throws CTFException |
| 198 | * if an error occurs |
| 199 | */ |
| 200 | private void createStreamInputReaders() throws CTFException { |
| 201 | /* |
| 202 | * For each stream. |
| 203 | */ |
| 204 | for (CTFStream stream : fTrace.getStreams()) { |
| 205 | Set<CTFStreamInput> streamInputs = stream.getStreamInputs(); |
| 206 | |
| 207 | /* |
| 208 | * For each trace file of the stream. |
| 209 | */ |
| 210 | for (CTFStreamInput streamInput : streamInputs) { |
| 211 | |
| 212 | /* |
| 213 | * Create a reader and add it to the group. |
| 214 | */ |
| 215 | fStreamInputReaders.add(new CTFStreamInputReader(checkNotNull(streamInput))); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Create the array to count the number of event per trace file. |
| 221 | */ |
| 222 | fEventCountPerTraceFile = new long[fStreamInputReaders.size()]; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Returns whether or not this CTFTraceReader has been closed |
| 227 | * |
| 228 | * @return true if it has been closed, false else |
| 229 | * @since 1.1 |
| 230 | */ |
| 231 | public boolean isClosed() { |
| 232 | return fClosed; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Update the priority queue to make it match the parent trace |
| 237 | * |
| 238 | * @throws CTFException |
| 239 | * An error occured |
| 240 | */ |
| 241 | public void update() throws CTFException { |
| 242 | Set<CTFStreamInputReader> readers = new HashSet<>(); |
| 243 | for (CTFStream stream : fTrace.getStreams()) { |
| 244 | Set<CTFStreamInput> streamInputs = stream.getStreamInputs(); |
| 245 | for (CTFStreamInput streamInput : streamInputs) { |
| 246 | /* |
| 247 | * Create a reader to check if it already exists in the list. If it doesn't, add it. |
| 248 | */ |
| 249 | try (CTFStreamInputReader streamInputReader = new CTFStreamInputReader(checkNotNull(streamInput))) { |
| 250 | if (!fStreamInputReaders.contains(streamInputReader)) { |
| 251 | CTFStreamInputReader streamInputReaderToAdd = new CTFStreamInputReader(checkNotNull(streamInput)); |
| 252 | streamInputReaderToAdd.readNextEvent(); |
| 253 | fStreamInputReaders.add(streamInputReaderToAdd); |
| 254 | readers.add(streamInputReaderToAdd); |
| 255 | } |
| 256 | } catch (IOException e) { |
| 257 | Activator.logError(e.getMessage(), e); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | long[] temp = fEventCountPerTraceFile; |
| 262 | fEventCountPerTraceFile = new long[readers.size() + temp.length]; |
| 263 | for (CTFStreamInputReader reader : readers) { |
| 264 | fPrio.add(reader); |
| 265 | } |
| 266 | for (int i = 0; i < temp.length; i++) { |
| 267 | fEventCountPerTraceFile[i] = temp[i]; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Gets an iterable of the stream input readers, useful for foreaches |
| 273 | * |
| 274 | * @return the iterable of the stream input readers |
| 275 | */ |
| 276 | public Iterable<IEventDeclaration> getEventDeclarations() { |
| 277 | Set<IEventDeclaration> retSet = new HashSet<>(); |
| 278 | for (CTFStreamInputReader sir : fStreamInputReaders) { |
| 279 | retSet.addAll(sir.getEventDeclarations()); |
| 280 | } |
| 281 | retSet.remove(null); |
| 282 | return retSet; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Initializes the priority queue used to choose the trace file with the |
| 287 | * lower next event timestamp. |
| 288 | * |
| 289 | * @throws CTFException |
| 290 | * if an error occurs |
| 291 | */ |
| 292 | private void populateStreamInputReaderHeap() throws CTFException { |
| 293 | if (fStreamInputReaders.isEmpty()) { |
| 294 | fPrio = new PriorityQueue<>(MIN_PRIO_SIZE, |
| 295 | new StreamInputReaderTimestampComparator()); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Create the priority queue with a size twice as bigger as the number |
| 301 | * of reader in order to avoid constant resizing. |
| 302 | */ |
| 303 | fPrio = new PriorityQueue<>( |
| 304 | Math.max(fStreamInputReaders.size() * 2, MIN_PRIO_SIZE), |
| 305 | new StreamInputReaderTimestampComparator()); |
| 306 | |
| 307 | int pos = 0; |
| 308 | |
| 309 | for (CTFStreamInputReader reader : fStreamInputReaders) { |
| 310 | /* |
| 311 | * Add each trace file reader in the priority queue, if we are able |
| 312 | * to read an event from it. |
| 313 | */ |
| 314 | CTFResponse readNextEvent = reader.readNextEvent(); |
| 315 | if (readNextEvent == CTFResponse.OK || readNextEvent == CTFResponse.WAIT) { |
| 316 | fPrio.add(reader); |
| 317 | |
| 318 | fEventCountPerTraceFile[pos] = 0; |
| 319 | reader.setName(pos); |
| 320 | |
| 321 | pos++; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Get the current event, which is the current event of the trace file |
| 328 | * reader with the lowest timestamp. |
| 329 | * |
| 330 | * @return An event definition, or null of the trace reader reached the end |
| 331 | * of the trace. |
| 332 | */ |
| 333 | public EventDefinition getCurrentEventDef() { |
| 334 | CTFStreamInputReader top = getTopStream(); |
| 335 | return (top != null) ? top.getCurrentEvent() : null; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Go to the next event. |
| 340 | * |
| 341 | * @return True if an event was read. |
| 342 | * @throws CTFException |
| 343 | * if an error occurs |
| 344 | */ |
| 345 | public boolean advance() throws CTFException { |
| 346 | /* |
| 347 | * Remove the reader from the top of the priority queue. |
| 348 | */ |
| 349 | CTFStreamInputReader top = fPrio.poll(); |
| 350 | |
| 351 | /* |
| 352 | * If the queue was empty. |
| 353 | */ |
| 354 | if (top == null) { |
| 355 | return false; |
| 356 | } |
| 357 | /* |
| 358 | * Read the next event of this reader. |
| 359 | */ |
| 360 | switch (top.readNextEvent()) { |
| 361 | case OK: { |
| 362 | /* |
| 363 | * Add it back in the queue. |
| 364 | */ |
| 365 | fPrio.add(top); |
| 366 | /* |
| 367 | * We're in OK, there's a guaranteed top#getCurrentEvent() unless another |
| 368 | * thread does something bad. |
| 369 | */ |
| 370 | EventDefinition currentEvent = checkNotNull(top.getCurrentEvent()); |
| 371 | final long topEnd = fTrace.timestampCyclesToNanos(currentEvent.getTimestamp()); |
| 372 | setEndTime(Math.max(topEnd, getEndTime())); |
| 373 | fEventCountPerTraceFile[top.getName()]++; |
| 374 | fEndTime = Math.max(currentEvent.getTimestamp(), fEndTime); |
| 375 | break; |
| 376 | } |
| 377 | case WAIT: { |
| 378 | fPrio.add(top); |
| 379 | break; |
| 380 | } |
| 381 | case FINISH: |
| 382 | break; |
| 383 | case ERROR: |
| 384 | default: |
| 385 | // something bad happend |
| 386 | } |
| 387 | /* |
| 388 | * If there is no reader in the queue, it means the trace reader reached |
| 389 | * the end of the trace. |
| 390 | */ |
| 391 | return hasMoreEvents(); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Go to the last event in the trace. |
| 396 | * |
| 397 | * @throws CTFException |
| 398 | * if an error occurs |
| 399 | */ |
| 400 | public void goToLastEvent() throws CTFException { |
| 401 | seek(getEndTime()); |
| 402 | while (fPrio.size() > 1) { |
| 403 | advance(); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Seeks to a given timestamp. It will seek to the nearest event greater or |
| 409 | * equal to timestamp. If a trace is [10 20 30 40] and you are looking for |
| 410 | * 19, it will give you 20. If you want 20, you will get 20, if you want 21, |
| 411 | * you will get 30. The value -inf will seek to the first element and the |
| 412 | * value +inf will seek to the end of the file (past the last event). |
| 413 | * |
| 414 | * @param timestamp |
| 415 | * the timestamp to seek to |
| 416 | * @return true if there are events above or equal the seek timestamp, false |
| 417 | * if seek at the end of the trace (no valid event). |
| 418 | * @throws CTFException |
| 419 | * if an error occurs |
| 420 | */ |
| 421 | public boolean seek(long timestamp) throws CTFException { |
| 422 | /* |
| 423 | * Remove all the trace readers from the priority queue |
| 424 | */ |
| 425 | fPrio.clear(); |
| 426 | for (CTFStreamInputReader streamInputReader : fStreamInputReaders) { |
| 427 | /* |
| 428 | * Seek the trace reader. |
| 429 | */ |
| 430 | streamInputReader.seek(timestamp); |
| 431 | |
| 432 | /* |
| 433 | * Add it to the priority queue if there is a current event. |
| 434 | */ |
| 435 | if (streamInputReader.getCurrentEvent() != null) { |
| 436 | fPrio.add(streamInputReader); |
| 437 | } |
| 438 | } |
| 439 | return hasMoreEvents(); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Gets the stream with the oldest event |
| 444 | * |
| 445 | * @return the stream with the oldest event |
| 446 | */ |
| 447 | public CTFStreamInputReader getTopStream() { |
| 448 | return fPrio.peek(); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Does the trace have more events? |
| 453 | * |
| 454 | * @return true if yes. |
| 455 | */ |
| 456 | public final boolean hasMoreEvents() { |
| 457 | return fPrio.size() > 0; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Prints the event count stats. |
| 462 | */ |
| 463 | public void printStats() { |
| 464 | printStats(LINE_LENGTH); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Prints the event count stats. |
| 469 | * |
| 470 | * @param width |
| 471 | * Width of the display. |
| 472 | */ |
| 473 | public void printStats(int width) { |
| 474 | int numEvents = 0; |
| 475 | if (width == 0) { |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | for (long i : fEventCountPerTraceFile) { |
| 480 | numEvents += i; |
| 481 | } |
| 482 | |
| 483 | for (int j = 0; j < fEventCountPerTraceFile.length; j++) { |
| 484 | CTFStreamInputReader se = fStreamInputReaders.get(j); |
| 485 | |
| 486 | long len = (width * fEventCountPerTraceFile[se.getName()]) |
| 487 | / numEvents; |
| 488 | |
| 489 | StringBuilder sb = new StringBuilder(se.getFilename()); |
| 490 | sb.append("\t["); //$NON-NLS-1$ |
| 491 | |
| 492 | for (int i = 0; i < len; i++) { |
| 493 | sb.append('+'); |
| 494 | } |
| 495 | |
| 496 | for (long i = len; i < width; i++) { |
| 497 | sb.append(' '); |
| 498 | } |
| 499 | |
| 500 | sb.append("]\t" + fEventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$ |
| 501 | Activator.log(sb.toString()); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Gets the last event timestamp that was read. This is NOT necessarily the |
| 507 | * last event in a trace, just the last one read so far. |
| 508 | * |
| 509 | * @return the last event |
| 510 | */ |
| 511 | public long getEndTime() { |
| 512 | return fEndTime; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Sets a trace to be live or not |
| 517 | * |
| 518 | * @param live |
| 519 | * whether the trace is live |
| 520 | */ |
| 521 | public void setLive(boolean live) { |
| 522 | for (CTFStreamInputReader s : fPrio) { |
| 523 | s.setLive(live); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Get if the trace is to read live or not |
| 529 | * |
| 530 | * @return whether the trace is live or not |
| 531 | */ |
| 532 | public boolean isLive() { |
| 533 | return getTopStream().isLive(); |
| 534 | } |
| 535 | |
| 536 | @Override |
| 537 | public int hashCode() { |
| 538 | final int prime = 31; |
| 539 | int result = 1; |
| 540 | result = (prime * result) + (int) (fStartTime ^ (fStartTime >>> 32)); |
| 541 | result = (prime * result) + fStreamInputReaders.hashCode(); |
| 542 | result = (prime * result) + ((fTrace == null) ? 0 : fTrace.hashCode()); |
| 543 | return result; |
| 544 | } |
| 545 | |
| 546 | @Override |
| 547 | public boolean equals(Object obj) { |
| 548 | if (this == obj) { |
| 549 | return true; |
| 550 | } |
| 551 | if (obj == null) { |
| 552 | return false; |
| 553 | } |
| 554 | if (!(obj instanceof CTFTraceReader)) { |
| 555 | return false; |
| 556 | } |
| 557 | CTFTraceReader other = (CTFTraceReader) obj; |
| 558 | if (!fStreamInputReaders.equals(other.fStreamInputReaders)) { |
| 559 | return false; |
| 560 | } |
| 561 | if (fTrace == null) { |
| 562 | if (other.fTrace != null) { |
| 563 | return false; |
| 564 | } |
| 565 | } else if (!fTrace.equals(other.fTrace)) { |
| 566 | return false; |
| 567 | } |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public String toString() { |
| 573 | /* Only for debugging, shouldn't be externalized */ |
| 574 | return "CTFTraceReader [trace=" + fTrace + ']'; //$NON-NLS-1$ |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Gets the parent trace |
| 579 | * |
| 580 | * @return the parent trace |
| 581 | */ |
| 582 | public CTFTrace getTrace() { |
| 583 | return fTrace; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * This will read the entire trace and populate all the indexes. The reader |
| 588 | * will then be reset to the first event in the trace. |
| 589 | * |
| 590 | * Do not call in the fast path. |
| 591 | * |
| 592 | * @throws CTFException |
| 593 | * A trace reading error occurred |
| 594 | * @since 1.0 |
| 595 | */ |
| 596 | public void populateIndex() throws CTFException { |
| 597 | for (CTFStreamInputReader sir : fPrio) { |
| 598 | sir.goToLastEvent(); |
| 599 | } |
| 600 | seek(0); |
| 601 | |
| 602 | } |
| 603 | } |