1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
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
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.ctf
.core
.trace
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
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
;
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
;
33 * A CTF trace reader. Reads the events of a trace.
36 * @author Matthew Khouzam
37 * @author Alexandre Montplaisir
39 public class CTFTraceReader
implements AutoCloseable
{
41 private static final int LINE_LENGTH
= 60;
43 private static final int MIN_PRIO_SIZE
= 16;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * The trace to read from.
52 private final CTFTrace fTrace
;
55 * Vector of all the trace file readers.
57 private final List
<CTFStreamInputReader
> fStreamInputReaders
= Collections
.synchronizedList(new ArrayList
<CTFStreamInputReader
>());
60 * Priority queue to order the trace file readers by timestamp.
62 private PriorityQueue
<CTFStreamInputReader
> fPrio
;
65 * Array to count the number of event per trace file.
67 private long[] fEventCountPerTraceFile
;
70 * Timestamp of the first event in the trace
72 private long fStartTime
;
75 * Timestamp of the last event read so far
77 private long fEndTime
;
80 * Boolean to indicate if the CTFTraceReader has been closed
82 private boolean fClosed
= false;
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 * Constructs a TraceReader to read a trace.
92 * The trace to read from.
93 * @throws CTFException
96 public CTFTraceReader(CTFTrace trace
) throws CTFException
{
98 fStreamInputReaders
.clear();
101 * Create the trace file readers.
103 createStreamInputReaders();
106 * Populate the timestamp-based priority queue.
108 populateStreamInputReaderHeap();
111 * Get the start Time of this trace bear in mind that the trace could be
115 if (hasMoreEvents()) {
116 fStartTime
= checkNotNull(getTopStream().getCurrentEvent()).getTimestamp();
117 setEndTime(fStartTime
);
124 * @return The new CTFTraceReader
125 * @throws CTFException
128 public CTFTraceReader
copyFrom() throws CTFException
{
129 CTFTraceReader newReader
= null;
131 newReader
= new CTFTraceReader(fTrace
);
132 newReader
.fStartTime
= fStartTime
;
133 newReader
.setEndTime(fEndTime
);
138 * Dispose the CTFTraceReader
141 public void close() {
142 synchronized (fStreamInputReaders
) {
143 for (CTFStreamInputReader reader
: fStreamInputReaders
) {
144 if (reader
!= null) {
147 } catch (IOException e
) {
148 Activator
.logError(e
.getMessage(), e
);
152 fStreamInputReaders
.clear();
158 // ------------------------------------------------------------------------
159 // Getters/Setters/Predicates
160 // ------------------------------------------------------------------------
163 * Return the start time of this trace (== timestamp of the first event)
165 * @return the trace start time
167 public long getStartTime() {
172 * Set the trace's end time
175 * The end time to use
177 protected final void setEndTime(long endTime
) {
182 * Get the priority queue of this trace reader.
184 * @return The priority queue of input readers
186 protected PriorityQueue
<CTFStreamInputReader
> getPrio() {
190 // ------------------------------------------------------------------------
192 // ------------------------------------------------------------------------
195 * Creates one trace file reader per trace file contained in the trace.
197 * @throws CTFException
200 private void createStreamInputReaders() throws CTFException
{
204 for (CTFStream stream
: fTrace
.getStreams()) {
205 Set
<CTFStreamInput
> streamInputs
= stream
.getStreamInputs();
208 * For each trace file of the stream.
210 for (CTFStreamInput streamInput
: streamInputs
) {
213 * Create a reader and add it to the group.
215 fStreamInputReaders
.add(new CTFStreamInputReader(checkNotNull(streamInput
)));
220 * Create the array to count the number of event per trace file.
222 fEventCountPerTraceFile
= new long[fStreamInputReaders
.size()];
226 * Returns whether or not this CTFTraceReader has been closed
228 * @return true if it has been closed, false else
231 public boolean isClosed() {
236 * Update the priority queue to make it match the parent trace
238 * @throws CTFException
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
) {
249 CTFStreamInputReader streamInputReader
= new CTFStreamInputReader(checkNotNull(streamInput
));
253 * Add it to the group.
255 if (!fStreamInputReaders
.contains(streamInputReader
)) {
256 streamInputReader
.readNextEvent();
257 fStreamInputReaders
.add(streamInputReader
);
258 readers
.add(streamInputReader
);
262 long[] temp
= fEventCountPerTraceFile
;
263 fEventCountPerTraceFile
= new long[readers
.size() + temp
.length
];
264 for (CTFStreamInputReader reader
: readers
) {
267 for (int i
= 0; i
< temp
.length
; i
++) {
268 fEventCountPerTraceFile
[i
] = temp
[i
];
273 * Gets an iterable of the stream input readers, useful for foreaches
275 * @return the iterable of the stream input readers
277 public Iterable
<IEventDeclaration
> getEventDeclarations() {
278 Set
<IEventDeclaration
> retSet
= new HashSet
<>();
279 for (CTFStreamInputReader sir
: fStreamInputReaders
) {
280 retSet
.addAll(sir
.getEventDeclarations());
287 * Initializes the priority queue used to choose the trace file with the
288 * lower next event timestamp.
290 * @throws CTFException
293 private void populateStreamInputReaderHeap() throws CTFException
{
294 if (fStreamInputReaders
.isEmpty()) {
295 fPrio
= new PriorityQueue
<>(MIN_PRIO_SIZE
,
296 new StreamInputReaderTimestampComparator());
301 * Create the priority queue with a size twice as bigger as the number
302 * of reader in order to avoid constant resizing.
304 fPrio
= new PriorityQueue
<>(
305 Math
.max(fStreamInputReaders
.size() * 2, MIN_PRIO_SIZE
),
306 new StreamInputReaderTimestampComparator());
310 for (CTFStreamInputReader reader
: fStreamInputReaders
) {
312 * Add each trace file reader in the priority queue, if we are able
313 * to read an event from it.
315 CTFResponse readNextEvent
= reader
.readNextEvent();
316 if (readNextEvent
== CTFResponse
.OK
|| readNextEvent
== CTFResponse
.WAIT
) {
319 fEventCountPerTraceFile
[pos
] = 0;
328 * Get the current event, which is the current event of the trace file
329 * reader with the lowest timestamp.
331 * @return An event definition, or null of the trace reader reached the end
334 public EventDefinition
getCurrentEventDef() {
335 CTFStreamInputReader top
= getTopStream();
336 return (top
!= null) ? top
.getCurrentEvent() : null;
340 * Go to the next event.
342 * @return True if an event was read.
343 * @throws CTFException
346 public boolean advance() throws CTFException
{
348 * Remove the reader from the top of the priority queue.
350 CTFStreamInputReader top
= fPrio
.poll();
353 * If the queue was empty.
359 * Read the next event of this reader.
361 switch (top
.readNextEvent()) {
364 * Add it back in the queue.
368 * We're in OK, there's a guaranteed top#getCurrentEvent() unless another
369 * thread does something bad.
371 EventDefinition currentEvent
= checkNotNull(top
.getCurrentEvent());
372 final long topEnd
= fTrace
.timestampCyclesToNanos(currentEvent
.getTimestamp());
373 setEndTime(Math
.max(topEnd
, getEndTime()));
374 fEventCountPerTraceFile
[top
.getName()]++;
375 fEndTime
= Math
.max(currentEvent
.getTimestamp(), fEndTime
);
386 // something bad happend
389 * If there is no reader in the queue, it means the trace reader reached
390 * the end of the trace.
392 return hasMoreEvents();
396 * Go to the last event in the trace.
398 * @throws CTFException
401 public void goToLastEvent() throws CTFException
{
403 while (fPrio
.size() > 1) {
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).
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
422 public boolean seek(long timestamp
) throws CTFException
{
424 * Remove all the trace readers from the priority queue
427 for (CTFStreamInputReader streamInputReader
: fStreamInputReaders
) {
429 * Seek the trace reader.
431 streamInputReader
.seek(timestamp
);
434 * Add it to the priority queue if there is a current event.
436 if (streamInputReader
.getCurrentEvent() != null) {
437 fPrio
.add(streamInputReader
);
440 return hasMoreEvents();
444 * Gets the stream with the oldest event
446 * @return the stream with the oldest event
448 public CTFStreamInputReader
getTopStream() {
453 * Does the trace have more events?
455 * @return true if yes.
457 public final boolean hasMoreEvents() {
458 return fPrio
.size() > 0;
462 * Prints the event count stats.
464 public void printStats() {
465 printStats(LINE_LENGTH
);
469 * Prints the event count stats.
472 * Width of the display.
474 public void printStats(int width
) {
480 for (long i
: fEventCountPerTraceFile
) {
484 for (int j
= 0; j
< fEventCountPerTraceFile
.length
; j
++) {
485 CTFStreamInputReader se
= fStreamInputReaders
.get(j
);
487 long len
= (width
* fEventCountPerTraceFile
[se
.getName()])
490 StringBuilder sb
= new StringBuilder(se
.getFilename());
491 sb
.append("\t["); //$NON-NLS-1$
493 for (int i
= 0; i
< len
; i
++) {
497 for (long i
= len
; i
< width
; i
++) {
501 sb
.append("]\t" + fEventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
502 Activator
.log(sb
.toString());
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.
510 * @return the last event
512 public long getEndTime() {
517 * Sets a trace to be live or not
520 * whether the trace is live
522 public void setLive(boolean live
) {
523 for (CTFStreamInputReader s
: fPrio
) {
529 * Get if the trace is to read live or not
531 * @return whether the trace is live or not
533 public boolean isLive() {
534 return getTopStream().isLive();
538 public int hashCode() {
539 final int prime
= 31;
541 result
= (prime
* result
) + (int) (fStartTime ^
(fStartTime
>>> 32));
542 result
= (prime
* result
) + fStreamInputReaders
.hashCode();
543 result
= (prime
* result
) + ((fTrace
== null) ?
0 : fTrace
.hashCode());
548 public boolean equals(Object obj
) {
555 if (!(obj
instanceof CTFTraceReader
)) {
558 CTFTraceReader other
= (CTFTraceReader
) obj
;
559 if (!fStreamInputReaders
.equals(other
.fStreamInputReaders
)) {
562 if (fTrace
== null) {
563 if (other
.fTrace
!= null) {
566 } else if (!fTrace
.equals(other
.fTrace
)) {
573 public String
toString() {
574 /* Only for debugging, shouldn't be externalized */
575 return "CTFTraceReader [trace=" + fTrace
+ ']'; //$NON-NLS-1$
579 * Gets the parent trace
581 * @return the parent trace
583 public CTFTrace
getTrace() {
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.
591 * Do not call in the fast path.
593 * @throws CTFException
594 * A trace reading error occurred
597 public void populateIndex() throws CTFException
{
598 for (CTFStreamInputReader sir
: fPrio
) {