< prev index next >

src/hotspot/share/compiler/compileBroker.cpp

Print this page
rev 49289 : 8198756: Lazy allocation of compiler threads
Reviewed-by:


  32 #include "compiler/compileBroker.hpp"
  33 #include "compiler/compileLog.hpp"
  34 #include "compiler/compilerOracle.hpp"
  35 #include "compiler/directivesParser.hpp"
  36 #include "interpreter/linkResolver.hpp"
  37 #include "logging/log.hpp"
  38 #include "logging/logStream.hpp"
  39 #include "memory/allocation.inline.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/methodData.hpp"
  42 #include "oops/method.inline.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "prims/nativeLookup.hpp"
  45 #include "prims/whitebox.hpp"
  46 #include "runtime/arguments.hpp"
  47 #include "runtime/atomic.hpp"
  48 #include "runtime/compilationPolicy.hpp"
  49 #include "runtime/init.hpp"
  50 #include "runtime/interfaceSupport.inline.hpp"
  51 #include "runtime/javaCalls.hpp"

  52 #include "runtime/os.hpp"
  53 #include "runtime/sharedRuntime.hpp"
  54 #include "runtime/sweeper.hpp"
  55 #include "runtime/timerTrace.hpp"
  56 #include "runtime/vframe.inline.hpp"
  57 #include "trace/tracing.hpp"
  58 #include "utilities/debug.hpp"
  59 #include "utilities/dtrace.hpp"
  60 #include "utilities/events.hpp"
  61 #include "utilities/formatBuffer.hpp"
  62 #ifdef COMPILER1
  63 #include "c1/c1_Compiler.hpp"
  64 #endif
  65 #if INCLUDE_JVMCI
  66 #include "jvmci/jvmciCompiler.hpp"
  67 #include "jvmci/jvmciRuntime.hpp"
  68 #include "jvmci/jvmciJavaClasses.hpp"
  69 #include "runtime/vframe.hpp"
  70 #endif
  71 #ifdef COMPILER2


  98       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
  99       (char *) name->bytes(), name->utf8_length(),                       \
 100       (char *) signature->bytes(), signature->utf8_length(), (success)); \
 101   }
 102 
 103 #else //  ndef DTRACE_ENABLED
 104 
 105 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
 106 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
 107 
 108 #endif // ndef DTRACE_ENABLED
 109 
 110 bool CompileBroker::_initialized = false;
 111 volatile bool CompileBroker::_should_block = false;
 112 volatile int  CompileBroker::_print_compilation_warning = 0;
 113 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
 114 
 115 // The installed compiler(s)
 116 AbstractCompiler* CompileBroker::_compilers[2];
 117 








 118 // These counters are used to assign an unique ID to each compilation.
 119 volatile jint CompileBroker::_compilation_id     = 0;
 120 volatile jint CompileBroker::_osr_compilation_id = 0;
 121 
 122 // Debugging information
 123 int  CompileBroker::_last_compile_type     = no_compile;
 124 int  CompileBroker::_last_compile_level    = CompLevel_none;
 125 char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
 126 
 127 // Performance counters
 128 PerfCounter* CompileBroker::_perf_total_compilation = NULL;
 129 PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
 130 PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
 131 
 132 PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
 133 PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
 134 PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
 135 PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
 136 PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
 137 


 268       if (!free_task) {
 269         // Notify the waiting thread that the compilation has completed
 270         // so that it can free the task.
 271         task->lock()->notify_all();
 272       }
 273     }
 274     if (free_task) {
 275       // The task can only be freed once the task lock is released.
 276       CompileTask::free(task);
 277     }
 278   } else {
 279     task->mark_complete();
 280 
 281     // By convention, the compiling thread is responsible for
 282     // recycling a non-blocking CompileTask.
 283     CompileTask::free(task);
 284   }
 285 }
 286 
 287 /**






















 288  * Add a CompileTask to a CompileQueue.
 289  */
 290 void CompileQueue::add(CompileTask* task) {
 291   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 292 
 293   task->set_next(NULL);
 294   task->set_prev(NULL);
 295 
 296   if (_last == NULL) {
 297     // The compile queue is empty.
 298     assert(_first == NULL, "queue is empty");
 299     _first = task;
 300     _last = task;
 301   } else {
 302     // Append the task to the queue.
 303     assert(_last->next() == NULL, "not last");
 304     _last->set_next(task);
 305     task->set_prev(_last);
 306     _last = task;
 307   }


 364   // having no compile jobs: First, we compiled everything we wanted. Second,
 365   // we ran out of code cache so compilation has been disabled. In the latter
 366   // case we perform code cache sweeps to free memory such that we can re-enable
 367   // compilation.
 368   while (_first == NULL) {
 369     // Exit loop if compilation is disabled forever
 370     if (CompileBroker::is_compilation_disabled_forever()) {
 371       return NULL;
 372     }
 373 
 374     // If there are no compilation tasks and we can compile new jobs
 375     // (i.e., there is enough free space in the code cache) there is
 376     // no need to invoke the sweeper. As a result, the hotness of methods
 377     // remains unchanged. This behavior is desired, since we want to keep
 378     // the stable state, i.e., we do not want to evict methods from the
 379     // code cache if it is unnecessary.
 380     // We need a timed wait here, since compiler threads can exit if compilation
 381     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
 382     // is not critical and we do not want idle compiler threads to wake up too often.
 383     MethodCompileQueue_lock->wait(!Mutex::_no_safepoint_check_flag, 5*1000);





 384   }
 385 
 386   if (CompileBroker::is_compilation_disabled_forever()) {
 387     return NULL;
 388   }
 389 
 390   CompileTask* task;
 391   {
 392     NoSafepointVerifier nsv;
 393     task = CompilationPolicy::policy()->select_task(this);
 394   }
 395 
 396   if (task != NULL) {
 397     // Save method pointers across unlock safepoint.  The task is removed from
 398     // the compilation queue, which is walked during RedefineClasses.
 399     save_method = methodHandle(task->method());
 400     save_hot_method = methodHandle(task->hot_method());
 401 
 402     remove(task);
 403     purge_stale_tasks(); // may temporarily release MCQ lock


 513   print(tty);
 514 }
 515 
 516 CompilerCounters::CompilerCounters() {
 517   _current_method[0] = '\0';
 518   _compile_type = CompileBroker::no_compile;
 519 }
 520 
 521 // ------------------------------------------------------------------
 522 // CompileBroker::compilation_init
 523 //
 524 // Initialize the Compilation object
 525 void CompileBroker::compilation_init(TRAPS) {
 526   _last_method_compiled[0] = '\0';
 527 
 528   // No need to initialize compilation system if we do not use it.
 529   if (!UseCompiler) {
 530     return;
 531   }
 532   // Set the interface to the current compiler(s).
 533   int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
 534   int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
 535 
 536 #if INCLUDE_JVMCI
 537   if (EnableJVMCI) {
 538     // This is creating a JVMCICompiler singleton.
 539     JVMCICompiler* jvmci = new JVMCICompiler();
 540 
 541     if (UseJVMCICompiler) {
 542       _compilers[1] = jvmci;
 543       if (FLAG_IS_DEFAULT(JVMCIThreads)) {
 544         if (BootstrapJVMCI) {
 545           // JVMCI will bootstrap so give it more threads
 546           c2_count = MIN2(32, os::active_processor_count());
 547         }
 548       } else {
 549         c2_count = JVMCIThreads;
 550       }
 551       if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
 552       } else {
 553         c1_count = JVMCIHostThreads;
 554       }
 555     }
 556   }
 557 #endif // INCLUDE_JVMCI
 558 
 559 #ifdef COMPILER1
 560   if (c1_count > 0) {
 561     _compilers[0] = new Compiler();
 562   }
 563 #endif // COMPILER1
 564 
 565 #ifdef COMPILER2
 566   if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
 567     if (c2_count > 0) {
 568       _compilers[1] = new C2Compiler();
 569     }
 570   }
 571 #endif // COMPILER2
 572 
 573   // Start the compiler thread(s) and the sweeper thread
 574   init_compiler_sweeper_threads(c1_count, c2_count);
 575   // totalTime performance counter is always created as it is required
 576   // by the implementation of java.lang.management.CompilationMBean.
 577   {
 578     EXCEPTION_MARK;
 579     _perf_total_compilation =
 580                  PerfDataManager::create_counter(JAVA_CI, "totalTime",
 581                                                  PerfData::U_Ticks, CHECK);
 582   }
 583 
 584   if (UsePerfData) {
 585 
 586     EXCEPTION_MARK;
 587 
 588     // create the jvmstat performance counters
 589     _perf_osr_compilation =
 590                  PerfDataManager::create_counter(SUN_CI, "osrTime",
 591                                                  PerfData::U_Ticks, CHECK);
 592 
 593     _perf_standard_compilation =
 594                  PerfDataManager::create_counter(SUN_CI, "standardTime",


 657                                               CHECK);
 658 
 659 
 660     _perf_last_failed_type =
 661              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 662                                               PerfData::U_None,
 663                                               (jlong)CompileBroker::no_compile,
 664                                               CHECK);
 665 
 666     _perf_last_invalidated_type =
 667          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 668                                           PerfData::U_None,
 669                                           (jlong)CompileBroker::no_compile,
 670                                           CHECK);
 671   }
 672 
 673   _initialized = true;
 674 }
 675 
 676 
 677 JavaThread* CompileBroker::make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
 678                                        AbstractCompiler* comp, bool compiler_thread, TRAPS) {
 679   JavaThread* thread = NULL;
 680   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_0);
 681   InstanceKlass* klass = InstanceKlass::cast(k);
 682   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
 683   Handle string = java_lang_String::create_from_str(name, CHECK_0);
 684 
 685   // Initialize thread_oop to put it into the system threadGroup
 686   Handle thread_group (THREAD,  Universe::system_thread_group());
 687   JavaValue result(T_VOID);
 688   JavaCalls::call_special(&result, thread_oop,
 689                        klass,
 690                        vmSymbols::object_initializer_name(),
 691                        vmSymbols::threadgroup_string_void_signature(),
 692                        thread_group,
 693                        string,
 694                        CHECK_0);



 695 




 696   {
 697     MutexLocker mu(Threads_lock, THREAD);
 698     if (compiler_thread) {


 699       thread = new CompilerThread(queue, counters);

 700     } else {
 701       thread = new CodeCacheSweeperThread();
 702     }
 703     // At this point the new CompilerThread data-races with this startup
 704     // thread (which I believe is the primoridal thread and NOT the VM
 705     // thread).  This means Java bytecodes being executed at startup can
 706     // queue compile jobs which will run at whatever default priority the
 707     // newly created CompilerThread runs at.
 708 
 709 
 710     // At this point it may be possible that no osthread was created for the
 711     // JavaThread due to lack of memory. We would have to throw an exception
 712     // in that case. However, since this must work and we do not allow
 713     // exceptions anyway, check and abort if this fails. But first release the
 714     // lock.
 715 
 716     if (thread != NULL && thread->osthread() != NULL) {
 717 
 718       java_lang_Thread::set_thread(thread_oop(), thread);
 719 
 720       // Note that this only sets the JavaThread _priority field, which by
 721       // definition is limited to Java priorities and not OS priorities.
 722       // The os-priority is set in the CompilerThread startup code itself
 723 
 724       java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
 725 
 726       // Note that we cannot call os::set_priority because it expects Java
 727       // priorities and we are *explicitly* using OS priorities so that it's
 728       // possible to set the compiler thread priority higher than any Java
 729       // thread.
 730 
 731       int native_prio = CompilerThreadPriority;
 732       if (native_prio == -1) {
 733         if (UseCriticalCompilerThreadPriority) {
 734           native_prio = os::java_to_os_priority[CriticalPriority];
 735         } else {
 736           native_prio = os::java_to_os_priority[NearMaxPriority];
 737         }
 738       }
 739       os::set_native_priority(thread, native_prio);
 740 
 741       java_lang_Thread::set_daemon(thread_oop());
 742 
 743       thread->set_threadObj(thread_oop());
 744       if (compiler_thread) {
 745         thread->as_CompilerThread()->set_compiler(comp);
 746       }
 747       Threads::add(thread);
 748       Thread::start(thread);
 749     }
 750   }
 751 
 752   // First release lock before aborting VM.
 753   if (thread == NULL || thread->osthread() == NULL) {




 754     vm_exit_during_initialization("java.lang.OutOfMemoryError",
 755                                   os::native_thread_creation_failed_msg());
 756   }
 757 
 758   // Let go of Threads_lock before yielding
 759   os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 760 
 761   return thread;
 762 }
 763 
 764 
 765 void CompileBroker::init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count) {
 766   EXCEPTION_MARK;
 767 #if !defined(ZERO)
 768   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
 769 #endif // !ZERO
 770   // Initialize the compilation queue
 771   if (c2_compiler_count > 0) {
 772     const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
 773     _c2_compile_queue  = new CompileQueue(name);
 774     _compilers[1]->set_num_compiler_threads(c2_compiler_count);
 775   }
 776   if (c1_compiler_count > 0) {
 777     _c1_compile_queue  = new CompileQueue("C1 compile queue");
 778     _compilers[0]->set_num_compiler_threads(c1_compiler_count);
 779   }
 780 
 781   int compiler_count = c1_compiler_count + c2_compiler_count;
 782 
 783   char name_buffer[256];
 784   const bool compiler_thread = true;
 785   for (int i = 0; i < c2_compiler_count; i++) {
 786     // Create a name for our thread.
 787     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
 788     CompilerCounters* counters = new CompilerCounters();
 789     make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK);











 790   }
 791 
 792   for (int i = c2_compiler_count; i < compiler_count; i++) {
 793     // Create a name for our thread.
 794     sprintf(name_buffer, "C1 CompilerThread%d", i);
 795     CompilerCounters* counters = new CompilerCounters();
 796     // C1
 797     make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK);










 798   }
 799 
 800   if (UsePerfData) {
 801     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
 802   }
 803 
 804   if (MethodFlushing) {
 805     // Initialize the sweeper thread
 806     make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK);


























 807   }























 808 }
 809 
 810 
 811 /**
 812  * Set the methods on the stack as on_stack so that redefine classes doesn't
 813  * reclaim them. This method is executed at a safepoint.
 814  */
 815 void CompileBroker::mark_on_stack() {
 816   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
 817   // Since we are at a safepoint, we do not need a lock to access
 818   // the compile queues.
 819   if (_c2_compile_queue != NULL) {
 820     _c2_compile_queue->mark_on_stack();
 821   }
 822   if (_c1_compile_queue != NULL) {
 823     _c1_compile_queue->mark_on_stack();
 824   }
 825 }
 826 
 827 // ------------------------------------------------------------------


1575     log->stamp();
1576     log->end_elem();
1577   }
1578 
1579   // If compiler thread/runtime initialization fails, exit the compiler thread
1580   if (!init_compiler_runtime()) {
1581     return;
1582   }
1583 
1584   // Poll for new compilation tasks as long as the JVM runs. Compilation
1585   // should only be disabled if something went wrong while initializing the
1586   // compiler runtimes. This, in turn, should not happen. The only known case
1587   // when compiler runtime initialization fails is if there is not enough free
1588   // space in the code cache to generate the necessary stubs, etc.
1589   while (!is_compilation_disabled_forever()) {
1590     // We need this HandleMark to avoid leaking VM handles.
1591     HandleMark hm(thread);
1592 
1593     CompileTask* task = queue->get();
1594     if (task == NULL) {










1595       continue;




1596     }
1597 
1598     // Give compiler threads an extra quanta.  They tend to be bursty and
1599     // this helps the compiler to finish up the job.
1600     if (CompilerThreadHintNoPreempt) {
1601       os::hint_no_preempt();
1602     }
1603 
1604     // Assign the task to the current thread.  Mark this compilation
1605     // thread as active for the profiler.
1606     CompileTaskWrapper ctw(task);
1607     nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
1608     task->set_code_handle(&result_handle);
1609     methodHandle method(thread, task->method());
1610 
1611     // Never compile a method if breakpoints are present in it
1612     if (method()->number_of_breakpoints() == 0) {
1613       // Compile the method.
1614       if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
1615         invoke_compiler_on_method(task);




  32 #include "compiler/compileBroker.hpp"
  33 #include "compiler/compileLog.hpp"
  34 #include "compiler/compilerOracle.hpp"
  35 #include "compiler/directivesParser.hpp"
  36 #include "interpreter/linkResolver.hpp"
  37 #include "logging/log.hpp"
  38 #include "logging/logStream.hpp"
  39 #include "memory/allocation.inline.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/methodData.hpp"
  42 #include "oops/method.inline.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "prims/nativeLookup.hpp"
  45 #include "prims/whitebox.hpp"
  46 #include "runtime/arguments.hpp"
  47 #include "runtime/atomic.hpp"
  48 #include "runtime/compilationPolicy.hpp"
  49 #include "runtime/init.hpp"
  50 #include "runtime/interfaceSupport.inline.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/jniHandles.inline.hpp"
  53 #include "runtime/os.hpp"
  54 #include "runtime/sharedRuntime.hpp"
  55 #include "runtime/sweeper.hpp"
  56 #include "runtime/timerTrace.hpp"
  57 #include "runtime/vframe.inline.hpp"
  58 #include "trace/tracing.hpp"
  59 #include "utilities/debug.hpp"
  60 #include "utilities/dtrace.hpp"
  61 #include "utilities/events.hpp"
  62 #include "utilities/formatBuffer.hpp"
  63 #ifdef COMPILER1
  64 #include "c1/c1_Compiler.hpp"
  65 #endif
  66 #if INCLUDE_JVMCI
  67 #include "jvmci/jvmciCompiler.hpp"
  68 #include "jvmci/jvmciRuntime.hpp"
  69 #include "jvmci/jvmciJavaClasses.hpp"
  70 #include "runtime/vframe.hpp"
  71 #endif
  72 #ifdef COMPILER2


  99       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
 100       (char *) name->bytes(), name->utf8_length(),                       \
 101       (char *) signature->bytes(), signature->utf8_length(), (success)); \
 102   }
 103 
 104 #else //  ndef DTRACE_ENABLED
 105 
 106 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
 107 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
 108 
 109 #endif // ndef DTRACE_ENABLED
 110 
 111 bool CompileBroker::_initialized = false;
 112 volatile bool CompileBroker::_should_block = false;
 113 volatile int  CompileBroker::_print_compilation_warning = 0;
 114 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
 115 
 116 // The installed compiler(s)
 117 AbstractCompiler* CompileBroker::_compilers[2];
 118 
 119 // The maximum numbers of compiler threads to be determined during startup.
 120 int CompileBroker::c1_count = 0;
 121 int CompileBroker::c2_count = 0;
 122 
 123 // An array of compiler names as Java String objects
 124 jobject* CompileBroker::_compiler1_objects = NULL;
 125 jobject* CompileBroker::_compiler2_objects = NULL;
 126 
 127 // These counters are used to assign an unique ID to each compilation.
 128 volatile jint CompileBroker::_compilation_id     = 0;
 129 volatile jint CompileBroker::_osr_compilation_id = 0;
 130 
 131 // Debugging information
 132 int  CompileBroker::_last_compile_type     = no_compile;
 133 int  CompileBroker::_last_compile_level    = CompLevel_none;
 134 char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
 135 
 136 // Performance counters
 137 PerfCounter* CompileBroker::_perf_total_compilation = NULL;
 138 PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
 139 PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
 140 
 141 PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
 142 PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
 143 PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
 144 PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
 145 PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
 146 


 277       if (!free_task) {
 278         // Notify the waiting thread that the compilation has completed
 279         // so that it can free the task.
 280         task->lock()->notify_all();
 281       }
 282     }
 283     if (free_task) {
 284       // The task can only be freed once the task lock is released.
 285       CompileTask::free(task);
 286     }
 287   } else {
 288     task->mark_complete();
 289 
 290     // By convention, the compiling thread is responsible for
 291     // recycling a non-blocking CompileTask.
 292     CompileTask::free(task);
 293   }
 294 }
 295 
 296 /**
 297  * Check if a CompilerThread can be removed and update count if requested.
 298  */
 299 static bool can_remove(CompilerThread *ct, bool do_it) {
 300   assert(UseDynamicNumberOfCompilerThreads, "or shouldn't be here");
 301   AbstractCompiler *compiler = ct->compiler();
 302   int compiler_count = compiler->num_compiler_threads();
 303   // Keep at least 1 compiler thread of each type.
 304   if (compiler_count < 2) return false;
 305   // We only allow the last compiler thread of each type to get removed.
 306   jobject last_compiler = (compiler->is_c1()) ? CompileBroker::compiler1_object(compiler_count - 1)
 307                                               : CompileBroker::compiler2_object(compiler_count - 1);
 308   if (ct->threadObj() == JNIHandles::resolve_non_null(last_compiler)) {
 309     if (do_it) {
 310       assert_locked_or_safepoint(CompileThread_lock); // Update must be consistent.
 311       compiler->set_num_compiler_threads(compiler_count - 1);
 312     }
 313     return true;
 314   }
 315   return false;
 316 }
 317 
 318 /**
 319  * Add a CompileTask to a CompileQueue.
 320  */
 321 void CompileQueue::add(CompileTask* task) {
 322   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 323 
 324   task->set_next(NULL);
 325   task->set_prev(NULL);
 326 
 327   if (_last == NULL) {
 328     // The compile queue is empty.
 329     assert(_first == NULL, "queue is empty");
 330     _first = task;
 331     _last = task;
 332   } else {
 333     // Append the task to the queue.
 334     assert(_last->next() == NULL, "not last");
 335     _last->set_next(task);
 336     task->set_prev(_last);
 337     _last = task;
 338   }


 395   // having no compile jobs: First, we compiled everything we wanted. Second,
 396   // we ran out of code cache so compilation has been disabled. In the latter
 397   // case we perform code cache sweeps to free memory such that we can re-enable
 398   // compilation.
 399   while (_first == NULL) {
 400     // Exit loop if compilation is disabled forever
 401     if (CompileBroker::is_compilation_disabled_forever()) {
 402       return NULL;
 403     }
 404 
 405     // If there are no compilation tasks and we can compile new jobs
 406     // (i.e., there is enough free space in the code cache) there is
 407     // no need to invoke the sweeper. As a result, the hotness of methods
 408     // remains unchanged. This behavior is desired, since we want to keep
 409     // the stable state, i.e., we do not want to evict methods from the
 410     // code cache if it is unnecessary.
 411     // We need a timed wait here, since compiler threads can exit if compilation
 412     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
 413     // is not critical and we do not want idle compiler threads to wake up too often.
 414     MethodCompileQueue_lock->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
 415 
 416     if (UseDynamicNumberOfCompilerThreads && _first == NULL) {
 417       // Still nothing to compile. Give caller a chance to stop this thread.
 418       if (can_remove(CompilerThread::current(), false)) return NULL;
 419     }
 420   }
 421 
 422   if (CompileBroker::is_compilation_disabled_forever()) {
 423     return NULL;
 424   }
 425 
 426   CompileTask* task;
 427   {
 428     NoSafepointVerifier nsv;
 429     task = CompilationPolicy::policy()->select_task(this);
 430   }
 431 
 432   if (task != NULL) {
 433     // Save method pointers across unlock safepoint.  The task is removed from
 434     // the compilation queue, which is walked during RedefineClasses.
 435     save_method = methodHandle(task->method());
 436     save_hot_method = methodHandle(task->hot_method());
 437 
 438     remove(task);
 439     purge_stale_tasks(); // may temporarily release MCQ lock


 549   print(tty);
 550 }
 551 
 552 CompilerCounters::CompilerCounters() {
 553   _current_method[0] = '\0';
 554   _compile_type = CompileBroker::no_compile;
 555 }
 556 
 557 // ------------------------------------------------------------------
 558 // CompileBroker::compilation_init
 559 //
 560 // Initialize the Compilation object
 561 void CompileBroker::compilation_init(TRAPS) {
 562   _last_method_compiled[0] = '\0';
 563 
 564   // No need to initialize compilation system if we do not use it.
 565   if (!UseCompiler) {
 566     return;
 567   }
 568   // Set the interface to the current compiler(s).
 569   c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
 570   c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
 571 
 572 #if INCLUDE_JVMCI
 573   if (EnableJVMCI) {
 574     // This is creating a JVMCICompiler singleton.
 575     JVMCICompiler* jvmci = new JVMCICompiler();
 576 
 577     if (UseJVMCICompiler) {
 578       _compilers[1] = jvmci;
 579       if (FLAG_IS_DEFAULT(JVMCIThreads)) {
 580         if (BootstrapJVMCI) {
 581           // JVMCI will bootstrap so give it more threads
 582           c2_count = MIN2(32, os::active_processor_count());
 583         }
 584       } else {
 585         c2_count = JVMCIThreads;
 586       }
 587       if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
 588       } else {
 589         c1_count = JVMCIHostThreads;
 590       }
 591     }
 592   }
 593 #endif // INCLUDE_JVMCI
 594 
 595 #ifdef COMPILER1
 596   if (c1_count > 0) {
 597     _compilers[0] = new Compiler();
 598   }
 599 #endif // COMPILER1
 600 
 601 #ifdef COMPILER2
 602   if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
 603     if (c2_count > 0) {
 604       _compilers[1] = new C2Compiler();
 605     }
 606   }
 607 #endif // COMPILER2
 608 
 609   // Start the compiler thread(s) and the sweeper thread
 610   init_compiler_sweeper_threads();
 611   // totalTime performance counter is always created as it is required
 612   // by the implementation of java.lang.management.CompilationMBean.
 613   {
 614     EXCEPTION_MARK;
 615     _perf_total_compilation =
 616                  PerfDataManager::create_counter(JAVA_CI, "totalTime",
 617                                                  PerfData::U_Ticks, CHECK);
 618   }
 619 
 620   if (UsePerfData) {
 621 
 622     EXCEPTION_MARK;
 623 
 624     // create the jvmstat performance counters
 625     _perf_osr_compilation =
 626                  PerfDataManager::create_counter(SUN_CI, "osrTime",
 627                                                  PerfData::U_Ticks, CHECK);
 628 
 629     _perf_standard_compilation =
 630                  PerfDataManager::create_counter(SUN_CI, "standardTime",


 693                                               CHECK);
 694 
 695 
 696     _perf_last_failed_type =
 697              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 698                                               PerfData::U_None,
 699                                               (jlong)CompileBroker::no_compile,
 700                                               CHECK);
 701 
 702     _perf_last_invalidated_type =
 703          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 704                                           PerfData::U_None,
 705                                           (jlong)CompileBroker::no_compile,
 706                                           CHECK);
 707   }
 708 
 709   _initialized = true;
 710 }
 711 
 712 
 713 oop CompileBroker::create_thread_oop(const char* name, TRAPS) {
 714   Klass* k = SystemDictionary::find(vmSymbols::java_lang_Thread(), Handle(), Handle(), CHECK_NULL);
 715   assert(k != NULL, "must be initialized");

 716   InstanceKlass* klass = InstanceKlass::cast(k);
 717   instanceHandle thread_handle = klass->allocate_instance_handle(CHECK_NULL);
 718   Handle string = java_lang_String::create_from_str(name, CHECK_NULL);
 719 
 720   // Initialize thread_oop to put it into the system threadGroup
 721   Handle thread_group(THREAD, Universe::system_thread_group());
 722   JavaValue result(T_VOID);
 723   JavaCalls::call_special(&result, thread_handle,
 724                        klass,
 725                        vmSymbols::object_initializer_name(),
 726                        vmSymbols::threadgroup_string_void_signature(),
 727                        thread_group,
 728                        string,
 729                        CHECK_NULL);
 730 
 731   return thread_handle();
 732 }
 733 
 734 
 735 JavaThread* CompileBroker::make_thread(jobject thread_handle, CompileQueue* queue,
 736                                        AbstractCompiler* comp, bool compiler_thread, TRAPS) {
 737   JavaThread* thread = NULL;
 738   {
 739     MutexLocker mu(Threads_lock, THREAD);
 740     if (compiler_thread) {
 741       if (!InjectCompilerCreationFailure || comp->num_compiler_threads() == 0) {
 742         CompilerCounters* counters = new CompilerCounters();
 743         thread = new CompilerThread(queue, counters);
 744       }
 745     } else {
 746       thread = new CodeCacheSweeperThread();
 747     }
 748     // At this point the new CompilerThread data-races with this startup
 749     // thread (which I believe is the primoridal thread and NOT the VM
 750     // thread).  This means Java bytecodes being executed at startup can
 751     // queue compile jobs which will run at whatever default priority the
 752     // newly created CompilerThread runs at.
 753 
 754 
 755     // At this point it may be possible that no osthread was created for the
 756     // JavaThread due to lack of memory. We would have to throw an exception
 757     // in that case. However, since this must work and we do not allow
 758     // exceptions anyway, check and abort if this fails. But first release the
 759     // lock.
 760 
 761     if (thread != NULL && thread->osthread() != NULL) {
 762 
 763       java_lang_Thread::set_thread(JNIHandles::resolve_non_null(thread_handle), thread);
 764 
 765       // Note that this only sets the JavaThread _priority field, which by
 766       // definition is limited to Java priorities and not OS priorities.
 767       // The os-priority is set in the CompilerThread startup code itself
 768 
 769       java_lang_Thread::set_priority(JNIHandles::resolve_non_null(thread_handle), NearMaxPriority);
 770 
 771       // Note that we cannot call os::set_priority because it expects Java
 772       // priorities and we are *explicitly* using OS priorities so that it's
 773       // possible to set the compiler thread priority higher than any Java
 774       // thread.
 775 
 776       int native_prio = CompilerThreadPriority;
 777       if (native_prio == -1) {
 778         if (UseCriticalCompilerThreadPriority) {
 779           native_prio = os::java_to_os_priority[CriticalPriority];
 780         } else {
 781           native_prio = os::java_to_os_priority[NearMaxPriority];
 782         }
 783       }
 784       os::set_native_priority(thread, native_prio);
 785 
 786       java_lang_Thread::set_daemon(JNIHandles::resolve_non_null(thread_handle));
 787 
 788       thread->set_threadObj(JNIHandles::resolve_non_null(thread_handle));
 789       if (compiler_thread) {
 790         thread->as_CompilerThread()->set_compiler(comp);
 791       }
 792       Threads::add(thread);
 793       Thread::start(thread);
 794     }
 795   }
 796 
 797   // First release lock before aborting VM.
 798   if (thread == NULL || thread->osthread() == NULL) {
 799     if (UseDynamicNumberOfCompilerThreads && comp->num_compiler_threads() > 0) {
 800       delete thread;
 801       return NULL;
 802     }
 803     vm_exit_during_initialization("java.lang.OutOfMemoryError",
 804                                   os::native_thread_creation_failed_msg());
 805   }
 806 
 807   // Let go of Threads_lock before yielding
 808   os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 809 
 810   return thread;
 811 }
 812 
 813 
 814 void CompileBroker::init_compiler_sweeper_threads() {
 815   EXCEPTION_MARK;
 816 #if !defined(ZERO)
 817   assert(c2_count > 0 || c1_count > 0, "No compilers?");
 818 #endif // !ZERO
 819   // Initialize the compilation queue
 820   if (c2_count > 0) {
 821     const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
 822     _c2_compile_queue  = new CompileQueue(name);
 823     _compiler2_objects = NEW_C_HEAP_ARRAY(jobject, c2_count, mtCompiler);
 824   }
 825   if (c1_count > 0) {
 826     _c1_compile_queue  = new CompileQueue("C1 compile queue");
 827     _compiler1_objects = NEW_C_HEAP_ARRAY(jobject, c1_count, mtCompiler);
 828   }
 829 


 830   char name_buffer[256];
 831   const bool compiler_thread = true;
 832   for (int i = 0; i < c2_count; i++) {
 833     // Create a name for our thread.
 834     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
 835     jobject thread_handle = JNIHandles::make_global(Handle(THREAD, CompileBroker::create_thread_oop(name_buffer, THREAD)));
 836     _compiler2_objects[i] = thread_handle;
 837 
 838     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
 839       JavaThread *ct = make_thread(thread_handle, _c2_compile_queue, _compilers[1], compiler_thread, CHECK);
 840       assert(ct != NULL, "should have been handled for initial thread");
 841       _compilers[1]->set_num_compiler_threads(i + 1);
 842       if (TraceCompilerThreads) {
 843         ResourceMark rm;
 844         MutexLocker mu(Threads_lock);
 845         tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
 846       }
 847     }
 848   }
 849 
 850   for (int i = 0; i < c1_count; i++) {
 851     // Create a name for our thread.
 852     sprintf(name_buffer, "C1 CompilerThread%d", i);
 853     jobject thread_handle = JNIHandles::make_global(Handle(THREAD, CompileBroker::create_thread_oop(name_buffer, THREAD)));
 854     _compiler1_objects[i] = thread_handle;
 855 
 856     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
 857       JavaThread *ct = make_thread(thread_handle, _c1_compile_queue, _compilers[0], compiler_thread, CHECK);
 858       assert(ct != NULL, "should have been handled for initial thread");
 859       _compilers[0]->set_num_compiler_threads(i + 1);
 860       if (TraceCompilerThreads) {
 861         ResourceMark rm;
 862         MutexLocker mu(Threads_lock);
 863         tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
 864       }
 865     }
 866   }
 867 
 868   if (UsePerfData) {
 869     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, c1_count + c2_count, CHECK);
 870   }
 871 
 872   if (MethodFlushing) {
 873     // Initialize the sweeper thread
 874     jobject thread_handle = JNIHandles::make_local(THREAD, CompileBroker::create_thread_oop("Sweeper thread", THREAD));
 875     make_thread(thread_handle, NULL, NULL, false, CHECK);
 876   }
 877 }
 878 
 879 void CompileBroker::possibly_add_compiler_threads() {
 880   EXCEPTION_MARK;
 881 
 882   julong available_memory = os::available_memory();
 883   // Only do attempt to start additional threads if the lock is free.
 884   if (!CompileThread_lock->try_lock()) return;
 885 
 886   if (_c2_compile_queue != NULL) {
 887     int old_c2_count = _compilers[1]->num_compiler_threads();
 888     int new_c2_count = MIN3(_c2_compile_queue->size() / 2,
 889         CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization),
 890         (int)(available_memory / 200*M));
 891 
 892     for (int i = old_c2_count; i < new_c2_count; i++) {
 893       JavaThread *ct = make_thread(compiler2_object(i), _c2_compile_queue, _compilers[1], true, CHECK);
 894       if (ct == NULL) break;
 895       _compilers[1]->set_num_compiler_threads(i + 1);
 896       if (TraceCompilerThreads) {
 897         ResourceMark rm;
 898         MutexLocker mu(Threads_lock);
 899         tty->print_cr("Added compiler thread %s (available memory: %dMB)",
 900                       ct->get_thread_name(), (int)(available_memory/M));
 901       }
 902     }
 903   }
 904 
 905   if (_c1_compile_queue != NULL) {
 906     int old_c1_count = _compilers[0]->num_compiler_threads();
 907     int new_c1_count = MIN3(_c1_compile_queue->size() / 2,
 908         CompilationPolicy::policy()->compiler_count(CompLevel_simple),
 909         (int)(available_memory / 100*M));
 910 
 911     for (int i = old_c1_count; i < new_c1_count; i++) {
 912       JavaThread *ct = make_thread(compiler1_object(i), _c1_compile_queue, _compilers[0], true, CHECK);
 913       if (ct == NULL) break;
 914       _compilers[0]->set_num_compiler_threads(i + 1);
 915       if (TraceCompilerThreads) {
 916         ResourceMark rm;
 917         MutexLocker mu(Threads_lock);
 918         tty->print_cr("Added compiler thread %s (available memory: %dMB)",
 919                       ct->get_thread_name(), (int)(available_memory/M));
 920       }
 921     }
 922   }
 923 
 924   CompileThread_lock->unlock();
 925 }
 926 
 927 
 928 /**
 929  * Set the methods on the stack as on_stack so that redefine classes doesn't
 930  * reclaim them. This method is executed at a safepoint.
 931  */
 932 void CompileBroker::mark_on_stack() {
 933   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
 934   // Since we are at a safepoint, we do not need a lock to access
 935   // the compile queues.
 936   if (_c2_compile_queue != NULL) {
 937     _c2_compile_queue->mark_on_stack();
 938   }
 939   if (_c1_compile_queue != NULL) {
 940     _c1_compile_queue->mark_on_stack();
 941   }
 942 }
 943 
 944 // ------------------------------------------------------------------


1692     log->stamp();
1693     log->end_elem();
1694   }
1695 
1696   // If compiler thread/runtime initialization fails, exit the compiler thread
1697   if (!init_compiler_runtime()) {
1698     return;
1699   }
1700 
1701   // Poll for new compilation tasks as long as the JVM runs. Compilation
1702   // should only be disabled if something went wrong while initializing the
1703   // compiler runtimes. This, in turn, should not happen. The only known case
1704   // when compiler runtime initialization fails is if there is not enough free
1705   // space in the code cache to generate the necessary stubs, etc.
1706   while (!is_compilation_disabled_forever()) {
1707     // We need this HandleMark to avoid leaking VM handles.
1708     HandleMark hm(thread);
1709 
1710     CompileTask* task = queue->get();
1711     if (task == NULL) {
1712       if (UseDynamicNumberOfCompilerThreads) {
1713         // Access compiler_count under lock to enforce consistency.
1714         MutexLocker only_one(CompileThread_lock);
1715         if (can_remove(thread, true)) {
1716           if (TraceCompilerThreads) {
1717             tty->print_cr("Removing compiler thread %s", thread->name());
1718           }
1719           return; // Stop this thread.
1720         }
1721       }
1722       continue;
1723     }
1724 
1725     if (UseDynamicNumberOfCompilerThreads) {
1726       possibly_add_compiler_threads();
1727     }
1728 
1729     // Give compiler threads an extra quanta.  They tend to be bursty and
1730     // this helps the compiler to finish up the job.
1731     if (CompilerThreadHintNoPreempt) {
1732       os::hint_no_preempt();
1733     }
1734 
1735     // Assign the task to the current thread.  Mark this compilation
1736     // thread as active for the profiler.
1737     CompileTaskWrapper ctw(task);
1738     nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
1739     task->set_code_handle(&result_handle);
1740     methodHandle method(thread, task->method());
1741 
1742     // Never compile a method if breakpoints are present in it
1743     if (method()->number_of_breakpoints() == 0) {
1744       // Compile the method.
1745       if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
1746         invoke_compiler_on_method(task);


< prev index next >