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
.tracecompass
.ctf
.core
.trace
;
16 import java
.io
.IOException
;
17 import java
.nio
.ByteOrder
;
18 import java
.nio
.channels
.FileChannel
;
19 import java
.nio
.file
.StandardOpenOption
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.CTFReaderException
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
26 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.Activator
;
27 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
29 import com
.google
.common
.collect
.ImmutableList
;
32 * A CTF trace event reader. Reads the events of a trace file.
34 * @author Matthew Khouzam
35 * @author Simon Marchi
37 public class CTFStreamInputReader
implements AutoCloseable
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 * The StreamInput we are reading.
46 private final @NonNull File fFile
;
48 private final @NonNull CTFStreamInput fStreamInput
;
50 private final FileChannel fFileChannel
;
53 * The packet reader used to read packets from this trace file.
55 private final CTFStreamInputPacketReader fPacketReader
;
58 * Iterator on the packet index
60 private int fPacketIndex
;
63 * Reference to the current event of this trace file (iow, the last on that
64 * was read, the next one to be returned)
66 private EventDefinition fCurrentEvent
= null;
70 private CTFTraceReader fParent
;
75 private boolean fLive
= false;
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 * Constructs a StreamInputReader that reads a StreamInput.
84 * The StreamInput to read.
85 * @throws CTFReaderException
86 * If the file cannot be opened
88 public CTFStreamInputReader(CTFStreamInput streamInput
) throws CTFReaderException
{
89 if (streamInput
== null) {
90 throw new IllegalArgumentException("stream cannot be null"); //$NON-NLS-1$
92 fStreamInput
= streamInput
;
93 fFile
= fStreamInput
.getFile();
95 fFileChannel
= FileChannel
.open(fFile
.toPath(), StandardOpenOption
.READ
);
96 } catch (IOException e
) {
97 throw new CTFReaderException(e
);
99 fPacketReader
= new CTFStreamInputPacketReader(this);
101 * Get the iterator on the packet index.
105 * Make first packet the current one.
111 * Dispose the StreamInputReader, closes the file channel and its packet
114 * @throws IOException
115 * If an I/O error occurs
118 public void close() throws IOException
{
119 fFileChannel
.close();
120 fPacketReader
.close();
123 // ------------------------------------------------------------------------
124 // Getters/Setters/Predicates
125 // ------------------------------------------------------------------------
128 * Gets the current event in this stream
130 * @return the current event in the stream, null if the stream is
131 * finished/empty/malformed
133 public EventDefinition
getCurrentEvent() {
134 return fCurrentEvent
;
138 * Gets the byte order for a trace
140 * @return the trace byte order
142 public ByteOrder
getByteOrder() {
143 return fStreamInput
.getStream().getTrace().getByteOrder();
147 * Gets the name of the stream (it's an id and a number)
149 * @return gets the stream name (it's a number)
151 public int getName() {
156 * Sets the name of the stream
159 * the name of the stream, (it's a number)
161 public void setName(int name
) {
166 * Gets the CPU of a stream. It's the same as the one in /proc or running
167 * the asm CPUID instruction
169 * @return The CPU id (a number)
171 public int getCPU() {
172 return fPacketReader
.getCPU();
176 * Gets the filename of the stream being read
178 * @return The filename of the stream being read
180 public String
getFilename() {
181 return fStreamInput
.getFilename();
185 * for internal use only
187 CTFStreamInput
getStreamInput() {
192 * Gets the event definition set for this StreamInput
194 * @return Unmodifiable set with the event definitions
196 public Iterable
<IEventDeclaration
> getEventDeclarations() {
197 return ImmutableList
.copyOf(fStreamInput
.getStream().getEventDeclarations());
201 * Set the trace to live mode
204 * whether the trace is read live or not
206 public void setLive(boolean live
) {
211 * Get if the trace is to read live or not
213 * @return whether the trace is live or not
215 public boolean isLive() {
220 * Get the event context of the stream
222 * @return the event context declaration of the stream
224 public StructDeclaration
getStreamEventContextDecl() {
225 return getStreamInput().getStream().getEventContextDecl();
228 // ------------------------------------------------------------------------
230 // ------------------------------------------------------------------------
232 * Reads the next event in the current event variable.
234 * @return If an event has been successfully read.
235 * @throws CTFReaderException
238 public CTFResponse
readNextEvent() throws CTFReaderException
{
241 * Change packet if needed
243 if (!fPacketReader
.hasMoreEvents()) {
244 final StreamInputPacketIndexEntry prevPacket
= fPacketReader
246 if (prevPacket
!= null || fLive
) {
253 * If an event is available, read it.
255 if (fPacketReader
.hasMoreEvents()) {
256 setCurrentEvent(fPacketReader
.readNextEvent());
257 return CTFResponse
.OK
;
259 this.setCurrentEvent(null);
260 return fLive ? CTFResponse
.WAIT
: CTFResponse
.FINISH
;
264 * Change the current packet of the packet reader to the next one.
266 * @throws CTFReaderException
269 private void goToNextPacket() throws CTFReaderException
{
271 // did we already index the packet?
272 if (getPacketSize() >= (fPacketIndex
+ 1)) {
273 fPacketReader
.setCurrentPacket(getPacket());
275 // go to the next packet if there is one, index it at the same time
276 if (fStreamInput
.addPacketHeaderIndex()) {
277 fPacketIndex
= getPacketSize() - 1;
278 fPacketReader
.setCurrentPacket(getPacket());
281 fPacketReader
.setCurrentPacket(null);
289 private int getPacketSize() {
290 return fStreamInput
.getIndex().size();
294 * Changes the location of the trace file reader so that the current event
295 * is the first event with a timestamp greater or equal the given timestamp.
298 * The timestamp to seek to.
299 * @return The offset compared to the current position
300 * @throws CTFReaderException
303 public long seek(long timestamp
) throws CTFReaderException
{
306 gotoPacket(timestamp
);
309 * index up to the desired timestamp.
311 while ((fPacketReader
.getCurrentPacket() != null)
312 && (fPacketReader
.getCurrentPacket().getTimestampEnd() < timestamp
)) {
314 fStreamInput
.addPacketHeaderIndex();
316 } catch (CTFReaderException e
) {
318 Activator
.log(e
.getMessage());
321 if (fPacketReader
.getCurrentPacket() == null) {
322 gotoPacket(timestamp
);
326 * Advance until either of these conditions are met:
328 * - reached the end of the trace file (the given timestamp is after the
331 * - found the first event with a timestamp greater or equal the given
335 boolean done
= (this.getCurrentEvent() == null);
336 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
338 done
= (this.getCurrentEvent() == null);
347 * @throws CTFReaderException
350 private void gotoPacket(long timestamp
) throws CTFReaderException
{
351 fPacketIndex
= fStreamInput
.getIndex().search(timestamp
)
354 * Switch to this packet.
360 * Seeks the last event of a stream and returns it.
362 * @throws CTFReaderException
365 public void goToLastEvent() throws CTFReaderException
{
367 * Search in the index for the packet to search in.
369 final int len
= fStreamInput
.getIndex().size();
372 * Go to beginning of trace.
376 * if the trace is empty.
378 if ((fStreamInput
.getIndex().isEmpty()) || (!fPacketReader
.hasMoreEvents())) {
380 * This means the trace is empty. abort.
385 * Go to the last packet that contains events.
387 for (int pos
= len
- 1; pos
> 0; pos
--) {
389 fPacketReader
.setCurrentPacket(getPacket());
390 if (fPacketReader
.hasMoreEvents()) {
396 * Go until the end of that packet
398 EventDefinition prevEvent
= null;
399 while (fCurrentEvent
!= null) {
400 prevEvent
= fCurrentEvent
;
401 this.readNextEvent();
404 * Go back to the previous event
406 this.setCurrentEvent(prevEvent
);
412 public CTFTraceReader
getParent() {
420 public void setParent(CTFTraceReader parent
) {
425 * Sets the current event in a stream input reader
427 * @param currentEvent
430 public void setCurrentEvent(EventDefinition currentEvent
) {
431 fCurrentEvent
= currentEvent
;
435 * @return the packetIndexIt
437 private int getPacketIndex() {
441 private StreamInputPacketIndexEntry
getPacket() {
442 return fStreamInput
.getIndex().getElement(getPacketIndex());
446 * Get the file channel wrapped by this reader
448 * @return the file channel
450 FileChannel
getFc() {
455 * @return the packetReader
457 public CTFStreamInputPacketReader
getPacketReader() {
458 return fPacketReader
;
462 public int hashCode() {
463 final int prime
= 31;
465 result
= (prime
* result
) + fId
;
466 result
= (prime
* result
)
472 public boolean equals(Object obj
) {
479 if (!(obj
instanceof CTFStreamInputReader
)) {
482 CTFStreamInputReader other
= (CTFStreamInputReader
) obj
;
483 if (fId
!= other
.fId
) {
486 return fFile
.equals(other
.fFile
);
490 public String
toString() {
491 // this helps debugging
492 return fId
+ ' ' + fCurrentEvent
.toString();