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