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
.HashMap
;
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
.Stream
;
24 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInput
;
25 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
26 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputReaderTimestampComparator
;
29 * Reads the events of a trace.
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 int[] eventCountPerTraceFile
;
59 * Timestamp of the first event in the trace
61 private long startTime
;
64 * Timestamp of the last event read so far
73 private final HashMap
<Integer
, Long
> startIndex
;
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * Constructs a TraceReader to read a trace.
83 * The trace to read from.
84 * @throws CTFReaderException
86 public CTFTraceReader(CTFTrace trace
) {
90 * Create the trace file readers.
92 createStreamInputReaders();
95 * Populate the timestamp-based priority queue.
97 populateStreamInputReaderHeap();
100 * Get the start Time of this trace
101 * bear in mind that the trace could be empty.
103 this.startTime
= 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
104 if (hasMoreEvents()) {
105 this.startTime
= prio
.peek().getCurrentEvent().timestamp
;
106 this.endTime
= this.startTime
;
109 startIndex
= new HashMap
<Integer
, Long
>();
115 public CTFTraceReader
copyFrom() {
116 CTFTraceReader newReader
= null;
118 newReader
= new CTFTraceReader(this.trace
);
119 newReader
.startTime
= this.startTime
;
120 newReader
.endTime
= this.endTime
;
124 // ------------------------------------------------------------------------
125 // Getters/Setters/Predicates
126 // ------------------------------------------------------------------------
129 * Return the start time of this trace (== timestamp of the first event)
131 * @return the trace start time
133 public long getStartTime() {
134 return this.startTime
;
140 public long getIndex() {
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 * Creates one trace file reader per trace file contained in the trace.
151 private void createStreamInputReaders() {
152 Collection
<Stream
> streams
= this.trace
.getStreams().values();
157 for (Stream stream
: streams
) {
158 Set
<StreamInput
> streamInputs
= stream
.getStreamInputs();
161 * For each trace file of the stream.
163 for (StreamInput streamInput
: streamInputs
) {
167 StreamInputReader streamInputReader
= new StreamInputReader(
171 * Add it to the group.
173 this.streamInputReaders
.add(streamInputReader
);
178 * Create the array to count the number of event per trace file.
180 this.eventCountPerTraceFile
= new int[this.streamInputReaders
.size()];
184 * Initializes the priority queue used to choose the trace file with the
185 * lower next event timestamp.
187 private void populateStreamInputReaderHeap() {
189 * Create the priority queue with a size twice as bigger as the number
190 * of reader in order to avoid constant resizing.
192 this.prio
= new PriorityQueue
<StreamInputReader
>(
193 this.streamInputReaders
.size() * 2,
194 new StreamInputReaderTimestampComparator());
198 for (StreamInputReader reader
: this.streamInputReaders
) {
200 * Add each trace file reader in the priority queue, if we are able
201 * to read an event from it.
203 if (reader
.readNextEvent()) {
204 this.prio
.add(reader
);
206 this.eventCountPerTraceFile
[pos
] = 0;
215 * Get the current event, which is the current event of the trace file
216 * reader with the lowest timestamp.
218 * @return An event definition, or null of the trace reader reached the end
221 public EventDefinition
getCurrentEventDef() {
222 StreamInputReader top
= getTopStream();
224 return (top
!= null) ? top
.getCurrentEvent() : null;
228 * Go to the next event.
230 * @return True if an event was read.
232 public boolean advance() {
237 * Remove the reader from the top of the priority queue.
239 StreamInputReader top
= this.prio
.poll();
242 * If the queue was empty.
250 if (hasMoreEvents()) {
251 StreamInputPacketReader packetReader
= top
.getPacketReader();
252 boolean packetHasMoreEvents
= packetReader
.hasMoreEvents();
253 StreamInputPacketIndexEntry currentPacket
= packetReader
255 if (!packetHasMoreEvents
) {
256 int n
= this.streamInputReaders
.indexOf(top
);
257 if (!startIndex
.containsKey(n
)) {
258 startIndex
.put(n
, 0L);
260 currentPacket
.setIndexBegin(startIndex
.get(n
));
261 currentPacket
.setIndexEnd(fIndex
);
262 startIndex
.put(n
, fIndex
+ 1);
266 * Read the next event of this reader.
268 if (top
.readNextEvent()) {
270 * Add it back in the queue.
273 final long topEnd
= top
.getCurrentEvent().timestamp
;
274 this.endTime
= Math
.max(topEnd
, this.endTime
);
275 this.eventCountPerTraceFile
[top
.getName()]++;
277 * increment the index
281 boolean hasMoreEvents
= hasMoreEvents();
284 * If there is no reader in the queue, it means the trace reader reached
285 * the end of the trace.
287 return hasMoreEvents
;
291 * Go to the last event in the trace.
293 * @throws CTFReaderException
295 public void goToLastEvent() throws CTFReaderException
{
297 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
299 * Seek the trace reader.
301 streamInputReader
.goToLastEvent();
303 int count
= prio
.size();
304 for (int i
= 0; i
< (count
- 1); i
++) {
310 * Seeks to a given timestamp It will go to the event just after the
311 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
312 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
313 * if you want 21, you'll get 30. You want -inf, you'll get the first
314 * element, you want +inf, you'll get the end of the file with no events.
317 * the timestamp to seek to
318 * @return true if the trace has more events following the timestamp
320 public boolean seek(long timestamp
) {
322 * Remove all the trace readers from the priority queue
327 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
329 * Seek the trace reader.
331 offset
+= streamInputReader
.seek(timestamp
);
334 * Add it to the priority queue if there is a current event.
338 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
339 if (streamInputReader
.getCurrentEvent() != null) {
340 this.prio
.add(streamInputReader
);
341 fIndex
= Math
.max(fIndex
, streamInputReader
.getPacketReader()
342 .getCurrentPacket().getIndexBegin()
346 return hasMoreEvents();
349 public boolean seekIndex(long index
) {
352 long tempIndex
= Long
.MIN_VALUE
;
353 long tempTimestamp
= Long
.MIN_VALUE
;
355 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
357 * Seek the trace reader.
359 final long streamIndex
= streamInputReader
.seekIndex(index
);
360 if (streamInputReader
.getCurrentEvent() != null) {
361 tempIndex
= Math
.max(tempIndex
, streamIndex
);
362 EventDefinition currentEvent
= streamInputReader
365 * Maybe we're at the beginning of a trace.
367 if (currentEvent
== null) {
368 streamInputReader
.readNextEvent();
369 currentEvent
= streamInputReader
.getCurrentEvent();
371 if (currentEvent
!= null) {
372 tempTimestamp
= Math
.max(tempTimestamp
,
373 currentEvent
.timestamp
);
376 * probably beyond the last event
378 tempIndex
= goToZero();
383 } catch (CTFReaderException e
) {
385 * Important, if it failed, it's because it's not yet indexed, so we
386 * have to manually advance to the right value.
388 tempIndex
= goToZero();
390 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
392 * Add it to the priority queue if there is a current event.
395 if (streamInputReader
.getCurrentEvent() != null) {
396 this.prio
.add(streamInputReader
);
399 if (tempIndex
== Long
.MAX_VALUE
) {
402 long pos
= tempIndex
;
403 if (index
> tempIndex
) {
407 while ((prio
.peek().getCurrentEvent().timestamp
< tempTimestamp
)
408 && hasMoreEvents()) {
412 for (pos
= tempIndex
; (pos
< index
) && hasMoreEvents(); pos
++) {
417 return hasMoreEvents();
421 * Go to the first entry of a trace
423 * @return 0, the first index.
425 private long goToZero() {
427 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
429 * Seek the trace reader.
431 streamInputReader
.seek(0);
437 public StreamInputReader
getTopStream() {
438 return this.prio
.peek();
442 * Does the trace have more events?
444 * @return true if yes.
446 public boolean hasMoreEvents() {
447 return this.prio
.size() > 0;
451 * Prints the event count stats.
453 public void printStats() {
458 * Prints the event count stats.
461 * Width of the display.
463 public void printStats(int width
) {
469 for (int i
: this.eventCountPerTraceFile
) {
473 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
474 StreamInputReader se
= this.streamInputReaders
.get(j
);
476 int len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
479 StringBuilder sb
= new StringBuilder(se
.getFilename() + "\t["); //$NON-NLS-1$
481 for (int i
= 0; i
< len
; i
++) {
485 for (int i
= len
; i
< width
; i
++) {
489 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
490 Activator
.getDefault().log(sb
.toString());
494 public long getEndTime() {
499 public int hashCode() {
500 final int prime
= 31;
502 result
= (prime
* result
) + (int) (endTime ^
(endTime
>>> 32));
503 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
504 result
= (prime
* result
)
505 + ((streamInputReaders
== null) ?
0 : streamInputReaders
507 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
512 public boolean equals(Object obj
) {
519 if (getClass() != obj
.getClass()) {
522 CTFTraceReader other
= (CTFTraceReader
) obj
;
523 if (endTime
!= other
.endTime
) {
526 if (startTime
!= other
.startTime
) {
529 if (streamInputReaders
== null) {
530 if (other
.streamInputReaders
!= null) {
533 } else if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
537 if (other
.trace
!= null) {
540 } else if (!trace
.equals(other
.trace
)) {
549 * @see java.lang.Object#toString()
552 public String
toString() {
553 /* Only for debugging, shouldn't be externalized */
554 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
557 public CTFTrace
getTrace() {