ctf: support traces with no content but a packet size
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInput.java
CommitLineData
866e5b51 1/*******************************************************************************
11252342 2 * Copyright (c) 2011, 2013 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
486efb2e 13package org.eclipse.linuxtools.ctf.core.trace;
866e5b51
FC
14
15import java.io.File;
aa3b05ef
FC
16import java.io.IOException;
17import java.nio.MappedByteBuffer;
866e5b51 18import java.nio.channels.FileChannel;
aa3b05ef 19import java.nio.channels.FileChannel.MapMode;
866e5b51
FC
20import java.util.UUID;
21
486efb2e 22import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
866e5b51
FC
23import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
24import org.eclipse.linuxtools.ctf.core.event.types.Definition;
21fb02fa
MK
25import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
26import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
866e5b51
FC
27import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
28import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
21fb02fa 29import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
866e5b51 30import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
486efb2e
AM
31import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndex;
32import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
866e5b51
FC
33
34/**
35 * <b><u>StreamInput</u></b>
36 * <p>
37 * Represents a trace file that belongs to a certain stream.
cf9a28da 38 *
486efb2e 39 * @since 2.0
866e5b51
FC
40 */
41public class StreamInput implements IDefinitionScope {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 /**
48 * The associated Stream
49 */
50 private final Stream stream;
51
52 /**
53 * FileChannel to the trace file
54 */
55 private final FileChannel fileChannel;
56
57 /**
58 * Information on the file (used for debugging)
59 */
0594c61c 60 private final File file;
866e5b51
FC
61
62 /**
63 * The packet index of this input
64 */
bfe038ff 65 private final StreamInputPacketIndex index;
866e5b51
FC
66
67 private long timestampEnd;
68
bfe038ff
MK
69 /*
70 * Definition of trace packet header
71 */
0594c61c 72 private StructDefinition tracePacketHeaderDef = null;
bfe038ff
MK
73
74 /*
75 * Definition of trace stream packet context
76 */
0594c61c 77 private StructDefinition streamPacketContextDef = null;
bfe038ff 78
132a02b0
MK
79 /*
80 * Total number of lost events in this stream
81 */
0594c61c 82 private long lostSoFar = 0;
132a02b0 83
866e5b51
FC
84 // ------------------------------------------------------------------------
85 // Constructors
86 // ------------------------------------------------------------------------
87
88 /**
89 * Constructs a StreamInput.
90 *
91 * @param stream
92 * The stream to which this StreamInput belongs to.
93 * @param fileChannel
94 * The FileChannel to the trace file.
95 * @param file
96 * Information about the trace file (for debugging purposes).
97 */
98 public StreamInput(Stream stream, FileChannel fileChannel, File file) {
99 this.stream = stream;
100 this.fileChannel = fileChannel;
101 this.file = file;
1fbaecd1 102 this.index = stream.getTrace().getIndex(this);
866e5b51
FC
103 }
104
105 // ------------------------------------------------------------------------
106 // Getters/Setters/Predicates
107 // ------------------------------------------------------------------------
108
9ac2eb62
MK
109 /**
110 * Gets the stream the streamInput wrapper is wrapping
21fb02fa 111 *
9ac2eb62
MK
112 * @return the stream the streamInput wrapper is wrapping
113 */
866e5b51
FC
114 public Stream getStream() {
115 return stream;
116 }
117
9ac2eb62 118 /**
ecb12461 119 * The common streamInput Index
21fb02fa 120 *
9ac2eb62
MK
121 * @return the stream input Index
122 */
486efb2e 123 StreamInputPacketIndex getIndex() {
866e5b51
FC
124 return index;
125 }
126
9ac2eb62 127 /**
cf9a28da 128 * Gets the filechannel of the streamInput. This is a limited Java resource.
21fb02fa 129 *
9ac2eb62
MK
130 * @return the filechannel
131 */
866e5b51
FC
132 public FileChannel getFileChannel() {
133 return fileChannel;
134 }
135
9ac2eb62
MK
136 /**
137 * Gets the filename of the streamInput file.
21fb02fa 138 *
9ac2eb62
MK
139 * @return the filename of the streaminput file.
140 */
866e5b51
FC
141 public String getFilename() {
142 return file.getName();
143 }
144
9ac2eb62 145 /**
ecb12461 146 * Gets the last read timestamp of a stream. (this is not necessarily the
21fb02fa
MK
147 * last time in the stream.)
148 *
9ac2eb62
MK
149 * @return the last read timestamp
150 */
866e5b51
FC
151 public long getTimestampEnd() {
152 return timestampEnd;
153 }
154
9ac2eb62 155 /**
21fb02fa
MK
156 * Sets the last read timestamp of a stream. (this is not necessarily the
157 * last time in the stream.)
158 *
159 * @param timestampEnd
160 * the last read timestamp
9ac2eb62 161 */
866e5b51
FC
162 public void setTimestampEnd(long timestampEnd) {
163 this.timestampEnd = timestampEnd;
164 }
165
9ac2eb62 166 /**
ecb12461 167 * Useless for streaminputs
9ac2eb62 168 */
866e5b51
FC
169 @Override
170 public String getPath() {
171 return ""; //$NON-NLS-1$
172 }
173
174 // ------------------------------------------------------------------------
175 // Operations
176 // ------------------------------------------------------------------------
177
178 @Override
179 public Definition lookupDefinition(String lookupPath) {
180 /* TODO: lookup in different dynamic scopes is not supported yet. */
181 return null;
182 }
183
184 /**
185 * Create the index for this trace file.
bfe038ff
MK
186 */
187 public void setupIndex() {
188
bfe038ff
MK
189 /*
190 * The BitBuffer to extract data from the StreamInput
191 */
192 BitBuffer bitBuffer = new BitBuffer();
8b8e48ed 193 bitBuffer.setByteOrder(this.getStream().getTrace().getByteOrder());
bfe038ff
MK
194
195 /*
196 * Create the definitions we need to read the packet headers + contexts
197 */
198 if (getStream().getTrace().getPacketHeader() != null) {
199 tracePacketHeaderDef = getStream().getTrace().getPacketHeader()
200 .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
201 }
202
203 if (getStream().getPacketContextDecl() != null) {
204 streamPacketContextDef = getStream().getPacketContextDecl()
205 .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
206 }
207
208 }
209
9ac2eb62
MK
210 /**
211 * Adds the next packet header index entry to the index of a stream input.
be6df2d8
AM
212 *
213 * @warning slow, can corrupt data if not used properly
9ac2eb62
MK
214 * @return true if there are more packets to add
215 * @throws CTFReaderException
be6df2d8 216 * If there was a problem reading the packed header
9ac2eb62 217 */
bfe038ff
MK
218 public boolean addPacketHeaderIndex() throws CTFReaderException {
219 long currentPos = 0L;
220 if (!index.getEntries().isEmpty()) {
221 StreamInputPacketIndexEntry pos = index.getEntries().lastElement();
222 currentPos = computeNextOffset(pos);
223 }
224 long fileSize = getStreamSize();
225 if (currentPos < fileSize) {
226 BitBuffer bitBuffer = new BitBuffer();
8b8e48ed 227 bitBuffer.setByteOrder(this.getStream().getTrace().getByteOrder());
bfe038ff
MK
228 StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry(
229 currentPos);
230 createPacketIndexEntry(fileSize, currentPos, packetIndex,
231 tracePacketHeaderDef, streamPacketContextDef, bitBuffer);
232 index.addEntry(packetIndex);
233 return true;
234 }
235 return false;
236 }
237
bfe038ff
MK
238 private long getStreamSize() {
239 return file.length();
240 }
241
bfe038ff
MK
242 private long createPacketIndexEntry(long fileSizeBytes,
243 long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
244 StructDefinition tracePacketHeaderDef,
245 StructDefinition streamPacketContextDef, BitBuffer bitBuffer)
246 throws CTFReaderException {
0594c61c 247
cf9a28da
MK
248 /*
249 * Ignoring the return value, but this call is needed to initialize the
250 * input
251 */
0594c61c 252 createPacketBitBuffer(fileSizeBytes, packetOffsetBytes, packetIndex, bitBuffer);
bfe038ff 253
866e5b51 254 /*
bfe038ff 255 * Read the trace packet header if it exists.
866e5b51 256 */
bfe038ff
MK
257 if (tracePacketHeaderDef != null) {
258 parseTracePacketHeader(tracePacketHeaderDef, bitBuffer);
866e5b51
FC
259 }
260
261 /*
bfe038ff 262 * Read the stream packet context if it exists.
866e5b51 263 */
bfe038ff
MK
264 if (streamPacketContextDef != null) {
265 parsePacketContext(fileSizeBytes, streamPacketContextDef,
266 bitBuffer, packetIndex);
267 } else {
268 setPacketContextNull(fileSizeBytes, packetIndex);
269 }
270
271 /* Basic validation */
272 if (packetIndex.getContentSizeBits() > packetIndex.getPacketSizeBits()) {
273 throw new CTFReaderException("Content size > packet size"); //$NON-NLS-1$
274 }
866e5b51 275
bfe038ff
MK
276 if (packetIndex.getPacketSizeBits() > ((fileSizeBytes - packetIndex
277 .getOffsetBytes()) * 8)) {
cf9a28da 278 throw new CTFReaderException("Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
bfe038ff
MK
279 }
280
281 /*
282 * Offset in the file, in bits
283 */
284 packetIndex.setDataOffsetBits(bitBuffer.position());
285
286 /*
287 * Update the counting packet offset
288 */
21fb02fa 289 return computeNextOffset(packetIndex);
bfe038ff
MK
290 }
291
292 /**
293 * @param packetIndex
294 * @return
295 */
296 private static long computeNextOffset(
297 StreamInputPacketIndexEntry packetIndex) {
298 return packetIndex.getOffsetBytes()
299 + ((packetIndex.getPacketSizeBits() + 7) / 8);
300 }
301
aa3b05ef
FC
302 /**
303 * @param fileSizeBytes
304 * @param packetOffsetBytes
305 * @param packetIndex
306 * @param bitBuffer
307 * @return
308 * @throws CTFReaderException
309 */
310 private MappedByteBuffer createPacketBitBuffer(long fileSizeBytes,
311 long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
312 BitBuffer bitBuffer) throws CTFReaderException {
313 /*
314 * Initial size, it should map at least the packet header + context
315 * size.
316 *
317 * TODO: use a less arbitrary size.
318 */
319 long mapSize = 4096;
320 /*
321 * If there is less data remaining than what we want to map, reduce the
322 * map size.
323 */
324 if ((fileSizeBytes - packetIndex.getOffsetBytes()) < mapSize) {
325 mapSize = fileSizeBytes - packetIndex.getOffsetBytes();
326 }
327
328 /*
329 * Map the packet.
330 */
331 MappedByteBuffer bb;
332
333 try {
334 bb = fileChannel.map(MapMode.READ_ONLY, packetOffsetBytes, mapSize);
335 } catch (IOException e) {
336 throw new CTFReaderException(e);
337 }
338 bitBuffer.setByteBuffer(bb);
339 return bb;
340 }
bfe038ff 341
bfe038ff
MK
342 private void parseTracePacketHeader(StructDefinition tracePacketHeaderDef,
343 BitBuffer bitBuffer) throws CTFReaderException {
344 tracePacketHeaderDef.read(bitBuffer);
866e5b51
FC
345
346 /*
bfe038ff 347 * Check the CTF magic number
866e5b51 348 */
bfe038ff
MK
349 IntegerDefinition magicDef = (IntegerDefinition) tracePacketHeaderDef
350 .lookupDefinition("magic"); //$NON-NLS-1$
351 if (magicDef != null) {
352 int magic = (int) magicDef.getValue();
353 if (magic != Utils.CTF_MAGIC) {
354 throw new CTFReaderException(
355 "CTF magic mismatch " + Integer.toHexString(magic) + " vs " + Integer.toHexString(Utils.CTF_MAGIC)); //$NON-NLS-1$//$NON-NLS-2$
356 }
866e5b51
FC
357 }
358
359 /*
bfe038ff 360 * Check the trace UUID
866e5b51 361 */
0594c61c
AM
362 ArrayDefinition uuidDef =
363 (ArrayDefinition) tracePacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
bfe038ff
MK
364 if (uuidDef != null) {
365 byte[] uuidArray = new byte[16];
366
0594c61c
AM
367 for (int i = 0; i < uuidArray.length; i++) {
368 IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getElem(i);
bfe038ff 369 uuidArray[i] = (byte) uuidByteDef.getValue();
866e5b51 370 }
866e5b51 371
bfe038ff 372 UUID uuid = Utils.makeUUID(uuidArray);
866e5b51 373
bfe038ff
MK
374 if (!getStream().getTrace().getUUID().equals(uuid)) {
375 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
866e5b51 376 }
bfe038ff 377 }
866e5b51 378
bfe038ff
MK
379 /*
380 * Check that the stream id did not change
381 */
382 IntegerDefinition streamIDDef = (IntegerDefinition) tracePacketHeaderDef
383 .lookupDefinition("stream_id"); //$NON-NLS-1$
384 if (streamIDDef != null) {
385 long streamID = streamIDDef.getValue();
866e5b51 386
bfe038ff 387 if (streamID != getStream().getId()) {
cf9a28da 388 throw new CTFReaderException("Stream ID changing within a StreamInput"); //$NON-NLS-1$
866e5b51 389 }
bfe038ff
MK
390 }
391 }
866e5b51 392
bfe038ff
MK
393 private static void setPacketContextNull(long fileSizeBytes,
394 StreamInputPacketIndexEntry packetIndex) {
395 /*
396 * If there is no packet context, infer the content and packet size from
397 * the file size (assume that there is only one packet and no padding)
398 */
47ca6c05
SM
399 packetIndex.setContentSizeBits(fileSizeBytes * 8);
400 packetIndex.setPacketSizeBits(fileSizeBytes * 8);
bfe038ff 401 }
866e5b51 402
bfe038ff
MK
403 private void parsePacketContext(long fileSizeBytes,
404 StructDefinition streamPacketContextDef, BitBuffer bitBuffer,
db8e8f7d 405 StreamInputPacketIndexEntry packetIndex) throws CTFReaderException {
bfe038ff 406 streamPacketContextDef.read(bitBuffer);
866e5b51 407
21fb02fa
MK
408 for (String field : streamPacketContextDef.getDeclaration()
409 .getFieldsList()) {
410 Definition id = streamPacketContextDef.lookupDefinition(field);
411 if (id instanceof IntegerDefinition) {
412 packetIndex.addAttribute(field,
413 ((IntegerDefinition) id).getValue());
414 } else if (id instanceof FloatDefinition) {
415 packetIndex.addAttribute(field,
416 ((FloatDefinition) id).getValue());
417 } else if (id instanceof EnumDefinition) {
418 packetIndex.addAttribute(field,
419 ((EnumDefinition) id).getValue());
420 } else if (id instanceof StringDefinition) {
421 packetIndex.addAttribute(field,
422 ((StringDefinition) id).getValue());
423 }
21fb02fa
MK
424 }
425
426 Long contentSize = (Long) packetIndex.lookupAttribute("content_size"); //$NON-NLS-1$
427 Long packetSize = (Long) packetIndex.lookupAttribute("packet_size"); //$NON-NLS-1$
0594c61c
AM
428 Long tsBegin = (Long) packetIndex.lookupAttribute("timestamp_begin"); //$NON-NLS-1$
429 Long tsEnd = (Long) packetIndex.lookupAttribute("timestamp_end"); //$NON-NLS-1$
21fb02fa
MK
430 String device = (String) packetIndex.lookupAttribute("device"); //$NON-NLS-1$
431 // LTTng Specific
0594c61c 432 Long cpuId = (Long) packetIndex.lookupAttribute("cpu_id"); //$NON-NLS-1$
cf9a28da 433 Long lostEvents = (Long) packetIndex.lookupAttribute("events_discarded"); //$NON-NLS-1$
132a02b0
MK
434
435 /* Read the content size in bits */
21fb02fa
MK
436 if (contentSize != null) {
437 packetIndex.setContentSizeBits(contentSize.intValue());
cf9a28da
MK
438 } else if (packetSize != null) {
439 packetIndex.setContentSizeBits(packetSize.longValue());
bfe038ff
MK
440 } else {
441 packetIndex.setContentSizeBits((int) (fileSizeBytes * 8));
442 }
866e5b51 443
cf9a28da 444
132a02b0 445 /* Read the packet size in bits */
21fb02fa
MK
446 if (packetSize != null) {
447 packetIndex.setPacketSizeBits(packetSize.intValue());
51598b21
MK
448 } else if (packetIndex.getContentSizeBits() != 0) {
449 packetIndex.setPacketSizeBits(packetIndex.getContentSizeBits());
bfe038ff 450 } else {
51598b21 451 packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8));
bfe038ff
MK
452 }
453
51598b21 454
132a02b0 455 /* Read the begin timestamp */
0594c61c
AM
456 if (tsBegin != null) {
457 packetIndex.setTimestampBegin(tsBegin.longValue());
bfe038ff
MK
458 }
459
132a02b0 460 /* Read the end timestamp */
0594c61c 461 if (tsEnd != null) {
ecb12461 462 if (tsEnd == -1) {
0594c61c 463 tsEnd = Long.MAX_VALUE;
21fb02fa 464 }
0594c61c 465 packetIndex.setTimestampEnd(tsEnd.longValue());
bfe038ff 466 setTimestampEnd(packetIndex.getTimestampEnd());
866e5b51 467 }
21fb02fa
MK
468
469 if (device != null) {
470 packetIndex.setTarget(device);
471 }
472
0594c61c
AM
473 if (cpuId != null) {
474 packetIndex.setTarget("CPU" + cpuId.toString()); //$NON-NLS-1$
21fb02fa 475 }
132a02b0
MK
476
477 if (lostEvents != null) {
478 packetIndex.setLostEvents(lostEvents - lostSoFar);
479 this.lostSoFar = lostEvents;
480 }
866e5b51
FC
481 }
482
81c8e6f7
MK
483 @Override
484 public int hashCode() {
485 final int prime = 31;
486 int result = 1;
487 result = (prime * result) + ((file == null) ? 0 : file.hashCode());
488 return result;
489 }
490
81c8e6f7
MK
491 @Override
492 public boolean equals(Object obj) {
493 if (this == obj) {
494 return true;
495 }
496 if (obj == null) {
497 return false;
498 }
499 if (!(obj instanceof StreamInput)) {
500 return false;
501 }
502 StreamInput other = (StreamInput) obj;
503 if (file == null) {
504 if (other.file != null) {
505 return false;
506 }
507 } else if (!file.equals(other.file)) {
508 return false;
509 }
510 return true;
511 }
512
866e5b51 513}
This page took 0.059648 seconds and 5 git commands to generate.