test/hotspot/jtreg/runtime/RedefineTests/RedefineRunningMethods.java
changeset 50735 2f2af62dfac7
parent 49066 4aa67aba6c85
child 51337 0bcb90968b3c
equal deleted inserted replaced
50734:0828a0f6676b 50735:2f2af62dfac7
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8055008 8197901
    26  * @bug 8055008 8197901 8010319
    27  * @summary Redefine EMCP and non-EMCP methods that are running in an infinite loop
    27  * @summary Redefine EMCP and non-EMCP methods that are running in an infinite loop
    28  * @library /test/lib
    28  * @library /test/lib
    29  * @modules java.base/jdk.internal.misc
    29  * @modules java.base/jdk.internal.misc
    30  * @modules java.compiler
    30  * @modules java.compiler
    31  *          java.instrument
    31  *          java.instrument
    32  *          jdk.jartool/sun.tools.jar
    32  *          jdk.jartool/sun.tools.jar
    33  * @run main RedefineClassHelper
    33  * @run main RedefineClassHelper
    34  * @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace,all=trace:file=all.log RedefineRunningMethods
    34  * @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace,all=trace:file=all.log RedefineRunningMethods
    35  */
    35  */
    36 // Test is executed with full trace logging redirected to a file to ensure there is no crash during logging anonymous classes - see JDK-8197901
    36 // Test is executed with full trace logging redirected to a file to ensure there is no crash during logging anonymous classes - see JDK-8197901
       
    37 
       
    38 
       
    39 // package access top-level class to avoid problem with RedefineClassHelper
       
    40 // and nested types.
       
    41 class RedefineRunningMethods_B {
       
    42     static int count1 = 0;
       
    43     static int count2 = 0;
       
    44     public static volatile boolean stop = false;
       
    45     static void localSleep() {
       
    46         try{
       
    47             Thread.currentThread().sleep(10);//sleep for 10 ms
       
    48         } catch(InterruptedException ie) {
       
    49         }
       
    50     }
       
    51 
       
    52     public static void infinite() {
       
    53         while (!stop) { count1++; localSleep(); }
       
    54     }
       
    55     public static void infinite_emcp() {
       
    56         while (!stop) { count2++; localSleep(); }
       
    57     }
       
    58 }
       
    59 
    37 public class RedefineRunningMethods {
    60 public class RedefineRunningMethods {
    38 
    61 
    39     public static String newB =
    62     public static String newB =
    40                 "class RedefineRunningMethods$B {" +
    63                 "class RedefineRunningMethods_B {" +
    41                 "   static int count1 = 0;" +
    64                 "   static int count1 = 0;" +
    42                 "   static int count2 = 0;" +
    65                 "   static int count2 = 0;" +
    43                 "   public static volatile boolean stop = false;" +
    66                 "   public static volatile boolean stop = false;" +
    44                 "  static void localSleep() { " +
    67                 "  static void localSleep() { " +
    45                 "    try{ " +
    68                 "    try{ " +
    54                 "       while (!stop) { count2++; localSleep(); }" +
    77                 "       while (!stop) { count2++; localSleep(); }" +
    55                 "   }" +
    78                 "   }" +
    56                 "}";
    79                 "}";
    57 
    80 
    58     public static String evenNewerB =
    81     public static String evenNewerB =
    59                 "class RedefineRunningMethods$B {" +
    82                 "class RedefineRunningMethods_B {" +
    60                 "   static int count1 = 0;" +
    83                 "   static int count1 = 0;" +
    61                 "   static int count2 = 0;" +
    84                 "   static int count2 = 0;" +
    62                 "   public static volatile boolean stop = false;" +
    85                 "   public static volatile boolean stop = false;" +
    63                 "  static void localSleep() { " +
    86                 "  static void localSleep() { " +
    64                 "    try{ " +
    87                 "    try{ " +
    70                 "   public static void infinite_emcp() { " +
    93                 "   public static void infinite_emcp() { " +
    71                 "       System.out.println(\"infinite_emcp now obsolete called\");" +
    94                 "       System.out.println(\"infinite_emcp now obsolete called\");" +
    72                 "   }" +
    95                 "   }" +
    73                 "}";
    96                 "}";
    74 
    97 
    75     static class B {
       
    76         static int count1 = 0;
       
    77         static int count2 = 0;
       
    78         public static volatile boolean stop = false;
       
    79         static void localSleep() {
       
    80           try{
       
    81             Thread.currentThread().sleep(10);//sleep for 10 ms
       
    82           } catch(InterruptedException ie) {
       
    83           }
       
    84         }
       
    85 
       
    86         public static void infinite() {
       
    87             while (!stop) { count1++; localSleep(); }
       
    88         }
       
    89         public static void infinite_emcp() {
       
    90             while (!stop) { count2++; localSleep(); }
       
    91         }
       
    92     }
       
    93 
       
    94 
    98 
    95     public static void main(String[] args) throws Exception {
    99     public static void main(String[] args) throws Exception {
    96 
   100 
    97         new Thread() {
   101         new Thread() {
    98             public void run() {
   102             public void run() {
    99                 B.infinite();
   103                 RedefineRunningMethods_B.infinite();
   100             }
   104             }
   101         }.start();
   105         }.start();
   102 
   106 
   103         new Thread() {
   107         new Thread() {
   104             public void run() {
   108             public void run() {
   105                 B.infinite_emcp();
   109                 RedefineRunningMethods_B.infinite_emcp();
   106             }
   110             }
   107         }.start();
   111         }.start();
   108 
   112 
   109         RedefineClassHelper.redefineClass(B.class, newB);
   113         RedefineClassHelper.redefineClass(RedefineRunningMethods_B.class, newB);
   110 
   114 
   111         System.gc();
   115         System.gc();
   112 
   116 
   113         B.infinite();
   117         RedefineRunningMethods_B.infinite();
   114 
   118 
   115         // Start a thread with the second version of infinite_emcp running
   119         // Start a thread with the second version of infinite_emcp running
   116         new Thread() {
   120         new Thread() {
   117             public void run() {
   121             public void run() {
   118                 B.infinite_emcp();
   122                 RedefineRunningMethods_B.infinite_emcp();
   119             }
   123             }
   120         }.start();
   124         }.start();
   121 
   125 
   122         for (int i = 0; i < 20 ; i++) {
   126         for (int i = 0; i < 20 ; i++) {
   123             String s = new String("some garbage");
   127             String s = new String("some garbage");
   124             System.gc();
   128             System.gc();
   125         }
   129         }
   126 
   130 
   127         RedefineClassHelper.redefineClass(B.class, evenNewerB);
   131         RedefineClassHelper.redefineClass(RedefineRunningMethods_B.class, evenNewerB);
   128         System.gc();
   132         System.gc();
   129 
   133 
   130         for (int i = 0; i < 20 ; i++) {
   134         for (int i = 0; i < 20 ; i++) {
   131             B.infinite();
   135             RedefineRunningMethods_B.infinite();
   132             String s = new String("some garbage");
   136             String s = new String("some garbage");
   133             System.gc();
   137             System.gc();
   134         }
   138         }
   135 
   139 
   136         B.infinite_emcp();
   140         RedefineRunningMethods_B.infinite_emcp();
   137 
   141 
   138         // purge should clean everything up.
   142         // purge should clean everything up.
   139         B.stop = true;
   143         RedefineRunningMethods_B.stop = true;
   140 
   144 
   141         for (int i = 0; i < 20 ; i++) {
   145         for (int i = 0; i < 20 ; i++) {
   142             B.infinite();
   146             RedefineRunningMethods_B.infinite();
   143             String s = new String("some garbage");
   147             String s = new String("some garbage");
   144             System.gc();
   148             System.gc();
   145         }
   149         }
   146     }
   150     }
   147 }
   151 }