test/hotspot/jtreg/runtime/cds/appcds/test-classes/ParallelLoad.java
changeset 58260 704e4ff399a2
parent 57567 b000362a89a0
child 58679 9c3209ff7550
equal deleted inserted replaced
58252:14c1ff687621 58260:704e4ff399a2
    23  */
    23  */
    24 
    24 
    25 import java.io.*;
    25 import java.io.*;
    26 import java.net.*;
    26 import java.net.*;
    27 import java.lang.reflect.Field;
    27 import java.lang.reflect.Field;
       
    28 import java.util.concurrent.atomic.AtomicInteger;
    28 
    29 
    29 
    30 
    30 // This test helper is parameterized by:
    31 // This test helper is parameterized by:
    31 // - class transformation mode: property "appcds.parallel.transform.mode"
    32 // - class transformation mode: property "appcds.parallel.transform.mode"
    32 // - class loader test types
    33 // - class loader test types
    76 
    77 
    77         if (customJar != null) {
    78         if (customJar != null) {
    78             if ("FINGERPRINT_MODE".equals(args[1])) {
    79             if ("FINGERPRINT_MODE".equals(args[1])) {
    79                 mode = FINGERPRINT_MODE;
    80                 mode = FINGERPRINT_MODE;
    80                 classLoaders = new ClassLoader[NUM_THREADS];
    81                 classLoaders = new ClassLoader[NUM_THREADS];
    81                 for (int i=0; i<NUM_THREADS; i++) {
    82                 for (int i = 0; i < NUM_THREADS; i++) {
    82                     URL url = new File(customJar).toURI().toURL();
    83                     URL url = new File(customJar).toURI().toURL();
    83                     URL[] urls = new URL[] {url};
    84                     URL[] urls = new URL[] {url};
    84                     classLoaders[i] = new URLClassLoader(urls);
    85                     classLoaders[i] = new URLClassLoader(urls);
    85                 }
    86                 }
    86             } else {
    87             } else {
    91         }
    92         }
    92 
    93 
    93         System.out.println("Start Parallel Load ...");
    94         System.out.println("Start Parallel Load ...");
    94 
    95 
    95         Thread thread[] = new Thread[NUM_THREADS];
    96         Thread thread[] = new Thread[NUM_THREADS];
    96         for (int i=0; i<NUM_THREADS; i++) {
    97         for (int i = 0; i < NUM_THREADS; i++) {
    97             Thread t = new ParallelLoadThread(i);
    98             Thread t = new ParallelLoadThread(i);
    98             t.start();
    99             t.start();
    99             thread[i] = t;
   100             thread[i] = t;
   100         }
   101         }
   101 
   102 
   102         Thread watchdog = new ParallelLoadWatchdog();
   103         Thread watchdog = new ParallelLoadWatchdog();
   103         watchdog.setDaemon(true);
   104         watchdog.setDaemon(true);
   104         watchdog.start();
   105         watchdog.start();
   105 
   106 
   106         for (int i=0; i<NUM_THREADS; i++) {
   107         for (int i = 0; i < NUM_THREADS; i++) {
   107             thread[i].join();
   108             thread[i].join();
   108         }
   109         }
   109         System.out.println("Parallel Load ... done");
   110         System.out.println("Parallel Load ... done");
   110         System.exit(0);
   111         System.exit(0);
   111     }
   112     }
   126     }
   127     }
   127 };
   128 };
   128 
   129 
   129 
   130 
   130 class ParallelLoadThread extends Thread {
   131 class ParallelLoadThread extends Thread {
   131     static int num_ready[] = new int[ParallelLoad.MAX_CLASSES];
   132     static AtomicInteger num_ready[];
   132     static Object lock = new Object();
   133     static {
       
   134         num_ready = new AtomicInteger[ParallelLoad.MAX_CLASSES];
       
   135         for (int i = 0; i < ParallelLoad.MAX_CLASSES; i++) {
       
   136             num_ready[i] = new AtomicInteger();
       
   137         }
       
   138     }
   133     static String transformMode =
   139     static String transformMode =
   134         System.getProperty("appcds.parallel.transform.mode", "none");
   140         System.getProperty("appcds.parallel.transform.mode", "none");
   135 
   141 
   136     int thread_id;
   142     int thread_id;
   137     ParallelLoadThread(int thread_id) {
   143     ParallelLoadThread(int thread_id) {
   151         String msg0 = "ParallelLoadThread: " + String.format(msg, args);
   157         String msg0 = "ParallelLoadThread: " + String.format(msg, args);
   152         System.out.println(msg0);
   158         System.out.println(msg0);
   153     }
   159     }
   154 
   160 
   155     private void run0() throws Throwable {
   161     private void run0() throws Throwable {
   156         for (int i=0; i<ParallelLoad.MAX_CLASSES; i++) {
   162         for (int i = 0;  i < ParallelLoad.MAX_CLASSES; i++) {
   157             synchronized(lock) {
   163             String className = "ParallelClass" + i;
   158                 num_ready[i] ++;
   164             if (transformMode.equals("cflh")) {
   159                 while (num_ready[i] < ParallelLoad.NUM_THREADS) {
   165                 className = "ParallelClassTr" + i;
   160                     lock.wait();
   166             }
       
   167             Class clazz = null;
       
   168 
       
   169             // Spin until every thread is ready to proceed
       
   170             num_ready[i].incrementAndGet();
       
   171             while (num_ready[i].intValue() < ParallelLoad.NUM_THREADS) {
       
   172                 ;
       
   173             }
       
   174 
       
   175             {   // Avoid logging in this block so the threads can proceed without
       
   176                 // waiting for the stdout lock, etc.
       
   177                 switch (ParallelLoad.loaderType) {
       
   178                 case ParallelLoad.SYSTEM_LOADER:
       
   179                     clazz = Class.forName(className);
       
   180                     break;
       
   181                 case ParallelLoad.SINGLE_CUSTOM_LOADER:
       
   182                     clazz = ParallelLoad.classLoaders[0].loadClass(className);
       
   183                     break;
       
   184                 case ParallelLoad.MULTI_CUSTOM_LOADER:
       
   185                     clazz = ParallelLoad.classLoaders[thread_id].loadClass(className);
       
   186                     break;
   161                 }
   187                 }
   162                 lock.notifyAll();
   188                 testTransformation(clazz);
   163             }
   189             }
   164             log("this = %s %d", this, i);
   190 
   165             String className = "ParallelClass" + i;
   191             log("thread[%d] t = %s, c = %s, l = %s", thread_id, this, clazz, clazz.getClassLoader());
   166             if (transformMode.equals("cflh"))
       
   167                 className = "ParallelClassTr" + i;
       
   168 
       
   169             Class clazz = null;
       
   170 
       
   171             switch (ParallelLoad.loaderType) {
       
   172             case ParallelLoad.SYSTEM_LOADER:
       
   173                 clazz = Class.forName(className);
       
   174                 break;
       
   175             case ParallelLoad.SINGLE_CUSTOM_LOADER:
       
   176                 clazz = ParallelLoad.classLoaders[0].loadClass(className);
       
   177                 break;
       
   178             case ParallelLoad.MULTI_CUSTOM_LOADER:
       
   179                 clazz = ParallelLoad.classLoaders[thread_id].loadClass(className);
       
   180                 break;
       
   181             }
       
   182 
       
   183             log("clazz = %s", clazz);
       
   184             testTransformation(clazz);
       
   185         }
   192         }
   186     }
   193     }
   187 
   194 
   188     private void testTransformation(Class c) throws Exception {
   195     private void testTransformation(Class c) throws Exception {
   189         if (transformMode.equals("none"))
   196         if (transformMode.equals("none"))