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[]) throws IOException {
 75         EventHandler handler = Handlers.FILE_READ;
 76         if (!handler.isEnabled()) {
 77             return read(b);
 78         }
 79         int bytesRead = 0;
 80         long start = 0;
 81         try {
 82             start = EventHandler.timestamp();
 83             bytesRead = read(b);
 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 int read(byte b[], int off, int len) throws IOException {
100         EventHandler handler = Handlers.FILE_READ;
101         if (!handler.isEnabled()) {
102             return read(b, off, len);
103         }
104         int bytesRead = 0;
105         long start = 0;
106         try {
107             start = EventHandler.timestamp();
108             bytesRead = read(b, off, len);
109         } finally {
110             long duration = EventHandler.timestamp() - start;
111             if (handler.shouldCommit(duration)) {
112                 if (bytesRead < 0) {
113                     handler.write(start, duration, path, 0L, true);
114                 } else {
115                     handler.write(start, duration, path, bytesRead, false);
116                 }
117             }
118         }
119         return bytesRead;
120     }
121 
122     @SuppressWarnings("deprecation")
123     @JIInstrumentationMethod
124     public void write(int b) throws IOException {
125         EventHandler handler = Handlers.FILE_WRITE;
126         if (!handler.isEnabled()) {
127             write(b);
128             return;
129         }
130         long bytesWritten = 0;
131         long start = 0;
132         try {
133             start = EventHandler.timestamp();
134             write(b);
135             bytesWritten = 1;
136         } finally {
137             long duration = EventHandler.timestamp() - start;
138             if (handler.shouldCommit(duration)) {
139                 handler.write(start, duration, path, bytesWritten);
140             }
141         }
142     }
143 
144     @SuppressWarnings("deprecation")
145     @JIInstrumentationMethod
146     public void write(byte b[]) throws IOException {
147         EventHandler handler = Handlers.FILE_WRITE;
148         if (!handler.isEnabled()) {
149             write(b);
150             return;
151         }
152         long bytesWritten = 0;
153         long start = 0;
154         try {
155             start = EventHandler.timestamp();
156             write(b);
157             bytesWritten = b.length;
158         } finally {
159             long duration = EventHandler.timestamp();
160             if (handler.shouldCommit(duration)) {
161                 handler.write(start, duration, path, bytesWritten);
162             }
163         }
164     }
165 
166     @SuppressWarnings("deprecation")
167     @JIInstrumentationMethod
168     public void write(byte b[], int off, int len) throws IOException {
169         EventHandler handler = Handlers.FILE_WRITE;
170         if (!handler.isEnabled()) {
171             write(b, off, len);
172             return;
173         }
174         long bytesWritten = 0;
175         long start = 0;
176         try {
177             start = EventHandler.timestamp();
178             write(b, off, len);
179             bytesWritten = len;
180         } finally {
181             long duration = EventHandler.timestamp() - start;
182             if (handler.shouldCommit(duration)) {
183                 handler.write(start, duration, path, bytesWritten);
184             }
185         }
186     }
187 }
--- EOF ---