1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
15 import java
.util
.Collection
;
16 import java
.util
.PriorityQueue
;
18 import java
.util
.Vector
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
21 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.Activator
;
22 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputReaderTimestampComparator
;
25 * A CTF trace reader. Reads the events of a trace.
28 * @author Matthew Khouzam
29 * @author Alexandre Montplaisir
31 public class CTFTraceReader
{
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
38 * The trace to read from.
40 private final CTFTrace trace
;
43 * Vector of all the trace file readers.
45 private final Vector
<StreamInputReader
> streamInputReaders
= new Vector
<StreamInputReader
>();
48 * Priority queue to order the trace file readers by timestamp.
50 protected PriorityQueue
<StreamInputReader
> prio
;
53 * Array to count the number of event per trace file.
55 private long[] eventCountPerTraceFile
;
58 * Timestamp of the first event in the trace
60 private long startTime
;
63 * Timestamp of the last event read so far
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * Constructs a TraceReader to read a trace.
75 * The trace to read from.
77 public CTFTraceReader(CTFTrace trace
) {
79 streamInputReaders
.clear();
82 * Create the trace file readers.
84 createStreamInputReaders();
87 * Populate the timestamp-based priority queue.
89 populateStreamInputReaderHeap();
92 * Get the start Time of this trace bear in mind that the trace could be
95 this.startTime
= 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
96 if (hasMoreEvents()) {
97 this.startTime
= prio
.peek().getCurrentEvent().getTimestamp();
98 this.setEndTime(this.startTime
);
105 * @return The new CTFTraceReader
107 public CTFTraceReader
copyFrom() {
108 CTFTraceReader newReader
= null;
110 newReader
= new CTFTraceReader(this.trace
);
111 newReader
.startTime
= this.startTime
;
112 newReader
.setEndTime(this.endTime
);
117 * Dispose the CTFTraceReader
120 public void dispose() {
121 for (StreamInputReader reader
: streamInputReaders
) {
122 if (reader
!= null) {
126 streamInputReaders
.clear();
129 // ------------------------------------------------------------------------
130 // Getters/Setters/Predicates
131 // ------------------------------------------------------------------------
134 * Return the start time of this trace (== timestamp of the first event)
136 * @return the trace start time
138 public long getStartTime() {
139 return this.startTime
;
143 * Set the trace's end time
146 * The end time to use
148 protected void setEndTime(long endTime
) {
149 this.endTime
= endTime
;
153 // ------------------------------------------------------------------------
155 // ------------------------------------------------------------------------
158 * Creates one trace file reader per trace file contained in the trace.
160 private void createStreamInputReaders() {
161 Collection
<Stream
> streams
= this.trace
.getStreams().values();
166 for (Stream stream
: streams
) {
167 Set
<StreamInput
> streamInputs
= stream
.getStreamInputs();
170 * For each trace file of the stream.
172 for (StreamInput streamInput
: streamInputs
) {
176 StreamInputReader streamInputReader
= new StreamInputReader(
180 * Add it to the group.
182 this.streamInputReaders
.add(streamInputReader
);
187 * Create the array to count the number of event per trace file.
189 this.eventCountPerTraceFile
= new long[this.streamInputReaders
.size()];
193 * Initializes the priority queue used to choose the trace file with the
194 * lower next event timestamp.
196 private void populateStreamInputReaderHeap() {
198 * Create the priority queue with a size twice as bigger as the number
199 * of reader in order to avoid constant resizing.
201 this.prio
= new PriorityQueue
<StreamInputReader
>(
202 this.streamInputReaders
.size() * 2,
203 new StreamInputReaderTimestampComparator());
207 for (StreamInputReader reader
: this.streamInputReaders
) {
209 * Add each trace file reader in the priority queue, if we are able
210 * to read an event from it.
212 reader
.setParent(this);
213 if (reader
.readNextEvent()) {
214 this.prio
.add(reader
);
216 this.eventCountPerTraceFile
[pos
] = 0;
225 * Get the current event, which is the current event of the trace file
226 * reader with the lowest timestamp.
228 * @return An event definition, or null of the trace reader reached the end
231 public EventDefinition
getCurrentEventDef() {
232 StreamInputReader top
= getTopStream();
234 return (top
!= null) ? top
.getCurrentEvent() : null;
238 * Go to the next event.
240 * @return True if an event was read.
242 public boolean advance() {
247 * Remove the reader from the top of the priority queue.
249 StreamInputReader top
= this.prio
.poll();
252 * If the queue was empty.
258 * Read the next event of this reader.
260 if (top
.readNextEvent()) {
262 * Add it back in the queue.
265 final long topEnd
= this.trace
.timestampCyclesToNanos(top
.getCurrentEvent().getTimestamp());
266 this.setEndTime(Math
.max(topEnd
, this.getEndTime()));
267 this.eventCountPerTraceFile
[top
.getName()]++;
269 if (top
.getCurrentEvent() != null) {
270 this.endTime
= Math
.max(top
.getCurrentEvent().getTimestamp(),
275 * If there is no reader in the queue, it means the trace reader reached
276 * the end of the trace.
278 return hasMoreEvents();
282 * Go to the last event in the trace.
284 public void goToLastEvent() {
285 seek(this.getEndTime());
286 while (this.prio
.size() > 1) {
292 * Seeks to a given timestamp It will go to the event just after the
293 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
294 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
295 * if you want 21, you'll get 30. You want -inf, you'll get the first
296 * element, you want +inf, you'll get the end of the file with no events.
299 * the timestamp to seek to
300 * @return true if the trace has more events following the timestamp
302 public boolean seek(long timestamp
) {
304 * Remove all the trace readers from the priority queue
307 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
309 * Seek the trace reader.
311 streamInputReader
.seek(timestamp
);
314 * Add it to the priority queue if there is a current event.
318 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
319 if (streamInputReader
.getCurrentEvent() != null) {
320 this.prio
.add(streamInputReader
);
324 return hasMoreEvents();
328 // * Go to the first entry of a trace
330 // * @return 0, the first index.
332 // private long goToZero() {
334 // for (StreamInputReader streamInputReader : this.streamInputReaders) {
336 // * Seek the trace reader.
338 // streamInputReader.seek(0);
345 * gets the stream with the oldest event
347 * @return the stream with the oldest event
349 public StreamInputReader
getTopStream() {
350 return this.prio
.peek();
354 * Does the trace have more events?
356 * @return true if yes.
358 public boolean hasMoreEvents() {
359 return this.prio
.size() > 0;
363 * Prints the event count stats.
365 public void printStats() {
370 * Prints the event count stats.
373 * Width of the display.
375 public void printStats(int width
) {
381 for (long i
: this.eventCountPerTraceFile
) {
385 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
386 StreamInputReader se
= this.streamInputReaders
.get(j
);
388 long len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
391 StringBuilder sb
= new StringBuilder(se
.getFilename() + "\t["); //$NON-NLS-1$
393 for (int i
= 0; i
< len
; i
++) {
397 for (long i
= len
; i
< width
; i
++) {
401 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
402 Activator
.getDefault().log(sb
.toString());
407 * gets the last event timestamp that was read. This is NOT necessarily the
408 * last event in a trace, just the last one read so far.
410 * @return the last event
412 public long getEndTime() {
417 public int hashCode() {
418 final int prime
= 31;
420 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
421 result
= (prime
* result
) + streamInputReaders
.hashCode();
422 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
427 public boolean equals(Object obj
) {
434 if (!(obj
instanceof CTFTraceReader
)) {
437 CTFTraceReader other
= (CTFTraceReader
) obj
;
438 if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
442 if (other
.trace
!= null) {
445 } else if (!trace
.equals(other
.trace
)) {
454 * @see java.lang.Object#toString()
457 public String
toString() {
458 /* Only for debugging, shouldn't be externalized */
459 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
463 * Gets the parent trace
465 * @return the parent trace
467 public CTFTrace
getTrace() {