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
;
16 import java
.util
.ListIterator
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
20 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInput
;
21 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
24 * <b><u>StreamInputReader</u></b>
26 * Reads the events of a trace file.
28 public class StreamInputReader
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
35 * The StreamInput we are reading.
37 private final StreamInput streamInput
;
40 * The packet reader used to read packets from this trace file.
42 private final StreamInputPacketReader packetReader
;
45 * Iterator on the packet index
47 private ListIterator
<StreamInputPacketIndexEntry
> packetIndexIt
;
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 currentEvent
= null;
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 * Constructs a StreamInputReader that reads a StreamInput.
65 * The StreamInput to read.
67 public StreamInputReader(StreamInput streamInput
) {
68 this.streamInput
= streamInput
;
69 this.packetReader
= new StreamInputPacketReader(this);
72 * Get the iterator on the packet index.
74 this.packetIndexIt
= streamInput
.getIndex().listIterator();
77 * Make first packet the current one.
82 // ------------------------------------------------------------------------
83 // Getters/Setters/Predicates
84 // ------------------------------------------------------------------------
86 public EventDefinition
getCurrentEvent() {
87 return this.currentEvent
;
90 public StructDefinition
getCurrentPacketContext() {
91 return this.packetReader
.getStreamPacketContextDef();
94 public ByteOrder
getByteOrder() {
95 return streamInput
.getStream().getTrace().getByteOrder();
98 public int getName() {
102 public void setName(int name
) {
106 public int getCPU() {
107 return this.packetReader
.getCPU();
110 public String
getFilename() {
111 return streamInput
.getFilename();
115 * for internal use only
117 StreamInput
getStreamInput() {
121 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
125 * Reads the next event in the current event variable.
127 * @return If an event has been successfully read.
129 public boolean readNextEvent() {
131 * Change packet if needed
133 if (!this.packetReader
.hasMoreEvents()) {
138 * If an event is available, read it.
140 if (this.packetReader
.hasMoreEvents()) {
142 this.setCurrentEvent(this.packetReader
.readNextEvent());
143 } catch (CTFReaderException e
) {
144 /* Some problem happened, we'll assume there is no more events */
149 this.setCurrentEvent(null);
154 * Change the current packet of the packet reader to the next one.
156 private void goToNextPacket() {
157 if (getPacketIndexIt().hasNext()) {
158 StreamInputPacketIndexEntry nextPacket
= getPacketIndexIt().next();
159 this.packetReader
.setCurrentPacket(nextPacket
);
161 this.packetReader
.setCurrentPacket(null);
166 * Changes the location of the trace file reader so that the current event
167 * is the first event with a timestamp greater than the given timestamp.
170 * The timestamp to seek to.
172 public long seek(long timestamp
) {
175 * Search in the index for the packet to search in.
177 this.packetIndexIt
= this.streamInput
.getIndex().search(timestamp
);
180 * Switch to this packet.
185 * Advance until A. we reached the end of the trace file (which means
186 * the given timestamp is after the last event), or B. we found the
187 * first event with a timestamp greater than the given timestamp.
190 boolean done
= (this.getCurrentEvent() == null);
191 while (!done
&& (this.getCurrentEvent().getTimestamp() < timestamp
)) {
193 done
= (this.getCurrentEvent() == null);
199 public long seekIndex(long index
) throws CTFReaderException
{
201 * we need to check if a trace is empty too.
203 StreamInputPacketIndexEntry sipie
= null;
205 * Search in the index for the packet to search in.
207 this.packetIndexIt
= this.streamInput
.getIndex().searchIndex(index
);
209 * Switch to this packet.
213 sipie
= this.packetReader
.getCurrentPacket();
215 * Read the first packet
219 * get the current index
221 if (this.packetReader
.getCurrentPacket() == null) {
222 if( !((sipie
.getIndexBegin() == 0) && (sipie
.getIndexEnd() == Long
.MAX_VALUE
))) {
223 throw new CTFReaderException(
224 "Current packet null in index seek, did you index your trace yet?"); //$NON-NLS-1$
228 return this.packetReader
.getCurrentPacket().getIndexBegin();
232 public void goToLastEvent() throws CTFReaderException
{
234 * Search in the index for the packet to search in.
236 int len
= this.streamInput
.getIndex().getEntries().size();
238 long desired_timestamp
= -1;
241 StreamInputPacketIndexEntry entry
= this.streamInput
.getIndex()
242 .getEntries().get(len
- back
);
243 desired_timestamp
= entry
.getTimestampBegin() + 1;
244 seek(desired_timestamp
);
246 } while (!this.packetReader
.hasMoreEvents());
248 * Go until the end of that packet
252 while (this.packetReader
.hasMoreEvents()) {
253 this.packetReader
.readNextEvent();
256 seek(desired_timestamp
);
257 for (int i
= 0; i
< (packet_size
- 1); i
++) {
258 this.packetReader
.readNextEvent();
262 public void setCurrentEvent(EventDefinition currentEvent
) {
263 this.currentEvent
= currentEvent
;
267 * @return the packetIndexIt
269 private ListIterator
<StreamInputPacketIndexEntry
> getPacketIndexIt() {
270 return packetIndexIt
;
274 * @return the packetReader
276 public StreamInputPacketReader
getPacketReader() {
281 * @see java.lang.Object#hashCode()
284 public int hashCode() {
285 final int prime
= 31;
287 result
= (prime
* result
) + name
;
288 result
= (prime
* result
)
289 + ((streamInput
== null) ?
0 : streamInput
.hashCode());
294 * @see java.lang.Object#equals(java.lang.Object)
297 public boolean equals(Object obj
) {
304 if (!(obj
instanceof StreamInputReader
)) {
307 StreamInputReader other
= (StreamInputReader
) obj
;
308 if (name
!= other
.name
) {
311 if (streamInput
== null) {
312 if (other
.streamInput
!= null) {
315 } else if (!streamInput
.equals(other
.streamInput
)) {