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
;
24 * Reads the events of a trace.
27 public class CTFTraceReader
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
34 * The trace to read from.
36 private final CTFTrace trace
;
39 * Vector of all the trace file readers.
41 private final Vector
<StreamInputReader
> streamInputReaders
= new Vector
<StreamInputReader
>();
44 * Priority queue to order the trace file readers by timestamp.
46 protected PriorityQueue
<StreamInputReader
> prio
;
49 * Array to count the number of event per trace file.
51 private int[] eventCountPerTraceFile
;
54 * Timestamp of the first event in the trace
56 private long startTime
;
59 * Timestamp of the last event read so far
68 private final long startIndex
[];
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Constructs a TraceReader to read a trace.
78 * The trace to read from.
79 * @throws CTFReaderException
81 public CTFTraceReader(CTFTrace trace
) {
85 * Create the trace file readers.
87 createStreamInputReaders();
90 * Populate the timestamp-based priority queue.
92 populateStreamInputReaderHeap();
95 * Get the start Time of this trace
97 this.startTime
= prio
.peek().getCurrentEvent().timestamp
;
98 this.endTime
= this.startTime
;
100 startIndex
= new long[prio
.size()];
101 for (int i
= 0; i
< prio
.size(); i
++) {
109 public CTFTraceReader
copyFrom() {
110 CTFTraceReader newReader
= null;
112 newReader
= new CTFTraceReader(this.trace
);
113 newReader
.startTime
= this.startTime
;
114 newReader
.endTime
= this.endTime
;
118 // ------------------------------------------------------------------------
119 // Getters/Setters/Predicates
120 // ------------------------------------------------------------------------
123 * Return the start time of this trace (== timestamp of the first event)
125 * @return the trace start time
127 public long getStartTime() {
128 return this.startTime
;
134 public long getIndex() {
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 * Creates one trace file reader per trace file contained in the trace.
145 private void createStreamInputReaders() {
146 Collection
<Stream
> streams
= this.trace
.getStreams().values();
151 for (Stream stream
: streams
) {
152 Set
<StreamInput
> streamInputs
= stream
.getStreamInputs();
155 * For each trace file of the stream.
157 for (StreamInput streamInput
: streamInputs
) {
161 StreamInputReader streamInputReader
= new StreamInputReader(
165 * Add it to the group.
167 this.streamInputReaders
.add(streamInputReader
);
172 * Create the array to count the number of event per trace file.
174 this.eventCountPerTraceFile
= new int[this.streamInputReaders
.size()];
178 * Initializes the priority queue used to choose the trace file with the
179 * lower next event timestamp.
181 private void populateStreamInputReaderHeap() {
183 * Create the priority queue with a size twice as bigger as the number
184 * of reader in order to avoid constant resizing.
186 this.prio
= new PriorityQueue
<StreamInputReader
>(
187 this.streamInputReaders
.size() * 2,
188 new StreamInputReaderTimestampComparator());
192 for (StreamInputReader reader
: this.streamInputReaders
) {
194 * Add each trace file reader in the priority queue, if we are able
195 * to read an event from it.
197 if (reader
.readNextEvent()) {
198 this.prio
.add(reader
);
200 this.eventCountPerTraceFile
[pos
] = 0;
209 * Get the current event, which is the current event of the trace file
210 * reader with the lowest timestamp.
212 * @return An event definition, or null of the trace reader reached the end
215 public EventDefinition
getCurrentEventDef() {
216 StreamInputReader top
= getTopStream();
218 return (top
!= null) ? top
.getCurrentEvent() : null;
222 * Go to the next event.
224 * @return True if an event was read.
226 public boolean advance() {
231 * Remove the reader from the top of the priority queue.
233 StreamInputReader top
= this.prio
.poll();
236 * If the queue was empty.
244 if (hasMoreEvents()) {
245 StreamInputPacketReader packetReader
= top
.getPacketReader();
246 boolean packetHasMoreEvents
= packetReader
.hasMoreEvents();
247 StreamInputPacketIndexEntry currentPacket
= packetReader
249 if (!packetHasMoreEvents
) {
250 int n
= this.streamInputReaders
.indexOf(top
);
251 currentPacket
.setIndexBegin(startIndex
[n
]);
252 currentPacket
.setIndexEnd(index
);
253 startIndex
[n
] = index
+ 1;
257 * Read the next event of this reader.
259 if (top
.readNextEvent()) {
261 * Add it back in the queue.
264 final long topEnd
= top
.getCurrentEvent().timestamp
;
265 this.endTime
= Math
.max(topEnd
, this.endTime
);
266 this.eventCountPerTraceFile
[top
.getName()]++;
268 * increment the index
272 boolean hasMoreEvents
= hasMoreEvents();
275 * If there is no reader in the queue, it means the trace reader reached
276 * the end of the trace.
278 return hasMoreEvents
;
282 * Go to the last event in the trace.
284 * @throws CTFReaderException
286 public void goToLastEvent() throws CTFReaderException
{
289 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
291 * Seek the trace reader.
293 streamInputReader
.goToLastEvent();
295 int count
= prio
.size();
296 for (int i
= 0; i
< (count
); i
++) {
302 * Seeks to a given timestamp It will go to the event just after the
303 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
304 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
305 * if you want 21, you'll get 30. You want -inf, you'll get the first
306 * element, you want +inf, you'll get the end of the file with no events.
309 * the timestamp to seek to
310 * @return true if the trace has more events following the timestamp
312 public boolean seek(long timestamp
) {
314 * Remove all the trace readers from the priority queue
319 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
321 * Seek the trace reader.
323 offset
+= streamInputReader
.seek(timestamp
);
326 * Add it to the priority queue if there is a current event.
330 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
331 if (streamInputReader
.getCurrentEvent() != null) {
332 this.prio
.add(streamInputReader
);
333 index
= Math
.max(index
, streamInputReader
.getPacketReader()
334 .getCurrentPacket().getIndexBegin()
338 return hasMoreEvents();
341 public boolean seekIndex(long index
) {
344 long tempIndex
= Long
.MIN_VALUE
;
345 long tempTimestamp
= Long
.MIN_VALUE
;
347 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
349 * Seek the trace reader.
351 final long streamIndex
= streamInputReader
.seekIndex(index
);
352 tempIndex
= Math
.max(tempIndex
, streamIndex
);
353 tempTimestamp
= Math
.max(tempTimestamp
,
354 streamInputReader
.getCurrentEvent().timestamp
);
357 } catch (CTFReaderException e
) {
359 * Important, if it failed, it's because it's not yet indexed, so we
360 * have to manually advance to the right value.
362 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
364 * Seek the trace reader.
366 streamInputReader
.seek(0);
370 for (StreamInputReader streamInputReader
: this.streamInputReaders
) {
372 * Add it to the priority queue if there is a current event.
375 if (streamInputReader
.getCurrentEvent() != null) {
376 this.prio
.add(streamInputReader
);
379 if (tempIndex
== Long
.MAX_VALUE
) {
382 long pos
= tempIndex
;
383 if (index
> tempIndex
) {
387 while ((prio
.peek().getCurrentEvent().timestamp
< tempTimestamp
)
388 && hasMoreEvents()) {
392 for (pos
= tempIndex
; (pos
< index
) && hasMoreEvents(); pos
++) {
397 return hasMoreEvents();
400 public StreamInputReader
getTopStream() {
401 return this.prio
.peek();
405 * Does the trace have more events?
407 * @return true if yes.
409 public boolean hasMoreEvents() {
410 return this.prio
.size() > 0;
414 * Prints the event count stats.
416 public void printStats() {
421 * Prints the event count stats.
424 * Width of the display.
426 public void printStats(int width
) {
432 for (int i
: this.eventCountPerTraceFile
) {
436 for (int j
= 0; j
< this.eventCountPerTraceFile
.length
; j
++) {
437 StreamInputReader se
= this.streamInputReaders
.get(j
);
439 int len
= (width
* this.eventCountPerTraceFile
[se
.getName()])
442 StringBuilder sb
= new StringBuilder(se
.getStreamInput()
443 .getFilename() + "\t["); //$NON-NLS-1$
445 for (int i
= 0; i
< len
; i
++) {
449 for (int i
= len
; i
< width
; i
++) {
453 sb
.append("]\t" + this.eventCountPerTraceFile
[se
.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
454 Activator
.getDefault().log(sb
.toString());
458 public long getEndTime() {
463 public int hashCode() {
464 final int prime
= 31;
466 result
= (prime
* result
) + (int) (endTime ^
(endTime
>>> 32));
467 result
= (prime
* result
) + (int) (startTime ^
(startTime
>>> 32));
468 result
= (prime
* result
)
469 + ((streamInputReaders
== null) ?
0 : streamInputReaders
471 result
= (prime
* result
) + ((trace
== null) ?
0 : trace
.hashCode());
476 public boolean equals(Object obj
) {
483 if (getClass() != obj
.getClass()) {
486 CTFTraceReader other
= (CTFTraceReader
) obj
;
487 if (endTime
!= other
.endTime
) {
490 if (startTime
!= other
.startTime
) {
493 if (streamInputReaders
== null) {
494 if (other
.streamInputReaders
!= null) {
497 } else if (!streamInputReaders
.equals(other
.streamInputReaders
)) {
501 if (other
.trace
!= null) {
504 } else if (!trace
.equals(other
.trace
)) {
513 * @see java.lang.Object#toString()
516 public String
toString() {
517 /* Only for debugging, shouldn't be externalized */
518 return "CTFTraceReader [trace=" + trace
+ ']'; //$NON-NLS-1$
521 public CTFTrace
getTrace() {