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
;
16 import java
.util
.Collections
;
17 import java
.util
.HashMap
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
22 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
25 * A CTF trace event reader. Reads the events of a trace file.
28 * @author Matthew Khouzam
29 * @author Simon Marchi
31 public class StreamInputReader
{
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
38 * The StreamInput we are reading.
40 private final StreamInput fStreamInput
;
43 * The packet reader used to read packets from this trace file.
45 private final StreamInputPacketReader fPacketReader
;
48 * Iterator on the packet index
50 private int fPacketIndex
;
53 * Reference to the current event of this trace file (iow, the last on that
54 * was read, the next one to be returned)
56 private EventDefinition fCurrentEvent
= null;
60 private CTFTraceReader fParent
;
62 /** Map of all the event types */
63 private final Map
<Long
, EventDefinition
> fEventDefs
= new HashMap
<>();
68 private boolean fLive
= false;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Constructs a StreamInputReader that reads a StreamInput.
78 * The StreamInput to read.
79 * @throws CTFReaderException
83 public StreamInputReader(StreamInput streamInput
) throws CTFReaderException
{
84 fStreamInput
= streamInput
;
85 fPacketReader
= new StreamInputPacketReader(this);
87 * Get the iterator on the packet index.
91 * Make first packet the current one.
97 * Dispose the StreamInputReader
101 public void dispose() {
102 fPacketReader
.dispose();
105 // ------------------------------------------------------------------------
106 // Getters/Setters/Predicates
107 // ------------------------------------------------------------------------
110 * Gets the current event in this stream
112 * @return the current event in the stream, null if the stream is
113 * finished/empty/malformed
115 public EventDefinition
getCurrentEvent() {
116 return fCurrentEvent
;
120 * Gets the current packet context
122 * @return the current packet context (size, lost events and such)
124 public StructDefinition
getCurrentPacketContext() {
125 return fPacketReader
.getStreamPacketContextDef();
129 * Gets the byte order for a trace
131 * @return the trace byte order
133 public ByteOrder
getByteOrder() {
134 return fStreamInput
.getStream().getTrace().getByteOrder();
138 * Gets the name of the stream (it's an id and a number)
140 * @return gets the stream name (it's a number)
142 public int getName() {
147 * Sets the name of the stream
150 * the name of the stream, (it's a number)
152 public void setName(int name
) {
157 * Gets the CPU of a stream. It's the same as the one in /proc or running
158 * the asm CPUID instruction
160 * @return The CPU id (a number)
162 public int getCPU() {
163 return fPacketReader
.getCPU();
167 * Gets the filename of the stream being read
169 * @return The filename of the stream being read
171 public String
getFilename() {
172 return fStreamInput
.getFilename();
176 * for internal use only
178 StreamInput
getStreamInput() {
183 * Gets the event definition hashmap for this StreamInput
185 * @return Unmodifiable map with the event definitions
188 public Map
<Long
, EventDefinition
> getEventDefinitions() {
189 return Collections
.unmodifiableMap(fEventDefs
);
193 * Add an event definition to this stream input reader.
196 * The id of the event definition. This will overwrite any
197 * existing definition with the same id.
199 * The matching event definition
202 public void addEventDefinition(Long id
, EventDefinition def
) {
203 fEventDefs
.put(id
, def
);
207 * Set the trace to live mode
210 * whether the trace is read live or not
213 public void setLive(boolean live
) {
218 * Get if the trace is to read live or not
220 * @return whether the trace is live or not
223 public boolean isLive() {
227 // ------------------------------------------------------------------------
229 // ------------------------------------------------------------------------
231 * Reads the next event in the current event variable.
233 * @return If an event has been successfully read.
234 * @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().getEntries().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
) {
320 if (fPacketReader
.getCurrentPacket() == null) {
321 gotoPacket(timestamp
);
325 * Advance until either of these conditions are met:
327 * - reached the end of the trace file (the given timestamp is after the
330 * - found the first event with a timestamp greater or equal the given
334 boolean done
= (this.getCurrentEvent() == null);
335 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
337 done
= (this.getCurrentEvent() == null);
346 * @throws CTFReaderException
349 private void gotoPacket(long timestamp
) throws CTFReaderException
{
350 fPacketIndex
= fStreamInput
.getIndex().search(timestamp
)
353 * Switch to this packet.
359 * Seeks the last event of a stream and returns it.
361 * @throws CTFReaderException
364 public void goToLastEvent() throws CTFReaderException
{
366 * Search in the index for the packet to search in.
368 final int len
= fStreamInput
.getIndex().getEntries().size();
371 * Go to beginning of trace.
375 * if the trace is empty.
377 if ((len
== 0) || (fPacketReader
.hasMoreEvents() == false)) {
379 * This means the trace is empty. abort.
384 * Go to the last packet that contains events.
386 for (int pos
= len
- 1; pos
> 0; pos
--) {
388 fPacketReader
.setCurrentPacket(getPacket());
389 if (fPacketReader
.hasMoreEvents()) {
395 * Go until the end of that packet
397 EventDefinition prevEvent
= null;
398 while (fCurrentEvent
!= null) {
399 prevEvent
= fCurrentEvent
;
400 this.readNextEvent();
403 * Go back to the previous event
405 this.setCurrentEvent(prevEvent
);
411 public CTFTraceReader
getParent() {
419 public void setParent(CTFTraceReader parent
) {
424 * Sets the current event in a stream input reader
426 * @param currentEvent
429 public void setCurrentEvent(EventDefinition currentEvent
) {
430 fCurrentEvent
= currentEvent
;
434 * @return the packetIndexIt
436 private int getPacketIndex() {
440 private StreamInputPacketIndexEntry
getPacket() {
441 return fStreamInput
.getIndex().getEntries().get(getPacketIndex());
445 * @return the packetReader
447 public StreamInputPacketReader
getPacketReader() {
448 return fPacketReader
;
452 public int hashCode() {
453 final int prime
= 31;
455 result
= (prime
* result
) + fId
;
456 result
= (prime
* result
)
457 + ((fStreamInput
== null) ?
0 : fStreamInput
.hashCode());
462 public boolean equals(Object obj
) {
469 if (!(obj
instanceof StreamInputReader
)) {
472 StreamInputReader other
= (StreamInputReader
) obj
;
473 if (fId
!= other
.fId
) {
476 if (fStreamInput
== null) {
477 if (other
.fStreamInput
!= null) {
480 } else if (!fStreamInput
.equals(other
.fStreamInput
)) {
487 public String
toString() {
488 // this helps debugging
489 return fId
+ ' ' + fCurrentEvent
.toString();