ctf: Fix Javadoc for all public methods
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputReader.java
CommitLineData
866e5b51
FC
1/*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.ctf.core.trace;
14
ce2388e0 15import java.nio.ByteOrder;
866e5b51
FC
16
17import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
18import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
ce2388e0
FC
19import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
20import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
866e5b51
FC
21
22/**
23 * <b><u>StreamInputReader</u></b>
24 * <p>
25 * Reads the events of a trace file.
26 */
27public class StreamInputReader {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 /**
34 * The StreamInput we are reading.
35 */
36 private final StreamInput streamInput;
37
38 /**
39 * The packet reader used to read packets from this trace file.
40 */
41 private final StreamInputPacketReader packetReader;
42
43 /**
44 * Iterator on the packet index
45 */
bfe038ff 46 private int packetIndex;
866e5b51
FC
47
48 /**
49 * Reference to the current event of this trace file (iow, the last on that
50 * was read, the next one to be returned)
51 */
52 private EventDefinition currentEvent = null;
53
54 private int name;
55
bfe038ff
MK
56 private CTFTraceReader parent;
57
58 private final long prevIndex;
59
866e5b51
FC
60 // ------------------------------------------------------------------------
61 // Constructors
62 // ------------------------------------------------------------------------
63
64 /**
65 * Constructs a StreamInputReader that reads a StreamInput.
66 *
67 * @param streamInput
68 * The StreamInput to read.
69 */
70 public StreamInputReader(StreamInput streamInput) {
71 this.streamInput = streamInput;
72 this.packetReader = new StreamInputPacketReader(this);
866e5b51
FC
73 /*
74 * Get the iterator on the packet index.
75 */
bfe038ff 76 this.packetIndex = 0;
866e5b51 77
bfe038ff 78 this.prevIndex = 0;
866e5b51
FC
79 /*
80 * Make first packet the current one.
81 */
82 goToNextPacket();
83 }
84
85 // ------------------------------------------------------------------------
86 // Getters/Setters/Predicates
87 // ------------------------------------------------------------------------
88
9ac2eb62
MK
89 /**
90 * Gets the current event in this stream
91 *
92 * @return the current event in the stream, null if the stream is
93 * finished/empty/malformed
94 */
866e5b51
FC
95 public EventDefinition getCurrentEvent() {
96 return this.currentEvent;
97 }
98
9ac2eb62
MK
99 /**
100 * gets the current packet context
101 *
102 * @return the current packet context (size, lost events and such)
103 */
866e5b51
FC
104 public StructDefinition getCurrentPacketContext() {
105 return this.packetReader.getStreamPacketContextDef();
106 }
107
9ac2eb62
MK
108 /**
109 * Gets the byte order for a trace
110 *
111 * @return the trace byte order
112 */
ce2388e0
FC
113 public ByteOrder getByteOrder() {
114 return streamInput.getStream().getTrace().getByteOrder();
866e5b51
FC
115 }
116
9ac2eb62
MK
117 /**
118 * Gets the name of the stream (it's an id and a number)
119 *
120 * @return gets the stream name (it's a number)
121 */
866e5b51
FC
122 public int getName() {
123 return this.name;
124 }
125
9ac2eb62
MK
126 /**
127 * Sets the name of the stream
128 *
129 * @param name
130 * the name of the stream, (it's a number)
131 */
866e5b51
FC
132 public void setName(int name) {
133 this.name = name;
134 }
135
9ac2eb62
MK
136 /**
137 * Gets the CPU of a stream. It's the same as the one in /proc or running
138 * the asm CPUID instruction
139 *
140 * @return The CPU id (a number)
141 */
866e5b51
FC
142 public int getCPU() {
143 return this.packetReader.getCPU();
144 }
145
9ac2eb62
MK
146 /**
147 * Gets the filename of the stream being read
148 * @return The filename of the stream being read
149 */
ce2388e0
FC
150 public String getFilename() {
151 return streamInput.getFilename();
152 }
153
154 /*
155 * for internal use only
156 */
157 StreamInput getStreamInput() {
158 return streamInput;
159 }
160
866e5b51
FC
161 // ------------------------------------------------------------------------
162 // Operations
163 // ------------------------------------------------------------------------
164 /**
165 * Reads the next event in the current event variable.
166 *
167 * @return If an event has been successfully read.
168 */
169 public boolean readNextEvent() {
bfe038ff 170
866e5b51
FC
171 /*
172 * Change packet if needed
173 */
174 if (!this.packetReader.hasMoreEvents()) {
bfe038ff
MK
175 final StreamInputPacketIndexEntry prevPacket = this.packetReader
176 .getCurrentPacket();
177 if (prevPacket != null) {
178 goToNextPacket();
179 final StreamInputPacketIndexEntry currentPacket = this.packetReader
180 .getCurrentPacket();
181 }
866e5b51
FC
182 }
183
9ac2eb62 184 /*autogenerate javadoc getter setter
866e5b51
FC
185 * If an event is available, read it.
186 */
187 if (this.packetReader.hasMoreEvents()) {
188 try {
189 this.setCurrentEvent(this.packetReader.readNextEvent());
190 } catch (CTFReaderException e) {
bfe038ff
MK
191 /*
192 * Some problem happened, we'll assume that there are no more
193 * events
194 */
866e5b51
FC
195 return false;
196 }
197 return true;
198 }
199 this.setCurrentEvent(null);
200 return false;
201 }
202
203 /**
204 * Change the current packet of the packet reader to the next one.
205 */
206 private void goToNextPacket() {
bfe038ff
MK
207 packetIndex++;
208 if (getPacketSize() >= (packetIndex + 1)) {
209 this.packetReader.setCurrentPacket(getPacket());
866e5b51 210 } else {
bfe038ff
MK
211 try {
212 if (this.streamInput.addPacketHeaderIndex()) {
213 packetIndex = getPacketSize() - 1;
214 this.packetReader.setCurrentPacket(getPacket());
215
216 } else {
217 this.packetReader.setCurrentPacket(null);
218 }
219
220 } catch (CTFReaderException e) {
221 this.packetReader.setCurrentPacket(null);
222 }
866e5b51
FC
223 }
224 }
225
bfe038ff
MK
226 /**
227 * @return
228 */
229 private int getPacketSize() {
230 return streamInput.getIndex().getEntries().size();
231 }
232
866e5b51
FC
233 /**
234 * Changes the location of the trace file reader so that the current event
235 * is the first event with a timestamp greater than the given timestamp.
236 *
237 * @param timestamp
238 * The timestamp to seek to.
be6df2d8 239 * @return The offset compared to the current position
866e5b51 240 */
ce2388e0
FC
241 public long seek(long timestamp) {
242 long offset = 0;
866e5b51 243
eb94f9c9 244 gotoPacket(timestamp);
866e5b51 245
0c59c1a6
MK
246 /*
247 * index up to the desired timestamp.
248 */
249 while ((this.packetReader.getCurrentPacket() != null)
250 && (this.packetReader.getCurrentPacket().getTimestampEnd() < timestamp)) {
251 try {
252 this.streamInput.addPacketHeaderIndex();
253 goToNextPacket();
254 } catch (CTFReaderException e) {
255 // do nothing here
256 }
257 }
9ac2eb62 258 if (this.packetReader.getCurrentPacket() == null) {
eb94f9c9
MK
259 gotoPacket(timestamp);
260 }
0c59c1a6 261
866e5b51
FC
262 /*
263 * Advance until A. we reached the end of the trace file (which means
264 * the given timestamp is after the last event), or B. we found the
265 * first event with a timestamp greater than the given timestamp.
266 */
267 readNextEvent();
268 boolean done = (this.getCurrentEvent() == null);
aa572e22 269 while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) {
866e5b51
FC
270 readNextEvent();
271 done = (this.getCurrentEvent() == null);
ce2388e0 272 offset++;
866e5b51 273 }
ce2388e0
FC
274 return offset;
275 }
276
eb94f9c9
MK
277 /**
278 * @param timestamp
279 */
280 private void gotoPacket(long timestamp) {
281 this.packetIndex = this.streamInput.getIndex().search(timestamp)
282 .previousIndex();
283 /*
284 * Switch to this packet.
285 */
286 goToNextPacket();
287 }
288
9ac2eb62
MK
289 /**
290 * Seeks the last event of a stream and returns it.
291 */
bfe038ff 292 public void goToLastEvent() {
866e5b51
FC
293 /*
294 * Search in the index for the packet to search in.
295 */
ec6f5beb
MK
296 final int len = this.streamInput.getIndex().getEntries().size();
297
298 StreamInputPacketIndexEntry entry = null;
866e5b51 299 /*
ec6f5beb 300 * Go to beginning of trace.
866e5b51 301 */
ec6f5beb
MK
302 seek(0);
303 /*
304 * if the trace is empty.
305 */
bfe038ff 306 if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) {
ec6f5beb
MK
307 /*
308 * This means the trace is empty. abort.
309 */
310 return;
ce2388e0 311 }
ec6f5beb
MK
312 /*
313 * Go to the last packet that contains events.
314 */
bfe038ff
MK
315 for (int pos = len - 1; pos > 0; pos--) {
316 packetIndex = pos;
317 this.packetReader.setCurrentPacket(getPacket());
318 if (this.packetReader.hasMoreEvents()) {
ec6f5beb
MK
319 break;
320 }
866e5b51 321 }
ec6f5beb
MK
322
323 /*
324 * Go until the end of that packet
325 */
326 EventDefinition prevEvent = null;
327 while (this.currentEvent != null) {
328 prevEvent = this.currentEvent;
329 this.readNextEvent();
330 }
331 /*
332 * Go back to the previous event
333 */
334 this.setCurrentEvent(prevEvent);
866e5b51
FC
335 }
336
bfe038ff
MK
337 /**
338 * @return the parent
339 */
340 public CTFTraceReader getParent() {
341 return parent;
342 }
343
344 /**
345 * @param parent
346 * the parent to set
347 */
348 public void setParent(CTFTraceReader parent) {
349 this.parent = parent;
350 }
351
9ac2eb62
MK
352 /**
353 * Sets the current event in a stream input reader
354 * @param currentEvent the event to set
355 */
866e5b51
FC
356 public void setCurrentEvent(EventDefinition currentEvent) {
357 this.currentEvent = currentEvent;
358 }
359
ce2388e0
FC
360 /**
361 * @return the packetIndexIt
362 */
bfe038ff
MK
363 private int getPacketIndex() {
364 return packetIndex;
365 }
366
367 private StreamInputPacketIndexEntry getPacket() {
368 return streamInput.getIndex().getEntries().get(getPacketIndex());
ce2388e0
FC
369 }
370
371 /**
372 * @return the packetReader
373 */
374 public StreamInputPacketReader getPacketReader() {
375 return packetReader;
376 }
377
bfe038ff
MK
378 /*
379 * (non-Javadoc)
380 *
81c8e6f7
MK
381 * @see java.lang.Object#hashCode()
382 */
383 @Override
384 public int hashCode() {
385 final int prime = 31;
386 int result = 1;
387 result = (prime * result) + name;
388 result = (prime * result)
389 + ((streamInput == null) ? 0 : streamInput.hashCode());
390 return result;
391 }
392
bfe038ff
MK
393 /*
394 * (non-Javadoc)
395 *
81c8e6f7
MK
396 * @see java.lang.Object#equals(java.lang.Object)
397 */
398 @Override
399 public boolean equals(Object obj) {
400 if (this == obj) {
401 return true;
402 }
403 if (obj == null) {
404 return false;
405 }
406 if (!(obj instanceof StreamInputReader)) {
407 return false;
408 }
409 StreamInputReader other = (StreamInputReader) obj;
410 if (name != other.name) {
411 return false;
412 }
413 if (streamInput == null) {
414 if (other.streamInput != null) {
415 return false;
416 }
417 } else if (!streamInput.equals(other.streamInput)) {
418 return false;
419 }
420 return true;
421 }
422
866e5b51 423}
This page took 0.04786 seconds and 5 git commands to generate.