1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
;
16 import java
.io
.IOException
;
17 import java
.nio
.MappedByteBuffer
;
18 import java
.nio
.channels
.FileChannel
;
19 import java
.nio
.channels
.FileChannel
.MapMode
;
20 import java
.util
.UUID
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.Utils
;
32 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.io
.BitBuffer
;
35 * <b><u>StreamInput</u></b>
37 * Represents a trace file that belongs to a certain stream.
39 public class StreamInput
implements IDefinitionScope
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * The associated Stream
48 private final Stream stream
;
51 * FileChannel to the trace file
53 private final FileChannel fileChannel
;
56 * Information on the file (used for debugging)
58 public final File file
;
61 * The packet index of this input
63 private final StreamInputPacketIndex index
;
65 private long timestampEnd
;
68 * Definition of trace packet header
70 StructDefinition tracePacketHeaderDef
= null;
73 * Definition of trace stream packet context
75 StructDefinition streamPacketContextDef
= null;
78 * Total number of lost events in this stream
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Constructs a StreamInput.
90 * The stream to which this StreamInput belongs to.
92 * The FileChannel to the trace file.
94 * Information about the trace file (for debugging purposes).
96 public StreamInput(Stream stream
, FileChannel fileChannel
, File file
) {
98 this.fileChannel
= fileChannel
;
100 this.index
= stream
.getTrace().getIndex(this);
103 // ------------------------------------------------------------------------
104 // Getters/Setters/Predicates
105 // ------------------------------------------------------------------------
108 * Gets the stream the streamInput wrapper is wrapping
110 * @return the stream the streamInput wrapper is wrapping
112 public Stream
getStream() {
117 * the common streamInput Index
119 * @return the stream input Index
121 public StreamInputPacketIndex
getIndex() {
126 * Gets the filechannel of the streamInput. This is a limited Java
129 * @return the filechannel
131 public FileChannel
getFileChannel() {
136 * Gets the filename of the streamInput file.
138 * @return the filename of the streaminput file.
140 public String
getFilename() {
141 return file
.getName();
145 * gets the last read timestamp of a stream. (this is not necessarily the
146 * last time in the stream.)
148 * @return the last read timestamp
150 public long getTimestampEnd() {
155 * Sets the last read timestamp of a stream. (this is not necessarily the
156 * last time in the stream.)
158 * @param timestampEnd
159 * the last read timestamp
161 public void setTimestampEnd(long timestampEnd
) {
162 this.timestampEnd
= timestampEnd
;
166 * useless for streaminputs
169 public String
getPath() {
170 return ""; //$NON-NLS-1$
173 // ------------------------------------------------------------------------
175 // ------------------------------------------------------------------------
178 public Definition
lookupDefinition(String lookupPath
) {
179 /* TODO: lookup in different dynamic scopes is not supported yet. */
184 * Create the index for this trace file.
186 public void setupIndex() {
189 * The BitBuffer to extract data from the StreamInput
191 BitBuffer bitBuffer
= new BitBuffer();
192 bitBuffer
.setByteOrder(this.getStream().getTrace().getByteOrder());
195 * Create the definitions we need to read the packet headers + contexts
197 if (getStream().getTrace().getPacketHeader() != null) {
198 tracePacketHeaderDef
= getStream().getTrace().getPacketHeader()
199 .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
202 if (getStream().getPacketContextDecl() != null) {
203 streamPacketContextDef
= getStream().getPacketContextDecl()
204 .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
210 * Adds the next packet header index entry to the index of a stream input.
212 * @warning slow, can corrupt data if not used properly
213 * @return true if there are more packets to add
214 * @throws CTFReaderException
215 * If there was a problem reading the packed header
217 public boolean addPacketHeaderIndex() throws CTFReaderException
{
218 long currentPos
= 0L;
219 if (!index
.getEntries().isEmpty()) {
220 StreamInputPacketIndexEntry pos
= index
.getEntries().lastElement();
221 currentPos
= computeNextOffset(pos
);
223 long fileSize
= getStreamSize();
224 if (currentPos
< fileSize
) {
225 BitBuffer bitBuffer
= new BitBuffer();
226 bitBuffer
.setByteOrder(this.getStream().getTrace().getByteOrder());
227 StreamInputPacketIndexEntry packetIndex
= new StreamInputPacketIndexEntry(
229 createPacketIndexEntry(fileSize
, currentPos
, packetIndex
,
230 tracePacketHeaderDef
, streamPacketContextDef
, bitBuffer
);
231 index
.addEntry(packetIndex
);
237 private long getStreamSize() {
238 return file
.length();
241 private long createPacketIndexEntry(long fileSizeBytes
,
242 long packetOffsetBytes
, StreamInputPacketIndexEntry packetIndex
,
243 StructDefinition tracePacketHeaderDef
,
244 StructDefinition streamPacketContextDef
, BitBuffer bitBuffer
)
245 throws CTFReaderException
{
246 @SuppressWarnings("unused")
247 MappedByteBuffer bb
= createPacketBitBuffer(fileSizeBytes
,
248 packetOffsetBytes
, packetIndex
, bitBuffer
);
251 * Read the trace packet header if it exists.
253 if (tracePacketHeaderDef
!= null) {
254 parseTracePacketHeader(tracePacketHeaderDef
, bitBuffer
);
258 * Read the stream packet context if it exists.
260 if (streamPacketContextDef
!= null) {
261 parsePacketContext(fileSizeBytes
, streamPacketContextDef
,
262 bitBuffer
, packetIndex
);
264 setPacketContextNull(fileSizeBytes
, packetIndex
);
267 /* Basic validation */
268 if (packetIndex
.getContentSizeBits() > packetIndex
.getPacketSizeBits()) {
269 throw new CTFReaderException("Content size > packet size"); //$NON-NLS-1$
272 if (packetIndex
.getPacketSizeBits() > ((fileSizeBytes
- packetIndex
273 .getOffsetBytes()) * 8)) {
274 throw new CTFReaderException(
275 "Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
279 * Offset in the file, in bits
281 packetIndex
.setDataOffsetBits(bitBuffer
.position());
284 * Update the counting packet offset
286 return computeNextOffset(packetIndex
);
293 private static long computeNextOffset(
294 StreamInputPacketIndexEntry packetIndex
) {
295 return packetIndex
.getOffsetBytes()
296 + ((packetIndex
.getPacketSizeBits() + 7) / 8);
300 * @param fileSizeBytes
301 * @param packetOffsetBytes
305 * @throws CTFReaderException
307 private MappedByteBuffer
createPacketBitBuffer(long fileSizeBytes
,
308 long packetOffsetBytes
, StreamInputPacketIndexEntry packetIndex
,
309 BitBuffer bitBuffer
) throws CTFReaderException
{
311 * Initial size, it should map at least the packet header + context
314 * TODO: use a less arbitrary size.
318 * If there is less data remaining than what we want to map, reduce the
321 if ((fileSizeBytes
- packetIndex
.getOffsetBytes()) < mapSize
) {
322 mapSize
= fileSizeBytes
- packetIndex
.getOffsetBytes();
331 bb
= fileChannel
.map(MapMode
.READ_ONLY
, packetOffsetBytes
, mapSize
);
332 } catch (IOException e
) {
333 throw new CTFReaderException(e
);
335 bitBuffer
.setByteBuffer(bb
);
339 private void parseTracePacketHeader(StructDefinition tracePacketHeaderDef
,
340 BitBuffer bitBuffer
) throws CTFReaderException
{
341 tracePacketHeaderDef
.read(bitBuffer
);
344 * Check the CTF magic number
346 IntegerDefinition magicDef
= (IntegerDefinition
) tracePacketHeaderDef
347 .lookupDefinition("magic"); //$NON-NLS-1$
348 if (magicDef
!= null) {
349 int magic
= (int) magicDef
.getValue();
350 if (magic
!= Utils
.CTF_MAGIC
) {
351 throw new CTFReaderException(
352 "CTF magic mismatch " + Integer
.toHexString(magic
) + " vs " + Integer
.toHexString(Utils
.CTF_MAGIC
)); //$NON-NLS-1$//$NON-NLS-2$
358 * Check the trace UUID
360 ArrayDefinition uuidDef
= (ArrayDefinition
) tracePacketHeaderDef
361 .lookupDefinition("uuid"); //$NON-NLS-1$
362 if (uuidDef
!= null) {
363 byte[] uuidArray
= new byte[16];
365 for (int i
= 0; i
< 16; i
++) {
366 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
368 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
371 UUID uuid
= Utils
.makeUUID(uuidArray
);
373 if (!getStream().getTrace().getUUID().equals(uuid
)) {
374 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
379 * Check that the stream id did not change
381 IntegerDefinition streamIDDef
= (IntegerDefinition
) tracePacketHeaderDef
382 .lookupDefinition("stream_id"); //$NON-NLS-1$
383 if (streamIDDef
!= null) {
384 long streamID
= streamIDDef
.getValue();
386 if (streamID
!= getStream().getId()) {
387 throw new CTFReaderException(
388 "Stream ID changing within a StreamInput"); //$NON-NLS-1$
393 private static void setPacketContextNull(long fileSizeBytes
,
394 StreamInputPacketIndexEntry packetIndex
) {
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)
399 packetIndex
.setContentSizeBits((int) (fileSizeBytes
* 8));
400 packetIndex
.setPacketSizeBits((int) (fileSizeBytes
* 8));
403 private void parsePacketContext(long fileSizeBytes
,
404 StructDefinition streamPacketContextDef
, BitBuffer bitBuffer
,
405 StreamInputPacketIndexEntry packetIndex
) {
406 streamPacketContextDef
.read(bitBuffer
);
408 for (String field
: streamPacketContextDef
.getDeclaration()
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());
426 Long contentSize
= (Long
) packetIndex
.lookupAttribute("content_size"); //$NON-NLS-1$
427 Long packetSize
= (Long
) packetIndex
.lookupAttribute("packet_size"); //$NON-NLS-1$
428 Long timestampBegin
= (Long
) packetIndex
.lookupAttribute("timestamp_begin"); //$NON-NLS-1$
429 Long timestampEnd
= (Long
) packetIndex
.lookupAttribute("timestamp_end"); //$NON-NLS-1$
430 String device
= (String
) packetIndex
.lookupAttribute("device"); //$NON-NLS-1$
432 Long CPU_ID
= (Long
) packetIndex
.lookupAttribute("cpu_id"); //$NON-NLS-1$
433 Long lostEvents
= (Long
) packetIndex
.lookupAttribute("events_discarded"); //$NON-NLS-1$
435 /* Read the content size in bits */
436 if (contentSize
!= null) {
437 packetIndex
.setContentSizeBits(contentSize
.intValue());
439 packetIndex
.setContentSizeBits((int) (fileSizeBytes
* 8));
442 /* Read the packet size in bits */
443 if (packetSize
!= null) {
444 packetIndex
.setPacketSizeBits(packetSize
.intValue());
446 if (packetIndex
.getContentSizeBits() != 0) {
447 packetIndex
.setPacketSizeBits(packetIndex
.getContentSizeBits());
449 packetIndex
.setPacketSizeBits((int) (fileSizeBytes
* 8));
453 /* Read the begin timestamp */
454 if (timestampBegin
!= null) {
455 packetIndex
.setTimestampBegin(timestampBegin
.longValue());
458 /* Read the end timestamp */
459 if (timestampEnd
!= null) {
460 if( timestampEnd
== -1 ) {
461 timestampEnd
= Long
.MAX_VALUE
;
463 packetIndex
.setTimestampEnd(timestampEnd
.longValue());
464 setTimestampEnd(packetIndex
.getTimestampEnd());
467 if (device
!= null) {
468 packetIndex
.setTarget(device
);
471 if (CPU_ID
!= null) {
472 packetIndex
.setTarget("CPU" + CPU_ID
.toString()); //$NON-NLS-1$
475 if (lostEvents
!= null) {
476 packetIndex
.setLostEvents(lostEvents
- lostSoFar
);
477 this.lostSoFar
= lostEvents
;
484 * @see java.lang.Object#hashCode()
487 public int hashCode() {
488 final int prime
= 31;
490 result
= (prime
* result
) + ((file
== null) ?
0 : file
.hashCode());
497 * @see java.lang.Object#equals(java.lang.Object)
500 public boolean equals(Object obj
) {
507 if (!(obj
instanceof StreamInput
)) {
510 StreamInput other
= (StreamInput
) obj
;
512 if (other
.file
!= null) {
515 } else if (!file
.equals(other
.file
)) {