ChunkHeader.java 11.8 KB
Newer Older
A
apetushkov 已提交
1
/*
2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
A
apetushkov 已提交
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
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.jfr.internal.consumer;

import java.io.IOException;

import jdk.jfr.internal.LogLevel;
import jdk.jfr.internal.LogTag;
import jdk.jfr.internal.Logger;
import jdk.jfr.internal.MetadataDescriptor;
34
import jdk.jfr.internal.Utils;
A
apetushkov 已提交
35 36

public final class ChunkHeader {
37 38 39 40 41
    private static final long HEADER_SIZE = 68;
    private static final byte UPDATING_CHUNK_HEADER = (byte) 255;
    private static final long CHUNK_SIZE_POSITION = 8;
    private static final long DURATION_NANOS_POSITION = 40;
    private static final long FILE_STATE_POSITION = 64;
42
    private static final long FLAG_BYTE_POSITION = 67;
A
apetushkov 已提交
43 44
    private static final long METADATA_TYPE_ID = 0;
    private static final byte[] FILE_MAGIC = { 'F', 'L', 'R', '\0' };
45
    private static final int MASK_FINAL_CHUNK = 1 << 1;
A
apetushkov 已提交
46 47 48 49 50 51 52 53 54

    private final short major;
    private final short minor;
    private final long chunkStartTicks;
    private final long ticksPerSecond;
    private final long chunkStartNanos;
    private final long absoluteChunkStart;
    private final RecordingInput input;
    private final long id;
55 56 57 58 59 60 61 62
    private long absoluteEventStart;
    private long chunkSize = 0;
    private long constantPoolPosition = 0;
    private long metadataPosition = 0;
    private long durationNanos;
    private long absoluteChunkEnd;
    private boolean isFinished;
    private boolean finished;
63
    private boolean finalChunk;
A
apetushkov 已提交
64 65 66 67 68 69

    public ChunkHeader(RecordingInput input) throws IOException {
        this(input, 0, 0);
    }

    private ChunkHeader(RecordingInput input, long absoluteChunkStart, long id) throws IOException {
70 71 72 73 74 75
        this.absoluteChunkStart = absoluteChunkStart;
        this.absoluteEventStart = absoluteChunkStart + HEADER_SIZE;
        if (input.getFileSize() < HEADER_SIZE) {
            throw new IOException("Not a complete Chunk header");
        }
        input.setValidSize(absoluteChunkStart + HEADER_SIZE);
A
apetushkov 已提交
76 77
        input.position(absoluteChunkStart);
        if (input.position() >= input.size()) {
78
           throw new IOException("Chunk contains no data");
A
apetushkov 已提交
79 80 81 82
        }
        verifyMagic(input);
        this.input = input;
        this.id = id;
83 84
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: " + id);
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: file=" + input.getFilename());
A
apetushkov 已提交
85 86 87 88 89 90 91 92
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startPosition=" + absoluteChunkStart);
        major = input.readRawShort();
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: major=" + major);
        minor = input.readRawShort();
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: minor=" + minor);
        if (major != 1 && major != 2) {
            throw new IOException("File version " + major + "." + minor + ". Only Flight Recorder files of version 1.x and 2.x can be read by this JDK.");
        }
93
        input.readRawLong(); // chunk size
A
apetushkov 已提交
94
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: chunkSize=" + chunkSize);
95
        input.readRawLong(); // constant pool position
A
apetushkov 已提交
96
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: constantPoolPosition=" + constantPoolPosition);
97
        input.readRawLong(); // metadata position
A
apetushkov 已提交
98 99 100 101 102 103 104 105 106
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: metadataPosition=" + metadataPosition);
        chunkStartNanos = input.readRawLong(); // nanos since epoch
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startNanos=" + chunkStartNanos);
        durationNanos = input.readRawLong(); // duration nanos, not used
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: durationNanos=" + durationNanos);
        chunkStartTicks = input.readRawLong();
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startTicks=" + chunkStartTicks);
        ticksPerSecond = input.readRawLong();
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: ticksPerSecond=" + ticksPerSecond);
107
        input.readRawInt(); // ignore file state and flag bits
108
        refresh();
A
apetushkov 已提交
109 110 111
        input.position(absoluteEventStart);
    }

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    void refresh() throws IOException {
        while (true) {
            byte fileState1;
            input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
            while ((fileState1 = input.readPhysicalByte()) == UPDATING_CHUNK_HEADER) {
                Utils.takeNap(1);
                input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
            }
            input.positionPhysical(absoluteChunkStart + CHUNK_SIZE_POSITION);
            long chunkSize = input.readPhysicalLong();
            long constantPoolPosition = input.readPhysicalLong();
            long metadataPosition = input.readPhysicalLong();
            input.positionPhysical(absoluteChunkStart + DURATION_NANOS_POSITION);
            long durationNanos = input.readPhysicalLong();
            input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
            byte fileState2 =  input.readPhysicalByte();
128 129
            input.positionPhysical(absoluteChunkStart + FLAG_BYTE_POSITION);
            int flagByte = input.readPhysicalByte();
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
            if (fileState1 == fileState2) { // valid header
                finished = fileState1 == 0;
                if (metadataPosition != 0) {
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Setting input size to " + (absoluteChunkStart + chunkSize));
                    if (finished) {
                        // This assumes that the whole recording
                        // is finished if the first chunk is.
                        // This is a limitation we may want to
                        // remove, but greatly improves performance as
                        // data can be read across chunk boundaries
                        // of multi-chunk files and only once.
                        input.setValidSize(input.getFileSize());
                    } else {
                        input.setValidSize(absoluteChunkStart + chunkSize);
                    }
                    this.chunkSize = chunkSize;
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: chunkSize=" + chunkSize);
                    this.constantPoolPosition = constantPoolPosition;
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: constantPoolPosition=" + constantPoolPosition);
                    this.metadataPosition = metadataPosition;
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: metadataPosition=" + metadataPosition);
                    this.durationNanos = durationNanos;
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: durationNanos =" + durationNanos);
                    isFinished = fileState2 == 0;
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: generation=" + fileState2);
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: finished=" + isFinished);
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: fileSize=" + input.size());
157 158
                    this.finalChunk = (flagByte & MASK_FINAL_CHUNK) != 0;
                    Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: finalChunk=" + finalChunk);
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
                    absoluteChunkEnd = absoluteChunkStart + chunkSize;
                    return;
                }
            }
        }
    }

    public void awaitFinished() throws IOException {
        if (finished) {
            return;
        }
        long pos = input.position();
        try {
            input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
            while (true) {
                byte filestate = input.readPhysicalByte();
                if (filestate == 0) {
                    finished = true;
                    return;
                }
                Utils.takeNap(1);
            }
        } finally {
            input.position(pos);
        }
    }

    public boolean isLastChunk() throws IOException {
        awaitFinished();
        // streaming files only have one chunk
        return input.getFileSize() == absoluteChunkEnd;
   }

192 193 194 195
    public boolean isFinalChunk() {
        return finalChunk;
    }

196 197 198 199
    public boolean isFinished() throws IOException {
        return isFinished;
    }

A
apetushkov 已提交
200 201 202 203
    public ChunkHeader nextHeader() throws IOException {
        return new ChunkHeader(input, absoluteChunkEnd, id + 1);
    }
    public MetadataDescriptor readMetadata() throws IOException {
204 205 206 207
        return readMetadata(null);
    }

    public MetadataDescriptor readMetadata(MetadataDescriptor previous) throws IOException {
A
apetushkov 已提交
208 209 210 211 212 213 214 215 216
        input.position(absoluteChunkStart + metadataPosition);
        input.readInt(); // size
        long id = input.readLong(); // event type id
        if (id != METADATA_TYPE_ID) {
            throw new IOException("Expected metadata event. Type id=" + id + ", should have been " + METADATA_TYPE_ID);
        }
        input.readLong(); // start time
        input.readLong(); // duration
        long metadataId = input.readLong();
217 218 219 220 221 222 223
        if (previous != null && metadataId == previous.metadataId) {
            return previous;
        }
        Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "New metadata id = " + metadataId);
        MetadataDescriptor m =  MetadataDescriptor.read(input);
        m.metadataId = metadataId;
        return m;
A
apetushkov 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    }


    public short getMajor() {
        return major;
    }

    public short getMinor() {
        return minor;
    }

    public long getAbsoluteChunkStart() {
        return absoluteChunkStart;
    }

239 240 241
    public long getAbsoluteEventStart() {
        return absoluteEventStart;
    }
A
apetushkov 已提交
242 243 244 245
    public long getConstantPoolPosition() {
        return constantPoolPosition;
    }

246 247 248
    public long getMetataPosition() {
        return metadataPosition;
    }
A
apetushkov 已提交
249 250 251
    public long getStartTicks() {
        return chunkStartTicks;
    }
252 253 254
    public long getChunkSize() {
        return chunkSize;
    }
A
apetushkov 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    public double getTicksPerSecond() {
        return ticksPerSecond;
    }

    public long getStartNanos() {
        return chunkStartNanos;
    }

    public long getEnd() {
        return absoluteChunkEnd;
    }

    public long getSize() {
        return chunkSize;
    }

E
egahlin 已提交
272
    public long getDurationNanos() {
A
apetushkov 已提交
273 274 275 276 277 278 279
        return durationNanos;
    }

    public RecordingInput getInput() {
        return input;
    }

280
    private static void verifyMagic(RecordingInput input) throws IOException {
A
apetushkov 已提交
281 282 283 284 285 286 287 288 289 290 291
        for (byte c : FILE_MAGIC) {
            if (input.readByte() != c) {
                throw new IOException("Not a Flight Recorder file");
            }
        }
    }

    public long getEventStart() {
        return absoluteEventStart;
    }

292 293 294
    static long headerSize() {
        return HEADER_SIZE;
    }
A
apetushkov 已提交
295
}