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