1 /*
  2  * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
  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.instrument;
 27 
 28 import java.io.IOException;
 29 
 30 import jdk.jfr.events.Handlers;
 31 import jdk.jfr.internal.handlers.EventHandler;
 32 
 33 /**
 34  * See {@link JITracer} for an explanation of this code.
 35  */
 36 @JIInstrumentationTarget("java.io.RandomAccessFile")
 37 final class RandomAccessFileInstrumentor {
 38 
 39     private RandomAccessFileInstrumentor() {
 40     }
 41 
 42     private String path;
 43 
 44     @SuppressWarnings("deprecation")
 45     @JIInstrumentationMethod
 46     public int read() throws IOException {
 47         EventHandler handler = Handlers.FILE_READ;
 48         if (!handler.isEnabled()) {
 49             return read();
 50         }
 51         int result = 0;
 52         long bytesRead = 0;
 53         boolean endOfFile = false;
 54         long start = 0;
 55         try {
 56             start = EventHandler.timestamp();
 57             result = read();
 58             if (result < 0) {
 59                 endOfFile = true;
 60             } else {
 61                 bytesRead = 1;
 62             }
 63         } finally {
 64             long duration = EventHandler.timestamp() - start;
 65             if (handler.shouldCommit(duration)) {
 66                 handler.write(start, duration, path, bytesRead, endOfFile);
 67             }
 68         }
 69         return result;
 70     }
 71 

























 72     @SuppressWarnings("deprecation")
 73     @JIInstrumentationMethod
 74     public int read(byte b[], int off, int len) throws IOException {
 75         EventHandler handler = Handlers.FILE_READ;
 76         if (!handler.isEnabled()) {
 77             return read(b, off, len);
 78         }
 79         int bytesRead = 0;
 80         long start = 0;
 81         try {
 82             start = EventHandler.timestamp();
 83             bytesRead = read(b, off, len);
 84         } finally {
 85             long duration = EventHandler.timestamp() - start;
 86             if (handler.shouldCommit(duration)) {
 87                 if (bytesRead < 0) {
 88                     handler.write(start, duration, path, 0L, true);
 89                 } else {
 90                     handler.write(start, duration, path, bytesRead, false);
 91                 }
 92             }
 93         }
 94         return bytesRead;
 95     }
 96 
 97     @SuppressWarnings("deprecation")
 98     @JIInstrumentationMethod
 99     public void write(int b) throws IOException {
100         EventHandler handler = Handlers.FILE_WRITE;
101         if (!handler.isEnabled()) {
102             write(b);
103             return;
104         }
105         long bytesWritten = 0;
106         long start = 0;
107         try {
108             start = EventHandler.timestamp();
109             write(b);
110             bytesWritten = 1;
111         } finally {
112             long duration = EventHandler.timestamp() - start;
113             if (handler.shouldCommit(duration)) {
114                 handler.write(start, duration, path, bytesWritten);
115             }
116         }
117     }
118 






















119     @SuppressWarnings("deprecation")
120     @JIInstrumentationMethod
121     public void write(byte b[], int off, int len) throws IOException {
122         EventHandler handler = Handlers.FILE_WRITE;
123         if (!handler.isEnabled()) {
124             write(b, off, len);
125             return;
126         }
127         long bytesWritten = 0;
128         long start = 0;
129         try {
130             start = EventHandler.timestamp();
131             write(b, off, len);
132             bytesWritten = len;
133         } finally {
134             long duration = EventHandler.timestamp() - start;
135             if (handler.shouldCommit(duration)) {
136                 handler.write(start, duration, path, bytesWritten);
137             }
138         }
139     }
140 }
--- EOF ---