1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
.StructDeclaration
;
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
implements AutoCloseable
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
35 * The StreamInput we are reading.
37 private final StreamInput fStreamInput
;
40 * The packet reader used to read packets from this trace file.
42 private final StreamInputPacketReader fPacketReader
;
45 * Iterator on the packet index
47 private int fPacketIndex
;
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 fCurrentEvent
= null;
57 private CTFTraceReader fParent
;
62 private boolean fLive
= false;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Constructs a StreamInputReader that reads a StreamInput.
72 * The StreamInput to read.
73 * @throws CTFReaderException
77 public StreamInputReader(StreamInput streamInput
) throws CTFReaderException
{
78 fStreamInput
= streamInput
;
79 fPacketReader
= new StreamInputPacketReader(this);
81 * Get the iterator on the packet index.
85 * Make first packet the current one.
91 * Dispose the StreamInputReader
97 fPacketReader
.close();
100 // ------------------------------------------------------------------------
101 // Getters/Setters/Predicates
102 // ------------------------------------------------------------------------
105 * Gets the current event in this stream
107 * @return the current event in the stream, null if the stream is
108 * finished/empty/malformed
110 public EventDefinition
getCurrentEvent() {
111 return fCurrentEvent
;
115 * Gets the byte order for a trace
117 * @return the trace byte order
119 public ByteOrder
getByteOrder() {
120 return fStreamInput
.getStream().getTrace().getByteOrder();
124 * Gets the name of the stream (it's an id and a number)
126 * @return gets the stream name (it's a number)
128 public int getName() {
133 * Sets the name of the stream
136 * the name of the stream, (it's a number)
138 public void setName(int name
) {
143 * Gets the CPU of a stream. It's the same as the one in /proc or running
144 * the asm CPUID instruction
146 * @return The CPU id (a number)
148 public int getCPU() {
149 return fPacketReader
.getCPU();
153 * Gets the filename of the stream being read
155 * @return The filename of the stream being read
157 public String
getFilename() {
158 return fStreamInput
.getFilename();
162 * for internal use only
164 StreamInput
getStreamInput() {
169 * Set the trace to live mode
172 * whether the trace is read live or not
175 public void setLive(boolean live
) {
180 * Get if the trace is to read live or not
182 * @return whether the trace is live or not
185 public boolean isLive() {
190 * Get the event context of the stream
192 * @return the event context declaration of the stream
195 public StructDeclaration
getStreamEventContextDecl() {
196 return getStreamInput().getStream().getEventContextDecl();
199 // ------------------------------------------------------------------------
201 // ------------------------------------------------------------------------
203 * Reads the next event in the current event variable.
205 * @return If an event has been successfully read.
206 * @throws CTFReaderException
210 public CTFResponse
readNextEvent() throws CTFReaderException
{
213 * Change packet if needed
215 if (!fPacketReader
.hasMoreEvents()) {
216 final StreamInputPacketIndexEntry prevPacket
= fPacketReader
218 if (prevPacket
!= null || fLive
) {
225 * If an event is available, read it.
227 if (fPacketReader
.hasMoreEvents()) {
228 setCurrentEvent(fPacketReader
.readNextEvent());
229 return CTFResponse
.OK
;
231 this.setCurrentEvent(null);
232 return fLive ? CTFResponse
.WAIT
: CTFResponse
.FINISH
;
236 * Change the current packet of the packet reader to the next one.
238 * @throws CTFReaderException
241 private void goToNextPacket() throws CTFReaderException
{
243 // did we already index the packet?
244 if (getPacketSize() >= (fPacketIndex
+ 1)) {
245 fPacketReader
.setCurrentPacket(getPacket());
247 // go to the next packet if there is one, index it at the same time
248 if (fStreamInput
.addPacketHeaderIndex()) {
249 fPacketIndex
= getPacketSize() - 1;
250 fPacketReader
.setCurrentPacket(getPacket());
253 fPacketReader
.setCurrentPacket(null);
261 private int getPacketSize() {
262 return fStreamInput
.getIndex().getEntries().size();
266 * Changes the location of the trace file reader so that the current event
267 * is the first event with a timestamp greater or equal the given timestamp.
270 * The timestamp to seek to.
271 * @return The offset compared to the current position
272 * @throws CTFReaderException
275 public long seek(long timestamp
) throws CTFReaderException
{
278 gotoPacket(timestamp
);
281 * index up to the desired timestamp.
283 while ((fPacketReader
.getCurrentPacket() != null)
284 && (fPacketReader
.getCurrentPacket().getTimestampEnd() < timestamp
)) {
286 fStreamInput
.addPacketHeaderIndex();
288 } catch (CTFReaderException e
) {
292 if (fPacketReader
.getCurrentPacket() == null) {
293 gotoPacket(timestamp
);
297 * Advance until either of these conditions are met:
299 * - reached the end of the trace file (the given timestamp is after the
302 * - found the first event with a timestamp greater or equal the given
306 boolean done
= (this.getCurrentEvent() == null);
307 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
309 done
= (this.getCurrentEvent() == null);
318 * @throws CTFReaderException
321 private void gotoPacket(long timestamp
) throws CTFReaderException
{
322 fPacketIndex
= fStreamInput
.getIndex().search(timestamp
)
325 * Switch to this packet.
331 * Seeks the last event of a stream and returns it.
333 * @throws CTFReaderException
336 public void goToLastEvent() throws CTFReaderException
{
338 * Search in the index for the packet to search in.
340 final int len
= fStreamInput
.getIndex().getEntries().size();
343 * Go to beginning of trace.
347 * if the trace is empty.
349 if ((len
== 0) || (fPacketReader
.hasMoreEvents() == false)) {
351 * This means the trace is empty. abort.
356 * Go to the last packet that contains events.
358 for (int pos
= len
- 1; pos
> 0; pos
--) {
360 fPacketReader
.setCurrentPacket(getPacket());
361 if (fPacketReader
.hasMoreEvents()) {
367 * Go until the end of that packet
369 EventDefinition prevEvent
= null;
370 while (fCurrentEvent
!= null) {
371 prevEvent
= fCurrentEvent
;
372 this.readNextEvent();
375 * Go back to the previous event
377 this.setCurrentEvent(prevEvent
);
383 public CTFTraceReader
getParent() {
391 public void setParent(CTFTraceReader parent
) {
396 * Sets the current event in a stream input reader
398 * @param currentEvent
401 public void setCurrentEvent(EventDefinition currentEvent
) {
402 fCurrentEvent
= currentEvent
;
406 * @return the packetIndexIt
408 private int getPacketIndex() {
412 private StreamInputPacketIndexEntry
getPacket() {
413 return fStreamInput
.getIndex().getEntries().get(getPacketIndex());
417 * @return the packetReader
419 public StreamInputPacketReader
getPacketReader() {
420 return fPacketReader
;
424 public int hashCode() {
425 final int prime
= 31;
427 result
= (prime
* result
) + fId
;
428 result
= (prime
* result
)
429 + ((fStreamInput
== null) ?
0 : fStreamInput
.hashCode());
434 public boolean equals(Object obj
) {
441 if (!(obj
instanceof StreamInputReader
)) {
444 StreamInputReader other
= (StreamInputReader
) obj
;
445 if (fId
!= other
.fId
) {
448 if (fStreamInput
== null) {
449 if (other
.fStreamInput
!= null) {
452 } else if (!fStreamInput
.equals(other
.fStreamInput
)) {
459 public String
toString() {
460 // this helps debugging
461 return fId
+ ' ' + fCurrentEvent
.toString();