author | egahlin |
Thu, 11 Jul 2019 02:18:31 +0200 | |
branch | JEP-349-branch |
changeset 57466 | faf3a3b0bab6 |
parent 57454 | 703fb247dc4e |
child 57690 | 9316d02dd4a5 |
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.DataInput; |
|
29 |
import java.io.EOFException; |
|
30 |
import java.io.File; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.RandomAccessFile; |
|
57386 | 33 |
import java.nio.file.Path; |
50113 | 34 |
|
35 |
public final class RecordingInput implements DataInput, AutoCloseable { |
|
36 |
||
57466 | 37 |
private final static int DEFAULT_BLOCK_SIZE = 64_000; |
50113 | 38 |
|
39 |
private static final class Block { |
|
40 |
private byte[] bytes = new byte[0]; |
|
41 |
private long blockPosition; |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
42 |
private long blockPositionEnd; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
43 |
|
50113 | 44 |
boolean contains(long position) { |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
45 |
return position >= blockPosition && position < blockPositionEnd; |
50113 | 46 |
} |
47 |
||
48 |
public void read(RandomAccessFile file, int amount) throws IOException { |
|
49 |
blockPosition = file.getFilePointer(); |
|
50 |
// reuse byte array, if possible |
|
57360 | 51 |
if (amount > bytes.length) { |
50113 | 52 |
bytes = new byte[amount]; |
53 |
} |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
54 |
this.blockPositionEnd = blockPosition + amount; |
57454 | 55 |
file.readFully(bytes, 0, amount); |
50113 | 56 |
} |
57 |
||
58 |
public byte get(long position) { |
|
59 |
return bytes[(int) (position - blockPosition)]; |
|
60 |
} |
|
57386 | 61 |
|
62 |
public void reset() { |
|
57454 | 63 |
blockPosition = 0; |
64 |
blockPositionEnd = 0; |
|
57386 | 65 |
} |
50113 | 66 |
} |
67 |
||
57425 | 68 |
private RandomAccessFile file; |
69 |
private String filename; |
|
50113 | 70 |
private Block currentBlock = new Block(); |
71 |
private Block previousBlock = new Block(); |
|
72 |
private long position; |
|
73 |
private final int blockSize; |
|
57454 | 74 |
private long size = -1; // Fail fast if setSize(...) has not been called |
75 |
// before parsing |
|
50113 | 76 |
|
57360 | 77 |
public RecordingInput(File f, int blockSize) throws IOException { |
50113 | 78 |
this.blockSize = blockSize; |
57425 | 79 |
initialize(f); |
80 |
} |
|
81 |
||
82 |
private void initialize(File f) throws IOException { |
|
57360 | 83 |
this.filename = f.getAbsolutePath().toString(); |
50113 | 84 |
this.file = new RandomAccessFile(f, "r"); |
57425 | 85 |
this.position = 0; |
86 |
this.size = -1; |
|
87 |
this.currentBlock.reset(); |
|
88 |
this.previousBlock.reset(); |
|
57360 | 89 |
if (f.length() < 8) { |
90 |
throw new IOException("Not a valid Flight Recorder file. File length is only " + f.length() + " bytes."); |
|
50113 | 91 |
} |
92 |
} |
|
93 |
||
94 |
public RecordingInput(File f) throws IOException { |
|
95 |
this(f, DEFAULT_BLOCK_SIZE); |
|
96 |
} |
|
57386 | 97 |
|
57360 | 98 |
public void positionPhysical(long position) throws IOException { |
99 |
file.seek(position); |
|
100 |
} |
|
57454 | 101 |
|
57360 | 102 |
public final byte readPhysicalByte() throws IOException { |
103 |
return file.readByte(); |
|
104 |
} |
|
57454 | 105 |
|
57360 | 106 |
public long readPhysicalLong() throws IOException { |
107 |
return file.readLong(); |
|
108 |
} |
|
50113 | 109 |
|
110 |
@Override |
|
111 |
public final byte readByte() throws IOException { |
|
112 |
if (!currentBlock.contains(position)) { |
|
113 |
position(position); |
|
114 |
} |
|
115 |
return currentBlock.get(position++); |
|
116 |
} |
|
117 |
||
118 |
@Override |
|
119 |
public final void readFully(byte[] dest, int offset, int length) throws IOException { |
|
120 |
// TODO: Optimize, use Arrays.copy if all bytes are in current block |
|
121 |
// array |
|
122 |
for (int i = 0; i < length; i++) { |
|
123 |
dest[i + offset] = readByte(); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
@Override |
|
128 |
public final void readFully(byte[] dst) throws IOException { |
|
129 |
readFully(dst, 0, dst.length); |
|
130 |
} |
|
131 |
||
132 |
public final short readRawShort() throws IOException { |
|
133 |
// copied from java.io.Bits |
|
134 |
byte b0 = readByte(); |
|
135 |
byte b1 = readByte(); |
|
136 |
return (short) ((b1 & 0xFF) + (b0 << 8)); |
|
137 |
} |
|
138 |
||
139 |
@Override |
|
140 |
public final double readDouble() throws IOException { |
|
141 |
// copied from java.io.Bits |
|
142 |
return Double.longBitsToDouble(readRawLong()); |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
public final float readFloat() throws IOException { |
|
147 |
// copied from java.io.Bits |
|
148 |
return Float.intBitsToFloat(readRawInt()); |
|
149 |
} |
|
150 |
||
151 |
public final int readRawInt() throws IOException { |
|
152 |
// copied from java.io.Bits |
|
153 |
byte b0 = readByte(); |
|
154 |
byte b1 = readByte(); |
|
155 |
byte b2 = readByte(); |
|
156 |
byte b3 = readByte(); |
|
157 |
return ((b3 & 0xFF)) + ((b2 & 0xFF) << 8) + ((b1 & 0xFF) << 16) + ((b0) << 24); |
|
158 |
} |
|
159 |
||
160 |
public final long readRawLong() throws IOException { |
|
161 |
// copied from java.io.Bits |
|
162 |
byte b0 = readByte(); |
|
163 |
byte b1 = readByte(); |
|
164 |
byte b2 = readByte(); |
|
165 |
byte b3 = readByte(); |
|
166 |
byte b4 = readByte(); |
|
167 |
byte b5 = readByte(); |
|
168 |
byte b6 = readByte(); |
|
169 |
byte b7 = readByte(); |
|
170 |
return ((b7 & 0xFFL)) + ((b6 & 0xFFL) << 8) + ((b5 & 0xFFL) << 16) + ((b4 & 0xFFL) << 24) + ((b3 & 0xFFL) << 32) + ((b2 & 0xFFL) << 40) + ((b1 & 0xFFL) << 48) + (((long) b0) << 56); |
|
171 |
} |
|
172 |
||
57360 | 173 |
public final long position() { |
50113 | 174 |
return position; |
175 |
} |
|
176 |
||
177 |
public final void position(long newPosition) throws IOException { |
|
178 |
if (!currentBlock.contains(newPosition)) { |
|
179 |
if (!previousBlock.contains(newPosition)) { |
|
57360 | 180 |
if (newPosition > size) { |
181 |
throw new EOFException("Trying to read at " + newPosition + ", but file is only " + size + " bytes."); |
|
50113 | 182 |
} |
183 |
long blockStart = trimToFileSize(calculateBlockStart(newPosition)); |
|
184 |
file.seek(blockStart); |
|
185 |
// trim amount to file size |
|
57360 | 186 |
long amount = Math.min(size - blockStart, blockSize); |
50113 | 187 |
previousBlock.read(file, (int) amount); |
188 |
} |
|
189 |
// swap previous and current |
|
190 |
Block tmp = currentBlock; |
|
191 |
currentBlock = previousBlock; |
|
192 |
previousBlock = tmp; |
|
193 |
} |
|
194 |
position = newPosition; |
|
195 |
} |
|
196 |
||
197 |
private final long trimToFileSize(long position) throws IOException { |
|
198 |
return Math.min(size(), Math.max(0, position)); |
|
199 |
} |
|
200 |
||
201 |
private final long calculateBlockStart(long newPosition) { |
|
202 |
// align to end of current block |
|
203 |
if (currentBlock.contains(newPosition - blockSize)) { |
|
204 |
return currentBlock.blockPosition + currentBlock.bytes.length; |
|
205 |
} |
|
206 |
// align before current block |
|
207 |
if (currentBlock.contains(newPosition + blockSize)) { |
|
208 |
return currentBlock.blockPosition - blockSize; |
|
209 |
} |
|
210 |
// not near current block, pick middle |
|
211 |
return newPosition - blockSize / 2; |
|
212 |
} |
|
213 |
||
57360 | 214 |
public final long size() { |
50113 | 215 |
return size; |
216 |
} |
|
217 |
||
218 |
public final void close() throws IOException { |
|
219 |
file.close(); |
|
220 |
} |
|
221 |
||
222 |
@Override |
|
223 |
public final int skipBytes(int n) throws IOException { |
|
224 |
long position = position(); |
|
225 |
position(position + n); |
|
226 |
return (int) (position() - position); |
|
227 |
} |
|
228 |
||
229 |
@Override |
|
230 |
public final boolean readBoolean() throws IOException { |
|
231 |
return readByte() != 0; |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public int readUnsignedByte() throws IOException { |
|
236 |
return readByte() & 0x00FF; |
|
237 |
} |
|
238 |
||
239 |
@Override |
|
240 |
public int readUnsignedShort() throws IOException { |
|
241 |
return readShort() & 0xFFFF; |
|
242 |
} |
|
243 |
||
244 |
@Override |
|
245 |
public final String readLine() throws IOException { |
|
246 |
throw new UnsupportedOperationException(); |
|
247 |
} |
|
248 |
||
249 |
// NOTE, this method should really be called readString |
|
250 |
// but can't be renamed without making RecordingInput a |
|
251 |
// public class. |
|
252 |
// |
|
253 |
// This method DOES Not read as expected (s2 + utf8 encoded character) |
|
254 |
// instead it read: |
|
255 |
// byte encoding |
|
256 |
// int size |
|
257 |
// data (byte or char) |
|
258 |
// |
|
259 |
// where encoding |
|
260 |
// |
|
261 |
// 0, means null |
|
262 |
// 1, means UTF8 encoded byte array |
|
263 |
// 2, means char array |
|
264 |
// 3, means latin-1 (ISO-8859-1) encoded byte array |
|
265 |
// 4, means "" |
|
266 |
@Override |
|
267 |
public String readUTF() throws IOException { |
|
57360 | 268 |
throw new UnsupportedOperationException("Use StringParser"); |
50113 | 269 |
} |
270 |
||
271 |
@Override |
|
272 |
public char readChar() throws IOException { |
|
273 |
return (char) readLong(); |
|
274 |
} |
|
275 |
||
276 |
@Override |
|
277 |
public short readShort() throws IOException { |
|
278 |
return (short) readLong(); |
|
279 |
} |
|
280 |
||
281 |
@Override |
|
282 |
public int readInt() throws IOException { |
|
283 |
return (int) readLong(); |
|
284 |
} |
|
285 |
||
57454 | 286 |
public long readLongExpanded() throws IOException { |
287 |
final byte[] bytes = currentBlock.bytes; |
|
288 |
final int index = (int) (position - currentBlock.blockPosition); |
|
289 |
||
290 |
if (index + 8 < bytes.length && index >= 0) { |
|
291 |
byte b0 = bytes[index]; |
|
292 |
if (b0 >= 0) { |
|
293 |
position += 1; |
|
294 |
return (b0 & 0x7FL); |
|
295 |
} |
|
296 |
int b1 = bytes[index + 1]; |
|
297 |
if (b1 >= 0) { |
|
298 |
position += 2; |
|
299 |
return (b0 & 0x7FL) + ((b1 & 0x7FL) << 7); |
|
300 |
} |
|
301 |
int b2 = bytes[index + 2]; |
|
302 |
if (b2 >= 0) { |
|
303 |
position += 3; |
|
304 |
return (b0 & 0x7FL) + ((b1 & 0x7FL) << 7) + ((b2 & 0x7FL) << 14); |
|
305 |
} |
|
306 |
int b3 = bytes[index + 3]; |
|
307 |
if (b3 >= 0) { |
|
308 |
position += 4; |
|
309 |
return (b0 & 0x7FL) + ((b1 & 0x7FL) << 7) + ((b2 & 0x7FL) << 14) + ((b3 & 0x7FL) << 21); |
|
310 |
} |
|
311 |
int b4 = bytes[index + 4]; |
|
312 |
if (b4 >= 0) { |
|
313 |
position += 5; |
|
314 |
return (b0 & 0x7FL) + ((b1 & 0x7FL) << 7) + ((b2 & 0x7FL) << 14) + |
|
315 |
((b3 & 0x7FL) << 21) + ((b4 & 0x7FL) << 28); |
|
316 |
} |
|
317 |
int b5 = bytes[index + 5]; |
|
318 |
if (b5 >= 0) { |
|
319 |
position += 6; |
|
320 |
return (b0 & 0x7FL) + ((b1 & 0x7FL) << 7) + ((b2 & 0x7FL) << 14) + |
|
321 |
((b3 & 0x7FL) << 21) + ((b4 & 0x7FL) << 28) + ((b5 & 0x7FL) << 35); |
|
322 |
} |
|
323 |
int b6 = bytes[index + 6]; |
|
324 |
if (b6 >= 0) { |
|
325 |
position += 7; |
|
326 |
return (b0 & 0x7FL) + ((b1 & 0x7FL) << 7) + ((b2 & 0x7FL) << 14) + |
|
327 |
((b3 & 0x7FL) << 21) + ((b4 & 0x7FL) << 28) + ((b5 & 0x7FL) << 35) + |
|
328 |
((b6 & 0x7FL) << 42); |
|
329 |
||
330 |
} |
|
331 |
int b7 = bytes[index + 7]; |
|
332 |
if (b7 >= 0) { |
|
333 |
position += 8; |
|
334 |
return (b0 & 0x7FL) + ((b1 & 0x7FL) << 7) + ((b2 & 0x7FL) << 14) + |
|
335 |
((b3 & 0x7FL) << 21) + ((b4 & 0x7FL) << 28) + ((b5 & 0x7FL) << 35) + |
|
336 |
((b6 & 0x7FL) << 42) + ((b7 & 0x7FL) << 49); |
|
337 |
} |
|
338 |
int b8 = bytes[index + 8];// read last byte raw |
|
339 |
position += 9; |
|
340 |
long ret = (b0 & 0x7FL) + ((b1 & 0x7FL) << 7) + ((b2 & 0x7FL) << 14) + |
|
341 |
((b3 & 0x7FL) << 21) + ((b4 & 0x7FL) << 28) + ((b5 & 0x7FL) << 35) + |
|
342 |
((b6 & 0x7FL) << 42) + ((b7 & 0x7FL) << 49); |
|
343 |
return ret + ((((long) (b8 & 0XFF)) << 56)); |
|
344 |
} else { |
|
345 |
return readLongSlow(); |
|
346 |
} |
|
347 |
} |
|
348 |
||
50113 | 349 |
public long readLong() throws IOException { |
57454 | 350 |
final byte[] bytes = currentBlock.bytes; |
351 |
final int index = (int) (position - currentBlock.blockPosition); |
|
352 |
||
353 |
if (index + 8 < bytes.length && index >= 0) { |
|
354 |
byte b0 = bytes[index]; |
|
355 |
long ret = (b0 & 0x7FL); |
|
356 |
if (b0 >= 0) { |
|
357 |
position += 1; |
|
358 |
return ret; |
|
359 |
} |
|
360 |
int b1 = bytes[index + 1]; |
|
361 |
ret += (b1 & 0x7FL) << 7; |
|
362 |
if (b1 >= 0) { |
|
363 |
position += 2; |
|
364 |
return ret; |
|
365 |
} |
|
366 |
int b2 = bytes[index + 2]; |
|
367 |
ret += (b2 & 0x7FL) << 14; |
|
368 |
if (b2 >= 0) { |
|
369 |
position += 3; |
|
370 |
return ret; |
|
371 |
} |
|
372 |
int b3 = bytes[index + 3]; |
|
373 |
ret += (b3 & 0x7FL) << 21; |
|
374 |
if (b3 >= 0) { |
|
375 |
position += 4; |
|
376 |
return ret; |
|
377 |
} |
|
378 |
int b4 = bytes[index + 4]; |
|
379 |
ret += (b4 & 0x7FL) << 28; |
|
380 |
if (b4 >= 0) { |
|
381 |
position += 5; |
|
382 |
return ret; |
|
383 |
} |
|
384 |
int b5 = bytes[index + 5]; |
|
385 |
ret += (b5 & 0x7FL) << 35; |
|
386 |
if (b5 >= 0) { |
|
387 |
position += 6; |
|
388 |
return ret; |
|
389 |
} |
|
390 |
int b6 = bytes[index + 6]; |
|
391 |
ret += (b6 & 0x7FL) << 42; |
|
392 |
if (b6 >= 0) { |
|
393 |
position += 7; |
|
394 |
return ret; |
|
395 |
} |
|
396 |
int b7 = bytes[index + 7]; |
|
397 |
ret += (b7 & 0x7FL) << 49; |
|
398 |
if (b7 >= 0) { |
|
399 |
position += 8; |
|
400 |
return ret; |
|
401 |
} |
|
402 |
int b8 = bytes[index + 8];// read last byte raw |
|
403 |
position += 9; |
|
404 |
return ret + (((long) (b8 & 0XFF)) << 56); |
|
405 |
} else { |
|
406 |
return readLongSlow(); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
private long readLongSlow() throws IOException { |
|
50113 | 411 |
byte b0 = readByte(); |
412 |
long ret = (b0 & 0x7FL); |
|
413 |
if (b0 >= 0) { |
|
414 |
return ret; |
|
415 |
} |
|
57454 | 416 |
|
50113 | 417 |
int b1 = readByte(); |
418 |
ret += (b1 & 0x7FL) << 7; |
|
419 |
if (b1 >= 0) { |
|
420 |
return ret; |
|
421 |
} |
|
57454 | 422 |
|
50113 | 423 |
int b2 = readByte(); |
424 |
ret += (b2 & 0x7FL) << 14; |
|
425 |
if (b2 >= 0) { |
|
426 |
return ret; |
|
427 |
} |
|
57454 | 428 |
|
50113 | 429 |
int b3 = readByte(); |
430 |
ret += (b3 & 0x7FL) << 21; |
|
431 |
if (b3 >= 0) { |
|
432 |
return ret; |
|
433 |
} |
|
57454 | 434 |
|
50113 | 435 |
int b4 = readByte(); |
436 |
ret += (b4 & 0x7FL) << 28; |
|
437 |
if (b4 >= 0) { |
|
438 |
return ret; |
|
439 |
} |
|
57454 | 440 |
|
50113 | 441 |
int b5 = readByte(); |
442 |
ret += (b5 & 0x7FL) << 35; |
|
443 |
if (b5 >= 0) { |
|
444 |
return ret; |
|
445 |
} |
|
57454 | 446 |
|
50113 | 447 |
int b6 = readByte(); |
448 |
ret += (b6 & 0x7FL) << 42; |
|
449 |
if (b6 >= 0) { |
|
450 |
return ret; |
|
451 |
} |
|
57454 | 452 |
|
50113 | 453 |
int b7 = readByte(); |
454 |
ret += (b7 & 0x7FL) << 49; |
|
455 |
if (b7 >= 0) { |
|
456 |
return ret; |
|
57454 | 457 |
|
50113 | 458 |
} |
57454 | 459 |
|
50113 | 460 |
int b8 = readByte(); // read last byte raw |
461 |
return ret + (((long) (b8 & 0XFF)) << 56); |
|
462 |
} |
|
57360 | 463 |
|
464 |
public void setValidSize(long size) { |
|
465 |
if (size > this.size) { |
|
466 |
this.size = size; |
|
467 |
} |
|
468 |
} |
|
469 |
||
470 |
public long getFileSize() throws IOException { |
|
471 |
return file.length(); |
|
472 |
} |
|
473 |
||
474 |
public String getFilename() { |
|
475 |
return filename; |
|
476 |
} |
|
477 |
||
57386 | 478 |
// Purpose of this method is to reuse block cache from a |
479 |
// previous RecordingInput |
|
57454 | 480 |
public void setFile(Path path) throws IOException { |
57386 | 481 |
try { |
57425 | 482 |
file.close(); |
57386 | 483 |
} catch (IOException e) { |
484 |
// perhaps deleted |
|
485 |
} |
|
57425 | 486 |
file = null; |
487 |
initialize(path.toFile()); |
|
57386 | 488 |
} |
57454 | 489 |
/* |
57386 | 490 |
|
57454 | 491 |
|
492 |
||
493 |
||
494 |
||
495 |
* |
|
496 |
* |
|
497 |
*/ |
|
50113 | 498 |
} |