Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 128342df136491680bd3b19e416520c1ea71088d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*******************************************************************************
 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Matthew Khouzam - Initial API and implementation
 * Contributors: Simon Marchi - Initial API and implementation
 *******************************************************************************/

package org.eclipse.tracecompass.ctf.core.trace;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.StandardOpenOption;
import java.util.UUID;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
import org.eclipse.tracecompass.internal.ctf.core.Activator;
import org.eclipse.tracecompass.internal.ctf.core.SafeMappedByteBuffer;
import org.eclipse.tracecompass.internal.ctf.core.trace.StreamInputPacketIndex;
import org.eclipse.tracecompass.internal.ctf.core.trace.StreamInputPacketIndexEntry;
import org.eclipse.tracecompass.internal.ctf.core.utils.Utils;

/**
 * <b><u>StreamInput</u></b>
 * <p>
 * Represents a trace file that belongs to a certain stream.
 */
@NonNullByDefault
public class CTFStreamInput implements IDefinitionScope {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

    private static final int MAP_SIZE = 4096;

    /**
     * The associated Stream
     */
    private final ICTFStream fStream;

    /**
     * Information on the file (used for debugging)
     */
    private final File fFile;

    /**
     * The file name
     */
    private final String fFileName;

    /**
     * The packet index of this input
     */
    private final StreamInputPacketIndex fIndex;

    private long fTimestampEnd;

    /**
     * Definition of trace packet header
     */
    private final StructDeclaration fTracePacketHeaderDecl;

    /**
     * Definition of trace stream packet context
     */
    private final StructDeclaration fStreamPacketContextDecl;

    /**
     * Total number of lost events in this stream
     */
    private long fLostSoFar = 0;

    private boolean fUUIDMismatchWarning = false;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    /**
     * Constructs a StreamInput.
     *
     * @param stream
     *            The stream to which this StreamInput belongs to.
     * @param file
     *            Information about the trace file (for debugging purposes).
     * @since 2.0
     */
    public CTFStreamInput(ICTFStream stream, File file) {
        fStream = stream;
        fFile = file;
        fFileName = fFile.getName();

        fIndex = new StreamInputPacketIndex();
        /*
         * Create the definitions we need to read the packet headers + contexts
         */
        StructDeclaration packetHeader = getStream().getTrace().getPacketHeader();
        if (packetHeader != null) {
            fTracePacketHeaderDecl = packetHeader;
        } else {
            fTracePacketHeaderDecl = new StructDeclaration(1);
        }
        StructDeclaration packetContextDecl = getStream().getPacketContextDecl();
        if (packetContextDecl != null) {
            fStreamPacketContextDecl = packetContextDecl;
        } else {
            fStreamPacketContextDecl = new StructDeclaration(1);
        }
    }

    // ------------------------------------------------------------------------
    // Getters/Setters/Predicates
    // ------------------------------------------------------------------------

    /**
     * Gets the stream the streamInput wrapper is wrapping
     *
     * @return the stream the streamInput wrapper is wrapping
     * @since 2.0
     */
    public ICTFStream getStream() {
        return fStream;
    }

    /**
     * The common streamInput Index
     *
     * @return the stream input Index
     */
    StreamInputPacketIndex getIndex() {
        return fIndex;
    }

    /**
     * Gets the filename of the streamInput file.
     *
     * @return the filename of the streaminput file.
     */
    public String getFilename() {
        return fFileName;
    }

    /**
     * Gets the last read timestamp of a stream. (this is not necessarily the
     * last time in the stream.)
     *
     * @return the last read timestamp
     */
    public long getTimestampEnd() {
        return fTimestampEnd;
    }

    /**
     * Sets the last read timestamp of a stream. (this is not necessarily the
     * last time in the stream.)
     *
     * @param timestampEnd
     *            the last read timestamp
     */
    public void setTimestampEnd(long timestampEnd) {
        fTimestampEnd = timestampEnd;
    }

    /**
     * Useless for streaminputs
     */
    @Override
    public LexicalScope getScopePath() {
        return ILexicalScope.STREAM;
    }

    // ------------------------------------------------------------------------
    // Operations
    // ------------------------------------------------------------------------

    @Override
    public @Nullable Definition lookupDefinition(@Nullable String lookupPath) {
        /* TODO: lookup in different dynamic scopes is not supported yet. */
        return null;
    }

    /**
     * Create the index for this trace file.
     */
    public void setupIndex() {

        /*
         * The BitBuffer to extract data from the StreamInput
         */
        BitBuffer bitBuffer = new BitBuffer();
        bitBuffer.setByteOrder(getStream().getTrace().getByteOrder());

    }

    /**
     * Adds the next packet header index entry to the index of a stream input.
     *
     * <strong>This method is slow and can corrupt data if not used
     * properly</strong>
     *
     * @return true if there are more packets to add
     * @throws CTFException
     *             If there was a problem reading the packed header
     */
    public boolean addPacketHeaderIndex() throws CTFException {
        long currentPosBits = 0L;
        if (!fIndex.isEmpty()) {
            ICTFPacketDescriptor pos = fIndex.lastElement();
            if (pos == null) {
                throw new IllegalStateException("Index contains null packet entries"); //$NON-NLS-1$
            }
            currentPosBits = pos.getOffsetBits() + pos.getPacketSizeBits();
        }
        if (currentPosBits < getStreamSizeBits()) {
            return fIndex.append(createPacketIndexEntry(currentPosBits));
        }
        return false;
    }

    private long getStreamSizeBits() {
        return fFile.length() * Byte.SIZE;
    }

    private ICTFPacketDescriptor createPacketIndexEntry(long dataOffsetbits)
            throws CTFException {

        try (FileChannel fc = FileChannel.open(fFile.toPath(), StandardOpenOption.READ)) {
            if (fc == null) {
                throw new IOException("Failed to create FileChannel"); //$NON-NLS-1$
            }
            BitBuffer bitBuffer = createBitBufferForPacketHeader(fc, dataOffsetbits);
            /*
             * Read the trace packet header if it exists.
             */
            parseTracePacketHeader(bitBuffer);

            /*
             * Read the stream packet context if it exists.
             */
            long size = fc.size();
            ICTFPacketDescriptor packetIndex = parsePacketContext(dataOffsetbits, size, bitBuffer);

            /* Basic validation */
            if (packetIndex.getContentSizeBits() > packetIndex.getPacketSizeBits()) {
                throw new CTFException("Content size (" + packetIndex.getContentSizeBits() + ") > packet size (" + packetIndex.getPacketSizeBits() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            }

            if (packetIndex.getPacketSizeBits() > ((size * Byte.SIZE - packetIndex.getOffsetBits()))) {
                throw new CTFException("Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
            }
            return packetIndex;
        } catch (IOException e) {
            throw new CTFException("Failed to create packet index entry", e); //$NON-NLS-1$
        } catch (CTFException e) {
            throw new CTFException("Cannot create packet entry at bit " + dataOffsetbits, e); //$NON-NLS-1$
        }
    }

    private BitBuffer createBitBufferForPacketHeader(FileChannel fc, long dataOffsetbits) throws CTFException, IOException {
        /*
         * create a packet bit buffer to read the packet header
         */
        int maximumSize = fStreamPacketContextDecl.getMaximumSize() + fTracePacketHeaderDecl.getMaximumSize();
        BitBuffer bitBuffer = new BitBuffer(createPacketBitBuffer(fc, dataOffsetbits/Byte.SIZE, maximumSize));
        bitBuffer.setByteOrder(getStream().getTrace().getByteOrder());
        return bitBuffer;
    }

    private static ByteBuffer getByteBufferAt(FileChannel fc, long position, long size) throws CTFException, IOException {
        return SafeMappedByteBuffer.map(fc, MapMode.READ_ONLY, position, size);
    }

    private static ByteBuffer createPacketBitBuffer(FileChannel fc,
            long packetOffsetBytes, long maxSize) throws CTFException, IOException {
        /*
         * If there is less data remaining than what we want to map, reduce the
         * map size.
         */
        long remain = fc.size() - packetOffsetBytes;
        /*
         * Initial size, it is the minimum of the the file size and the maximum
         * possible size of the
         */
        long mapSize = Math.min(remain, MAP_SIZE);
        if (maxSize < mapSize) {
            mapSize = maxSize;
        }

        /*
         * Map the packet.
         */
        try {
            return getByteBufferAt(fc, packetOffsetBytes, mapSize);
        } catch (IllegalArgumentException | IOException e) {
            throw new CTFException(e);
        }
    }

    private StructDefinition parseTracePacketHeader(
            BitBuffer bitBuffer) throws CTFException {

        StructDefinition tracePacketHeaderDef = fTracePacketHeaderDecl.createDefinition(fStream.getTrace(), ILexicalScope.TRACE_PACKET_HEADER, bitBuffer);

        /*
         * Check the CTF magic number
         */
        IntegerDefinition magicDef = (IntegerDefinition) tracePacketHeaderDef
                .lookupDefinition("magic"); //$NON-NLS-1$
        if (magicDef != null) {
            int magic = (int) magicDef.getValue();
            if (magic != Utils.CTF_MAGIC) {
                throw new CTFException(
                        "CTF magic mismatch " + Integer.toHexString(magic) + " vs " + Integer.toHexString(Utils.CTF_MAGIC)); //$NON-NLS-1$//$NON-NLS-2$
            }
        }

        /*
         * Check the trace UUID
         */
        AbstractArrayDefinition uuidDef =
                (AbstractArrayDefinition) tracePacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
        if (uuidDef != null) {
            UUID uuid = Utils.getUUIDfromDefinition(uuidDef);

            if (!getStream().getTrace().getUUID().equals(uuid) && !fUUIDMismatchWarning ) {
                fUUIDMismatchWarning = true;
                Activator.log(IStatus.WARNING, "Reading CTF trace: UUID mismatch for trace " + getStream().getTrace()); //$NON-NLS-1$
            }
        }

        /*
         * Check that the stream id did not change
         */
        IntegerDefinition streamIDDef = (IntegerDefinition) tracePacketHeaderDef
                .lookupDefinition("stream_id"); //$NON-NLS-1$
        if (streamIDDef != null) {
            long streamID = streamIDDef.getValue();

            if (streamID != getStream().getId()) {
                throw new CTFException("Stream ID changing within a StreamInput"); //$NON-NLS-1$
            }
        }
        return tracePacketHeaderDef;
    }

    private ICTFPacketDescriptor parsePacketContext(long dataOffsetBits, long fileSizeBytes,
            BitBuffer bitBuffer) throws CTFException {
        ICTFPacketDescriptor packetIndex;
        StructDefinition streamPacketContextDef = fStreamPacketContextDecl.createDefinition(this, ILexicalScope.STREAM_PACKET_CONTEXT, bitBuffer);
        packetIndex = new StreamInputPacketIndexEntry(dataOffsetBits, streamPacketContextDef, fileSizeBytes, fLostSoFar, bitBuffer.position());
        fLostSoFar = packetIndex.getLostEvents() + fLostSoFar;
        setTimestampEnd(packetIndex.getTimestampEnd());
        return packetIndex;
    }

    /**
     * Get the file
     *
     * @return the file
     * @since 1.0
     */
    public File getFile() {
        return fFile;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = (prime * result) + fFile.hashCode();
        return result;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof CTFStreamInput)) {
            return false;
        }
        CTFStreamInput other = (CTFStreamInput) obj;
        return (fFile.equals(other.fFile));
    }
}

Back to the top