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
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
16 import java
.util
.Collection
;
17 import java
.util
.PriorityQueue
;
19 import java
.util
.Vector
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
22 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.Activator
;
23 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputReaderTimestampComparator
;
26 * A CTF trace reader. Reads the events of a trace.
29 * @author Matthew Khouzam
30 * @author Alexandre Montplaisir
32 public class CTFTraceReader
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
39 * The trace to read from.
41 private final CTFTrace trace
;
44 * Vector of all the trace file readers.
46 private final Vector
<StreamInputReader
> streamInputReaders
= new Vector
<StreamInputReader
>();
49 * Priority queue to order the trace file readers by timestamp.
51 protected PriorityQueue
<StreamInputReader
> prio
;
54 * Array to count the number of event per trace file.
56 private long[] eventCountPerTraceFile
;
59 * Timestamp of the first event in the trace
61 private long startTime
;
64 * Timestamp of the last event read so far
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
73 * Constructs a TraceReader to read a trace.
76 * The trace to read from.
78 public CTFTraceReader(CTFTrace trace
) {
80 streamInputReaders
.clear();
83 * Create the trace file readers.
85 createStreamInputReaders();
88 * Populate the timestamp-based priority queue.
90 populateStreamInputReaderHeap();
93 * Get the start Time of this trace bear in mind that the trace could be
96 this.startTime
= 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
97 if (hasMoreEvents()) {
98 this.startTime
= prio
.peek().getCurrentEvent().getTimestamp();
99 this.setEndTime(this.startTime
);
106 * @return The new CTFTraceReader
108 public CTFTraceReader
copyFrom() {
109 CTFTraceReader newReader
= null;
111 newReader
= new CTFTraceReader(this.trace
);
112 newReader
.startTime
= this.startTime
;
113 newReader
.setEndTime(this.endTime
);
118 * Dispose the CTFTraceReader
121 public void dispose() {
122 for (StreamInputReader reader
: streamInputReaders
) {
123 if (reader
!= null) {
127 streamInputReaders
.clear();
130 // ------------------------------------------------------------------------
131 // Getters/Setters/Predicates
132 // ------------------------------------------------------------------------
135 * Return the start time of this trace (== timestamp of the first event)
137 * @return the trace start time
139 public long getStartTime() {
140 return this.startTime
;
144 * Set the trace's end time
147 * The end time to use
149 protected void setEndTime(long endTime
) {
150 this.endTime
= endTime
;
154 // ------------------------------------------------------------------------
156 // ------------------------------------------------------------------------
159 * Creates one trace file reader per trace file contained in the trace.
161 private void createStreamInputReaders() {
162 Collection
<Stream
> streams
= this.trace
.getStreams().values();
167 for (Stream stream
: streams
) {
168 Set
<StreamInput
> streamInputs
= stream
.getStreamInputs();
171 * For each trace file of the stream.
173 for (StreamInput streamInput
: streamInputs
) {
177 StreamInputReader streamInputReader
= new StreamInputReader(
181 * Add it to the group.
183 this.streamInputReaders
.add(streamInputReader
);
188 * Create the array to count the number of event per trace file.
190 this.eventCountPerTraceFile
= new long[this.streamInputReaders
.size()];
194 * Initializes the priority queue used to choose the trace file with the
195 * lower next event timestamp.
197 private void populateStreamInputReaderHeap() {
198 if (this.streamInputReaders
.isEmpty()) {
199 this.prio
= new PriorityQueue
<StreamInputReader
>();
204 * Create the priority queue with a size twice as bigger as the number
205 * of reader in order to avoid constant resizing.
207 this.prio
= new PriorityQueue
<StreamInputReader
>(
208 this.streamInputReaders
.size() * 2,
209 new StreamInputReaderTimestampComparator());
213 for (StreamInputReader reader
: this.streamInputReaders
) {
215 * Add each trace file reader in the priority queue, if we are able
216 * to read an event from it.
218 reader
.setParent(this);
219 if (reader
.readNextEvent()) {
220 this.prio
.add(reader
);
222 this.eventCountPerTraceFile
[pos
] = 0;
231 * Get the current event, which is the current event of the trace file
232 * reader with the lowest timestamp.
234 * @return An event definition, or null of the trace reader reached the end
237 public EventDefinition
getCurrentEventDef() {
238 StreamInputReader top
= getTopStream();
240 return (top
!= null) ? top
.getCurrentEvent() : null;
244 * Go to the next event.
246 * @return True if an event was read.
248 public boolean advance() {
253 * Remove the reader from the top of the priority queue.
255 StreamInputReader top
= this.prio
.poll();
258 * If the queue was empty.
264 * Read the next event of this reader.
266 if (top
.readNextEvent()) {
268 * Add it back in the queue.
271 final long topEnd
= this.trace
.timestampCyclesToNanos(top
.getCurrentEvent().getTimestamp());
272 this.setEndTime(Math
.max(topEnd
, this.getEndTime()));
273 this.eventCountPerTraceFile
[top
.getName()]++;
275 if (top
.getCurrentEvent() != null) {
276 this.endTime
= Math
.max(top
.getCurrentEvent().getTimestamp(),
281 * If there is no reader in the queue, it means the trace reader reached
282 * the end of the trace.
284 return hasMoreEvents();
288 * Go to the last event in the trace.
290 public void goToLastEvent() {
291 seek(this.getEndTime());
292 while (this.prio
.size() > 1) {
298 * Seeks to a given timestamp It will go to the event just after the
299 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
300 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
301 * if you want 21, you'll get 30. You want -inf, you'll get the first
302 * element, you want +inf, you'll get the end of the file with no events.
305 * the timestamp to seek to
306 * @return true if the trace has more events following the timestamp
308 public boolean seek(long timestamp
) {
310 * Remove all the trace readers from the priority queue
313 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
315 * Seek the trace reader.
317 streamInputReader
.seek(timestamp
);
320 * Add it to the priority queue if there is a current event.
324 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
325 if (streamInputReader
.getCurrentEvent() != null) {
326 this.prio
.add(streamInputReader
);
330 return hasMoreEvents();
334 // * Go to the first entry of a trace
336 // * @return 0, the first index.
338 // private long goToZero() {
340 // for (StreamInputReader streamInputReader : this.streamInputReaders) {
342 // * Seek the trace reader.
344 // streamInputReader.seek(0);
351 * gets the stream with the oldest event
353 * @return the stream with the oldest event
355 public StreamInputReader
getTopStream() {
356 return this.prio
.peek();
360 * Does the trace have more events?
362 * @return true if yes.
364 public boolean hasMoreEvents() {
365 return this.prio
.size() > 0;
369 * Prints the event count stats.
371 public void printStats() {
376 * Prints the event count stats.
379 * Width of the display.
381 public void printStats(int width
) {
387 for (long i
: this.eventCountPerTraceFile
) {
391 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
392 StreamInputReader se
= this.streamInputReaders
.get(j
);
394 long len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
397 StringBuilder sb
= new StringBuilder(se
.getFilename() + "\t["); //$NON-NLS-1$
399 for (int i
= 0; i
< len
; i
++) {
403 for (long i
= len
; i
< width
; i
++) {
407 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
408 Activator
.log(sb
.toString());
413 * gets the last event timestamp that was read. This is NOT necessarily the
414 * last event in a trace, just the last one read so far.
416 * @return the last event
418 public long getEndTime() {
423 public int hashCode() {
424 final int prime
= 31;
426 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
427 result
= (prime
* result
) + streamInputReaders
.hashCode();
428 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
433 public boolean equals(Object obj
) {
440 if (!(obj
instanceof CTFTraceReader
)) {
443 CTFTraceReader other
= (CTFTraceReader
) obj
;
444 if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
448 if (other
.trace
!= null) {
451 } else if (!trace
.equals(other
.trace
)) {
458 public String
toString() {
459 /* Only for debugging, shouldn't be externalized */
460 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
464 * Gets the parent trace
466 * @return the parent trace
468 public CTFTrace
getTrace() {