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
.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
.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
.IDefinitionScope
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
30 * <b><u>StreamInput</u></b>
32 * Represents a trace file that belongs to a certain stream.
34 public class StreamInput
implements IDefinitionScope
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 * The associated Stream
43 private final Stream stream
;
46 * FileChannel to the trace file
48 private final FileChannel fileChannel
;
51 * Information on the file (used for debugging)
53 public final File file
;
56 * The packet index of this input
58 private final StreamInputPacketIndex index
= new StreamInputPacketIndex();
60 private long timestampEnd
;
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Constructs a StreamInput.
70 * The stream to which this StreamInput belongs to.
72 * The FileChannel to the trace file.
74 * Information about the trace file (for debugging purposes).
76 public StreamInput(Stream stream
, FileChannel fileChannel
, File file
) {
78 this.fileChannel
= fileChannel
;
82 // ------------------------------------------------------------------------
83 // Getters/Setters/Predicates
84 // ------------------------------------------------------------------------
86 public Stream
getStream() {
90 public StreamInputPacketIndex
getIndex() {
94 public FileChannel
getFileChannel() {
98 public String
getFilename() {
99 return file
.getName();
102 public long getTimestampEnd() {
106 public void setTimestampEnd(long timestampEnd
) {
107 this.timestampEnd
= timestampEnd
;
111 public String
getPath() {
112 return ""; //$NON-NLS-1$
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
120 public Definition
lookupDefinition(String lookupPath
) {
121 /* TODO: lookup in different dynamic scopes is not supported yet. */
126 * Create the index for this trace file.
128 * @throws CTFReaderException
130 public void createIndex() throws CTFReaderException
{
132 * The size of the file in bytes
134 long fileSizeBytes
= 0;
136 fileSizeBytes
= fileChannel
.size();
137 } catch (IOException e
) {
138 throw new CTFReaderException(e
);
142 * Offset of the current packet in bytes
144 long packetOffsetBytes
= 0;
147 * Initial size, it should map at least the packet header + context
150 * TODO: use a less arbitrary size.
155 * Definition of trace packet header
157 StructDefinition tracePacketHeaderDef
= null;
160 * Definition of trace stream packet context
162 StructDefinition streamPacketContextDef
= null;
165 * The BitBuffer to extract data from the StreamInput
167 BitBuffer bitBuffer
= new BitBuffer();
168 bitBuffer
.order(this.getStream().getTrace().getByteOrder());
171 * Create the definitions we need to read the packet headers + contexts
173 if (getStream().getTrace().getPacketHeader() != null) {
174 tracePacketHeaderDef
= getStream().getTrace().getPacketHeader()
175 .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
178 if (getStream().getPacketContextDecl() != null) {
179 streamPacketContextDef
= getStream().getPacketContextDecl()
180 .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
184 * Scan through the packets of the file.
186 while (packetOffsetBytes
< fileSizeBytes
) {
188 * If there is less data remaining than what we want to map, reduce
191 if ((fileSizeBytes
- packetOffsetBytes
) < mapSize
) {
192 mapSize
= fileSizeBytes
- packetOffsetBytes
;
200 bb
= fileChannel
.map(MapMode
.READ_ONLY
, packetOffsetBytes
,
202 } catch (IOException e
) {
203 throw new CTFReaderException(e
);
205 bitBuffer
.setByteBuffer(bb
);
208 * Create the index entry
210 StreamInputPacketIndexEntry packetIndex
= new StreamInputPacketIndexEntry(
214 * Read the trace packet header if it exists.
216 if (tracePacketHeaderDef
!= null) {
217 tracePacketHeaderDef
.read(bitBuffer
);
220 * Check the CTF magic number
222 IntegerDefinition magicDef
= (IntegerDefinition
) tracePacketHeaderDef
223 .lookupDefinition("magic"); //$NON-NLS-1$
224 if (magicDef
!= null) {
225 int magic
= (int) magicDef
.getValue();
226 if (magic
!= Utils
.CTF_MAGIC
) {
227 throw new CTFReaderException(
228 "CTF magic mismatch " + Integer
.toHexString(magic
) + " vs " + Integer
.toHexString(Utils
.CTF_MAGIC
)); //$NON-NLS-1$//$NON-NLS-2$
234 * Check the trace UUID
236 ArrayDefinition uuidDef
= (ArrayDefinition
) tracePacketHeaderDef
237 .lookupDefinition("uuid"); //$NON-NLS-1$
238 if (uuidDef
!= null) {
239 byte[] uuidArray
= new byte[16];
241 for (int i
= 0; i
< 16; i
++) {
242 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
244 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
247 UUID uuid
= Utils
.makeUUID(uuidArray
);
249 if (!getStream().getTrace().getUUID().equals(uuid
)) {
250 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
255 * Check that the stream id did not change
257 IntegerDefinition streamIDDef
= (IntegerDefinition
) tracePacketHeaderDef
258 .lookupDefinition("stream_id"); //$NON-NLS-1$
259 if (streamIDDef
!= null) {
260 long streamID
= streamIDDef
.getValue();
262 if (streamID
!= getStream().getId()) {
263 throw new CTFReaderException(
264 "Stream ID changing within a StreamInput"); //$NON-NLS-1$
270 * Read the stream packet context if it exists.
272 if (streamPacketContextDef
!= null) {
273 streamPacketContextDef
.read(bitBuffer
);
276 * Read the content size in bits
278 IntegerDefinition contentSizeDef
= (IntegerDefinition
) streamPacketContextDef
279 .lookupDefinition("content_size"); //$NON-NLS-1$
280 if (contentSizeDef
!= null) {
281 packetIndex
.setContentSizeBits((int) contentSizeDef
284 packetIndex
.setContentSizeBits((int) (fileSizeBytes
* 8));
288 * Read the packet size in bits
290 IntegerDefinition packetSizeDef
= (IntegerDefinition
) streamPacketContextDef
291 .lookupDefinition("packet_size"); //$NON-NLS-1$
292 if (packetSizeDef
!= null) {
293 packetIndex
.setPacketSizeBits((int) packetSizeDef
296 if (packetIndex
.getContentSizeBits() != 0) {
297 packetIndex
.setPacketSizeBits(packetIndex
298 .getContentSizeBits());
301 .setPacketSizeBits((int) (fileSizeBytes
* 8));
306 * Read the begin timestamp
308 IntegerDefinition timestampBeginDef
= (IntegerDefinition
) streamPacketContextDef
309 .lookupDefinition("timestamp_begin"); //$NON-NLS-1$
310 if (timestampBeginDef
!= null) {
311 packetIndex
.setTimestampBegin( timestampBeginDef
.getValue());
315 * Read the end timestamp
317 IntegerDefinition timestampEndDef
= (IntegerDefinition
) streamPacketContextDef
318 .lookupDefinition("timestamp_end"); //$NON-NLS-1$
319 if (timestampEndDef
!= null) {
320 packetIndex
.setTimestampEnd(timestampEndDef
322 setTimestampEnd(packetIndex
.getTimestampEnd());
326 * If there is no packet context, infer the content and packet
327 * size from the file size (assume that there is only one packet
330 packetIndex
.setContentSizeBits( (int) (fileSizeBytes
* 8));
331 packetIndex
.setPacketSizeBits( (int) (fileSizeBytes
* 8));
334 /* Basic validation */
335 if (packetIndex
.getContentSizeBits() > packetIndex
.getPacketSizeBits()) {
336 throw new CTFReaderException("Content size > packet size"); //$NON-NLS-1$
339 if (packetIndex
.getPacketSizeBits() > ((fileSizeBytes
- packetIndex
.getOffsetBytes()) * 8)) {
340 throw new CTFReaderException(
341 "Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
345 * Offset in the file, in bits
347 packetIndex
.setDataOffsetBits( bitBuffer
.position());
350 * Add the packet index entry to the index
352 index
.addEntry(packetIndex
);
355 * Update the counting packet offset
357 packetOffsetBytes
+= (packetIndex
.getPacketSizeBits() + 7) / 8;
360 index
.getEntries().get(0).setIndexBegin(0L);