1 /*
   2  * Copyright (c) 2013, 2019, 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 import java.nio.ByteBuffer;
  30 
  31 import jdk.jfr.events.FileForceEvent;
  32 import jdk.jfr.events.FileReadEvent;
  33 import jdk.jfr.events.FileWriteEvent;
  34 
  35 /**
  36  * See {@link JITracer} for an explanation of this code.
  37  */
  38 @JIInstrumentationTarget("sun.nio.ch.FileChannelImpl")
  39 final class FileChannelImplInstrumentor {
  40 
  41     private FileChannelImplInstrumentor() {
  42     }
  43 
  44     private String path;
  45 
  46     @SuppressWarnings("deprecation")
  47     @JIInstrumentationMethod
  48     public void force(boolean metaData) throws IOException {
  49         FileForceEvent event = FileForceEvent.EVENT.get();
  50         if (!event.isEnabled()) {
  51             force(metaData);
  52             return;
  53         }
  54         try {
  55             event.begin();
  56             force(metaData);
  57         } finally {
  58             event.path = path;
  59             event.metaData = metaData;
  60             event.commit();
  61             event.reset();
  62         }
  63     }
  64 
  65     @SuppressWarnings("deprecation")
  66     @JIInstrumentationMethod
  67     public int read(ByteBuffer dst) throws IOException {
  68         FileReadEvent event = FileReadEvent.EVENT.get();
  69         if (!event.isEnabled()) {
  70             return read(dst);
  71         }
  72         int bytesRead = 0;
  73         try {
  74             event.begin();
  75             bytesRead = read(dst);
  76         } finally {
  77             if (bytesRead < 0) {
  78                 event.endOfFile = true;
  79             } else {
  80                 event.bytesRead = bytesRead;
  81             }
  82             event.path = path;
  83             event.commit();
  84             event.reset();
  85         }
  86         return bytesRead;
  87     }
  88 
  89     @SuppressWarnings("deprecation")
  90     @JIInstrumentationMethod
  91     public int read(ByteBuffer dst, long position) throws IOException {
  92         FileReadEvent event = FileReadEvent.EVENT.get();
  93         if (!event.isEnabled()) {
  94             return read(dst, position);
  95         }
  96         int bytesRead = 0;
  97         try {
  98             event.begin();
  99             bytesRead = read(dst, position);
 100         } finally {
 101             if (bytesRead < 0) {
 102                 event.endOfFile = true;
 103             } else {
 104                 event.bytesRead = bytesRead;
 105             }
 106             event.path = path;
 107             event.commit();
 108             event.reset();
 109         }
 110         return bytesRead;
 111     }
 112 
 113     @SuppressWarnings("deprecation")
 114     @JIInstrumentationMethod
 115     public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
 116         FileReadEvent event = FileReadEvent.EVENT.get();
 117         if (!event.isEnabled()) {
 118             return read(dsts, offset, length);
 119         }
 120         long bytesRead = 0;
 121         try {
 122             event.begin();
 123             bytesRead = read(dsts, offset, length);
 124         } finally {
 125             if (bytesRead < 0) {
 126                 event.endOfFile = true;
 127             } else {
 128                 event.bytesRead = bytesRead;
 129             }
 130             event.path = path;
 131             event.commit();
 132             event.reset();
 133         }
 134         return bytesRead;
 135     }
 136 
 137     @SuppressWarnings("deprecation")
 138     @JIInstrumentationMethod
 139     public int write(ByteBuffer src) throws IOException {
 140         FileWriteEvent event = FileWriteEvent.EVENT.get();
 141         if (!event.isEnabled()) {
 142             return write(src);
 143         }
 144         int bytesWritten = 0;
 145         try {
 146             event.begin();
 147             bytesWritten = write(src);
 148         } finally {
 149             event.bytesWritten = bytesWritten > 0 ? bytesWritten : 0;
 150             event.path = path;
 151             event.commit();
 152             event.reset();
 153         }
 154         return bytesWritten;
 155     }
 156 
 157     @SuppressWarnings("deprecation")
 158     @JIInstrumentationMethod
 159     public int write(ByteBuffer src, long position) throws IOException {
 160         FileWriteEvent event = FileWriteEvent.EVENT.get();
 161         if (!event.isEnabled()) {
 162             return write(src, position);
 163         }
 164 
 165         int bytesWritten = 0;
 166         try {
 167             event.begin();
 168             bytesWritten = write(src, position);
 169         } finally {
 170             event.bytesWritten = bytesWritten > 0 ? bytesWritten : 0;
 171             event.path = path;
 172             event.commit();
 173             event.reset();
 174         }
 175         return bytesWritten;
 176     }
 177 
 178     @SuppressWarnings("deprecation")
 179     @JIInstrumentationMethod
 180     public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
 181         FileWriteEvent event = FileWriteEvent.EVENT.get();
 182         if (!event.isEnabled()) {
 183             return write(srcs, offset, length);
 184         }
 185         long bytesWritten = 0;
 186         try {
 187             event.begin();
 188             bytesWritten = write(srcs, offset, length);
 189         } finally {
 190             event.bytesWritten = bytesWritten > 0 ? bytesWritten : 0;
 191             event.path = path;
 192             event.commit();
 193             event.reset();
 194         }
 195         return bytesWritten;
 196     }
 197 }