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