50113
|
1 |
/*
|
58863
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
50113
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr.internal.consumer;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
|
|
30 |
import jdk.jfr.internal.LogLevel;
|
|
31 |
import jdk.jfr.internal.LogTag;
|
|
32 |
import jdk.jfr.internal.Logger;
|
|
33 |
import jdk.jfr.internal.MetadataDescriptor;
|
58863
|
34 |
import jdk.jfr.internal.Utils;
|
50113
|
35 |
|
|
36 |
public final class ChunkHeader {
|
58863
|
37 |
private static final long HEADER_SIZE = 68;
|
|
38 |
private static final byte UPDATING_CHUNK_HEADER = (byte) 255;
|
|
39 |
private static final long CHUNK_SIZE_POSITION = 8;
|
|
40 |
private static final long DURATION_NANOS_POSITION = 40;
|
|
41 |
private static final long FILE_STATE_POSITION = 64;
|
50113
|
42 |
private static final long METADATA_TYPE_ID = 0;
|
|
43 |
private static final byte[] FILE_MAGIC = { 'F', 'L', 'R', '\0' };
|
|
44 |
|
|
45 |
private final short major;
|
|
46 |
private final short minor;
|
|
47 |
private final long chunkStartTicks;
|
|
48 |
private final long ticksPerSecond;
|
|
49 |
private final long chunkStartNanos;
|
|
50 |
private final long absoluteChunkStart;
|
|
51 |
private final RecordingInput input;
|
|
52 |
private final long id;
|
58863
|
53 |
private long absoluteEventStart;
|
|
54 |
private long chunkSize = 0;
|
|
55 |
private long constantPoolPosition = 0;
|
|
56 |
private long metadataPosition = 0;
|
|
57 |
private long durationNanos;
|
|
58 |
private long absoluteChunkEnd;
|
|
59 |
private boolean isFinished;
|
|
60 |
private boolean finished;
|
50113
|
61 |
|
|
62 |
public ChunkHeader(RecordingInput input) throws IOException {
|
|
63 |
this(input, 0, 0);
|
|
64 |
}
|
|
65 |
|
|
66 |
private ChunkHeader(RecordingInput input, long absoluteChunkStart, long id) throws IOException {
|
58863
|
67 |
this.absoluteChunkStart = absoluteChunkStart;
|
|
68 |
this.absoluteEventStart = absoluteChunkStart + HEADER_SIZE;
|
|
69 |
if (input.getFileSize() < HEADER_SIZE) {
|
|
70 |
throw new IOException("Not a complete Chunk header");
|
|
71 |
}
|
|
72 |
input.setValidSize(absoluteChunkStart + HEADER_SIZE);
|
50113
|
73 |
input.position(absoluteChunkStart);
|
|
74 |
if (input.position() >= input.size()) {
|
58863
|
75 |
throw new IOException("Chunk contains no data");
|
50113
|
76 |
}
|
|
77 |
verifyMagic(input);
|
|
78 |
this.input = input;
|
|
79 |
this.id = id;
|
58863
|
80 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: " + id);
|
|
81 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: file=" + input.getFilename());
|
50113
|
82 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startPosition=" + absoluteChunkStart);
|
|
83 |
major = input.readRawShort();
|
|
84 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: major=" + major);
|
|
85 |
minor = input.readRawShort();
|
|
86 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: minor=" + minor);
|
|
87 |
if (major != 1 && major != 2) {
|
|
88 |
throw new IOException("File version " + major + "." + minor + ". Only Flight Recorder files of version 1.x and 2.x can be read by this JDK.");
|
|
89 |
}
|
58863
|
90 |
input.readRawLong(); // chunk size
|
50113
|
91 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: chunkSize=" + chunkSize);
|
58863
|
92 |
input.readRawLong(); // constant pool position
|
50113
|
93 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: constantPoolPosition=" + constantPoolPosition);
|
58863
|
94 |
input.readRawLong(); // metadata position
|
50113
|
95 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: metadataPosition=" + metadataPosition);
|
|
96 |
chunkStartNanos = input.readRawLong(); // nanos since epoch
|
|
97 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startNanos=" + chunkStartNanos);
|
|
98 |
durationNanos = input.readRawLong(); // duration nanos, not used
|
|
99 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: durationNanos=" + durationNanos);
|
|
100 |
chunkStartTicks = input.readRawLong();
|
|
101 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startTicks=" + chunkStartTicks);
|
|
102 |
ticksPerSecond = input.readRawLong();
|
|
103 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: ticksPerSecond=" + ticksPerSecond);
|
|
104 |
input.readRawInt(); // features, not used
|
|
105 |
|
58863
|
106 |
refresh();
|
|
107 |
input.position(absoluteEventStart);
|
|
108 |
}
|
50113
|
109 |
|
58863
|
110 |
void refresh() throws IOException {
|
|
111 |
while (true) {
|
|
112 |
byte fileState1;
|
|
113 |
input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
|
|
114 |
while ((fileState1 = input.readPhysicalByte()) == UPDATING_CHUNK_HEADER) {
|
|
115 |
Utils.takeNap(1);
|
|
116 |
input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
|
|
117 |
}
|
|
118 |
input.positionPhysical(absoluteChunkStart + CHUNK_SIZE_POSITION);
|
|
119 |
long chunkSize = input.readPhysicalLong();
|
|
120 |
long constantPoolPosition = input.readPhysicalLong();
|
|
121 |
long metadataPosition = input.readPhysicalLong();
|
|
122 |
input.positionPhysical(absoluteChunkStart + DURATION_NANOS_POSITION);
|
|
123 |
long durationNanos = input.readPhysicalLong();
|
|
124 |
input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
|
|
125 |
byte fileState2 = input.readPhysicalByte();
|
|
126 |
if (fileState1 == fileState2) { // valid header
|
|
127 |
finished = fileState1 == 0;
|
|
128 |
if (metadataPosition != 0) {
|
|
129 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Setting input size to " + (absoluteChunkStart + chunkSize));
|
|
130 |
if (finished) {
|
|
131 |
// This assumes that the whole recording
|
|
132 |
// is finished if the first chunk is.
|
|
133 |
// This is a limitation we may want to
|
|
134 |
// remove, but greatly improves performance as
|
|
135 |
// data can be read across chunk boundaries
|
|
136 |
// of multi-chunk files and only once.
|
|
137 |
input.setValidSize(input.getFileSize());
|
|
138 |
} else {
|
|
139 |
input.setValidSize(absoluteChunkStart + chunkSize);
|
|
140 |
}
|
|
141 |
this.chunkSize = chunkSize;
|
|
142 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: chunkSize=" + chunkSize);
|
|
143 |
this.constantPoolPosition = constantPoolPosition;
|
|
144 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: constantPoolPosition=" + constantPoolPosition);
|
|
145 |
this.metadataPosition = metadataPosition;
|
|
146 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: metadataPosition=" + metadataPosition);
|
|
147 |
this.durationNanos = durationNanos;
|
|
148 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: durationNanos =" + durationNanos);
|
|
149 |
isFinished = fileState2 == 0;
|
|
150 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: generation=" + fileState2);
|
|
151 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: finished=" + isFinished);
|
|
152 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: fileSize=" + input.size());
|
|
153 |
absoluteChunkEnd = absoluteChunkStart + chunkSize;
|
|
154 |
return;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
public void awaitFinished() throws IOException {
|
|
161 |
if (finished) {
|
|
162 |
return;
|
|
163 |
}
|
|
164 |
long pos = input.position();
|
|
165 |
try {
|
|
166 |
input.positionPhysical(absoluteChunkStart + FILE_STATE_POSITION);
|
|
167 |
while (true) {
|
|
168 |
byte filestate = input.readPhysicalByte();
|
|
169 |
if (filestate == 0) {
|
|
170 |
finished = true;
|
|
171 |
return;
|
|
172 |
}
|
|
173 |
Utils.takeNap(1);
|
|
174 |
}
|
|
175 |
} finally {
|
|
176 |
input.position(pos);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
public boolean isLastChunk() throws IOException {
|
|
181 |
awaitFinished();
|
|
182 |
// streaming files only have one chunk
|
|
183 |
return input.getFileSize() == absoluteChunkEnd;
|
|
184 |
}
|
|
185 |
|
|
186 |
public boolean isFinished() throws IOException {
|
|
187 |
return isFinished;
|
50113
|
188 |
}
|
|
189 |
|
|
190 |
public ChunkHeader nextHeader() throws IOException {
|
|
191 |
return new ChunkHeader(input, absoluteChunkEnd, id + 1);
|
|
192 |
}
|
58863
|
193 |
public MetadataDescriptor readMetadata() throws IOException {
|
|
194 |
return readMetadata(null);
|
|
195 |
}
|
50113
|
196 |
|
58863
|
197 |
public MetadataDescriptor readMetadata(MetadataDescriptor previous) throws IOException {
|
50113
|
198 |
input.position(absoluteChunkStart + metadataPosition);
|
|
199 |
input.readInt(); // size
|
|
200 |
long id = input.readLong(); // event type id
|
|
201 |
if (id != METADATA_TYPE_ID) {
|
|
202 |
throw new IOException("Expected metadata event. Type id=" + id + ", should have been " + METADATA_TYPE_ID);
|
|
203 |
}
|
|
204 |
input.readLong(); // start time
|
|
205 |
input.readLong(); // duration
|
|
206 |
long metadataId = input.readLong();
|
58863
|
207 |
if (previous != null && metadataId == previous.metadataId) {
|
|
208 |
return previous;
|
|
209 |
}
|
|
210 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "New metadata id = " + metadataId);
|
|
211 |
MetadataDescriptor m = MetadataDescriptor.read(input);
|
|
212 |
m.metadataId = metadataId;
|
|
213 |
return m;
|
50113
|
214 |
}
|
|
215 |
|
|
216 |
|
|
217 |
public short getMajor() {
|
|
218 |
return major;
|
|
219 |
}
|
|
220 |
|
|
221 |
public short getMinor() {
|
|
222 |
return minor;
|
|
223 |
}
|
|
224 |
|
|
225 |
public long getAbsoluteChunkStart() {
|
|
226 |
return absoluteChunkStart;
|
|
227 |
}
|
|
228 |
|
58863
|
229 |
public long getAbsoluteEventStart() {
|
|
230 |
return absoluteEventStart;
|
|
231 |
}
|
50113
|
232 |
public long getConstantPoolPosition() {
|
|
233 |
return constantPoolPosition;
|
|
234 |
}
|
|
235 |
|
58863
|
236 |
public long getMetataPosition() {
|
|
237 |
return metadataPosition;
|
|
238 |
}
|
50113
|
239 |
public long getStartTicks() {
|
|
240 |
return chunkStartTicks;
|
|
241 |
}
|
58863
|
242 |
public long getChunkSize() {
|
|
243 |
return chunkSize;
|
|
244 |
}
|
50113
|
245 |
|
|
246 |
public double getTicksPerSecond() {
|
|
247 |
return ticksPerSecond;
|
|
248 |
}
|
|
249 |
|
|
250 |
public long getStartNanos() {
|
|
251 |
return chunkStartNanos;
|
|
252 |
}
|
|
253 |
|
|
254 |
public long getEnd() {
|
|
255 |
return absoluteChunkEnd;
|
|
256 |
}
|
|
257 |
|
|
258 |
public long getSize() {
|
|
259 |
return chunkSize;
|
|
260 |
}
|
|
261 |
|
52850
|
262 |
public long getDurationNanos() {
|
50113
|
263 |
return durationNanos;
|
|
264 |
}
|
|
265 |
|
|
266 |
public RecordingInput getInput() {
|
|
267 |
return input;
|
|
268 |
}
|
|
269 |
|
58863
|
270 |
private static void verifyMagic(RecordingInput input) throws IOException {
|
50113
|
271 |
for (byte c : FILE_MAGIC) {
|
|
272 |
if (input.readByte() != c) {
|
|
273 |
throw new IOException("Not a Flight Recorder file");
|
|
274 |
}
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
public long getEventStart() {
|
|
279 |
return absoluteEventStart;
|
|
280 |
}
|
|
281 |
|
58863
|
282 |
static long headerSize() {
|
|
283 |
return HEADER_SIZE;
|
|
284 |
}
|
50113
|
285 |
}
|