author | egahlin |
Fri, 05 Jul 2019 03:36:40 +0200 | |
branch | JEP-349-branch |
changeset 57452 | 6fabe73e5d9a |
parent 57425 | 1da8552f0b59 |
child 57454 | 703fb247dc4e |
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 |
||
37 |
||
38 |
private final static int DEFAULT_BLOCK_SIZE = 16 * 1024 * 1024; |
|
39 |
||
40 |
private static final class Block { |
|
41 |
private byte[] bytes = new byte[0]; |
|
42 |
private long blockPosition; |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
43 |
private long blockPositionEnd; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
44 |
|
50113 | 45 |
boolean contains(long position) { |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
46 |
return position >= blockPosition && position < blockPositionEnd; |
50113 | 47 |
} |
48 |
||
49 |
public void read(RandomAccessFile file, int amount) throws IOException { |
|
50 |
blockPosition = file.getFilePointer(); |
|
51 |
// reuse byte array, if possible |
|
57360 | 52 |
if (amount > bytes.length) { |
50113 | 53 |
bytes = new byte[amount]; |
54 |
} |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
55 |
this.blockPositionEnd = blockPosition + amount; |
57360 | 56 |
file.readFully(bytes, 0 , amount); |
50113 | 57 |
} |
58 |
||
59 |
public byte get(long position) { |
|
60 |
return bytes[(int) (position - blockPosition)]; |
|
61 |
} |
|
57386 | 62 |
|
63 |
public void reset() { |
|
64 |
blockPosition = 0; |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57425
diff
changeset
|
65 |
blockPositionEnd = 0; |
57386 | 66 |
} |
50113 | 67 |
} |
68 |
||
57425 | 69 |
private RandomAccessFile file; |
70 |
private String filename; |
|
50113 | 71 |
private Block currentBlock = new Block(); |
72 |
private Block previousBlock = new Block(); |
|
73 |
private long position; |
|
74 |
private final int blockSize; |
|
57360 | 75 |
private long size = -1; // Fail fast if setSize(...) has not been called 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 |
} |
|
101 |
public final byte readPhysicalByte() throws IOException { |
|
102 |
return file.readByte(); |
|
103 |
} |
|
104 |
public long readPhysicalLong() throws IOException { |
|
105 |
return file.readLong(); |
|
106 |
} |
|
50113 | 107 |
|
108 |
@Override |
|
109 |
public final byte readByte() throws IOException { |
|
110 |
if (!currentBlock.contains(position)) { |
|
111 |
position(position); |
|
112 |
} |
|
113 |
return currentBlock.get(position++); |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public final void readFully(byte[] dest, int offset, int length) throws IOException { |
|
118 |
// TODO: Optimize, use Arrays.copy if all bytes are in current block |
|
119 |
// array |
|
120 |
for (int i = 0; i < length; i++) { |
|
121 |
dest[i + offset] = readByte(); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
@Override |
|
126 |
public final void readFully(byte[] dst) throws IOException { |
|
127 |
readFully(dst, 0, dst.length); |
|
128 |
} |
|
129 |
||
130 |
public final short readRawShort() throws IOException { |
|
131 |
// copied from java.io.Bits |
|
132 |
byte b0 = readByte(); |
|
133 |
byte b1 = readByte(); |
|
134 |
return (short) ((b1 & 0xFF) + (b0 << 8)); |
|
135 |
} |
|
136 |
||
137 |
@Override |
|
138 |
public final double readDouble() throws IOException { |
|
139 |
// copied from java.io.Bits |
|
140 |
return Double.longBitsToDouble(readRawLong()); |
|
141 |
} |
|
142 |
||
143 |
@Override |
|
144 |
public final float readFloat() throws IOException { |
|
145 |
// copied from java.io.Bits |
|
146 |
return Float.intBitsToFloat(readRawInt()); |
|
147 |
} |
|
148 |
||
149 |
public final int readRawInt() throws IOException { |
|
150 |
// copied from java.io.Bits |
|
151 |
byte b0 = readByte(); |
|
152 |
byte b1 = readByte(); |
|
153 |
byte b2 = readByte(); |
|
154 |
byte b3 = readByte(); |
|
155 |
return ((b3 & 0xFF)) + ((b2 & 0xFF) << 8) + ((b1 & 0xFF) << 16) + ((b0) << 24); |
|
156 |
} |
|
157 |
||
158 |
public final long readRawLong() throws IOException { |
|
159 |
// copied from java.io.Bits |
|
160 |
byte b0 = readByte(); |
|
161 |
byte b1 = readByte(); |
|
162 |
byte b2 = readByte(); |
|
163 |
byte b3 = readByte(); |
|
164 |
byte b4 = readByte(); |
|
165 |
byte b5 = readByte(); |
|
166 |
byte b6 = readByte(); |
|
167 |
byte b7 = readByte(); |
|
168 |
return ((b7 & 0xFFL)) + ((b6 & 0xFFL) << 8) + ((b5 & 0xFFL) << 16) + ((b4 & 0xFFL) << 24) + ((b3 & 0xFFL) << 32) + ((b2 & 0xFFL) << 40) + ((b1 & 0xFFL) << 48) + (((long) b0) << 56); |
|
169 |
} |
|
170 |
||
57360 | 171 |
public final long position() { |
50113 | 172 |
return position; |
173 |
} |
|
174 |
||
175 |
public final void position(long newPosition) throws IOException { |
|
176 |
if (!currentBlock.contains(newPosition)) { |
|
177 |
if (!previousBlock.contains(newPosition)) { |
|
57360 | 178 |
if (newPosition > size) { |
179 |
throw new EOFException("Trying to read at " + newPosition + ", but file is only " + size + " bytes."); |
|
50113 | 180 |
} |
181 |
long blockStart = trimToFileSize(calculateBlockStart(newPosition)); |
|
182 |
file.seek(blockStart); |
|
183 |
// trim amount to file size |
|
57360 | 184 |
long amount = Math.min(size - blockStart, blockSize); |
50113 | 185 |
previousBlock.read(file, (int) amount); |
186 |
} |
|
187 |
// swap previous and current |
|
188 |
Block tmp = currentBlock; |
|
189 |
currentBlock = previousBlock; |
|
190 |
previousBlock = tmp; |
|
191 |
} |
|
192 |
position = newPosition; |
|
193 |
} |
|
194 |
||
195 |
private final long trimToFileSize(long position) throws IOException { |
|
196 |
return Math.min(size(), Math.max(0, position)); |
|
197 |
} |
|
198 |
||
199 |
private final long calculateBlockStart(long newPosition) { |
|
200 |
// align to end of current block |
|
201 |
if (currentBlock.contains(newPosition - blockSize)) { |
|
202 |
return currentBlock.blockPosition + currentBlock.bytes.length; |
|
203 |
} |
|
204 |
// align before current block |
|
205 |
if (currentBlock.contains(newPosition + blockSize)) { |
|
206 |
return currentBlock.blockPosition - blockSize; |
|
207 |
} |
|
208 |
// not near current block, pick middle |
|
209 |
return newPosition - blockSize / 2; |
|
210 |
} |
|
211 |
||
57360 | 212 |
public final long size() { |
50113 | 213 |
return size; |
214 |
} |
|
215 |
||
216 |
public final void close() throws IOException { |
|
217 |
file.close(); |
|
218 |
} |
|
219 |
||
220 |
@Override |
|
221 |
public final int skipBytes(int n) throws IOException { |
|
222 |
long position = position(); |
|
223 |
position(position + n); |
|
224 |
return (int) (position() - position); |
|
225 |
} |
|
226 |
||
227 |
@Override |
|
228 |
public final boolean readBoolean() throws IOException { |
|
229 |
return readByte() != 0; |
|
230 |
} |
|
231 |
||
232 |
@Override |
|
233 |
public int readUnsignedByte() throws IOException { |
|
234 |
return readByte() & 0x00FF; |
|
235 |
} |
|
236 |
||
237 |
@Override |
|
238 |
public int readUnsignedShort() throws IOException { |
|
239 |
return readShort() & 0xFFFF; |
|
240 |
} |
|
241 |
||
242 |
@Override |
|
243 |
public final String readLine() throws IOException { |
|
244 |
throw new UnsupportedOperationException(); |
|
245 |
} |
|
246 |
||
247 |
// NOTE, this method should really be called readString |
|
248 |
// but can't be renamed without making RecordingInput a |
|
249 |
// public class. |
|
250 |
// |
|
251 |
// This method DOES Not read as expected (s2 + utf8 encoded character) |
|
252 |
// instead it read: |
|
253 |
// byte encoding |
|
254 |
// int size |
|
255 |
// data (byte or char) |
|
256 |
// |
|
257 |
// where encoding |
|
258 |
// |
|
259 |
// 0, means null |
|
260 |
// 1, means UTF8 encoded byte array |
|
261 |
// 2, means char array |
|
262 |
// 3, means latin-1 (ISO-8859-1) encoded byte array |
|
263 |
// 4, means "" |
|
264 |
@Override |
|
265 |
public String readUTF() throws IOException { |
|
57360 | 266 |
throw new UnsupportedOperationException("Use StringParser"); |
50113 | 267 |
} |
268 |
||
269 |
@Override |
|
270 |
public char readChar() throws IOException { |
|
271 |
return (char) readLong(); |
|
272 |
} |
|
273 |
||
274 |
@Override |
|
275 |
public short readShort() throws IOException { |
|
276 |
return (short) readLong(); |
|
277 |
} |
|
278 |
||
279 |
@Override |
|
280 |
public int readInt() throws IOException { |
|
281 |
return (int) readLong(); |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
285 |
public long readLong() throws IOException { |
|
286 |
// can be optimized by branching checks, but will do for now |
|
287 |
byte b0 = readByte(); |
|
288 |
long ret = (b0 & 0x7FL); |
|
289 |
if (b0 >= 0) { |
|
290 |
return ret; |
|
291 |
} |
|
292 |
int b1 = readByte(); |
|
293 |
ret += (b1 & 0x7FL) << 7; |
|
294 |
if (b1 >= 0) { |
|
295 |
return ret; |
|
296 |
} |
|
297 |
int b2 = readByte(); |
|
298 |
ret += (b2 & 0x7FL) << 14; |
|
299 |
if (b2 >= 0) { |
|
300 |
return ret; |
|
301 |
} |
|
302 |
int b3 = readByte(); |
|
303 |
ret += (b3 & 0x7FL) << 21; |
|
304 |
if (b3 >= 0) { |
|
305 |
return ret; |
|
306 |
} |
|
307 |
int b4 = readByte(); |
|
308 |
ret += (b4 & 0x7FL) << 28; |
|
309 |
if (b4 >= 0) { |
|
310 |
return ret; |
|
311 |
} |
|
312 |
int b5 = readByte(); |
|
313 |
ret += (b5 & 0x7FL) << 35; |
|
314 |
if (b5 >= 0) { |
|
315 |
return ret; |
|
316 |
} |
|
317 |
int b6 = readByte(); |
|
318 |
ret += (b6 & 0x7FL) << 42; |
|
319 |
if (b6 >= 0) { |
|
320 |
return ret; |
|
321 |
} |
|
322 |
int b7 = readByte(); |
|
323 |
ret += (b7 & 0x7FL) << 49; |
|
324 |
if (b7 >= 0) { |
|
325 |
return ret; |
|
326 |
} |
|
327 |
int b8 = readByte(); // read last byte raw |
|
328 |
return ret + (((long) (b8 & 0XFF)) << 56); |
|
329 |
} |
|
57360 | 330 |
|
331 |
public void setValidSize(long size) { |
|
332 |
if (size > this.size) { |
|
333 |
this.size = size; |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
public long getFileSize() throws IOException { |
|
338 |
return file.length(); |
|
339 |
} |
|
340 |
||
341 |
public String getFilename() { |
|
342 |
return filename; |
|
343 |
} |
|
344 |
||
57386 | 345 |
// Purpose of this method is to reuse block cache from a |
346 |
// previous RecordingInput |
|
57425 | 347 |
public void setFile(Path path) throws IOException { |
57386 | 348 |
try { |
57425 | 349 |
file.close(); |
57386 | 350 |
} catch (IOException e) { |
351 |
// perhaps deleted |
|
352 |
} |
|
57425 | 353 |
file = null; |
354 |
initialize(path.toFile()); |
|
57386 | 355 |
} |
356 |
||
50113 | 357 |
} |