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