ctf: support event-header-less traces
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFStreamInputReader.java
CommitLineData
866e5b51 1/*******************************************************************************
1ae2ec14 2 * Copyright (c) 2011, 2015 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
f357bcd4 13package org.eclipse.tracecompass.ctf.core.trace;
866e5b51 14
b3151232
MK
15import java.io.File;
16import java.io.IOException;
42f8feff 17import java.nio.ByteBuffer;
b3151232 18import java.nio.channels.FileChannel;
42f8feff 19import java.nio.channels.FileChannel.MapMode;
b3151232 20import java.nio.file.StandardOpenOption;
42f8feff 21import java.util.List;
866e5b51 22
90cefe9f
MK
23import org.eclipse.jdt.annotation.NonNullByDefault;
24import org.eclipse.jdt.annotation.Nullable;
25import org.eclipse.tracecompass.common.core.NonNullUtils;
680f9173 26import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
27import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
28import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
42f8feff
MK
29import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
30import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
f357bcd4
AM
31import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
32import org.eclipse.tracecompass.internal.ctf.core.Activator;
42f8feff
MK
33import org.eclipse.tracecompass.internal.ctf.core.SafeMappedByteBuffer;
34import org.eclipse.tracecompass.internal.ctf.core.trace.CTFPacketReader;
35import org.eclipse.tracecompass.internal.ctf.core.trace.NullPacketReader;
53359017 36
866e5b51 37/**
d37aaa7f 38 * A CTF trace event reader. Reads the events of a trace file.
32ede2ec 39 *
d37aaa7f
FC
40 * @author Matthew Khouzam
41 * @author Simon Marchi
bbf3873a 42 * @since 2.0
866e5b51 43 */
90cefe9f 44@NonNullByDefault
d84419e1 45public class CTFStreamInputReader implements AutoCloseable {
866e5b51 46
42f8feff
MK
47 private static final int BITS_PER_BYTE = Byte.SIZE;
48
866e5b51
FC
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52
53 /**
54 * The StreamInput we are reading.
55 */
90cefe9f 56 private final File fFile;
b3151232 57
90cefe9f 58 private final CTFStreamInput fStreamInput;
866e5b51 59
90cefe9f 60 private final @Nullable FileChannel fFileChannel;
b3151232 61
866e5b51
FC
62 /**
63 * The packet reader used to read packets from this trace file.
64 */
42f8feff 65 private IPacketReader fPacketReader;
866e5b51
FC
66
67 /**
68 * Iterator on the packet index
69 */
93a45b54 70 private int fPacketIndex;
866e5b51
FC
71
72 /**
73 * Reference to the current event of this trace file (iow, the last on that
74 * was read, the next one to be returned)
75 */
90cefe9f 76 private @Nullable EventDefinition fCurrentEvent = null;
866e5b51 77
93a45b54 78 private int fId;
866e5b51 79
6a5251eb
MK
80 /**
81 * Live trace reading
82 */
83 private boolean fLive = false;
84
866e5b51
FC
85 // ------------------------------------------------------------------------
86 // Constructors
87 // ------------------------------------------------------------------------
866e5b51
FC
88 /**
89 * Constructs a StreamInputReader that reads a StreamInput.
90 *
91 * @param streamInput
92 * The StreamInput to read.
680f9173 93 * @throws CTFException
b3151232 94 * If the file cannot be opened
866e5b51 95 */
680f9173 96 public CTFStreamInputReader(CTFStreamInput streamInput) throws CTFException {
93a45b54 97 fStreamInput = streamInput;
b3151232
MK
98 fFile = fStreamInput.getFile();
99 try {
100 fFileChannel = FileChannel.open(fFile.toPath(), StandardOpenOption.READ);
101 } catch (IOException e) {
680f9173 102 throw new CTFIOException(e);
b3151232 103 }
1ae2ec14 104 try {
1ae2ec14
MAL
105 /*
106 * Get the iterator on the packet index.
107 */
108 fPacketIndex = 0;
109 /*
110 * Make first packet the current one.
111 */
42f8feff
MK
112 // did we already index the packet?
113 if (getPacketSize() < (fPacketIndex + 1)) {
114 // go to the next packet if there is one, index it at the same
115 // time
116 if (fStreamInput.addPacketHeaderIndex()) {
117 fPacketIndex = getPacketSize() - 1;
118 }
119 }
120 ICTFPacketDescriptor packet = getPacket();
121 fPacketReader = getCurrentPacketReader(packet);
1ae2ec14
MAL
122 } catch (Exception e) {
123 try {
124 close();
125 } catch (IOException e1) {
126 // Ignore
127 }
128 throw e;
129 }
866e5b51
FC
130 }
131
42f8feff
MK
132 private IPacketReader getCurrentPacketReader(@Nullable ICTFPacketDescriptor packet) throws CTFException {
133 IPacketReader ctfPacketReader = NullPacketReader.INSTANCE;
134 if (packet != null) {
135 long size = packet.getContentSizeBits();
136 if (size < 0) {
137 throw new CTFIOException("Cannot have negative sized buffers."); //$NON-NLS-1$
138 }
139 BitBuffer bitBuffer = new BitBuffer(getByteBufferAt(packet.getOffsetBits(), size));
140 bitBuffer.position(packet.getPayloadStartBits());
141 IDeclaration eventHeaderDeclaration = getStreamInput().getStream().getEventHeaderDeclaration();
142 CTFTrace trace = getStreamInput().getStream().getTrace();
32ea78ed 143 ctfPacketReader = new CTFPacketReader(bitBuffer, packet, getEventDeclarations(), eventHeaderDeclaration, getStreamEventContextDecl(), trace.getPacketHeaderDef(), trace);
42f8feff
MK
144 }
145 return ctfPacketReader;
146 }
147
148 /**
149 * Get a bytebuffer map of the file
150 *
151 * @param position
152 * start offset in bits
153 * @param size
154 * size of the map in bits, use caution
155 * @return a byte buffer
156 * @throws CTFException
157 * if the map failed in its allocation
158 *
159 * @since 2.0
160 */
161 public ByteBuffer getByteBufferAt(long position, long size) throws CTFException {
162 ByteBuffer map;
163 try {
164 map = SafeMappedByteBuffer.map(fFileChannel, MapMode.READ_ONLY, position / BITS_PER_BYTE, (size + BITS_PER_BYTE - 1) / BITS_PER_BYTE);
165 } catch (IOException e) {
166 throw new CTFIOException(e.getMessage(), e);
167 }
168 if (map == null) {
169 throw new CTFIOException("Failed to allocate mapped byte buffer"); //$NON-NLS-1$
170 }
171 return map;
172 }
173
5d1c6919 174 /**
b3151232
MK
175 * Dispose the StreamInputReader, closes the file channel and its packet
176 * reader
177 *
178 * @throws IOException
179 * If an I/O error occurs
5d1c6919 180 */
dd9752d5 181 @Override
b3151232 182 public void close() throws IOException {
1ae2ec14
MAL
183 if (fFileChannel != null) {
184 fFileChannel.close();
185 }
42f8feff 186 fPacketReader = NullPacketReader.INSTANCE;
5d1c6919
PT
187 }
188
866e5b51
FC
189 // ------------------------------------------------------------------------
190 // Getters/Setters/Predicates
191 // ------------------------------------------------------------------------
192
9ac2eb62
MK
193 /**
194 * Gets the current event in this stream
195 *
196 * @return the current event in the stream, null if the stream is
197 * finished/empty/malformed
198 */
90cefe9f 199 public @Nullable EventDefinition getCurrentEvent() {
93a45b54 200 return fCurrentEvent;
866e5b51
FC
201 }
202
9ac2eb62
MK
203 /**
204 * Gets the name of the stream (it's an id and a number)
205 *
206 * @return gets the stream name (it's a number)
207 */
866e5b51 208 public int getName() {
93a45b54 209 return fId;
866e5b51
FC
210 }
211
9ac2eb62
MK
212 /**
213 * Sets the name of the stream
214 *
215 * @param name
216 * the name of the stream, (it's a number)
217 */
866e5b51 218 public void setName(int name) {
93a45b54 219 fId = name;
866e5b51
FC
220 }
221
9ac2eb62
MK
222 /**
223 * Gets the CPU of a stream. It's the same as the one in /proc or running
224 * the asm CPUID instruction
225 *
226 * @return The CPU id (a number)
227 */
866e5b51 228 public int getCPU() {
93a45b54 229 return fPacketReader.getCPU();
866e5b51
FC
230 }
231
9ac2eb62
MK
232 /**
233 * Gets the filename of the stream being read
db8e8f7d 234 *
9ac2eb62
MK
235 * @return The filename of the stream being read
236 */
ce2388e0 237 public String getFilename() {
93a45b54 238 return fStreamInput.getFilename();
ce2388e0
FC
239 }
240
241 /*
242 * for internal use only
243 */
d84419e1 244 CTFStreamInput getStreamInput() {
93a45b54 245 return fStreamInput;
ce2388e0
FC
246 }
247
53359017
MK
248 /**
249 * Gets the event definition set for this StreamInput
250 *
251 * @return Unmodifiable set with the event definitions
42f8feff 252 * @since 2.0
53359017 253 */
32ea78ed 254 public List<@Nullable IEventDeclaration> getEventDeclarations() {
42f8feff 255 return fStreamInput.getStream().getEventDeclarations();
53359017
MK
256 }
257
6a5251eb
MK
258 /**
259 * Set the trace to live mode
260 *
261 * @param live
262 * whether the trace is read live or not
6a5251eb
MK
263 */
264 public void setLive(boolean live) {
265 fLive = live;
266 }
267
268 /**
269 * Get if the trace is to read live or not
270 *
271 * @return whether the trace is live or not
6a5251eb
MK
272 */
273 public boolean isLive() {
274 return fLive;
275 }
276
a4fa4e36
MK
277 /**
278 * Get the event context of the stream
279 *
280 * @return the event context declaration of the stream
a4fa4e36 281 */
90cefe9f 282 public @Nullable StructDeclaration getStreamEventContextDecl() {
a4fa4e36
MK
283 return getStreamInput().getStream().getEventContextDecl();
284 }
285
866e5b51
FC
286 // ------------------------------------------------------------------------
287 // Operations
288 // ------------------------------------------------------------------------
289 /**
290 * Reads the next event in the current event variable.
291 *
292 * @return If an event has been successfully read.
680f9173 293 * @throws CTFException
db8e8f7d 294 * if an error occurs
866e5b51 295 */
680f9173 296 public CTFResponse readNextEvent() throws CTFException {
bfe038ff 297
866e5b51
FC
298 /*
299 * Change packet if needed
300 */
93a45b54 301 if (!fPacketReader.hasMoreEvents()) {
90cefe9f 302 final ICTFPacketDescriptor prevPacket = fPacketReader.getCurrentPacket();
a4fa4e36 303 if (prevPacket != null || fLive) {
bfe038ff 304 goToNextPacket();
bfe038ff 305 }
6a5251eb 306
866e5b51
FC
307 }
308
32ede2ec 309 /*
866e5b51
FC
310 * If an event is available, read it.
311 */
93a45b54 312 if (fPacketReader.hasMoreEvents()) {
6a5251eb
MK
313 setCurrentEvent(fPacketReader.readNextEvent());
314 return CTFResponse.OK;
866e5b51
FC
315 }
316 this.setCurrentEvent(null);
6a5251eb 317 return fLive ? CTFResponse.WAIT : CTFResponse.FINISH;
866e5b51
FC
318 }
319
320 /**
321 * Change the current packet of the packet reader to the next one.
db8e8f7d 322 *
680f9173 323 * @throws CTFException
db8e8f7d 324 * if an error occurs
866e5b51 325 */
680f9173 326 private void goToNextPacket() throws CTFException {
93a45b54 327 fPacketIndex++;
6a5251eb 328 // did we already index the packet?
42f8feff 329 while (getPacketSize() < (fPacketIndex + 1)) {
6a5251eb 330 // go to the next packet if there is one, index it at the same time
93a45b54
MK
331 if (fStreamInput.addPacketHeaderIndex()) {
332 fPacketIndex = getPacketSize() - 1;
cf9a28da 333 } else {
42f8feff
MK
334 fPacketReader = NullPacketReader.INSTANCE;
335 return;
bfe038ff 336 }
42f8feff 337
866e5b51 338 }
42f8feff
MK
339 ICTFPacketDescriptor packet = getPacket();
340 fPacketReader = getCurrentPacketReader(packet);
341
866e5b51
FC
342 }
343
bfe038ff
MK
344 /**
345 * @return
346 */
347 private int getPacketSize() {
3f02ac64 348 return fStreamInput.getIndex().size();
bfe038ff
MK
349 }
350
866e5b51
FC
351 /**
352 * Changes the location of the trace file reader so that the current event
ecb12461 353 * is the first event with a timestamp greater or equal the given timestamp.
866e5b51
FC
354 *
355 * @param timestamp
356 * The timestamp to seek to.
be6df2d8 357 * @return The offset compared to the current position
680f9173 358 * @throws CTFException
db8e8f7d 359 * if an error occurs
866e5b51 360 */
680f9173 361 public long seek(long timestamp) throws CTFException {
ce2388e0 362 long offset = 0;
866e5b51 363
eb94f9c9 364 gotoPacket(timestamp);
866e5b51 365
0c59c1a6
MK
366 /*
367 * index up to the desired timestamp.
368 */
93a45b54
MK
369 while ((fPacketReader.getCurrentPacket() != null)
370 && (fPacketReader.getCurrentPacket().getTimestampEnd() < timestamp)) {
0c59c1a6 371 try {
93a45b54 372 fStreamInput.addPacketHeaderIndex();
0c59c1a6 373 goToNextPacket();
680f9173 374 } catch (CTFException e) {
0c59c1a6 375 // do nothing here
b3151232 376 Activator.log(e.getMessage());
0c59c1a6
MK
377 }
378 }
93a45b54 379 if (fPacketReader.getCurrentPacket() == null) {
eb94f9c9
MK
380 gotoPacket(timestamp);
381 }
0c59c1a6 382
866e5b51 383 /*
6a5251eb
MK
384 * Advance until either of these conditions are met:
385 *
386 * - reached the end of the trace file (the given timestamp is after the
387 * last event)
388 *
389 * - found the first event with a timestamp greater or equal the given
390 * timestamp.
866e5b51
FC
391 */
392 readNextEvent();
90cefe9f
MK
393 EventDefinition currentEvent = getCurrentEvent();
394 while (currentEvent != null && (currentEvent.getTimestamp() < timestamp)) {
866e5b51 395 readNextEvent();
90cefe9f 396 currentEvent = getCurrentEvent();
ce2388e0 397 offset++;
866e5b51 398 }
ce2388e0
FC
399 return offset;
400 }
401
eb94f9c9
MK
402 /**
403 * @param timestamp
6a5251eb 404 * the time to seek
680f9173 405 * @throws CTFException
db8e8f7d 406 * if an error occurs
eb94f9c9 407 */
680f9173 408 private void gotoPacket(long timestamp) throws CTFException {
6af89f01 409 fPacketIndex = fStreamInput.getIndex().search(timestamp) - 1;
eb94f9c9
MK
410 /*
411 * Switch to this packet.
412 */
413 goToNextPacket();
414 }
415
9ac2eb62
MK
416 /**
417 * Seeks the last event of a stream and returns it.
db8e8f7d 418 *
680f9173 419 * @throws CTFException
db8e8f7d 420 * if an error occurs
9ac2eb62 421 */
680f9173 422 public void goToLastEvent() throws CTFException {
ec6f5beb 423
866e5b51 424 /*
4a32dd11 425 * Go to the beginning of the trace
866e5b51 426 */
ec6f5beb 427 seek(0);
4a32dd11 428
ec6f5beb 429 /*
4a32dd11 430 * Check that there is at least one event
ec6f5beb 431 */
3f02ac64 432 if ((fStreamInput.getIndex().isEmpty()) || (!fPacketReader.hasMoreEvents())) {
ec6f5beb
MK
433 /*
434 * This means the trace is empty. abort.
435 */
436 return;
ce2388e0 437 }
4a32dd11
MK
438
439 fPacketIndex = fStreamInput.getIndex().size() - 1;
440 /*
441 * Go to last indexed packet
442 */
42f8feff 443 fPacketReader = getCurrentPacketReader(getPacket());
4a32dd11
MK
444
445 /*
446 * Keep going until you cannot
447 */
448 while (fPacketReader.getCurrentPacket() != null) {
449 goToNextPacket();
450 }
451
452 final int lastPacketIndex = fStreamInput.getIndex().size() - 1;
ec6f5beb
MK
453 /*
454 * Go to the last packet that contains events.
455 */
4a32dd11 456 for (int pos = lastPacketIndex; pos > 0; pos--) {
93a45b54 457 fPacketIndex = pos;
42f8feff 458 fPacketReader = getCurrentPacketReader(getPacket());
4a32dd11 459
93a45b54 460 if (fPacketReader.hasMoreEvents()) {
ec6f5beb
MK
461 break;
462 }
866e5b51 463 }
ec6f5beb
MK
464
465 /*
466 * Go until the end of that packet
467 */
468 EventDefinition prevEvent = null;
93a45b54
MK
469 while (fCurrentEvent != null) {
470 prevEvent = fCurrentEvent;
ec6f5beb
MK
471 this.readNextEvent();
472 }
473 /*
474 * Go back to the previous event
475 */
476 this.setCurrentEvent(prevEvent);
866e5b51
FC
477 }
478
9ac2eb62
MK
479 /**
480 * Sets the current event in a stream input reader
db8e8f7d
AM
481 *
482 * @param currentEvent
483 * the event to set
9ac2eb62 484 */
90cefe9f 485 public void setCurrentEvent(@Nullable EventDefinition currentEvent) {
93a45b54 486 fCurrentEvent = currentEvent;
866e5b51
FC
487 }
488
ce2388e0
FC
489 /**
490 * @return the packetIndexIt
491 */
bfe038ff 492 private int getPacketIndex() {
93a45b54 493 return fPacketIndex;
bfe038ff
MK
494 }
495
90cefe9f
MK
496 private @Nullable ICTFPacketDescriptor getPacket() {
497 if (getPacketIndex() >= fStreamInput.getIndex().size()) {
498 return null;
499 }
3f02ac64 500 return fStreamInput.getIndex().getElement(getPacketIndex());
ce2388e0
FC
501 }
502
b3151232 503 /**
42f8feff 504 * Get the current packet reader
b3151232 505 *
ce2388e0 506 * @return the packetReader
42f8feff 507 * @since 2.0
ce2388e0 508 */
42f8feff 509 public IPacketReader getCurrentPacketReader() {
93a45b54 510 return fPacketReader;
ce2388e0
FC
511 }
512
81c8e6f7
MK
513 @Override
514 public int hashCode() {
515 final int prime = 31;
516 int result = 1;
93a45b54 517 result = (prime * result) + fId;
81c8e6f7 518 result = (prime * result)
b3151232 519 + fFile.hashCode();
81c8e6f7
MK
520 return result;
521 }
522
81c8e6f7 523 @Override
90cefe9f 524 public boolean equals(@Nullable Object obj) {
81c8e6f7
MK
525 if (this == obj) {
526 return true;
527 }
528 if (obj == null) {
529 return false;
530 }
d84419e1 531 if (!(obj instanceof CTFStreamInputReader)) {
81c8e6f7
MK
532 return false;
533 }
d84419e1 534 CTFStreamInputReader other = (CTFStreamInputReader) obj;
93a45b54 535 if (fId != other.fId) {
81c8e6f7
MK
536 return false;
537 }
b3151232 538 return fFile.equals(other.fFile);
81c8e6f7
MK
539 }
540
87b60a47
MK
541 @Override
542 public String toString() {
543 // this helps debugging
90cefe9f 544 return fId + ' ' + NonNullUtils.nullToEmptyString(fCurrentEvent);
87b60a47 545 }
b3151232 546
866e5b51 547}
This page took 0.098927 seconds and 5 git commands to generate.