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
.StreamInputPacketIndexEntry
;
25 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputReaderTimestampComparator
;
28 * Reads the events of a trace.
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 int[] eventCountPerTraceFile
;
58 * Timestamp of the first event in the trace
60 private long startTime
;
63 * Timestamp of the last event read so far
72 private final long startIndex
[];
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Constructs a TraceReader to read a trace.
82 * The trace to read from.
83 * @throws CTFReaderException
85 public CTFTraceReader(CTFTrace trace
) {
89 * Create the trace file readers.
91 createStreamInputReaders();
94 * Populate the timestamp-based priority queue.
96 populateStreamInputReaderHeap();
99 * Get the start Time of this trace
101 this.startTime
= prio
.peek().getCurrentEvent().timestamp
;
102 this.endTime
= this.startTime
;
104 startIndex
= new long[prio
.size()];
105 for (int i
= 0; i
< prio
.size(); i
++) {
113 public CTFTraceReader
copyFrom() {
114 CTFTraceReader newReader
= null;
116 newReader
= new CTFTraceReader(this.trace
);
117 newReader
.startTime
= this.startTime
;
118 newReader
.endTime
= 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
;
138 public long getIndex() {
142 // ------------------------------------------------------------------------
144 // ------------------------------------------------------------------------
147 * Creates one trace file reader per trace file contained in the trace.
149 private void createStreamInputReaders() {
150 Collection
<Stream
> streams
= this.trace
.getStreams().values();
155 for (Stream stream
: streams
) {
156 Set
<StreamInput
> streamInputs
= stream
.getStreamInputs();
159 * For each trace file of the stream.
161 for (StreamInput streamInput
: streamInputs
) {
165 StreamInputReader streamInputReader
= new StreamInputReader(
169 * Add it to the group.
171 this.streamInputReaders
.add(streamInputReader
);
176 * Create the array to count the number of event per trace file.
178 this.eventCountPerTraceFile
= new int[this.streamInputReaders
.size()];
182 * Initializes the priority queue used to choose the trace file with the
183 * lower next event timestamp.
185 private void populateStreamInputReaderHeap() {
187 * Create the priority queue with a size twice as bigger as the number
188 * of reader in order to avoid constant resizing.
190 this.prio
= new PriorityQueue
<StreamInputReader
>(
191 this.streamInputReaders
.size() * 2,
192 new StreamInputReaderTimestampComparator());
196 for (StreamInputReader reader
: this.streamInputReaders
) {
198 * Add each trace file reader in the priority queue, if we are able
199 * to read an event from it.
201 if (reader
.readNextEvent()) {
202 this.prio
.add(reader
);
204 this.eventCountPerTraceFile
[pos
] = 0;
213 * Get the current event, which is the current event of the trace file
214 * reader with the lowest timestamp.
216 * @return An event definition, or null of the trace reader reached the end
219 public EventDefinition
getCurrentEventDef() {
220 StreamInputReader top
= getTopStream();
222 return (top
!= null) ? top
.getCurrentEvent() : null;
226 * Go to the next event.
228 * @return True if an event was read.
230 public boolean advance() {
235 * Remove the reader from the top of the priority queue.
237 StreamInputReader top
= this.prio
.poll();
240 * If the queue was empty.
248 if (hasMoreEvents()) {
249 StreamInputPacketReader packetReader
= top
.getPacketReader();
250 boolean packetHasMoreEvents
= packetReader
.hasMoreEvents();
251 StreamInputPacketIndexEntry currentPacket
= packetReader
253 if (!packetHasMoreEvents
) {
254 int n
= this.streamInputReaders
.indexOf(top
);
255 currentPacket
.setIndexBegin(startIndex
[n
]);
256 currentPacket
.setIndexEnd(fIndex
);
257 startIndex
[n
] = fIndex
+ 1;
261 * Read the next event of this reader.
263 if (top
.readNextEvent()) {
265 * Add it back in the queue.
268 final long topEnd
= top
.getCurrentEvent().timestamp
;
269 this.endTime
= Math
.max(topEnd
, this.endTime
);
270 this.eventCountPerTraceFile
[top
.getName()]++;
272 * increment the index
276 boolean hasMoreEvents
= hasMoreEvents();
279 * If there is no reader in the queue, it means the trace reader reached
280 * the end of the trace.
282 return hasMoreEvents
;
286 * Go to the last event in the trace.
288 * @throws CTFReaderException
290 public void goToLastEvent() throws CTFReaderException
{
292 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
294 * Seek the trace reader.
296 streamInputReader
.goToLastEvent();
298 int count
= prio
.size();
299 for (int i
= 0; i
< (count
-1); i
++) {
305 * Seeks to a given timestamp It will go to the event just after the
306 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
307 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
308 * if you want 21, you'll get 30. You want -inf, you'll get the first
309 * element, you want +inf, you'll get the end of the file with no events.
312 * the timestamp to seek to
313 * @return true if the trace has more events following the timestamp
315 public boolean seek(long timestamp
) {
317 * Remove all the trace readers from the priority queue
322 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
324 * Seek the trace reader.
326 offset
+= streamInputReader
.seek(timestamp
);
329 * Add it to the priority queue if there is a current event.
333 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
334 if (streamInputReader
.getCurrentEvent() != null) {
335 this.prio
.add(streamInputReader
);
336 fIndex
= Math
.max(fIndex
, streamInputReader
.getPacketReader()
337 .getCurrentPacket().getIndexBegin()
341 return hasMoreEvents();
344 public boolean seekIndex(long index
) {
347 long tempIndex
= Long
.MIN_VALUE
;
348 long tempTimestamp
= Long
.MIN_VALUE
;
350 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
352 * Seek the trace reader.
354 final long streamIndex
= streamInputReader
.seekIndex(index
);
355 if( streamIndex
!= -1 )
357 tempIndex
= Math
.max(tempIndex
, streamIndex
);
358 EventDefinition currentEvent
= streamInputReader
.getCurrentEvent();
360 * Maybe we're at the beginning of a trace.
362 if( currentEvent
== null ){
363 streamInputReader
.readNextEvent();
364 currentEvent
= streamInputReader
.getCurrentEvent();
366 if( currentEvent
!= null ) {
367 tempTimestamp
= Math
.max(tempTimestamp
,
368 currentEvent
.timestamp
);
371 * probably beyond the last event
373 tempIndex
= goToZero();
379 } catch (CTFReaderException e
) {
381 * Important, if it failed, it's because it's not yet indexed, so we
382 * have to manually advance to the right value.
384 tempIndex
= goToZero();
386 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
388 * Add it to the priority queue if there is a current event.
391 if (streamInputReader
.getCurrentEvent() != null) {
392 this.prio
.add(streamInputReader
);
395 if (tempIndex
== Long
.MAX_VALUE
) {
398 long pos
= tempIndex
;
399 if (index
> tempIndex
) {
403 while ((prio
.peek().getCurrentEvent().timestamp
< tempTimestamp
)
404 && hasMoreEvents()) {
408 for (pos
= tempIndex
; (pos
< index
) && hasMoreEvents(); pos
++) {
413 return hasMoreEvents();
417 * Go to the first entry of a trace
418 * @return 0, the first index.
420 private long goToZero() {
422 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
424 * Seek the trace reader.
426 streamInputReader
.seek(0);
432 public StreamInputReader
getTopStream() {
433 return this.prio
.peek();
437 * Does the trace have more events?
439 * @return true if yes.
441 public boolean hasMoreEvents() {
442 return this.prio
.size() > 0;
446 * Prints the event count stats.
448 public void printStats() {
453 * Prints the event count stats.
456 * Width of the display.
458 public void printStats(int width
) {
464 for (int i
: this.eventCountPerTraceFile
) {
468 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
469 StreamInputReader se
= this.streamInputReaders
.get(j
);
471 int len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
474 StringBuilder sb
= new StringBuilder(se
.getFilename() + "\t["); //$NON-NLS-1$
476 for (int i
= 0; i
< len
; i
++) {
480 for (int i
= len
; i
< width
; i
++) {
484 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
485 Activator
.getDefault().log(sb
.toString());
489 public long getEndTime() {
494 public int hashCode() {
495 final int prime
= 31;
497 result
= (prime
* result
) + (int) (endTime ^
(endTime
>>> 32));
498 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
499 result
= (prime
* result
)
500 + ((streamInputReaders
== null) ?
0 : streamInputReaders
502 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
507 public boolean equals(Object obj
) {
514 if (getClass() != obj
.getClass()) {
517 CTFTraceReader other
= (CTFTraceReader
) obj
;
518 if (endTime
!= other
.endTime
) {
521 if (startTime
!= other
.startTime
) {
524 if (streamInputReaders
== null) {
525 if (other
.streamInputReaders
!= null) {
528 } else if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
532 if (other
.trace
!= null) {
535 } else if (!trace
.equals(other
.trace
)) {
544 * @see java.lang.Object#toString()
547 public String
toString() {
548 /* Only for debugging, shouldn't be externalized */
549 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
552 public CTFTrace
getTrace() {