test/hotspot/jtreg/runtime/jni/FastGetField/FastGetField.java
changeset 57570 d7304cf430f1
equal deleted inserted replaced
57569:be47f3ccdf12 57570:d7304cf430f1
       
     1 /*
       
     2  * Copyright (c) 2019 SAP SE 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 8227680
       
    27  * @summary Tests that all FieldAccess notifications for Get*Field
       
    28             with primitive type are generated.
       
    29  * @compile FastGetField.java
       
    30  * @run main/othervm/native -agentlib:FastGetField -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyJNIFields FastGetField
       
    31  * @run main/othervm/native -agentlib:FastGetField -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyJNIFields -XX:+UnlockDiagnosticVMOptions -XX:+ForceUnreachable -XX:+SafepointALot -XX:GuaranteedSafepointInterval=1 FastGetField
       
    32  */
       
    33 
       
    34 import java.lang.reflect.Field;
       
    35 
       
    36 
       
    37 public class FastGetField {
       
    38 
       
    39     private static final String agentLib = "FastGetField";
       
    40 
       
    41     private native boolean initFieldIDs(Class c);
       
    42     private native boolean initWatchers(Class c);
       
    43     public native long accessFields(MyItem i);
       
    44     public static native long getFieldAccessCount();
       
    45 
       
    46     static final int loop_cnt = 10000;
       
    47 
       
    48 
       
    49     class MyItem {
       
    50         // Names should match JNI types.
       
    51         boolean Z;
       
    52         byte B;
       
    53         short S;
       
    54         char C;
       
    55         int I;
       
    56         long J;
       
    57         float F;
       
    58         double D;
       
    59 
       
    60         public void change_values() {
       
    61             Z = true;
       
    62             B = 1;
       
    63             C = 1;
       
    64             S = 1;
       
    65             I = 1;
       
    66             J = 1l;
       
    67             F = 1.0f;
       
    68             D = 1.0;
       
    69         }
       
    70 
       
    71         public void reset_values() {
       
    72             Z = false;
       
    73             B = 0;
       
    74             C = 0;
       
    75             S = 0;
       
    76             I = 0;
       
    77             J = 0l;
       
    78             F = 0.0f;
       
    79             D = 0.0;
       
    80         }
       
    81     }
       
    82 
       
    83     // Static initialization.
       
    84     static {
       
    85         try {
       
    86             System.loadLibrary(agentLib);
       
    87         } catch (UnsatisfiedLinkError ex) {
       
    88             System.err.println("Failed to load " + agentLib + " lib");
       
    89             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
       
    90             throw ex;
       
    91         }
       
    92     }
       
    93 
       
    94     public void TestFieldAccess() throws Exception {
       
    95         MyItem i = new MyItem();
       
    96         if (!initFieldIDs(MyItem.class)) throw new RuntimeException("FieldID initialization failed!");
       
    97 
       
    98         long duration = System.nanoTime();
       
    99         for (int c = 0; c < loop_cnt; ++c) {
       
   100             if (accessFields(i) != 0l) throw new RuntimeException("Wrong initial result!");
       
   101             i.change_values();
       
   102             if (accessFields(i) != 8l) throw new RuntimeException("Wrong result after changing!");
       
   103             i.reset_values();
       
   104         }
       
   105         duration = System.nanoTime() - duration;
       
   106         System.out.println(loop_cnt + " iterations took " + duration + "ns.");
       
   107 
       
   108         if (getFieldAccessCount() != 0) throw new RuntimeException("Watch not yet active!");
       
   109 
       
   110         // Install watchers.
       
   111         if (!initWatchers(MyItem.class)) throw new RuntimeException("JVMTI missing!");
       
   112 
       
   113         // Try again with watchers.
       
   114         if (accessFields(i) != 0l) throw new RuntimeException("Wrong initial result!");
       
   115         i.change_values();
       
   116         if (accessFields(i) != 8l) throw new RuntimeException("Wrong result after changing!");
       
   117         if (getFieldAccessCount() != 16) throw new RuntimeException("Unexpected event count!");
       
   118     }
       
   119 
       
   120     public static void main(String[] args) throws Exception {
       
   121         FastGetField inst = new FastGetField();
       
   122         inst.TestFieldAccess();
       
   123     }
       
   124 }