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 * <b><u>StreamInputReader</u></b>
25 * Reads the events of a trace file.
27 public class StreamInputReader
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
34 * The StreamInput we are reading.
36 private final StreamInput streamInput
;
39 * The packet reader used to read packets from this trace file.
41 private final StreamInputPacketReader packetReader
;
44 * Iterator on the packet index
46 private int packetIndex
;
49 * Reference to the current event of this trace file (iow, the last on that
50 * was read, the next one to be returned)
52 private EventDefinition currentEvent
= null;
56 private CTFTraceReader parent
;
58 private final long prevIndex
;
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.
80 * Make first packet the current one.
85 // ------------------------------------------------------------------------
86 // Getters/Setters/Predicates
87 // ------------------------------------------------------------------------
90 * Gets the current event in this stream
92 * @return the current event in the stream, null if the stream is
93 * finished/empty/malformed
95 public EventDefinition
getCurrentEvent() {
96 return this.currentEvent
;
100 * gets the current packet context
102 * @return the current packet context (size, lost events and such)
104 public StructDefinition
getCurrentPacketContext() {
105 return this.packetReader
.getStreamPacketContextDef();
109 * Gets the byte order for a trace
111 * @return the trace byte order
113 public ByteOrder
getByteOrder() {
114 return streamInput
.getStream().getTrace().getByteOrder();
118 * Gets the name of the stream (it's an id and a number)
120 * @return gets the stream name (it's a number)
122 public int getName() {
127 * Sets the name of the stream
130 * the name of the stream, (it's a number)
132 public void setName(int name
) {
137 * Gets the CPU of a stream. It's the same as the one in /proc or running
138 * the asm CPUID instruction
140 * @return The CPU id (a number)
142 public int getCPU() {
143 return this.packetReader
.getCPU();
147 * Gets the filename of the stream being read
148 * @return The filename of the stream being read
150 public String
getFilename() {
151 return streamInput
.getFilename();
155 * for internal use only
157 StreamInput
getStreamInput() {
161 // ------------------------------------------------------------------------
163 // ------------------------------------------------------------------------
165 * Reads the next event in the current event variable.
167 * @return If an event has been successfully read.
169 public boolean readNextEvent() {
172 * Change packet if needed
174 if (!this.packetReader
.hasMoreEvents()) {
175 final StreamInputPacketIndexEntry prevPacket
= this.packetReader
177 if (prevPacket
!= null) {
179 final StreamInputPacketIndexEntry currentPacket
= this.packetReader
184 /*autogenerate javadoc getter setter
185 * If an event is available, read it.
187 if (this.packetReader
.hasMoreEvents()) {
189 this.setCurrentEvent(this.packetReader
.readNextEvent());
190 } catch (CTFReaderException e
) {
192 * Some problem happened, we'll assume that there are no more
199 this.setCurrentEvent(null);
204 * Change the current packet of the packet reader to the next one.
206 private void goToNextPacket() {
208 if (getPacketSize() >= (packetIndex
+ 1)) {
209 this.packetReader
.setCurrentPacket(getPacket());
212 if (this.streamInput
.addPacketHeaderIndex()) {
213 packetIndex
= getPacketSize() - 1;
214 this.packetReader
.setCurrentPacket(getPacket());
217 this.packetReader
.setCurrentPacket(null);
220 } catch (CTFReaderException e
) {
221 this.packetReader
.setCurrentPacket(null);
229 private int getPacketSize() {
230 return streamInput
.getIndex().getEntries().size();
234 * Changes the location of the trace file reader so that the current event
235 * is the first event with a timestamp greater than the given timestamp.
238 * The timestamp to seek to.
240 public long seek(long timestamp
) {
243 gotoPacket(timestamp
);
246 * index up to the desired timestamp.
248 while ((this.packetReader
.getCurrentPacket() != null)
249 && (this.packetReader
.getCurrentPacket().getTimestampEnd() < timestamp
)) {
251 this.streamInput
.addPacketHeaderIndex();
253 } catch (CTFReaderException e
) {
257 if (this.packetReader
.getCurrentPacket() == null) {
258 gotoPacket(timestamp
);
262 * Advance until A. we reached the end of the trace file (which means
263 * the given timestamp is after the last event), or B. we found the
264 * first event with a timestamp greater than the given timestamp.
267 boolean done
= (this.getCurrentEvent() == null);
268 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
270 done
= (this.getCurrentEvent() == null);
279 private void gotoPacket(long timestamp
) {
280 this.packetIndex
= this.streamInput
.getIndex().search(timestamp
)
283 * Switch to this packet.
289 * Seeks the last event of a stream and returns it.
291 public void goToLastEvent() {
293 * Search in the index for the packet to search in.
295 final int len
= this.streamInput
.getIndex().getEntries().size();
297 StreamInputPacketIndexEntry entry
= null;
299 * Go to beginning of trace.
303 * if the trace is empty.
305 if ((len
== 0) || (this.packetReader
.hasMoreEvents() == false)) {
307 * This means the trace is empty. abort.
312 * Go to the last packet that contains events.
314 for (int pos
= len
- 1; pos
> 0; pos
--) {
316 this.packetReader
.setCurrentPacket(getPacket());
317 if (this.packetReader
.hasMoreEvents()) {
323 * Go until the end of that packet
325 EventDefinition prevEvent
= null;
326 while (this.currentEvent
!= null) {
327 prevEvent
= this.currentEvent
;
328 this.readNextEvent();
331 * Go back to the previous event
333 this.setCurrentEvent(prevEvent
);
339 public CTFTraceReader
getParent() {
347 public void setParent(CTFTraceReader parent
) {
348 this.parent
= parent
;
352 * Sets the current event in a stream input reader
353 * @param currentEvent the event to set
355 public void setCurrentEvent(EventDefinition currentEvent
) {
356 this.currentEvent
= currentEvent
;
360 * @return the packetIndexIt
362 private int getPacketIndex() {
366 private StreamInputPacketIndexEntry
getPacket() {
367 return streamInput
.getIndex().getEntries().get(getPacketIndex());
371 * @return the packetReader
373 public StreamInputPacketReader
getPacketReader() {
380 * @see java.lang.Object#hashCode()
383 public int hashCode() {
384 final int prime
= 31;
386 result
= (prime
* result
) + name
;
387 result
= (prime
* result
)
388 + ((streamInput
== null) ?
0 : streamInput
.hashCode());
395 * @see java.lang.Object#equals(java.lang.Object)
398 public boolean equals(Object obj
) {
405 if (!(obj
instanceof StreamInputReader
)) {
408 StreamInputReader other
= (StreamInputReader
) obj
;
409 if (name
!= other
.name
) {
412 if (streamInput
== null) {
413 if (other
.streamInput
!= null) {
416 } else if (!streamInput
.equals(other
.streamInput
)) {