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 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
;
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
;
30 import com
.google
.common
.collect
.ImmutableSet
;
31 import com
.google
.common
.collect
.ImmutableSet
.Builder
;
34 * A CTF trace reader. Reads the events of a trace.
37 * @author Matthew Khouzam
38 * @author Alexandre Montplaisir
40 public class CTFTraceReader
implements AutoCloseable
{
42 private static final int LINE_LENGTH
= 60;
44 private static final int MIN_PRIO_SIZE
= 16;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * The trace to read from.
53 private final CTFTrace fTrace
;
56 * Vector of all the trace file readers.
58 private final List
<CTFStreamInputReader
> fStreamInputReaders
=
59 Collections
.synchronizedList(new ArrayList
<CTFStreamInputReader
>());
62 * Priority queue to order the trace file readers by timestamp.
64 private PriorityQueue
<CTFStreamInputReader
> fPrio
;
67 * Array to count the number of event per trace file.
69 private long[] fEventCountPerTraceFile
;
72 * Timestamp of the first event in the trace
74 private long fStartTime
;
77 * Timestamp of the last event read so far
79 private long fEndTime
;
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 * Constructs a TraceReader to read a trace.
89 * The trace to read from.
90 * @throws CTFException
93 public CTFTraceReader(CTFTrace trace
) throws CTFException
{
95 fStreamInputReaders
.clear();
98 * Create the trace file readers.
100 createStreamInputReaders();
103 * Populate the timestamp-based priority queue.
105 populateStreamInputReaderHeap();
108 * Get the start Time of this trace bear in mind that the trace could be
112 if (hasMoreEvents()) {
113 fStartTime
= getTopStream().getCurrentEvent().getTimestamp();
114 setEndTime(fStartTime
);
121 * @return The new CTFTraceReader
122 * @throws CTFException
125 public CTFTraceReader
copyFrom() throws CTFException
{
126 CTFTraceReader newReader
= null;
128 newReader
= new CTFTraceReader(fTrace
);
129 newReader
.fStartTime
= fStartTime
;
130 newReader
.setEndTime(fEndTime
);
135 * Dispose the CTFTraceReader
138 public void close() {
139 synchronized (fStreamInputReaders
) {
140 for (CTFStreamInputReader reader
: fStreamInputReaders
) {
141 if (reader
!= null) {
144 } catch (IOException e
) {
145 Activator
.logError(e
.getMessage(), e
);
149 fStreamInputReaders
.clear();
153 // ------------------------------------------------------------------------
154 // Getters/Setters/Predicates
155 // ------------------------------------------------------------------------
158 * Return the start time of this trace (== timestamp of the first event)
160 * @return the trace start time
162 public long getStartTime() {
167 * Set the trace's end time
170 * The end time to use
172 protected final void setEndTime(long endTime
) {
177 * Get the priority queue of this trace reader.
179 * @return The priority queue of input readers
181 protected PriorityQueue
<CTFStreamInputReader
> getPrio() {
185 // ------------------------------------------------------------------------
187 // ------------------------------------------------------------------------
190 * Creates one trace file reader per trace file contained in the trace.
192 * @throws CTFException
195 private void createStreamInputReaders() throws CTFException
{
199 for (CTFStream stream
: fTrace
.getStreams()) {
200 Set
<CTFStreamInput
> streamInputs
= stream
.getStreamInputs();
203 * For each trace file of the stream.
205 for (CTFStreamInput streamInput
: streamInputs
) {
208 * Create a reader and add it to the group.
210 fStreamInputReaders
.add(new CTFStreamInputReader(streamInput
));
215 * Create the array to count the number of event per trace file.
217 fEventCountPerTraceFile
= new long[fStreamInputReaders
.size()];
221 * Update the priority queue to make it match the parent trace
223 * @throws CTFException
226 public void update() throws CTFException
{
227 Set
<CTFStreamInputReader
> readers
= new HashSet
<>();
228 for (CTFStream stream
: fTrace
.getStreams()) {
229 Set
<CTFStreamInput
> streamInputs
= stream
.getStreamInputs();
230 for (CTFStreamInput streamInput
: streamInputs
) {
234 CTFStreamInputReader streamInputReader
= new CTFStreamInputReader(
238 * Add it to the group.
240 if (!fStreamInputReaders
.contains(streamInputReader
)) {
241 streamInputReader
.readNextEvent();
242 fStreamInputReaders
.add(streamInputReader
);
243 readers
.add(streamInputReader
);
247 long[] temp
= fEventCountPerTraceFile
;
248 fEventCountPerTraceFile
= new long[readers
.size() + temp
.length
];
249 for (CTFStreamInputReader reader
: readers
) {
252 for (int i
= 0; i
< temp
.length
; i
++) {
253 fEventCountPerTraceFile
[i
] = temp
[i
];
258 * Gets an iterable of the stream input readers, useful for foreaches
260 * @return the iterable of the stream input readers
262 public Iterable
<IEventDeclaration
> getEventDeclarations() {
263 ImmutableSet
.Builder
<IEventDeclaration
> builder
= new Builder
<>();
264 for (CTFStreamInputReader sir
: fStreamInputReaders
) {
265 builder
.addAll(sir
.getEventDeclarations());
267 return builder
.build();
271 * Initializes the priority queue used to choose the trace file with the
272 * lower next event timestamp.
274 * @throws CTFException
277 private void populateStreamInputReaderHeap() throws CTFException
{
278 if (fStreamInputReaders
.isEmpty()) {
279 fPrio
= new PriorityQueue
<>(MIN_PRIO_SIZE
,
280 new StreamInputReaderTimestampComparator());
285 * Create the priority queue with a size twice as bigger as the number
286 * of reader in order to avoid constant resizing.
288 fPrio
= new PriorityQueue
<>(
289 Math
.max(fStreamInputReaders
.size() * 2, MIN_PRIO_SIZE
),
290 new StreamInputReaderTimestampComparator());
294 for (CTFStreamInputReader reader
: fStreamInputReaders
) {
296 * Add each trace file reader in the priority queue, if we are able
297 * to read an event from it.
299 reader
.setParent(this);
300 CTFResponse readNextEvent
= reader
.readNextEvent();
301 if (readNextEvent
== CTFResponse
.OK
|| readNextEvent
== CTFResponse
.WAIT
) {
304 fEventCountPerTraceFile
[pos
] = 0;
313 * Get the current event, which is the current event of the trace file
314 * reader with the lowest timestamp.
316 * @return An event definition, or null of the trace reader reached the end
319 public EventDefinition
getCurrentEventDef() {
320 CTFStreamInputReader top
= getTopStream();
321 return (top
!= null) ? top
.getCurrentEvent() : null;
325 * Go to the next event.
327 * @return True if an event was read.
328 * @throws CTFException
331 public boolean advance() throws CTFException
{
333 * Remove the reader from the top of the priority queue.
335 CTFStreamInputReader top
= fPrio
.poll();
338 * If the queue was empty.
344 * Read the next event of this reader.
346 switch (top
.readNextEvent()) {
349 * Add it back in the queue.
352 final long topEnd
= fTrace
.timestampCyclesToNanos(top
.getCurrentEvent().getTimestamp());
353 setEndTime(Math
.max(topEnd
, getEndTime()));
354 fEventCountPerTraceFile
[top
.getName()]++;
356 if (top
.getCurrentEvent() != null) {
357 fEndTime
= Math
.max(top
.getCurrentEvent().getTimestamp(),
370 // something bad happend
373 * If there is no reader in the queue, it means the trace reader reached
374 * the end of the trace.
376 return hasMoreEvents();
380 * Go to the last event in the trace.
382 * @throws CTFException
385 public void goToLastEvent() throws CTFException
{
387 while (fPrio
.size() > 1) {
393 * Seeks to a given timestamp. It will seek to the nearest event greater or
394 * equal to timestamp. If a trace is [10 20 30 40] and you are looking for
395 * 19, it will give you 20. If you want 20, you will get 20, if you want 21,
396 * you will get 30. The value -inf will seek to the first element and the
397 * value +inf will seek to the end of the file (past the last event).
400 * the timestamp to seek to
401 * @return true if there are events above or equal the seek timestamp, false
402 * if seek at the end of the trace (no valid event).
403 * @throws CTFException
406 public boolean seek(long timestamp
) throws CTFException
{
408 * Remove all the trace readers from the priority queue
411 for (CTFStreamInputReader streamInputReader
: fStreamInputReaders
) {
413 * Seek the trace reader.
415 streamInputReader
.seek(timestamp
);
418 * Add it to the priority queue if there is a current event.
420 if (streamInputReader
.getCurrentEvent() != null) {
421 fPrio
.add(streamInputReader
);
424 return hasMoreEvents();
428 * Gets the stream with the oldest event
430 * @return the stream with the oldest event
432 public CTFStreamInputReader
getTopStream() {
437 * Does the trace have more events?
439 * @return true if yes.
441 public final boolean hasMoreEvents() {
442 return fPrio
.size() > 0;
446 * Prints the event count stats.
448 public void printStats() {
449 printStats(LINE_LENGTH
);
453 * Prints the event count stats.
456 * Width of the display.
458 public void printStats(int width
) {
464 for (long i
: fEventCountPerTraceFile
) {
468 for (int j
= 0; j
< fEventCountPerTraceFile
.length
; j
++) {
469 CTFStreamInputReader se
= fStreamInputReaders
.get(j
);
471 long len
= (width
* fEventCountPerTraceFile
[se
.getName()])
474 StringBuilder sb
= new StringBuilder(se
.getFilename());
475 sb
.append("\t["); //$NON-NLS-1$
477 for (int i
= 0; i
< len
; i
++) {
481 for (long i
= len
; i
< width
; i
++) {
485 sb
.append("]\t" + fEventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
486 Activator
.log(sb
.toString());
491 * Gets the last event timestamp that was read. This is NOT necessarily the
492 * last event in a trace, just the last one read so far.
494 * @return the last event
496 public long getEndTime() {
501 * Sets a trace to be live or not
504 * whether the trace is live
506 public void setLive(boolean live
) {
507 for (CTFStreamInputReader s
: fPrio
) {
513 * Get if the trace is to read live or not
515 * @return whether the trace is live or not
517 public boolean isLive() {
518 return getTopStream().isLive();
522 public int hashCode() {
523 final int prime
= 31;
525 result
= (prime
* result
) + (int) (fStartTime ^
(fStartTime
>>> 32));
526 result
= (prime
* result
) + fStreamInputReaders
.hashCode();
527 result
= (prime
* result
) + ((fTrace
== null) ?
0 : fTrace
.hashCode());
532 public boolean equals(Object obj
) {
539 if (!(obj
instanceof CTFTraceReader
)) {
542 CTFTraceReader other
= (CTFTraceReader
) obj
;
543 if (!fStreamInputReaders
.equals(other
.fStreamInputReaders
)) {
546 if (fTrace
== null) {
547 if (other
.fTrace
!= null) {
550 } else if (!fTrace
.equals(other
.fTrace
)) {
557 public String
toString() {
558 /* Only for debugging, shouldn't be externalized */
559 return "CTFTraceReader [trace=" + fTrace
+ ']'; //$NON-NLS-1$
563 * Gets the parent trace
565 * @return the parent trace
567 public CTFTrace
getTrace() {
572 * This will read the entire trace and populate all the indexes. The reader
573 * will then be reset to the first event in the trace.
575 * Do not call in the fast path.
577 * @throws CTFException
578 * A trace reading error occurred
581 public void populateIndex() throws CTFException
{
582 for (CTFStreamInputReader sir
: fPrio
) {