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
102 this.startTime
= prio
.peek().getCurrentEvent().timestamp
;
103 this.endTime
= this.startTime
;
105 startIndex
= new HashMap
<Integer
, Long
>();
111 public CTFTraceReader
copyFrom() {
112 CTFTraceReader newReader
= null;
114 newReader
= new CTFTraceReader(this.trace
);
115 newReader
.startTime
= this.startTime
;
116 newReader
.endTime
= this.endTime
;
120 // ------------------------------------------------------------------------
121 // Getters/Setters/Predicates
122 // ------------------------------------------------------------------------
125 * Return the start time of this trace (== timestamp of the first event)
127 * @return the trace start time
129 public long getStartTime() {
130 return this.startTime
;
136 public long getIndex() {
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 int[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 if (reader
.readNextEvent()) {
200 this.prio
.add(reader
);
202 this.eventCountPerTraceFile
[pos
] = 0;
211 * Get the current event, which is the current event of the trace file
212 * reader with the lowest timestamp.
214 * @return An event definition, or null of the trace reader reached the end
217 public EventDefinition
getCurrentEventDef() {
218 StreamInputReader top
= getTopStream();
220 return (top
!= null) ? top
.getCurrentEvent() : null;
224 * Go to the next event.
226 * @return True if an event was read.
228 public boolean advance() {
233 * Remove the reader from the top of the priority queue.
235 StreamInputReader top
= this.prio
.poll();
238 * If the queue was empty.
246 if (hasMoreEvents()) {
247 StreamInputPacketReader packetReader
= top
.getPacketReader();
248 boolean packetHasMoreEvents
= packetReader
.hasMoreEvents();
249 StreamInputPacketIndexEntry currentPacket
= packetReader
251 if (!packetHasMoreEvents
) {
252 int n
= this.streamInputReaders
.indexOf(top
);
254 if(!startIndex
.containsKey(n
)){
255 startIndex
.put(n
, 0L);
257 currentPacket
.setIndexBegin(startIndex
.get(n
));
258 currentPacket
.setIndexEnd(index
);
259 startIndex
.put(n
, index
+ 1);
263 * Read the next event of this reader.
265 if (top
.readNextEvent()) {
267 * Add it back in the queue.
270 final long topEnd
= top
.getCurrentEvent().timestamp
;
271 this.endTime
= Math
.max(topEnd
, this.endTime
);
272 this.eventCountPerTraceFile
[top
.getName()]++;
274 * increment the index
278 boolean hasMoreEvents
= hasMoreEvents();
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 * @throws CTFReaderException
292 public void goToLastEvent() throws CTFReaderException
{
294 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
296 * Seek the trace reader.
298 streamInputReader
.goToLastEvent();
300 int count
= prio
.size();
301 for (int i
= 0; i
< (count
-1); i
++) {
307 * Seeks to a given timestamp It will go to the event just after the
308 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
309 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
310 * if you want 21, you'll get 30. You want -inf, you'll get the first
311 * element, you want +inf, you'll get the end of the file with no events.
314 * the timestamp to seek to
315 * @return true if the trace has more events following the timestamp
317 public boolean seek(long timestamp
) {
319 * Remove all the trace readers from the priority queue
324 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
326 * Seek the trace reader.
328 offset
+= streamInputReader
.seek(timestamp
);
331 * Add it to the priority queue if there is a current event.
335 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
336 if (streamInputReader
.getCurrentEvent() != null) {
337 this.prio
.add(streamInputReader
);
338 fIndex
= Math
.max(fIndex
, streamInputReader
.getPacketReader()
339 .getCurrentPacket().getIndexBegin()
343 return hasMoreEvents();
346 public boolean seekIndex(long index
) {
349 long tempIndex
= Long
.MIN_VALUE
;
350 long tempTimestamp
= Long
.MIN_VALUE
;
352 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
354 * Seek the trace reader.
356 final long streamIndex
= streamInputReader
.seekIndex(index
);
357 if( streamInputReader
.getCurrentEvent() != null )
359 tempIndex
= Math
.max(tempIndex
, streamIndex
);
360 EventDefinition currentEvent
= streamInputReader
.getCurrentEvent();
362 * Maybe we're at the beginning of a trace.
364 if( currentEvent
== null ){
365 streamInputReader
.readNextEvent();
366 currentEvent
= streamInputReader
.getCurrentEvent();
368 if( currentEvent
!= null ) {
369 tempTimestamp
= Math
.max(tempTimestamp
,
370 currentEvent
.timestamp
);
373 * probably beyond the last event
375 tempIndex
= goToZero();
381 } catch (CTFReaderException e
) {
383 * Important, if it failed, it's because it's not yet indexed, so we
384 * have to manually advance to the right value.
386 tempIndex
= goToZero();
388 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
390 * Add it to the priority queue if there is a current event.
393 if (streamInputReader
.getCurrentEvent() != null) {
394 this.prio
.add(streamInputReader
);
397 if (tempIndex
== Long
.MAX_VALUE
) {
400 long pos
= tempIndex
;
401 if (index
> tempIndex
) {
405 while ((prio
.peek().getCurrentEvent().timestamp
< tempTimestamp
)
406 && hasMoreEvents()) {
410 for (pos
= tempIndex
; (pos
< index
) && hasMoreEvents(); pos
++) {
415 return hasMoreEvents();
419 * Go to the first entry of a trace
420 * @return 0, the first index.
422 private long goToZero() {
424 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
426 * Seek the trace reader.
428 streamInputReader
.seek(0);
434 public StreamInputReader
getTopStream() {
435 return this.prio
.peek();
439 * Does the trace have more events?
441 * @return true if yes.
443 public boolean hasMoreEvents() {
444 return this.prio
.size() > 0;
448 * Prints the event count stats.
450 public void printStats() {
455 * Prints the event count stats.
458 * Width of the display.
460 public void printStats(int width
) {
466 for (int i
: this.eventCountPerTraceFile
) {
470 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
471 StreamInputReader se
= this.streamInputReaders
.get(j
);
473 int len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
476 StringBuilder sb
= new StringBuilder(se
.getFilename() + "\t["); //$NON-NLS-1$
478 for (int i
= 0; i
< len
; i
++) {
482 for (int i
= len
; i
< width
; i
++) {
486 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
487 Activator
.getDefault().log(sb
.toString());
491 public long getEndTime() {
496 public int hashCode() {
497 final int prime
= 31;
499 result
= (prime
* result
) + (int) (endTime ^
(endTime
>>> 32));
500 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
501 result
= (prime
* result
)
502 + ((streamInputReaders
== null) ?
0 : streamInputReaders
504 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
509 public boolean equals(Object obj
) {
516 if (getClass() != obj
.getClass()) {
519 CTFTraceReader other
= (CTFTraceReader
) obj
;
520 if (endTime
!= other
.endTime
) {
523 if (startTime
!= other
.startTime
) {
526 if (streamInputReaders
== null) {
527 if (other
.streamInputReaders
!= null) {
530 } else if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
534 if (other
.trace
!= null) {
537 } else if (!trace
.equals(other
.trace
)) {
546 * @see java.lang.Object#toString()
549 public String
toString() {
550 /* Only for debugging, shouldn't be externalized */
551 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
554 public CTFTrace
getTrace() {