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
.Stream
;
23 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInput
;
24 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputReaderTimestampComparator
;
27 * A CTF trace reader. Reads the events of a trace.
30 * @author Matthew Khouzam
31 * @author Alexandre Montplaisir
33 public class CTFTraceReader
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
40 * The trace to read from.
42 private final CTFTrace trace
;
45 * Vector of all the trace file readers.
47 private final Vector
<StreamInputReader
> streamInputReaders
= new Vector
<StreamInputReader
>();
50 * Priority queue to order the trace file readers by timestamp.
52 protected PriorityQueue
<StreamInputReader
> prio
;
55 * Array to count the number of event per trace file.
57 private long[] eventCountPerTraceFile
;
60 * Timestamp of the first event in the trace
62 private long startTime
;
65 * Timestamp of the last event read so far
69 protected void setEndTime(long endTime
) {
70 this.endTime
= endTime
;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Constructs a TraceReader to read a trace.
81 * The trace to read from.
83 public CTFTraceReader(CTFTrace trace
) {
85 streamInputReaders
.clear();
88 * Create the trace file readers.
90 createStreamInputReaders();
93 * Populate the timestamp-based priority queue.
95 populateStreamInputReaderHeap();
98 * Get the start Time of this trace bear in mind that the trace could be
101 this.startTime
= 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
102 if (hasMoreEvents()) {
103 this.startTime
= prio
.peek().getCurrentEvent().getTimestamp();
104 this.setEndTime(this.startTime
);
111 * @return The new CTFTraceReader
113 public CTFTraceReader
copyFrom() {
114 CTFTraceReader newReader
= null;
116 newReader
= new CTFTraceReader(this.trace
);
117 newReader
.startTime
= this.startTime
;
118 newReader
.setEndTime(this.endTime
);
122 // ------------------------------------------------------------------------
123 // Getters/Setters/Predicates
124 // ------------------------------------------------------------------------
127 * Return the start time of this trace (== timestamp of the first event)
129 * @return the trace start time
131 public long getStartTime() {
132 return this.startTime
;
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
140 * Creates one trace file reader per trace file contained in the trace.
142 private void createStreamInputReaders() {
143 Collection
<Stream
> streams
= this.trace
.getStreams().values();
148 for (Stream stream
: streams
) {
149 Set
<StreamInput
> streamInputs
= stream
.getStreamInputs();
152 * For each trace file of the stream.
154 for (StreamInput streamInput
: streamInputs
) {
158 StreamInputReader streamInputReader
= new StreamInputReader(
162 * Add it to the group.
164 this.streamInputReaders
.add(streamInputReader
);
169 * Create the array to count the number of event per trace file.
171 this.eventCountPerTraceFile
= new long[this.streamInputReaders
.size()];
175 * Initializes the priority queue used to choose the trace file with the
176 * lower next event timestamp.
178 private void populateStreamInputReaderHeap() {
180 * Create the priority queue with a size twice as bigger as the number
181 * of reader in order to avoid constant resizing.
183 this.prio
= new PriorityQueue
<StreamInputReader
>(
184 this.streamInputReaders
.size() * 2,
185 new StreamInputReaderTimestampComparator());
189 for (StreamInputReader reader
: this.streamInputReaders
) {
191 * Add each trace file reader in the priority queue, if we are able
192 * to read an event from it.
194 reader
.setParent(this);
195 if (reader
.readNextEvent()) {
196 this.prio
.add(reader
);
198 this.eventCountPerTraceFile
[pos
] = 0;
207 * Get the current event, which is the current event of the trace file
208 * reader with the lowest timestamp.
210 * @return An event definition, or null of the trace reader reached the end
213 public EventDefinition
getCurrentEventDef() {
214 StreamInputReader top
= getTopStream();
216 return (top
!= null) ? top
.getCurrentEvent() : null;
220 * Go to the next event.
222 * @return True if an event was read.
224 public boolean advance() {
229 * Remove the reader from the top of the priority queue.
231 StreamInputReader top
= this.prio
.poll();
234 * If the queue was empty.
240 * Read the next event of this reader.
242 if (top
.readNextEvent()) {
244 * Add it back in the queue.
247 final long topEnd
= top
.getCurrentEvent().getTimestamp()
248 + this.getTrace().getOffset();
249 this.setEndTime(Math
.max(topEnd
, this.getEndTime()));
250 this.eventCountPerTraceFile
[top
.getName()]++;
252 if (top
.getCurrentEvent() != null) {
253 this.endTime
= Math
.max(top
.getCurrentEvent().getTimestamp(),
258 * If there is no reader in the queue, it means the trace reader reached
259 * the end of the trace.
261 return hasMoreEvents();
265 * Go to the last event in the trace.
267 public void goToLastEvent() {
268 seek(this.getEndTime());
269 while (this.prio
.size() > 1) {
275 * Seeks to a given timestamp It will go to the event just after the
276 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
277 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
278 * if you want 21, you'll get 30. You want -inf, you'll get the first
279 * element, you want +inf, you'll get the end of the file with no events.
282 * the timestamp to seek to
283 * @return true if the trace has more events following the timestamp
285 public boolean seek(long timestamp
) {
287 * Remove all the trace readers from the priority queue
290 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
292 * Seek the trace reader.
294 streamInputReader
.seek(timestamp
);
297 * Add it to the priority queue if there is a current event.
301 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
302 if (streamInputReader
.getCurrentEvent() != null) {
303 this.prio
.add(streamInputReader
);
307 return hasMoreEvents();
311 // * Go to the first entry of a trace
313 // * @return 0, the first index.
315 // private long goToZero() {
317 // for (StreamInputReader streamInputReader : this.streamInputReaders) {
319 // * Seek the trace reader.
321 // streamInputReader.seek(0);
328 * gets the stream with the oldest event
330 * @return the stream with the oldest event
332 public StreamInputReader
getTopStream() {
333 return this.prio
.peek();
337 * Does the trace have more events?
339 * @return true if yes.
341 public boolean hasMoreEvents() {
342 return this.prio
.size() > 0;
346 * Prints the event count stats.
348 public void printStats() {
353 * Prints the event count stats.
356 * Width of the display.
358 public void printStats(int width
) {
364 for (long i
: this.eventCountPerTraceFile
) {
368 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
369 StreamInputReader se
= this.streamInputReaders
.get(j
);
371 long len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
374 StringBuilder sb
= new StringBuilder(se
.getFilename() + "\t["); //$NON-NLS-1$
376 for (int i
= 0; i
< len
; i
++) {
380 for (long i
= len
; i
< width
; i
++) {
384 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
385 Activator
.getDefault().log(sb
.toString());
390 * gets the last event timestamp that was read. This is NOT necessarily the
391 * last event in a trace, just the last one read so far.
393 * @return the last event
395 public long getEndTime() {
400 public int hashCode() {
401 final int prime
= 31;
403 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
404 result
= (prime
* result
)
405 + ((streamInputReaders
== null) ?
0 : streamInputReaders
407 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
412 public boolean equals(Object obj
) {
419 if (!(obj
instanceof CTFTraceReader
)) {
422 CTFTraceReader other
= (CTFTraceReader
) obj
;
423 if (streamInputReaders
== null) {
424 if (other
.streamInputReaders
!= null) {
427 } else if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
431 if (other
.trace
!= null) {
434 } else if (!trace
.equals(other
.trace
)) {
443 * @see java.lang.Object#toString()
446 public String
toString() {
447 /* Only for debugging, shouldn't be externalized */
448 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
452 * Gets the parent trace
454 * @return the parent trace
456 public CTFTrace
getTrace() {