< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/CompileTheWorld.java

Print this page




  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.lang.annotation.Annotation;
  38 import java.lang.reflect.Constructor;
  39 import java.lang.reflect.Method;
  40 import java.lang.reflect.Modifier;
  41 import java.net.URI;
  42 import java.net.URL;
  43 import java.net.URLClassLoader;
  44 import java.nio.file.FileSystem;
  45 import java.nio.file.FileSystems;
  46 import java.nio.file.FileVisitResult;
  47 import java.nio.file.Files;
  48 import java.nio.file.Path;
  49 import java.nio.file.SimpleFileVisitor;
  50 import java.nio.file.attribute.BasicFileAttributes;
  51 import java.util.ArrayList;
  52 import java.util.Arrays;
  53 import java.util.Collections;
  54 import java.util.Enumeration;

  55 import java.util.HashSet;
  56 import java.util.List;

  57 import java.util.ServiceLoader;
  58 import java.util.Set;
  59 import java.util.concurrent.ExecutionException;
  60 import java.util.concurrent.Future;
  61 import java.util.concurrent.LinkedBlockingQueue;
  62 import java.util.concurrent.ThreadPoolExecutor;
  63 import java.util.concurrent.TimeUnit;
  64 import java.util.concurrent.atomic.AtomicLong;
  65 import java.util.jar.JarEntry;
  66 import java.util.jar.JarFile;
  67 import java.util.stream.Collectors;
  68 
  69 import org.graalvm.compiler.api.replacements.Snippet;
  70 import org.graalvm.compiler.bytecode.Bytecodes;
  71 import org.graalvm.compiler.core.CompilerThreadFactory;
  72 import org.graalvm.compiler.core.test.ReflectionOptionDescriptors;
  73 import org.graalvm.compiler.debug.DebugOptions;
  74 import org.graalvm.compiler.debug.GraalError;
  75 import org.graalvm.compiler.debug.MethodFilter;
  76 import org.graalvm.compiler.debug.TTY;


 458     private boolean isClassIncluded(String className) {
 459         if (methodFilters != null && !MethodFilter.matchesClassName(methodFilters, className)) {
 460             return false;
 461         }
 462         if (excludeMethodFilters != null && MethodFilter.matchesClassName(excludeMethodFilters, className)) {
 463             return false;
 464         }
 465         return true;
 466     }
 467 
 468     /**
 469      * Compiles all methods in all classes in a given class path.
 470      *
 471      * @param classPath class path denoting classes to compile
 472      * @throws IOException
 473      */
 474     @SuppressWarnings("try")
 475     private void compile(String classPath) throws IOException {
 476         final String[] entries = classPath.split(File.pathSeparator);
 477         long start = System.currentTimeMillis();

 478 
 479         try {
 480             // compile dummy method to get compiler initialized outside of the
 481             // config debug override.
 482             HotSpotResolvedJavaMethod dummyMethod = (HotSpotResolvedJavaMethod) JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaMethod(
 483                             CompileTheWorld.class.getDeclaredMethod("dummy"));
 484             int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
 485             boolean useProfilingInfo = false;
 486             boolean installAsDefault = false;
 487             CompilationTask task = new CompilationTask(jvmciRuntime, compiler, new HotSpotCompilationRequest(dummyMethod, entryBCI, 0L), useProfilingInfo, installAsDefault, currentOptions);
 488             task.runCompilation();
 489         } catch (NoSuchMethodException | SecurityException e1) {
 490             printStackTrace(e1);
 491         }
 492 
 493         /*
 494          * Always use a thread pool, even for single threaded mode since it simplifies the use of
 495          * DebugValueThreadFilter to filter on the thread names.
 496          */
 497         int threadCount = 1;


 532                     String include = Arrays.asList(methodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", "));
 533                     println("CompileTheWorld : Compiling all methods in " + entry + " matching one of the following filters: " + include);
 534                 }
 535                 if (excludeMethodFilters != null && excludeMethodFilters.length > 0) {
 536                     String exclude = Arrays.asList(excludeMethodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", "));
 537                     println("CompileTheWorld : Excluding all methods matching one of the following filters: " + exclude);
 538                 }
 539                 println();
 540 
 541                 ClassLoader loader = cpe.createClassLoader();
 542 
 543                 for (String className : cpe.getClassNames()) {
 544 
 545                     // Are we done?
 546                     if (classFileCounter >= stopAt) {
 547                         break;
 548                     }
 549 
 550                     classFileCounter++;
 551 
 552                     if (className.startsWith("jdk.management.") || className.startsWith("jdk.internal.cmm.*")) {






 553                         continue;
 554                     }
 555 
 556                     try {
 557                         // Load and initialize class
 558                         Class<?> javaClass = Class.forName(className, true, loader);
 559 
 560                         // Pre-load all classes in the constant pool.
 561                         try {
 562                             HotSpotResolvedObjectType objectType = HotSpotResolvedObjectType.fromObjectClass(javaClass);
 563                             ConstantPool constantPool = objectType.getConstantPool();
 564                             for (int cpi = 1; cpi < constantPool.length(); cpi++) {
 565                                 constantPool.loadReferencedType(cpi, Bytecodes.LDC);
 566                             }
 567                         } catch (Throwable t) {
 568                             // If something went wrong during pre-loading we just ignore it.
 569                             if (isClassIncluded(className)) {
 570                                 println("Preloading failed for (%d) %s: %s", classFileCounter, className, t);
 571                             }
 572                             continue;


 625         while (threadPool.getCompletedTaskCount() != threadPool.getTaskCount()) {
 626             if (wakeups % 15 == 0) {
 627                 TTY.println("CompileTheWorld : Waiting for " + (threadPool.getTaskCount() - threadPool.getCompletedTaskCount()) + " compiles");
 628             }
 629             try {
 630                 threadPool.awaitTermination(1, TimeUnit.SECONDS);
 631                 wakeups++;
 632             } catch (InterruptedException e) {
 633             }
 634         }
 635         threadPool = null;
 636 
 637         long elapsedTime = System.currentTimeMillis() - start;
 638 
 639         println();
 640         if (Options.MultiThreaded.getValue(currentOptions)) {
 641             TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms elapsed, %d ms compile time, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), elapsedTime,
 642                             compileTime.get(), memoryUsed.get());
 643         } else {
 644             TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), compileTime.get(), memoryUsed.get());



























 645         }
 646     }
 647 
 648     private synchronized void startThreads() {
 649         running = true;
 650         // Wake up any waiting threads
 651         notifyAll();
 652     }
 653 
 654     private synchronized void waitToRun() {
 655         while (!running) {
 656             try {
 657                 wait();
 658             } catch (InterruptedException e) {
 659             }
 660         }
 661     }
 662 
 663     @SuppressWarnings("try")
 664     private void compileMethod(HotSpotResolvedJavaMethod method) throws InterruptedException, ExecutionException {




  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.lang.annotation.Annotation;
  38 import java.lang.reflect.Constructor;
  39 import java.lang.reflect.Method;
  40 import java.lang.reflect.Modifier;
  41 import java.net.URI;
  42 import java.net.URL;
  43 import java.net.URLClassLoader;
  44 import java.nio.file.FileSystem;
  45 import java.nio.file.FileSystems;
  46 import java.nio.file.FileVisitResult;
  47 import java.nio.file.Files;
  48 import java.nio.file.Path;
  49 import java.nio.file.SimpleFileVisitor;
  50 import java.nio.file.attribute.BasicFileAttributes;
  51 import java.util.ArrayList;
  52 import java.util.Arrays;
  53 import java.util.Collections;
  54 import java.util.Enumeration;
  55 import java.util.HashMap;
  56 import java.util.HashSet;
  57 import java.util.List;
  58 import java.util.Map;
  59 import java.util.ServiceLoader;
  60 import java.util.Set;
  61 import java.util.concurrent.ExecutionException;
  62 import java.util.concurrent.Future;
  63 import java.util.concurrent.LinkedBlockingQueue;
  64 import java.util.concurrent.ThreadPoolExecutor;
  65 import java.util.concurrent.TimeUnit;
  66 import java.util.concurrent.atomic.AtomicLong;
  67 import java.util.jar.JarEntry;
  68 import java.util.jar.JarFile;
  69 import java.util.stream.Collectors;
  70 
  71 import org.graalvm.compiler.api.replacements.Snippet;
  72 import org.graalvm.compiler.bytecode.Bytecodes;
  73 import org.graalvm.compiler.core.CompilerThreadFactory;
  74 import org.graalvm.compiler.core.test.ReflectionOptionDescriptors;
  75 import org.graalvm.compiler.debug.DebugOptions;
  76 import org.graalvm.compiler.debug.GraalError;
  77 import org.graalvm.compiler.debug.MethodFilter;
  78 import org.graalvm.compiler.debug.TTY;


 460     private boolean isClassIncluded(String className) {
 461         if (methodFilters != null && !MethodFilter.matchesClassName(methodFilters, className)) {
 462             return false;
 463         }
 464         if (excludeMethodFilters != null && MethodFilter.matchesClassName(excludeMethodFilters, className)) {
 465             return false;
 466         }
 467         return true;
 468     }
 469 
 470     /**
 471      * Compiles all methods in all classes in a given class path.
 472      *
 473      * @param classPath class path denoting classes to compile
 474      * @throws IOException
 475      */
 476     @SuppressWarnings("try")
 477     private void compile(String classPath) throws IOException {
 478         final String[] entries = classPath.split(File.pathSeparator);
 479         long start = System.currentTimeMillis();
 480         Map<Thread, StackTraceElement[]> initialThreads = Thread.getAllStackTraces();
 481 
 482         try {
 483             // compile dummy method to get compiler initialized outside of the
 484             // config debug override.
 485             HotSpotResolvedJavaMethod dummyMethod = (HotSpotResolvedJavaMethod) JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaMethod(
 486                             CompileTheWorld.class.getDeclaredMethod("dummy"));
 487             int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
 488             boolean useProfilingInfo = false;
 489             boolean installAsDefault = false;
 490             CompilationTask task = new CompilationTask(jvmciRuntime, compiler, new HotSpotCompilationRequest(dummyMethod, entryBCI, 0L), useProfilingInfo, installAsDefault, currentOptions);
 491             task.runCompilation();
 492         } catch (NoSuchMethodException | SecurityException e1) {
 493             printStackTrace(e1);
 494         }
 495 
 496         /*
 497          * Always use a thread pool, even for single threaded mode since it simplifies the use of
 498          * DebugValueThreadFilter to filter on the thread names.
 499          */
 500         int threadCount = 1;


 535                     String include = Arrays.asList(methodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", "));
 536                     println("CompileTheWorld : Compiling all methods in " + entry + " matching one of the following filters: " + include);
 537                 }
 538                 if (excludeMethodFilters != null && excludeMethodFilters.length > 0) {
 539                     String exclude = Arrays.asList(excludeMethodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", "));
 540                     println("CompileTheWorld : Excluding all methods matching one of the following filters: " + exclude);
 541                 }
 542                 println();
 543 
 544                 ClassLoader loader = cpe.createClassLoader();
 545 
 546                 for (String className : cpe.getClassNames()) {
 547 
 548                     // Are we done?
 549                     if (classFileCounter >= stopAt) {
 550                         break;
 551                     }
 552 
 553                     classFileCounter++;
 554 
 555                     if (className.startsWith("jdk.management.") ||
 556                                     className.startsWith("jdk.internal.cmm.*") ||
 557                                     // GR-5881: The class initializer for
 558                                     // sun.tools.jconsole.OutputViewer
 559                                     // spawns non-daemon threads for redirecting sysout and syserr.
 560                                     // These threads tend to cause deadlock at VM exit
 561                                     className.startsWith("sun.tools.jconsole.")) {
 562                         continue;
 563                     }
 564 
 565                     try {
 566                         // Load and initialize class
 567                         Class<?> javaClass = Class.forName(className, true, loader);
 568 
 569                         // Pre-load all classes in the constant pool.
 570                         try {
 571                             HotSpotResolvedObjectType objectType = HotSpotResolvedObjectType.fromObjectClass(javaClass);
 572                             ConstantPool constantPool = objectType.getConstantPool();
 573                             for (int cpi = 1; cpi < constantPool.length(); cpi++) {
 574                                 constantPool.loadReferencedType(cpi, Bytecodes.LDC);
 575                             }
 576                         } catch (Throwable t) {
 577                             // If something went wrong during pre-loading we just ignore it.
 578                             if (isClassIncluded(className)) {
 579                                 println("Preloading failed for (%d) %s: %s", classFileCounter, className, t);
 580                             }
 581                             continue;


 634         while (threadPool.getCompletedTaskCount() != threadPool.getTaskCount()) {
 635             if (wakeups % 15 == 0) {
 636                 TTY.println("CompileTheWorld : Waiting for " + (threadPool.getTaskCount() - threadPool.getCompletedTaskCount()) + " compiles");
 637             }
 638             try {
 639                 threadPool.awaitTermination(1, TimeUnit.SECONDS);
 640                 wakeups++;
 641             } catch (InterruptedException e) {
 642             }
 643         }
 644         threadPool = null;
 645 
 646         long elapsedTime = System.currentTimeMillis() - start;
 647 
 648         println();
 649         if (Options.MultiThreaded.getValue(currentOptions)) {
 650             TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms elapsed, %d ms compile time, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), elapsedTime,
 651                             compileTime.get(), memoryUsed.get());
 652         } else {
 653             TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), compileTime.get(), memoryUsed.get());
 654         }
 655 
 656         // Apart from the main thread, there should be only be daemon threads
 657         // alive now. If not, then a class initializer has probably started
 658         // a thread that could cause a deadlock while trying to exit the VM.
 659         // One known example of this is sun.tools.jconsole.OutputViewer which
 660         // spawns threads to redirect sysout and syserr. To help debug such
 661         // scenarios, the stacks of potentially problematic threads are dumped.
 662         Map<Thread, StackTraceElement[]> suspiciousThreads = new HashMap<>();
 663         for (Map.Entry<Thread, StackTraceElement[]> e : Thread.getAllStackTraces().entrySet()) {
 664             Thread thread = e.getKey();
 665             if (thread != Thread.currentThread() && !initialThreads.containsKey(thread) && !thread.isDaemon() && thread.isAlive()) {
 666                 suspiciousThreads.put(thread, e.getValue());
 667             }
 668         }
 669         if (!suspiciousThreads.isEmpty()) {
 670             TTY.println("--- Non-daemon threads started during CTW ---");
 671             for (Map.Entry<Thread, StackTraceElement[]> e : suspiciousThreads.entrySet()) {
 672                 Thread thread = e.getKey();
 673                 if (thread.isAlive()) {
 674                     TTY.println(thread.toString() + " " + thread.getState());
 675                     for (StackTraceElement ste : e.getValue()) {
 676                         TTY.println("\tat " + ste);
 677                     }
 678                 }
 679             }
 680             TTY.println("---------------------------------------------");
 681         }
 682     }
 683 
 684     private synchronized void startThreads() {
 685         running = true;
 686         // Wake up any waiting threads
 687         notifyAll();
 688     }
 689 
 690     private synchronized void waitToRun() {
 691         while (!running) {
 692             try {
 693                 wait();
 694             } catch (InterruptedException e) {
 695             }
 696         }
 697     }
 698 
 699     @SuppressWarnings("try")
 700     private void compileMethod(HotSpotResolvedJavaMethod method) throws InterruptedException, ExecutionException {


< prev index next >