1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
);
116 // ------------------------------------------------------------------------
117 // Getters/Setters/Predicates
118 // ------------------------------------------------------------------------
121 * Return the start time of this trace (== timestamp of the first event)
123 * @return the trace start time
125 public long getStartTime() {
126 return this.startTime
;
130 * Set the trace's end time
133 * The end time to use
135 protected void setEndTime(long endTime
) {
136 this.endTime
= endTime
;
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Creates one trace file reader per trace file contained in the trace.
147 private void createStreamInputReaders() {
148 Collection
<Stream
> streams
= this.trace
.getStreams().values();
153 for (Stream stream
: streams
) {
154 Set
<StreamInput
> streamInputs
= stream
.getStreamInputs();
157 * For each trace file of the stream.
159 for (StreamInput streamInput
: streamInputs
) {
163 StreamInputReader streamInputReader
= new StreamInputReader(
167 * Add it to the group.
169 this.streamInputReaders
.add(streamInputReader
);
174 * Create the array to count the number of event per trace file.
176 this.eventCountPerTraceFile
= new long[this.streamInputReaders
.size()];
180 * Initializes the priority queue used to choose the trace file with the
181 * lower next event timestamp.
183 private void populateStreamInputReaderHeap() {
185 * Create the priority queue with a size twice as bigger as the number
186 * of reader in order to avoid constant resizing.
188 this.prio
= new PriorityQueue
<StreamInputReader
>(
189 this.streamInputReaders
.size() * 2,
190 new StreamInputReaderTimestampComparator());
194 for (StreamInputReader reader
: this.streamInputReaders
) {
196 * Add each trace file reader in the priority queue, if we are able
197 * to read an event from it.
199 reader
.setParent(this);
200 if (reader
.readNextEvent()) {
201 this.prio
.add(reader
);
203 this.eventCountPerTraceFile
[pos
] = 0;
212 * Get the current event, which is the current event of the trace file
213 * reader with the lowest timestamp.
215 * @return An event definition, or null of the trace reader reached the end
218 public EventDefinition
getCurrentEventDef() {
219 StreamInputReader top
= getTopStream();
221 return (top
!= null) ? top
.getCurrentEvent() : null;
225 * Go to the next event.
227 * @return True if an event was read.
229 public boolean advance() {
234 * Remove the reader from the top of the priority queue.
236 StreamInputReader top
= this.prio
.poll();
239 * If the queue was empty.
245 * Read the next event of this reader.
247 if (top
.readNextEvent()) {
249 * Add it back in the queue.
252 final long topEnd
= this.trace
.timestampCyclesToNanos(top
.getCurrentEvent().getTimestamp());
253 this.setEndTime(Math
.max(topEnd
, this.getEndTime()));
254 this.eventCountPerTraceFile
[top
.getName()]++;
256 if (top
.getCurrentEvent() != null) {
257 this.endTime
= Math
.max(top
.getCurrentEvent().getTimestamp(),
262 * If there is no reader in the queue, it means the trace reader reached
263 * the end of the trace.
265 return hasMoreEvents();
269 * Go to the last event in the trace.
271 public void goToLastEvent() {
272 seek(this.getEndTime());
273 while (this.prio
.size() > 1) {
279 * Seeks to a given timestamp It will go to the event just after the
280 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
281 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
282 * if you want 21, you'll get 30. You want -inf, you'll get the first
283 * element, you want +inf, you'll get the end of the file with no events.
286 * the timestamp to seek to
287 * @return true if the trace has more events following the timestamp
289 public boolean seek(long timestamp
) {
291 * Remove all the trace readers from the priority queue
294 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
296 * Seek the trace reader.
298 streamInputReader
.seek(timestamp
);
301 * Add it to the priority queue if there is a current event.
305 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
306 if (streamInputReader
.getCurrentEvent() != null) {
307 this.prio
.add(streamInputReader
);
311 return hasMoreEvents();
315 // * Go to the first entry of a trace
317 // * @return 0, the first index.
319 // private long goToZero() {
321 // for (StreamInputReader streamInputReader : this.streamInputReaders) {
323 // * Seek the trace reader.
325 // streamInputReader.seek(0);
332 * gets the stream with the oldest event
334 * @return the stream with the oldest event
336 public StreamInputReader
getTopStream() {
337 return this.prio
.peek();
341 * Does the trace have more events?
343 * @return true if yes.
345 public boolean hasMoreEvents() {
346 return this.prio
.size() > 0;
350 * Prints the event count stats.
352 public void printStats() {
357 * Prints the event count stats.
360 * Width of the display.
362 public void printStats(int width
) {
368 for (long i
: this.eventCountPerTraceFile
) {
372 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
373 StreamInputReader se
= this.streamInputReaders
.get(j
);
375 long len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
378 StringBuilder sb
= new StringBuilder(se
.getFilename() + "\t["); //$NON-NLS-1$
380 for (int i
= 0; i
< len
; i
++) {
384 for (long i
= len
; i
< width
; i
++) {
388 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
389 Activator
.getDefault().log(sb
.toString());
394 * gets the last event timestamp that was read. This is NOT necessarily the
395 * last event in a trace, just the last one read so far.
397 * @return the last event
399 public long getEndTime() {
404 public int hashCode() {
405 final int prime
= 31;
407 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
408 result
= (prime
* result
)
409 + ((streamInputReaders
== null) ?
0 : streamInputReaders
411 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
416 public boolean equals(Object obj
) {
423 if (!(obj
instanceof CTFTraceReader
)) {
426 CTFTraceReader other
= (CTFTraceReader
) obj
;
427 if (streamInputReaders
== null) {
428 if (other
.streamInputReaders
!= null) {
431 } else if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
435 if (other
.trace
!= null) {
438 } else if (!trace
.equals(other
.trace
)) {
447 * @see java.lang.Object#toString()
450 public String
toString() {
451 /* Only for debugging, shouldn't be externalized */
452 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
456 * Gets the parent trace
458 * @return the parent trace
460 public CTFTrace
getTrace() {