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