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: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
15 import java
.nio
.ByteOrder
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
19 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
22 * A CTF trace event reader. Reads the events of a trace file.
25 * @author Matthew Khouzam
26 * @author Simon Marchi
28 public class StreamInputReader
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
35 * The StreamInput we are reading.
37 private final StreamInput streamInput
;
40 * The packet reader used to read packets from this trace file.
42 private final StreamInputPacketReader packetReader
;
45 * Iterator on the packet index
47 private int packetIndex
;
50 * Reference to the current event of this trace file (iow, the last on that
51 * was read, the next one to be returned)
53 private EventDefinition currentEvent
= null;
57 private CTFTraceReader parent
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 * Constructs a StreamInputReader that reads a StreamInput.
68 * The StreamInput to read.
70 public StreamInputReader(StreamInput streamInput
) {
71 this.streamInput
= streamInput
;
72 this.packetReader
= new StreamInputPacketReader(this);
74 * Get the iterator on the packet index.
78 * Make first packet the current one.
83 // ------------------------------------------------------------------------
84 // Getters/Setters/Predicates
85 // ------------------------------------------------------------------------
88 * Gets the current event in this stream
90 * @return the current event in the stream, null if the stream is
91 * finished/empty/malformed
93 public EventDefinition
getCurrentEvent() {
94 return this.currentEvent
;
98 * gets the current packet context
100 * @return the current packet context (size, lost events and such)
102 public StructDefinition
getCurrentPacketContext() {
103 return this.packetReader
.getStreamPacketContextDef();
107 * Gets the byte order for a trace
109 * @return the trace byte order
111 public ByteOrder
getByteOrder() {
112 return streamInput
.getStream().getTrace().getByteOrder();
116 * Gets the name of the stream (it's an id and a number)
118 * @return gets the stream name (it's a number)
120 public int getName() {
125 * Sets the name of the stream
128 * the name of the stream, (it's a number)
130 public void setName(int name
) {
135 * Gets the CPU of a stream. It's the same as the one in /proc or running
136 * the asm CPUID instruction
138 * @return The CPU id (a number)
140 public int getCPU() {
141 return this.packetReader
.getCPU();
145 * Gets the filename of the stream being read
146 * @return The filename of the stream being read
148 public String
getFilename() {
149 return streamInput
.getFilename();
153 * for internal use only
155 StreamInput
getStreamInput() {
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
163 * Reads the next event in the current event variable.
165 * @return If an event has been successfully read.
167 public boolean readNextEvent() {
170 * Change packet if needed
172 if (!this.packetReader
.hasMoreEvents()) {
173 final StreamInputPacketIndexEntry prevPacket
= this.packetReader
175 if (prevPacket
!= null) {
181 * If an event is available, read it.
183 if (this.packetReader
.hasMoreEvents()) {
185 this.setCurrentEvent(this.packetReader
.readNextEvent());
186 } catch (CTFReaderException e
) {
188 * Some problem happened, we'll assume that there are no more
195 this.setCurrentEvent(null);
200 * Change the current packet of the packet reader to the next one.
202 private void goToNextPacket() {
204 if (getPacketSize() >= (packetIndex
+ 1)) {
205 this.packetReader
.setCurrentPacket(getPacket());
208 if (this.streamInput
.addPacketHeaderIndex()) {
209 packetIndex
= getPacketSize() - 1;
210 this.packetReader
.setCurrentPacket(getPacket());
213 this.packetReader
.setCurrentPacket(null);
216 } catch (CTFReaderException e
) {
217 this.packetReader
.setCurrentPacket(null);
225 private int getPacketSize() {
226 return streamInput
.getIndex().getEntries().size();
230 * Changes the location of the trace file reader so that the current event
231 * is the first event with a timestamp greater than the given timestamp.
234 * The timestamp to seek to.
235 * @return The offset compared to the current position
237 public long seek(long timestamp
) {
240 gotoPacket(timestamp
);
243 * index up to the desired timestamp.
245 while ((this.packetReader
.getCurrentPacket() != null)
246 && (this.packetReader
.getCurrentPacket().getTimestampEnd() < timestamp
)) {
248 this.streamInput
.addPacketHeaderIndex();
250 } catch (CTFReaderException e
) {
254 if (this.packetReader
.getCurrentPacket() == null) {
255 gotoPacket(timestamp
);
259 * Advance until A. we reached the end of the trace file (which means
260 * the given timestamp is after the last event), or B. we found the
261 * first event with a timestamp greater than the given timestamp.
264 boolean done
= (this.getCurrentEvent() == null);
265 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
267 done
= (this.getCurrentEvent() == null);
276 private void gotoPacket(long timestamp
) {
277 this.packetIndex
= this.streamInput
.getIndex().search(timestamp
)
280 * Switch to this packet.
286 * Seeks the last event of a stream and returns it.
288 public void goToLastEvent() {
290 * Search in the index for the packet to search in.
292 final int len
= this.streamInput
.getIndex().getEntries().size();
294 @SuppressWarnings("unused")
295 StreamInputPacketIndexEntry entry
= null;
297 * Go to beginning of trace.
301 * if the trace is empty.
303 if ((len
== 0) || (this.packetReader
.hasMoreEvents() == false)) {
305 * This means the trace is empty. abort.
310 * Go to the last packet that contains events.
312 for (int pos
= len
- 1; pos
> 0; pos
--) {
314 this.packetReader
.setCurrentPacket(getPacket());
315 if (this.packetReader
.hasMoreEvents()) {
321 * Go until the end of that packet
323 EventDefinition prevEvent
= null;
324 while (this.currentEvent
!= null) {
325 prevEvent
= this.currentEvent
;
326 this.readNextEvent();
329 * Go back to the previous event
331 this.setCurrentEvent(prevEvent
);
337 public CTFTraceReader
getParent() {
345 public void setParent(CTFTraceReader parent
) {
346 this.parent
= parent
;
350 * Sets the current event in a stream input reader
351 * @param currentEvent the event to set
353 public void setCurrentEvent(EventDefinition currentEvent
) {
354 this.currentEvent
= currentEvent
;
358 * @return the packetIndexIt
360 private int getPacketIndex() {
364 private StreamInputPacketIndexEntry
getPacket() {
365 return streamInput
.getIndex().getEntries().get(getPacketIndex());
369 * @return the packetReader
371 public StreamInputPacketReader
getPacketReader() {
378 * @see java.lang.Object#hashCode()
381 public int hashCode() {
382 final int prime
= 31;
384 result
= (prime
* result
) + name
;
385 result
= (prime
* result
)
386 + ((streamInput
== null) ?
0 : streamInput
.hashCode());
393 * @see java.lang.Object#equals(java.lang.Object)
396 public boolean equals(Object obj
) {
403 if (!(obj
instanceof StreamInputReader
)) {
406 StreamInputReader other
= (StreamInputReader
) obj
;
407 if (name
!= other
.name
) {
410 if (streamInput
== null) {
411 if (other
.streamInput
!= null) {
414 } else if (!streamInput
.equals(other
.streamInput
)) {