60
60
import jdk .internal .misc .VM .BufferPool ;
61
61
import jdk .internal .ref .Cleaner ;
62
62
import jdk .internal .ref .CleanerFactory ;
63
-
63
+ import jdk .internal .event .FileReadEvent ;
64
+ import jdk .internal .event .FileWriteEvent ;
64
65
import jdk .internal .access .foreign .UnmapperProxy ;
65
66
66
67
public class FileChannelImpl
@@ -73,6 +74,10 @@ public class FileChannelImpl
73
74
// Used to make native read and write calls
74
75
private static final FileDispatcher nd = new FileDispatcherImpl ();
75
76
77
+ // Flag set by jdk.internal.event.JFRTracing to indicate if
78
+ // file reads and writes should be traced by JFR.
79
+ private static boolean jfrTracing ;
80
+
76
81
// File descriptor
77
82
private final FileDescriptor fd ;
78
83
@@ -220,6 +225,13 @@ protected void implCloseChannel() throws IOException {
220
225
221
226
@ Override
222
227
public int read (ByteBuffer dst ) throws IOException {
228
+ if (jfrTracing && FileReadEvent .enabled ()) {
229
+ return traceImplRead (dst );
230
+ }
231
+ return implRead (dst );
232
+ }
233
+
234
+ private int implRead (ByteBuffer dst ) throws IOException {
223
235
ensureOpen ();
224
236
if (!readable )
225
237
throw new NonReadableChannelException ();
@@ -250,8 +262,34 @@ public int read(ByteBuffer dst) throws IOException {
250
262
}
251
263
}
252
264
265
+ private int traceImplRead (ByteBuffer dst ) throws IOException {
266
+ int bytesRead = 0 ;
267
+ long start = 0 ;
268
+ try {
269
+ start = FileReadEvent .timestamp ();
270
+ bytesRead = implRead (dst );
271
+ } finally {
272
+ long duration = FileReadEvent .timestamp () - start ;
273
+ if (FileReadEvent .shouldCommit (duration )) {
274
+ if (bytesRead < 0 ) {
275
+ FileReadEvent .commit (start , duration , path , 0L , true );
276
+ } else {
277
+ FileReadEvent .commit (start , duration , path , bytesRead , false );
278
+ }
279
+ }
280
+ }
281
+ return bytesRead ;
282
+ }
283
+
253
284
@ Override
254
- public long read (ByteBuffer [] dsts , int offset , int length )
285
+ public long read (ByteBuffer [] dsts , int offset , int length ) throws IOException {
286
+ if (jfrTracing && FileReadEvent .enabled ()) {
287
+ return traceImplRead (dsts , offset , length );
288
+ }
289
+ return implRead (dsts , offset , length );
290
+ }
291
+
292
+ private long implRead (ByteBuffer [] dsts , int offset , int length )
255
293
throws IOException
256
294
{
257
295
Objects .checkFromIndexSize (offset , length , dsts .length );
@@ -286,8 +324,34 @@ public long read(ByteBuffer[] dsts, int offset, int length)
286
324
}
287
325
}
288
326
327
+ private long traceImplRead (ByteBuffer [] dsts , int offset , int length ) throws IOException {
328
+ long bytesRead = 0 ;
329
+ long start = 0 ;
330
+ try {
331
+ start = FileReadEvent .timestamp ();
332
+ bytesRead = implRead (dsts , offset , length );
333
+ } finally {
334
+ long duration = FileReadEvent .timestamp () - start ;
335
+ if (FileReadEvent .shouldCommit (duration )) {
336
+ if (bytesRead < 0 ) {
337
+ FileReadEvent .commit (start , duration , path , 0L , true );
338
+ } else {
339
+ FileReadEvent .commit (start , duration , path , bytesRead , false );
340
+ }
341
+ }
342
+ }
343
+ return bytesRead ;
344
+ }
345
+
289
346
@ Override
290
347
public int write (ByteBuffer src ) throws IOException {
348
+ if (jfrTracing && FileWriteEvent .enabled ()) {
349
+ return traceImplWrite (src );
350
+ }
351
+ return implWrite (src );
352
+ }
353
+
354
+ private int implWrite (ByteBuffer src ) throws IOException {
291
355
ensureOpen ();
292
356
if (!writable )
293
357
throw new NonWritableChannelException ();
@@ -319,10 +383,31 @@ public int write(ByteBuffer src) throws IOException {
319
383
}
320
384
}
321
385
386
+ private int traceImplWrite (ByteBuffer src ) throws IOException {
387
+ int bytesWritten = 0 ;
388
+ long start = 0 ;
389
+ try {
390
+ start = FileWriteEvent .timestamp ();
391
+ bytesWritten = implWrite (src );
392
+ } finally {
393
+ long duration = FileWriteEvent .timestamp () - start ;
394
+ if (FileWriteEvent .shouldCommit (duration )) {
395
+ long bytes = bytesWritten > 0 ? bytesWritten : 0 ;
396
+ FileWriteEvent .commit (start , duration , path , bytes );
397
+ }
398
+ }
399
+ return bytesWritten ;
400
+ }
401
+
322
402
@ Override
323
- public long write (ByteBuffer [] srcs , int offset , int length )
324
- throws IOException
325
- {
403
+ public long write (ByteBuffer [] srcs , int offset , int length ) throws IOException {
404
+ if (jfrTracing && FileWriteEvent .enabled ()) {
405
+ return traceImplWrite (srcs , offset , length );
406
+ }
407
+ return implWrite (srcs , offset , length );
408
+ }
409
+
410
+ private long implWrite (ByteBuffer [] srcs , int offset , int length ) throws IOException {
326
411
Objects .checkFromIndexSize (offset , length , srcs .length );
327
412
ensureOpen ();
328
413
if (!writable )
@@ -354,6 +439,21 @@ public long write(ByteBuffer[] srcs, int offset, int length)
354
439
}
355
440
}
356
441
442
+ private long traceImplWrite (ByteBuffer [] srcs , int offset , int length ) throws IOException {
443
+ long bytesWritten = 0 ;
444
+ long start = 0 ;
445
+ try {
446
+ start = FileWriteEvent .timestamp ();
447
+ bytesWritten = implWrite (srcs , offset , length );
448
+ } finally {
449
+ long duration = FileWriteEvent .timestamp () - start ;
450
+ if (FileWriteEvent .shouldCommit (duration )) {
451
+ long bytes = bytesWritten > 0 ? bytesWritten : 0 ;
452
+ FileWriteEvent .commit (start , duration , path , bytes );
453
+ }
454
+ }
455
+ return bytesWritten ;
456
+ }
357
457
// -- Other operations --
358
458
359
459
@ Override
@@ -1028,6 +1128,13 @@ public long transferFrom(ReadableByteChannel src, long position, long count)
1028
1128
1029
1129
@ Override
1030
1130
public int read (ByteBuffer dst , long position ) throws IOException {
1131
+ if (jfrTracing && FileReadEvent .enabled ()) {
1132
+ return traceImplRead (dst , position );
1133
+ }
1134
+ return implRead (dst , position );
1135
+ }
1136
+
1137
+ private int implRead (ByteBuffer dst , long position ) throws IOException {
1031
1138
if (dst == null )
1032
1139
throw new NullPointerException ();
1033
1140
if (position < 0 )
@@ -1046,6 +1153,25 @@ public int read(ByteBuffer dst, long position) throws IOException {
1046
1153
}
1047
1154
}
1048
1155
1156
+ private int traceImplRead (ByteBuffer dst , long position ) throws IOException {
1157
+ int bytesRead = 0 ;
1158
+ long start = 0 ;
1159
+ try {
1160
+ start = FileReadEvent .timestamp ();
1161
+ bytesRead = implRead (dst , position );
1162
+ } finally {
1163
+ long duration = FileReadEvent .timestamp () - start ;
1164
+ if (FileReadEvent .shouldCommit (duration )) {
1165
+ if (bytesRead < 0 ) {
1166
+ FileReadEvent .commit (start , duration , path , 0L , true );
1167
+ } else {
1168
+ FileReadEvent .commit (start , duration , path , bytesRead , false );
1169
+ }
1170
+ }
1171
+ }
1172
+ return bytesRead ;
1173
+ }
1174
+
1049
1175
private int readInternal (ByteBuffer dst , long position ) throws IOException {
1050
1176
assert !nd .needsPositionLock () || Thread .holdsLock (positionLock );
1051
1177
int n = 0 ;
@@ -1074,6 +1200,13 @@ private int readInternal(ByteBuffer dst, long position) throws IOException {
1074
1200
1075
1201
@ Override
1076
1202
public int write (ByteBuffer src , long position ) throws IOException {
1203
+ if (jfrTracing && FileReadEvent .enabled ()) {
1204
+ return traceImplWrite (src , position );
1205
+ }
1206
+ return implWrite (src , position );
1207
+ }
1208
+
1209
+ private int implWrite (ByteBuffer src , long position ) throws IOException {
1077
1210
if (src == null )
1078
1211
throw new NullPointerException ();
1079
1212
if (position < 0 )
@@ -1092,6 +1225,22 @@ public int write(ByteBuffer src, long position) throws IOException {
1092
1225
}
1093
1226
}
1094
1227
1228
+ private int traceImplWrite (ByteBuffer src , long position ) throws IOException {
1229
+ int bytesWritten = 0 ;
1230
+ long start = 0 ;
1231
+ try {
1232
+ start = FileWriteEvent .timestamp ();
1233
+ bytesWritten = implWrite (src , position );
1234
+ } finally {
1235
+ long duration = FileWriteEvent .timestamp () - start ;
1236
+ if (FileWriteEvent .shouldCommit (duration )) {
1237
+ long bytes = bytesWritten > 0 ? bytesWritten : 0 ;
1238
+ FileWriteEvent .commit (start , duration , path , bytes );
1239
+ }
1240
+ }
1241
+ return bytesWritten ;
1242
+ }
1243
+
1095
1244
private int writeInternal (ByteBuffer src , long position ) throws IOException {
1096
1245
assert !nd .needsPositionLock () || Thread .holdsLock (positionLock );
1097
1246
int n = 0 ;
0 commit comments