1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
<>();
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * Constructs a StreamInputReader that reads a StreamInput.
73 * The StreamInput to read.
74 * @throws CTFReaderException
78 public StreamInputReader(StreamInput streamInput
) throws CTFReaderException
{
79 fStreamInput
= streamInput
;
80 fPacketReader
= new StreamInputPacketReader(this);
82 * Get the iterator on the packet index.
86 * Make first packet the current one.
92 * Dispose the StreamInputReader
96 public void dispose() {
97 fPacketReader
.dispose();
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 current packet context
117 * @return the current packet context (size, lost events and such)
119 public StructDefinition
getCurrentPacketContext() {
120 return fPacketReader
.getStreamPacketContextDef();
124 * Gets the byte order for a trace
126 * @return the trace byte order
128 public ByteOrder
getByteOrder() {
129 return fStreamInput
.getStream().getTrace().getByteOrder();
133 * Gets the name of the stream (it's an id and a number)
135 * @return gets the stream name (it's a number)
137 public int getName() {
142 * Sets the name of the stream
145 * the name of the stream, (it's a number)
147 public void setName(int name
) {
152 * Gets the CPU of a stream. It's the same as the one in /proc or running
153 * the asm CPUID instruction
155 * @return The CPU id (a number)
157 public int getCPU() {
158 return fPacketReader
.getCPU();
162 * Gets the filename of the stream being read
164 * @return The filename of the stream being read
166 public String
getFilename() {
167 return fStreamInput
.getFilename();
171 * for internal use only
173 StreamInput
getStreamInput() {
178 * Gets the event definition hashmap for this StreamInput
180 * @return Unmodifiable map with the event definitions
183 public Map
<Long
, EventDefinition
> getEventDefinitions() {
184 return Collections
.unmodifiableMap(fEventDefs
);
188 * Add an event definition to this stream input reader.
191 * The id of the event definition. This will overwrite any
192 * existing definition with the same id.
194 * The matching event definition
197 public void addEventDefinition(Long id
, EventDefinition def
) {
198 fEventDefs
.put(id
, def
);
201 // ------------------------------------------------------------------------
203 // ------------------------------------------------------------------------
205 * Reads the next event in the current event variable.
207 * @return If an event has been successfully read.
208 * @throws CTFReaderException
211 public boolean readNextEvent() throws CTFReaderException
{
214 * Change packet if needed
216 if (!fPacketReader
.hasMoreEvents()) {
217 final StreamInputPacketIndexEntry prevPacket
= fPacketReader
219 if (prevPacket
!= null) {
225 * If an event is available, read it.
227 if (fPacketReader
.hasMoreEvents()) {
228 this.setCurrentEvent(fPacketReader
.readNextEvent());
231 this.setCurrentEvent(null);
236 * Change the current packet of the packet reader to the next one.
238 * @throws CTFReaderException
241 private void goToNextPacket() throws CTFReaderException
{
243 if (getPacketSize() >= (fPacketIndex
+ 1)) {
244 fPacketReader
.setCurrentPacket(getPacket());
246 if (fStreamInput
.addPacketHeaderIndex()) {
247 fPacketIndex
= getPacketSize() - 1;
248 fPacketReader
.setCurrentPacket(getPacket());
250 fPacketReader
.setCurrentPacket(null);
258 private int getPacketSize() {
259 return fStreamInput
.getIndex().getEntries().size();
263 * Changes the location of the trace file reader so that the current event
264 * is the first event with a timestamp greater or equal the given timestamp.
267 * The timestamp to seek to.
268 * @return The offset compared to the current position
269 * @throws CTFReaderException
272 public long seek(long timestamp
) throws CTFReaderException
{
275 gotoPacket(timestamp
);
278 * index up to the desired timestamp.
280 while ((fPacketReader
.getCurrentPacket() != null)
281 && (fPacketReader
.getCurrentPacket().getTimestampEnd() < timestamp
)) {
283 fStreamInput
.addPacketHeaderIndex();
285 } catch (CTFReaderException e
) {
289 if (fPacketReader
.getCurrentPacket() == null) {
290 gotoPacket(timestamp
);
294 * Advance until either of these conditions are met
296 * <li> reached the end of the trace file (the given timestamp is after the last event), </li>
297 * <li> found the first event with a timestamp greater or equal the given timestamp. </li>
301 boolean done
= (this.getCurrentEvent() == null);
302 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
304 done
= (this.getCurrentEvent() == null);
312 * @throws CTFReaderException
315 private void gotoPacket(long timestamp
) throws CTFReaderException
{
316 fPacketIndex
= fStreamInput
.getIndex().search(timestamp
)
319 * Switch to this packet.
325 * Seeks the last event of a stream and returns it.
327 * @throws CTFReaderException
330 public void goToLastEvent() throws CTFReaderException
{
332 * Search in the index for the packet to search in.
334 final int len
= fStreamInput
.getIndex().getEntries().size();
337 * Go to beginning of trace.
341 * if the trace is empty.
343 if ((len
== 0) || (fPacketReader
.hasMoreEvents() == false)) {
345 * This means the trace is empty. abort.
350 * Go to the last packet that contains events.
352 for (int pos
= len
- 1; pos
> 0; pos
--) {
354 fPacketReader
.setCurrentPacket(getPacket());
355 if (fPacketReader
.hasMoreEvents()) {
361 * Go until the end of that packet
363 EventDefinition prevEvent
= null;
364 while (fCurrentEvent
!= null) {
365 prevEvent
= fCurrentEvent
;
366 this.readNextEvent();
369 * Go back to the previous event
371 this.setCurrentEvent(prevEvent
);
377 public CTFTraceReader
getParent() {
385 public void setParent(CTFTraceReader parent
) {
390 * Sets the current event in a stream input reader
392 * @param currentEvent
395 public void setCurrentEvent(EventDefinition currentEvent
) {
396 fCurrentEvent
= currentEvent
;
400 * @return the packetIndexIt
402 private int getPacketIndex() {
406 private StreamInputPacketIndexEntry
getPacket() {
407 return fStreamInput
.getIndex().getEntries().get(getPacketIndex());
411 * @return the packetReader
413 public StreamInputPacketReader
getPacketReader() {
414 return fPacketReader
;
418 public int hashCode() {
419 final int prime
= 31;
421 result
= (prime
* result
) + fId
;
422 result
= (prime
* result
)
423 + ((fStreamInput
== null) ?
0 : fStreamInput
.hashCode());
428 public boolean equals(Object obj
) {
435 if (!(obj
instanceof StreamInputReader
)) {
438 StreamInputReader other
= (StreamInputReader
) obj
;
439 if (fId
!= other
.fId
) {
442 if (fStreamInput
== null) {
443 if (other
.fStreamInput
!= null) {
446 } else if (!fStreamInput
.equals(other
.fStreamInput
)) {
453 public String
toString() {
454 // this helps debugging
455 return fId
+ ' ' + fCurrentEvent
.toString();