author | egahlin |
Fri, 30 Aug 2019 20:39:38 +0200 | |
branch | JEP-349-branch |
changeset 57971 | aa7b1ea52413 |
parent 57690 | 9316d02dd4a5 |
child 58197 | 0ef79bd7fb5c |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 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; |
|
57360 | 34 |
import jdk.jfr.internal.Utils; |
50113 | 35 |
|
36 |
public final class ChunkHeader { |
|
57360 | 37 |
public static final long HEADER_SIZE = 68; |
38 |
public static final byte MARKED_FOR_DELETION = (byte) 255; |
|
39 |
private static final byte UPDATING_CHUNK_HEADER = (byte) 255; |
|
40 |
private static final long CHUNK_SIZE_POSITION = 8; |
|
41 |
private static final long DURATION_NANOS_POSITION = 40; |
|
42 |
private static final long FILE_STATE_POSITION = 64; |
|
50113 | 43 |
private static final long METADATA_TYPE_ID = 0; |
44 |
private static final byte[] FILE_MAGIC = { 'F', 'L', 'R', '\0' }; |
|
45 |
||
46 |
private final short major; |
|
47 |
private final short minor; |
|
48 |
private final long chunkStartTicks; |
|
49 |
private final long ticksPerSecond; |
|
50 |
private final long chunkStartNanos; |
|
51 |
private final long absoluteChunkStart; |
|
52 |
private final RecordingInput input; |
|
53 |
private final long id; |
|
57360 | 54 |
private long absoluteEventStart; |
57971 | 55 |
private long chunkSize = 0; |
56 |
private long constantPoolPosition = 0; |
|
57 |
private long metadataPosition = 0; |
|
57360 | 58 |
private long durationNanos; |
59 |
private long absoluteChunkEnd; |
|
60 |
private boolean isFinished; |
|
61 |
private boolean finished; |
|
50113 | 62 |
|
63 |
public ChunkHeader(RecordingInput input) throws IOException { |
|
64 |
this(input, 0, 0); |
|
65 |
} |
|
66 |
||
67 |
private ChunkHeader(RecordingInput input, long absoluteChunkStart, long id) throws IOException { |
|
57360 | 68 |
this.absoluteChunkStart = absoluteChunkStart; |
69 |
this.absoluteEventStart = absoluteChunkStart + HEADER_SIZE; |
|
70 |
if (input.getFileSize() < HEADER_SIZE) { |
|
71 |
throw new IOException("Not a complete Chunk header"); |
|
72 |
} |
|
73 |
input.setValidSize(absoluteChunkStart + HEADER_SIZE); |
|
50113 | 74 |
input.position(absoluteChunkStart); |
75 |
if (input.position() >= input.size()) { |
|
57360 | 76 |
throw new IOException("Chunk contains no data"); |
50113 | 77 |
} |
78 |
verifyMagic(input); |
|
79 |
this.input = input; |
|
80 |
this.id = id; |
|
57360 | 81 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: " + id); |
82 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: file=" + input.getFilename()); |
|
50113 | 83 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startPosition=" + absoluteChunkStart); |
84 |
major = input.readRawShort(); |
|
85 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: major=" + major); |
|
86 |
minor = input.readRawShort(); |
|
87 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: minor=" + minor); |
|
88 |
if (major != 1 && major != 2) { |
|
89 |
throw new IOException("File version " + major + "." + minor + ". Only Flight Recorder files of version 1.x and 2.x can be read by this JDK."); |
|
90 |
} |
|
57360 | 91 |
input.readRawLong(); // chunk size |
50113 | 92 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: chunkSize=" + chunkSize); |
57360 | 93 |
input.readRawLong(); // constant pool position |
50113 | 94 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: constantPoolPosition=" + constantPoolPosition); |
57360 | 95 |
input.readRawLong(); // metadata position |
50113 | 96 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: metadataPosition=" + metadataPosition); |
97 |
chunkStartNanos = input.readRawLong(); // nanos since epoch |
|
98 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startNanos=" + chunkStartNanos); |
|
99 |
durationNanos = input.readRawLong(); // duration nanos, not used |
|
100 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: durationNanos=" + durationNanos); |
|
101 |
chunkStartTicks = input.readRawLong(); |
|
102 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: startTicks=" + chunkStartTicks); |
|
103 |
ticksPerSecond = input.readRawLong(); |
|
104 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Chunk: ticksPerSecond=" + ticksPerSecond); |
|
105 |
input.readRawInt(); // features, not used |
|
106 |
||
57360 | 107 |
refresh(); |
108 |
input.position(absoluteEventStart); |
|
109 |
} |
|
110 |
public 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) { |
|
57459 | 115 |
Utils.takeNap(1); |
57360 | 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; |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57459
diff
changeset
|
128 |
if (metadataPosition != 0) { |
57360 | 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 |
} |
|
50113 | 159 |
|
57360 | 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(); |
|
57459 | 169 |
if (filestate == 0) { |
57360 | 170 |
finished = true; |
171 |
return; |
|
172 |
} |
|
57459 | 173 |
Utils.takeNap(1); |
57360 | 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 |
} |
|
57360 | 193 |
public MetadataDescriptor readMetadata() throws IOException { |
194 |
return readMetadata(null); |
|
195 |
} |
|
50113 | 196 |
|
57360 | 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(); |
|
57360 | 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 |
||
57360 | 229 |
public long getAbsoluteEventStart() { |
230 |
return absoluteEventStart; |
|
231 |
} |
|
50113 | 232 |
public long getConstantPoolPosition() { |
233 |
return constantPoolPosition; |
|
234 |
} |
|
235 |
||
57360 | 236 |
public long getMetataPosition() { |
237 |
return metadataPosition; |
|
238 |
} |
|
50113 | 239 |
public long getStartTicks() { |
240 |
return chunkStartTicks; |
|
241 |
} |
|
57360 | 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 |
||
57360 | 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 |
||
282 |
} |