1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
.ctf
.core
.trace
;
16 import java
.io
.IOException
;
17 import java
.nio
.ByteBuffer
;
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
.io
.BitBuffer
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
31 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndex
;
32 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
35 * <b><u>StreamInput</u></b>
37 * Represents a trace file that belongs to a certain stream.
41 public class StreamInput
implements IDefinitionScope
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 * The associated Stream
50 private final Stream fStream
;
53 * FileChannel to the trace file
55 private final FileChannel fFileChannel
;
58 * Information on the file (used for debugging)
60 private final File fFile
;
63 * The packet index of this input
65 private final StreamInputPacketIndex fIndex
;
67 private long fTimestampEnd
;
70 * Definition of trace packet header
72 private StructDefinition fTracePacketHeaderDef
= null;
75 * Definition of trace stream packet context
77 private StructDefinition fStreamPacketContextDef
= null;
80 * Total number of lost events in this stream
82 private long fLostSoFar
= 0;
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 * Constructs a StreamInput.
92 * The stream to which this StreamInput belongs to.
94 * The FileChannel to the trace file.
96 * Information about the trace file (for debugging purposes).
98 public StreamInput(Stream stream
, FileChannel fileChannel
, File file
) {
100 fFileChannel
= fileChannel
;
102 fIndex
= new StreamInputPacketIndex();
105 // ------------------------------------------------------------------------
106 // Getters/Setters/Predicates
107 // ------------------------------------------------------------------------
110 * Gets the stream the streamInput wrapper is wrapping
112 * @return the stream the streamInput wrapper is wrapping
114 public Stream
getStream() {
119 * The common streamInput Index
121 * @return the stream input Index
123 StreamInputPacketIndex
getIndex() {
128 * Gets the filename of the streamInput file.
130 * @return the filename of the streaminput file.
132 public String
getFilename() {
133 return fFile
.getName();
137 * Gets the last read timestamp of a stream. (this is not necessarily the
138 * last time in the stream.)
140 * @return the last read timestamp
142 public long getTimestampEnd() {
143 return fTimestampEnd
;
147 * Sets the last read timestamp of a stream. (this is not necessarily the
148 * last time in the stream.)
150 * @param timestampEnd
151 * the last read timestamp
153 public void setTimestampEnd(long timestampEnd
) {
154 fTimestampEnd
= timestampEnd
;
158 * Useless for streaminputs
161 public String
getPath() {
162 return ""; //$NON-NLS-1$
165 // ------------------------------------------------------------------------
167 // ------------------------------------------------------------------------
170 public Definition
lookupDefinition(String lookupPath
) {
171 /* TODO: lookup in different dynamic scopes is not supported yet. */
176 * Create the index for this trace file.
178 public void setupIndex() {
181 * The BitBuffer to extract data from the StreamInput
183 BitBuffer bitBuffer
= new BitBuffer();
184 bitBuffer
.setByteOrder(getStream().getTrace().getByteOrder());
187 * Create the definitions we need to read the packet headers + contexts
189 if (getStream().getTrace().getPacketHeader() != null) {
190 fTracePacketHeaderDef
= getStream().getTrace().getPacketHeader()
191 .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
194 if (getStream().getPacketContextDecl() != null) {
195 fStreamPacketContextDef
= getStream().getPacketContextDecl()
196 .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
202 * Adds the next packet header index entry to the index of a stream input.
204 * @warning slow, can corrupt data if not used properly
205 * @return true if there are more packets to add
206 * @throws CTFReaderException
207 * If there was a problem reading the packed header
209 public boolean addPacketHeaderIndex() throws CTFReaderException
{
210 long currentPos
= 0L;
211 if (!fIndex
.getEntries().isEmpty()) {
212 StreamInputPacketIndexEntry pos
= fIndex
.getEntries().lastElement();
213 currentPos
= computeNextOffset(pos
);
215 long fileSize
= getStreamSize();
216 if (currentPos
< fileSize
) {
217 BitBuffer bitBuffer
= new BitBuffer();
218 bitBuffer
.setByteOrder(getStream().getTrace().getByteOrder());
219 StreamInputPacketIndexEntry packetIndex
= new StreamInputPacketIndexEntry(
221 createPacketIndexEntry(fileSize
, currentPos
, packetIndex
,
222 fTracePacketHeaderDef
, fStreamPacketContextDef
, bitBuffer
);
223 fIndex
.addEntry(packetIndex
);
229 private long getStreamSize() {
230 return fFile
.length();
233 private long createPacketIndexEntry(long fileSizeBytes
,
234 long packetOffsetBytes
, StreamInputPacketIndexEntry packetIndex
,
235 StructDefinition tracePacketHeaderDef
,
236 StructDefinition streamPacketContextDef
, BitBuffer bitBuffer
)
237 throws CTFReaderException
{
240 * Ignoring the return value, but this call is needed to initialize the
243 createPacketBitBuffer(fileSizeBytes
, packetOffsetBytes
, packetIndex
, bitBuffer
);
246 * Read the trace packet header if it exists.
248 if (tracePacketHeaderDef
!= null) {
249 parseTracePacketHeader(tracePacketHeaderDef
, bitBuffer
);
253 * Read the stream packet context if it exists.
255 if (streamPacketContextDef
!= null) {
256 parsePacketContext(fileSizeBytes
, streamPacketContextDef
,
257 bitBuffer
, packetIndex
);
259 setPacketContextNull(fileSizeBytes
, packetIndex
);
262 /* Basic validation */
263 if (packetIndex
.getContentSizeBits() > packetIndex
.getPacketSizeBits()) {
264 throw new CTFReaderException("Content size > packet size"); //$NON-NLS-1$
267 if (packetIndex
.getPacketSizeBits() > ((fileSizeBytes
- packetIndex
268 .getOffsetBytes()) * 8)) {
269 throw new CTFReaderException("Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
273 * Offset in the file, in bits
275 packetIndex
.setDataOffsetBits(bitBuffer
.position());
278 * Update the counting packet offset
280 return computeNextOffset(packetIndex
);
287 private static long computeNextOffset(
288 StreamInputPacketIndexEntry packetIndex
) {
289 return packetIndex
.getOffsetBytes()
290 + ((packetIndex
.getPacketSizeBits() + 7) / 8);
293 ByteBuffer
getByteBufferAt(long position
, long size
) throws IOException
{
294 return fFileChannel
.map(MapMode
.READ_ONLY
, position
, size
);
298 * @param fileSizeBytes
299 * @param packetOffsetBytes
303 * @throws CTFReaderException
305 private ByteBuffer
createPacketBitBuffer(long fileSizeBytes
,
306 long packetOffsetBytes
, StreamInputPacketIndexEntry packetIndex
,
307 BitBuffer bitBuffer
) throws CTFReaderException
{
309 * Initial size, it should map at least the packet header + context
312 * TODO: use a less arbitrary size.
316 * If there is less data remaining than what we want to map, reduce the
319 if ((fileSizeBytes
- packetIndex
.getOffsetBytes()) < mapSize
) {
320 mapSize
= fileSizeBytes
- packetIndex
.getOffsetBytes();
329 bb
= getByteBufferAt(packetOffsetBytes
, mapSize
);
330 } catch (IOException e
) {
331 throw new CTFReaderException(e
);
333 bitBuffer
.setByteBuffer(bb
);
337 private void parseTracePacketHeader(StructDefinition tracePacketHeaderDef
,
338 BitBuffer bitBuffer
) throws CTFReaderException
{
339 tracePacketHeaderDef
.read(bitBuffer
);
342 * Check the CTF magic number
344 IntegerDefinition magicDef
= (IntegerDefinition
) tracePacketHeaderDef
345 .lookupDefinition("magic"); //$NON-NLS-1$
346 if (magicDef
!= null) {
347 int magic
= (int) magicDef
.getValue();
348 if (magic
!= Utils
.CTF_MAGIC
) {
349 throw new CTFReaderException(
350 "CTF magic mismatch " + Integer
.toHexString(magic
) + " vs " + Integer
.toHexString(Utils
.CTF_MAGIC
)); //$NON-NLS-1$//$NON-NLS-2$
355 * Check the trace UUID
357 ArrayDefinition uuidDef
=
358 (ArrayDefinition
) tracePacketHeaderDef
.lookupDefinition("uuid"); //$NON-NLS-1$
359 if (uuidDef
!= null) {
360 byte[] uuidArray
= new byte[16];
362 for (int i
= 0; i
< uuidArray
.length
; i
++) {
363 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
.getElem(i
);
364 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
367 UUID uuid
= Utils
.makeUUID(uuidArray
);
369 if (!getStream().getTrace().getUUID().equals(uuid
)) {
370 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
375 * Check that the stream id did not change
377 IntegerDefinition streamIDDef
= (IntegerDefinition
) tracePacketHeaderDef
378 .lookupDefinition("stream_id"); //$NON-NLS-1$
379 if (streamIDDef
!= null) {
380 long streamID
= streamIDDef
.getValue();
382 if (streamID
!= getStream().getId()) {
383 throw new CTFReaderException("Stream ID changing within a StreamInput"); //$NON-NLS-1$
388 private static void setPacketContextNull(long fileSizeBytes
,
389 StreamInputPacketIndexEntry packetIndex
) {
391 * If there is no packet context, infer the content and packet size from
392 * the file size (assume that there is only one packet and no padding)
394 packetIndex
.setContentSizeBits(fileSizeBytes
* 8);
395 packetIndex
.setPacketSizeBits(fileSizeBytes
* 8);
398 private void parsePacketContext(long fileSizeBytes
,
399 StructDefinition streamPacketContextDef
, BitBuffer bitBuffer
,
400 StreamInputPacketIndexEntry packetIndex
) throws CTFReaderException
{
401 streamPacketContextDef
.read(bitBuffer
);
403 for (String field
: streamPacketContextDef
.getDeclaration()
405 Definition id
= streamPacketContextDef
.lookupDefinition(field
);
406 if (id
instanceof IntegerDefinition
) {
407 packetIndex
.addAttribute(field
,
408 ((IntegerDefinition
) id
).getValue());
409 } else if (id
instanceof FloatDefinition
) {
410 packetIndex
.addAttribute(field
,
411 ((FloatDefinition
) id
).getValue());
412 } else if (id
instanceof EnumDefinition
) {
413 packetIndex
.addAttribute(field
,
414 ((EnumDefinition
) id
).getValue());
415 } else if (id
instanceof StringDefinition
) {
416 packetIndex
.addAttribute(field
,
417 ((StringDefinition
) id
).getValue());
421 Long contentSize
= (Long
) packetIndex
.lookupAttribute("content_size"); //$NON-NLS-1$
422 Long packetSize
= (Long
) packetIndex
.lookupAttribute("packet_size"); //$NON-NLS-1$
423 Long tsBegin
= (Long
) packetIndex
.lookupAttribute("timestamp_begin"); //$NON-NLS-1$
424 Long tsEnd
= (Long
) packetIndex
.lookupAttribute("timestamp_end"); //$NON-NLS-1$
425 String device
= (String
) packetIndex
.lookupAttribute("device"); //$NON-NLS-1$
427 Long cpuId
= (Long
) packetIndex
.lookupAttribute("cpu_id"); //$NON-NLS-1$
428 Long lostEvents
= (Long
) packetIndex
.lookupAttribute("events_discarded"); //$NON-NLS-1$
430 /* Read the content size in bits */
431 if (contentSize
!= null) {
432 packetIndex
.setContentSizeBits(contentSize
.intValue());
433 } else if (packetSize
!= null) {
434 packetIndex
.setContentSizeBits(packetSize
.longValue());
436 packetIndex
.setContentSizeBits((int) (fileSizeBytes
* 8));
440 /* Read the packet size in bits */
441 if (packetSize
!= null) {
442 packetIndex
.setPacketSizeBits(packetSize
.intValue());
443 } else if (packetIndex
.getContentSizeBits() != 0) {
444 packetIndex
.setPacketSizeBits(packetIndex
.getContentSizeBits());
446 packetIndex
.setPacketSizeBits((int) (fileSizeBytes
* 8));
450 /* Read the begin timestamp */
451 if (tsBegin
!= null) {
452 packetIndex
.setTimestampBegin(tsBegin
.longValue());
455 /* Read the end timestamp */
458 tsEnd
= Long
.MAX_VALUE
;
460 packetIndex
.setTimestampEnd(tsEnd
.longValue());
461 setTimestampEnd(packetIndex
.getTimestampEnd());
464 if (device
!= null) {
465 packetIndex
.setTarget(device
);
469 packetIndex
.setTarget("CPU" + cpuId
.toString()); //$NON-NLS-1$
472 if (lostEvents
!= null) {
473 packetIndex
.setLostEvents(lostEvents
- fLostSoFar
);
474 fLostSoFar
= lostEvents
;
479 public int hashCode() {
480 final int prime
= 31;
482 result
= (prime
* result
) + ((fFile
== null) ?
0 : fFile
.hashCode());
487 public boolean equals(Object obj
) {
494 if (!(obj
instanceof StreamInput
)) {
497 StreamInput other
= (StreamInput
) obj
;
499 if (other
.fFile
!= null) {
502 } else if (!fFile
.equals(other
.fFile
)) {