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
.StreamInput
;
20 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
23 * A CTF trace event reader. Reads the events of a trace file.
26 * @author Matthew Khouzam
27 * @author Simon Marchi
29 public class StreamInputReader
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
36 * The StreamInput we are reading.
38 private final StreamInput streamInput
;
41 * The packet reader used to read packets from this trace file.
43 private final StreamInputPacketReader packetReader
;
46 * Iterator on the packet index
48 private int packetIndex
;
51 * Reference to the current event of this trace file (iow, the last on that
52 * was read, the next one to be returned)
54 private EventDefinition currentEvent
= null;
58 private CTFTraceReader parent
;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Constructs a StreamInputReader that reads a StreamInput.
69 * The StreamInput to read.
71 public StreamInputReader(StreamInput streamInput
) {
72 this.streamInput
= streamInput
;
73 this.packetReader
= new StreamInputPacketReader(this);
75 * Get the iterator on the packet index.
79 * Make first packet the current one.
84 // ------------------------------------------------------------------------
85 // Getters/Setters/Predicates
86 // ------------------------------------------------------------------------
89 * Gets the current event in this stream
91 * @return the current event in the stream, null if the stream is
92 * finished/empty/malformed
94 public EventDefinition
getCurrentEvent() {
95 return this.currentEvent
;
99 * gets the current packet context
101 * @return the current packet context (size, lost events and such)
103 public StructDefinition
getCurrentPacketContext() {
104 return this.packetReader
.getStreamPacketContextDef();
108 * Gets the byte order for a trace
110 * @return the trace byte order
112 public ByteOrder
getByteOrder() {
113 return streamInput
.getStream().getTrace().getByteOrder();
117 * Gets the name of the stream (it's an id and a number)
119 * @return gets the stream name (it's a number)
121 public int getName() {
126 * Sets the name of the stream
129 * the name of the stream, (it's a number)
131 public void setName(int name
) {
136 * Gets the CPU of a stream. It's the same as the one in /proc or running
137 * the asm CPUID instruction
139 * @return The CPU id (a number)
141 public int getCPU() {
142 return this.packetReader
.getCPU();
146 * Gets the filename of the stream being read
147 * @return The filename of the stream being read
149 public String
getFilename() {
150 return streamInput
.getFilename();
154 * for internal use only
156 StreamInput
getStreamInput() {
160 // ------------------------------------------------------------------------
162 // ------------------------------------------------------------------------
164 * Reads the next event in the current event variable.
166 * @return If an event has been successfully read.
168 public boolean readNextEvent() {
171 * Change packet if needed
173 if (!this.packetReader
.hasMoreEvents()) {
174 final StreamInputPacketIndexEntry prevPacket
= this.packetReader
176 if (prevPacket
!= null) {
182 * If an event is available, read it.
184 if (this.packetReader
.hasMoreEvents()) {
186 this.setCurrentEvent(this.packetReader
.readNextEvent());
187 } catch (CTFReaderException e
) {
189 * Some problem happened, we'll assume that there are no more
196 this.setCurrentEvent(null);
201 * Change the current packet of the packet reader to the next one.
203 private void goToNextPacket() {
205 if (getPacketSize() >= (packetIndex
+ 1)) {
206 this.packetReader
.setCurrentPacket(getPacket());
209 if (this.streamInput
.addPacketHeaderIndex()) {
210 packetIndex
= getPacketSize() - 1;
211 this.packetReader
.setCurrentPacket(getPacket());
214 this.packetReader
.setCurrentPacket(null);
217 } catch (CTFReaderException e
) {
218 this.packetReader
.setCurrentPacket(null);
226 private int getPacketSize() {
227 return streamInput
.getIndex().getEntries().size();
231 * Changes the location of the trace file reader so that the current event
232 * is the first event with a timestamp greater than the given timestamp.
235 * The timestamp to seek to.
236 * @return The offset compared to the current position
238 public long seek(long timestamp
) {
241 gotoPacket(timestamp
);
244 * index up to the desired timestamp.
246 while ((this.packetReader
.getCurrentPacket() != null)
247 && (this.packetReader
.getCurrentPacket().getTimestampEnd() < timestamp
)) {
249 this.streamInput
.addPacketHeaderIndex();
251 } catch (CTFReaderException e
) {
255 if (this.packetReader
.getCurrentPacket() == null) {
256 gotoPacket(timestamp
);
260 * Advance until A. we reached the end of the trace file (which means
261 * the given timestamp is after the last event), or B. we found the
262 * first event with a timestamp greater than the given timestamp.
265 boolean done
= (this.getCurrentEvent() == null);
266 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
268 done
= (this.getCurrentEvent() == null);
277 private void gotoPacket(long timestamp
) {
278 this.packetIndex
= this.streamInput
.getIndex().search(timestamp
)
281 * Switch to this packet.
287 * Seeks the last event of a stream and returns it.
289 public void goToLastEvent() {
291 * Search in the index for the packet to search in.
293 final int len
= this.streamInput
.getIndex().getEntries().size();
295 @SuppressWarnings("unused")
296 StreamInputPacketIndexEntry entry
= null;
298 * Go to beginning of trace.
302 * if the trace is empty.
304 if ((len
== 0) || (this.packetReader
.hasMoreEvents() == false)) {
306 * This means the trace is empty. abort.
311 * Go to the last packet that contains events.
313 for (int pos
= len
- 1; pos
> 0; pos
--) {
315 this.packetReader
.setCurrentPacket(getPacket());
316 if (this.packetReader
.hasMoreEvents()) {
322 * Go until the end of that packet
324 EventDefinition prevEvent
= null;
325 while (this.currentEvent
!= null) {
326 prevEvent
= this.currentEvent
;
327 this.readNextEvent();
330 * Go back to the previous event
332 this.setCurrentEvent(prevEvent
);
338 public CTFTraceReader
getParent() {
346 public void setParent(CTFTraceReader parent
) {
347 this.parent
= parent
;
351 * Sets the current event in a stream input reader
352 * @param currentEvent the event to set
354 public void setCurrentEvent(EventDefinition currentEvent
) {
355 this.currentEvent
= currentEvent
;
359 * @return the packetIndexIt
361 private int getPacketIndex() {
365 private StreamInputPacketIndexEntry
getPacket() {
366 return streamInput
.getIndex().getEntries().get(getPacketIndex());
370 * @return the packetReader
372 public StreamInputPacketReader
getPacketReader() {
379 * @see java.lang.Object#hashCode()
382 public int hashCode() {
383 final int prime
= 31;
385 result
= (prime
* result
) + name
;
386 result
= (prime
* result
)
387 + ((streamInput
== null) ?
0 : streamInput
.hashCode());
394 * @see java.lang.Object#equals(java.lang.Object)
397 public boolean equals(Object obj
) {
404 if (!(obj
instanceof StreamInputReader
)) {
407 StreamInputReader other
= (StreamInputReader
) obj
;
408 if (name
!= other
.name
) {
411 if (streamInput
== null) {
412 if (other
.streamInput
!= null) {
415 } else if (!streamInput
.equals(other
.streamInput
)) {