src/hotspot/share/compiler/compileBroker.cpp
changeset 49848 fcd5df7aa235
parent 49618 c5b066caefba
child 50113 caf115bb98ad
equal deleted inserted replaced
49847:f22c0b4caad7 49848:fcd5df7aa235
    48 #include "runtime/atomic.hpp"
    48 #include "runtime/atomic.hpp"
    49 #include "runtime/compilationPolicy.hpp"
    49 #include "runtime/compilationPolicy.hpp"
    50 #include "runtime/init.hpp"
    50 #include "runtime/init.hpp"
    51 #include "runtime/interfaceSupport.inline.hpp"
    51 #include "runtime/interfaceSupport.inline.hpp"
    52 #include "runtime/javaCalls.hpp"
    52 #include "runtime/javaCalls.hpp"
       
    53 #include "runtime/jniHandles.inline.hpp"
    53 #include "runtime/os.hpp"
    54 #include "runtime/os.hpp"
    54 #include "runtime/safepointVerifiers.hpp"
    55 #include "runtime/safepointVerifiers.hpp"
    55 #include "runtime/sharedRuntime.hpp"
    56 #include "runtime/sharedRuntime.hpp"
    56 #include "runtime/sweeper.hpp"
    57 #include "runtime/sweeper.hpp"
    57 #include "runtime/timerTrace.hpp"
    58 #include "runtime/timerTrace.hpp"
   115 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
   116 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
   116 
   117 
   117 // The installed compiler(s)
   118 // The installed compiler(s)
   118 AbstractCompiler* CompileBroker::_compilers[2];
   119 AbstractCompiler* CompileBroker::_compilers[2];
   119 
   120 
       
   121 // The maximum numbers of compiler threads to be determined during startup.
       
   122 int CompileBroker::_c1_count = 0;
       
   123 int CompileBroker::_c2_count = 0;
       
   124 
       
   125 // An array of compiler names as Java String objects
       
   126 jobject* CompileBroker::_compiler1_objects = NULL;
       
   127 jobject* CompileBroker::_compiler2_objects = NULL;
       
   128 
       
   129 CompileLog** CompileBroker::_compiler1_logs = NULL;
       
   130 CompileLog** CompileBroker::_compiler2_logs = NULL;
       
   131 
   120 // These counters are used to assign an unique ID to each compilation.
   132 // These counters are used to assign an unique ID to each compilation.
   121 volatile jint CompileBroker::_compilation_id     = 0;
   133 volatile jint CompileBroker::_compilation_id     = 0;
   122 volatile jint CompileBroker::_osr_compilation_id = 0;
   134 volatile jint CompileBroker::_osr_compilation_id = 0;
   123 
   135 
   124 // Debugging information
   136 // Debugging information
   285     CompileTask::free(task);
   297     CompileTask::free(task);
   286   }
   298   }
   287 }
   299 }
   288 
   300 
   289 /**
   301 /**
       
   302  * Check if a CompilerThread can be removed and update count if requested.
       
   303  */
       
   304 static bool can_remove(CompilerThread *ct, bool do_it) {
       
   305   assert(UseDynamicNumberOfCompilerThreads, "or shouldn't be here");
       
   306   if (!ReduceNumberOfCompilerThreads) return false;
       
   307 
       
   308   AbstractCompiler *compiler = ct->compiler();
       
   309   int compiler_count = compiler->num_compiler_threads();
       
   310   bool c1 = compiler->is_c1();
       
   311 
       
   312   // Keep at least 1 compiler thread of each type.
       
   313   if (compiler_count < 2) return false;
       
   314 
       
   315   // Keep thread alive for at least some time.
       
   316   if (ct->idle_time_millis() < (c1 ? 500 : 100)) return false;
       
   317 
       
   318   // We only allow the last compiler thread of each type to get removed.
       
   319   jobject last_compiler = c1 ? CompileBroker::compiler1_object(compiler_count - 1)
       
   320                              : CompileBroker::compiler2_object(compiler_count - 1);
       
   321   if (oopDesc::equals(ct->threadObj(), JNIHandles::resolve_non_null(last_compiler))) {
       
   322     if (do_it) {
       
   323       assert_locked_or_safepoint(CompileThread_lock); // Update must be consistent.
       
   324       compiler->set_num_compiler_threads(compiler_count - 1);
       
   325     }
       
   326     return true;
       
   327   }
       
   328   return false;
       
   329 }
       
   330 
       
   331 /**
   290  * Add a CompileTask to a CompileQueue.
   332  * Add a CompileTask to a CompileQueue.
   291  */
   333  */
   292 void CompileQueue::add(CompileTask* task) {
   334 void CompileQueue::add(CompileTask* task) {
   293   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
   335   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
   294 
   336 
   381     // code cache if it is unnecessary.
   423     // code cache if it is unnecessary.
   382     // We need a timed wait here, since compiler threads can exit if compilation
   424     // We need a timed wait here, since compiler threads can exit if compilation
   383     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
   425     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
   384     // is not critical and we do not want idle compiler threads to wake up too often.
   426     // is not critical and we do not want idle compiler threads to wake up too often.
   385     MethodCompileQueue_lock->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
   427     MethodCompileQueue_lock->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
       
   428 
       
   429     if (UseDynamicNumberOfCompilerThreads && _first == NULL) {
       
   430       // Still nothing to compile. Give caller a chance to stop this thread.
       
   431       if (can_remove(CompilerThread::current(), false)) return NULL;
       
   432     }
   386   }
   433   }
   387 
   434 
   388   if (CompileBroker::is_compilation_disabled_forever()) {
   435   if (CompileBroker::is_compilation_disabled_forever()) {
   389     return NULL;
   436     return NULL;
   390   }
   437   }
   530   // No need to initialize compilation system if we do not use it.
   577   // No need to initialize compilation system if we do not use it.
   531   if (!UseCompiler) {
   578   if (!UseCompiler) {
   532     return;
   579     return;
   533   }
   580   }
   534   // Set the interface to the current compiler(s).
   581   // Set the interface to the current compiler(s).
   535   int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
   582   _c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
   536   int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
   583   _c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
   537 
   584 
   538 #if INCLUDE_JVMCI
   585 #if INCLUDE_JVMCI
   539   if (EnableJVMCI) {
   586   if (EnableJVMCI) {
   540     // This is creating a JVMCICompiler singleton.
   587     // This is creating a JVMCICompiler singleton.
   541     JVMCICompiler* jvmci = new JVMCICompiler();
   588     JVMCICompiler* jvmci = new JVMCICompiler();
   543     if (UseJVMCICompiler) {
   590     if (UseJVMCICompiler) {
   544       _compilers[1] = jvmci;
   591       _compilers[1] = jvmci;
   545       if (FLAG_IS_DEFAULT(JVMCIThreads)) {
   592       if (FLAG_IS_DEFAULT(JVMCIThreads)) {
   546         if (BootstrapJVMCI) {
   593         if (BootstrapJVMCI) {
   547           // JVMCI will bootstrap so give it more threads
   594           // JVMCI will bootstrap so give it more threads
   548           c2_count = MIN2(32, os::active_processor_count());
   595           _c2_count = MIN2(32, os::active_processor_count());
   549         }
   596         }
   550       } else {
   597       } else {
   551         c2_count = JVMCIThreads;
   598         _c2_count = JVMCIThreads;
   552       }
   599       }
   553       if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
   600       if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
   554       } else {
   601       } else {
   555         c1_count = JVMCIHostThreads;
   602         _c1_count = JVMCIHostThreads;
   556       }
   603       }
   557     }
   604     }
   558   }
   605   }
   559 #endif // INCLUDE_JVMCI
   606 #endif // INCLUDE_JVMCI
   560 
   607 
   561 #ifdef COMPILER1
   608 #ifdef COMPILER1
   562   if (c1_count > 0) {
   609   if (_c1_count > 0) {
   563     _compilers[0] = new Compiler();
   610     _compilers[0] = new Compiler();
   564   }
   611   }
   565 #endif // COMPILER1
   612 #endif // COMPILER1
   566 
   613 
   567 #ifdef COMPILER2
   614 #ifdef COMPILER2
   568   if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
   615   if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
   569     if (c2_count > 0) {
   616     if (_c2_count > 0) {
   570       _compilers[1] = new C2Compiler();
   617       _compilers[1] = new C2Compiler();
   571     }
   618     }
   572   }
   619   }
   573 #endif // COMPILER2
   620 #endif // COMPILER2
   574 
   621 
   575   // Start the compiler thread(s) and the sweeper thread
   622   // Start the compiler thread(s) and the sweeper thread
   576   init_compiler_sweeper_threads(c1_count, c2_count);
   623   init_compiler_sweeper_threads();
   577   // totalTime performance counter is always created as it is required
   624   // totalTime performance counter is always created as it is required
   578   // by the implementation of java.lang.management.CompilationMBean.
   625   // by the implementation of java.lang.management.CompilationMBean.
   579   {
   626   {
   580     EXCEPTION_MARK;
   627     EXCEPTION_MARK;
   581     _perf_total_compilation =
   628     _perf_total_compilation =
   677 // prior to this will be silently ignored.
   724 // prior to this will be silently ignored.
   678 void CompileBroker::compilation_init_phase2() {
   725 void CompileBroker::compilation_init_phase2() {
   679   _initialized = true;
   726   _initialized = true;
   680 }
   727 }
   681 
   728 
   682 JavaThread* CompileBroker::make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
   729 Handle CompileBroker::create_thread_oop(const char* name, TRAPS) {
   683                                        AbstractCompiler* comp, bool compiler_thread, TRAPS) {
   730   Klass* k = SystemDictionary::find(vmSymbols::java_lang_Thread(), Handle(), Handle(), CHECK_NH);
   684   JavaThread* thread = NULL;
   731   assert(k != NULL, "must be initialized");
   685   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_0);
       
   686   InstanceKlass* klass = InstanceKlass::cast(k);
   732   InstanceKlass* klass = InstanceKlass::cast(k);
   687   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
   733   instanceHandle thread_handle = klass->allocate_instance_handle(CHECK_NH);
   688   Handle string = java_lang_String::create_from_str(name, CHECK_0);
   734   Handle string = java_lang_String::create_from_str(name, CHECK_NH);
   689 
   735 
   690   // Initialize thread_oop to put it into the system threadGroup
   736   // Initialize thread_oop to put it into the system threadGroup
   691   Handle thread_group (THREAD,  Universe::system_thread_group());
   737   Handle thread_group(THREAD, Universe::system_thread_group());
   692   JavaValue result(T_VOID);
   738   JavaValue result(T_VOID);
   693   JavaCalls::call_special(&result, thread_oop,
   739   JavaCalls::call_special(&result, thread_handle,
   694                        klass,
   740                        klass,
   695                        vmSymbols::object_initializer_name(),
   741                        vmSymbols::object_initializer_name(),
   696                        vmSymbols::threadgroup_string_void_signature(),
   742                        vmSymbols::threadgroup_string_void_signature(),
   697                        thread_group,
   743                        thread_group,
   698                        string,
   744                        string,
   699                        CHECK_0);
   745                        CHECK_NH);
   700 
   746 
       
   747   return thread_handle;
       
   748 }
       
   749 
       
   750 
       
   751 JavaThread* CompileBroker::make_thread(jobject thread_handle, CompileQueue* queue,
       
   752                                        AbstractCompiler* comp, bool compiler_thread, TRAPS) {
       
   753   JavaThread* thread = NULL;
   701   {
   754   {
   702     MutexLocker mu(Threads_lock, THREAD);
   755     MutexLocker mu(Threads_lock, THREAD);
   703     if (compiler_thread) {
   756     if (compiler_thread) {
   704       thread = new CompilerThread(queue, counters);
   757       if (!InjectCompilerCreationFailure || comp->num_compiler_threads() == 0) {
       
   758         CompilerCounters* counters = new CompilerCounters();
       
   759         thread = new CompilerThread(queue, counters);
       
   760       }
   705     } else {
   761     } else {
   706       thread = new CodeCacheSweeperThread();
   762       thread = new CodeCacheSweeperThread();
   707     }
   763     }
   708     // At this point the new CompilerThread data-races with this startup
   764     // At this point the new CompilerThread data-races with this startup
   709     // thread (which I believe is the primoridal thread and NOT the VM
   765     // thread (which I believe is the primoridal thread and NOT the VM
   718     // exceptions anyway, check and abort if this fails. But first release the
   774     // exceptions anyway, check and abort if this fails. But first release the
   719     // lock.
   775     // lock.
   720 
   776 
   721     if (thread != NULL && thread->osthread() != NULL) {
   777     if (thread != NULL && thread->osthread() != NULL) {
   722 
   778 
   723       java_lang_Thread::set_thread(thread_oop(), thread);
   779       java_lang_Thread::set_thread(JNIHandles::resolve_non_null(thread_handle), thread);
   724 
   780 
   725       // Note that this only sets the JavaThread _priority field, which by
   781       // Note that this only sets the JavaThread _priority field, which by
   726       // definition is limited to Java priorities and not OS priorities.
   782       // definition is limited to Java priorities and not OS priorities.
   727       // The os-priority is set in the CompilerThread startup code itself
   783       // The os-priority is set in the CompilerThread startup code itself
   728 
   784 
   729       java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
   785       java_lang_Thread::set_priority(JNIHandles::resolve_non_null(thread_handle), NearMaxPriority);
   730 
   786 
   731       // Note that we cannot call os::set_priority because it expects Java
   787       // Note that we cannot call os::set_priority because it expects Java
   732       // priorities and we are *explicitly* using OS priorities so that it's
   788       // priorities and we are *explicitly* using OS priorities so that it's
   733       // possible to set the compiler thread priority higher than any Java
   789       // possible to set the compiler thread priority higher than any Java
   734       // thread.
   790       // thread.
   741           native_prio = os::java_to_os_priority[NearMaxPriority];
   797           native_prio = os::java_to_os_priority[NearMaxPriority];
   742         }
   798         }
   743       }
   799       }
   744       os::set_native_priority(thread, native_prio);
   800       os::set_native_priority(thread, native_prio);
   745 
   801 
   746       java_lang_Thread::set_daemon(thread_oop());
   802       java_lang_Thread::set_daemon(JNIHandles::resolve_non_null(thread_handle));
   747 
   803 
   748       thread->set_threadObj(thread_oop());
   804       thread->set_threadObj(JNIHandles::resolve_non_null(thread_handle));
   749       if (compiler_thread) {
   805       if (compiler_thread) {
   750         thread->as_CompilerThread()->set_compiler(comp);
   806         thread->as_CompilerThread()->set_compiler(comp);
   751       }
   807       }
   752       Threads::add(thread);
   808       Threads::add(thread);
   753       Thread::start(thread);
   809       Thread::start(thread);
   754     }
   810     }
   755   }
   811   }
   756 
   812 
   757   // First release lock before aborting VM.
   813   // First release lock before aborting VM.
   758   if (thread == NULL || thread->osthread() == NULL) {
   814   if (thread == NULL || thread->osthread() == NULL) {
       
   815     if (UseDynamicNumberOfCompilerThreads && comp->num_compiler_threads() > 0) {
       
   816       if (thread != NULL) {
       
   817         thread->smr_delete();
       
   818       }
       
   819       return NULL;
       
   820     }
   759     vm_exit_during_initialization("java.lang.OutOfMemoryError",
   821     vm_exit_during_initialization("java.lang.OutOfMemoryError",
   760                                   os::native_thread_creation_failed_msg());
   822                                   os::native_thread_creation_failed_msg());
   761   }
   823   }
   762 
   824 
   763   // Let go of Threads_lock before yielding
   825   // Let go of Threads_lock before yielding
   765 
   827 
   766   return thread;
   828   return thread;
   767 }
   829 }
   768 
   830 
   769 
   831 
   770 void CompileBroker::init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count) {
   832 void CompileBroker::init_compiler_sweeper_threads() {
   771   EXCEPTION_MARK;
   833   EXCEPTION_MARK;
   772 #if !defined(ZERO)
   834 #if !defined(ZERO)
   773   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
   835   assert(_c2_count > 0 || _c1_count > 0, "No compilers?");
   774 #endif // !ZERO
   836 #endif // !ZERO
   775   // Initialize the compilation queue
   837   // Initialize the compilation queue
   776   if (c2_compiler_count > 0) {
   838   if (_c2_count > 0) {
   777     const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
   839     const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
   778     _c2_compile_queue  = new CompileQueue(name);
   840     _c2_compile_queue  = new CompileQueue(name);
   779     _compilers[1]->set_num_compiler_threads(c2_compiler_count);
   841     _compiler2_objects = NEW_C_HEAP_ARRAY(jobject, _c2_count, mtCompiler);
   780   }
   842     _compiler2_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c2_count, mtCompiler);
   781   if (c1_compiler_count > 0) {
   843   }
       
   844   if (_c1_count > 0) {
   782     _c1_compile_queue  = new CompileQueue("C1 compile queue");
   845     _c1_compile_queue  = new CompileQueue("C1 compile queue");
   783     _compilers[0]->set_num_compiler_threads(c1_compiler_count);
   846     _compiler1_objects = NEW_C_HEAP_ARRAY(jobject, _c1_count, mtCompiler);
   784   }
   847     _compiler1_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c1_count, mtCompiler);
   785 
   848   }
   786   int compiler_count = c1_compiler_count + c2_compiler_count;
       
   787 
   849 
   788   char name_buffer[256];
   850   char name_buffer[256];
   789   const bool compiler_thread = true;
   851 
   790   for (int i = 0; i < c2_compiler_count; i++) {
   852   for (int i = 0; i < _c2_count; i++) {
   791     // Create a name for our thread.
   853     // Create a name for our thread.
   792     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
   854     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
   793     CompilerCounters* counters = new CompilerCounters();
   855     jobject thread_handle = JNIHandles::make_global(create_thread_oop(name_buffer, THREAD));
   794     make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK);
   856     _compiler2_objects[i] = thread_handle;
   795   }
   857     _compiler2_logs[i] = NULL;
   796 
   858 
   797   for (int i = c2_compiler_count; i < compiler_count; i++) {
   859     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
       
   860       JavaThread *ct = make_thread(thread_handle, _c2_compile_queue, _compilers[1], /* compiler_thread */ true, CHECK);
       
   861       assert(ct != NULL, "should have been handled for initial thread");
       
   862       _compilers[1]->set_num_compiler_threads(i + 1);
       
   863       if (TraceCompilerThreads) {
       
   864         ResourceMark rm;
       
   865         MutexLocker mu(Threads_lock);
       
   866         tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
       
   867       }
       
   868     }
       
   869   }
       
   870 
       
   871   for (int i = 0; i < _c1_count; i++) {
   798     // Create a name for our thread.
   872     // Create a name for our thread.
   799     sprintf(name_buffer, "C1 CompilerThread%d", i);
   873     sprintf(name_buffer, "C1 CompilerThread%d", i);
   800     CompilerCounters* counters = new CompilerCounters();
   874     jobject thread_handle = JNIHandles::make_global(create_thread_oop(name_buffer, THREAD));
   801     // C1
   875     _compiler1_objects[i] = thread_handle;
   802     make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK);
   876     _compiler1_logs[i] = NULL;
       
   877 
       
   878     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
       
   879       JavaThread *ct = make_thread(thread_handle, _c1_compile_queue, _compilers[0], /* compiler_thread */ true, CHECK);
       
   880       assert(ct != NULL, "should have been handled for initial thread");
       
   881       _compilers[0]->set_num_compiler_threads(i + 1);
       
   882       if (TraceCompilerThreads) {
       
   883         ResourceMark rm;
       
   884         MutexLocker mu(Threads_lock);
       
   885         tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
       
   886       }
       
   887     }
   803   }
   888   }
   804 
   889 
   805   if (UsePerfData) {
   890   if (UsePerfData) {
   806     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
   891     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, _c1_count + _c2_count, CHECK);
   807   }
   892   }
   808 
   893 
   809   if (MethodFlushing) {
   894   if (MethodFlushing) {
   810     // Initialize the sweeper thread
   895     // Initialize the sweeper thread
   811     make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK);
   896     jobject thread_handle = JNIHandles::make_local(THREAD, create_thread_oop("Sweeper thread", THREAD)());
   812   }
   897     make_thread(thread_handle, NULL, NULL, /* compiler_thread */ false, CHECK);
       
   898   }
       
   899 }
       
   900 
       
   901 void CompileBroker::possibly_add_compiler_threads() {
       
   902   EXCEPTION_MARK;
       
   903 
       
   904   julong available_memory = os::available_memory();
       
   905   // Only do attempt to start additional threads if the lock is free.
       
   906   if (!CompileThread_lock->try_lock()) return;
       
   907 
       
   908   if (_c2_compile_queue != NULL) {
       
   909     int old_c2_count = _compilers[1]->num_compiler_threads();
       
   910     int new_c2_count = MIN3(_c2_count,
       
   911         _c2_compile_queue->size() / 2,
       
   912         (int)(available_memory / 200*M));
       
   913 
       
   914     for (int i = old_c2_count; i < new_c2_count; i++) {
       
   915       JavaThread *ct = make_thread(compiler2_object(i), _c2_compile_queue, _compilers[1], true, CHECK);
       
   916       if (ct == NULL) break;
       
   917       _compilers[1]->set_num_compiler_threads(i + 1);
       
   918       if (TraceCompilerThreads) {
       
   919         ResourceMark rm;
       
   920         MutexLocker mu(Threads_lock);
       
   921         tty->print_cr("Added compiler thread %s (available memory: %dMB)",
       
   922                       ct->get_thread_name(), (int)(available_memory/M));
       
   923       }
       
   924     }
       
   925   }
       
   926 
       
   927   if (_c1_compile_queue != NULL) {
       
   928     int old_c1_count = _compilers[0]->num_compiler_threads();
       
   929     int new_c1_count = MIN3(_c1_count,
       
   930         _c1_compile_queue->size() / 4,
       
   931         (int)(available_memory / 100*M));
       
   932 
       
   933     for (int i = old_c1_count; i < new_c1_count; i++) {
       
   934       JavaThread *ct = make_thread(compiler1_object(i), _c1_compile_queue, _compilers[0], true, CHECK);
       
   935       if (ct == NULL) break;
       
   936       _compilers[0]->set_num_compiler_threads(i + 1);
       
   937       if (TraceCompilerThreads) {
       
   938         ResourceMark rm;
       
   939         MutexLocker mu(Threads_lock);
       
   940         tty->print_cr("Added compiler thread %s (available memory: %dMB)",
       
   941                       ct->get_thread_name(), (int)(available_memory/M));
       
   942       }
       
   943     }
       
   944   }
       
   945 
       
   946   CompileThread_lock->unlock();
   813 }
   947 }
   814 
   948 
   815 
   949 
   816 /**
   950 /**
   817  * Set the methods on the stack as on_stack so that redefine classes doesn't
   951  * Set the methods on the stack as on_stack so that redefine classes doesn't
  1544     // the compiler runtime(s) (e.g.,  nmethod::is_compiled_by_c1()) which then
  1678     // the compiler runtime(s) (e.g.,  nmethod::is_compiled_by_c1()) which then
  1545     // fail. This can be done later if necessary.
  1679     // fail. This can be done later if necessary.
  1546   }
  1680   }
  1547 }
  1681 }
  1548 
  1682 
       
  1683 /**
       
  1684  * Helper function to create new or reuse old CompileLog.
       
  1685  */
       
  1686 CompileLog* CompileBroker::get_log(CompilerThread* ct) {
       
  1687   if (!LogCompilation) return NULL;
       
  1688 
       
  1689   AbstractCompiler *compiler = ct->compiler();
       
  1690   bool c1 = compiler->is_c1();
       
  1691   jobject* compiler_objects = c1 ? _compiler1_objects : _compiler2_objects;
       
  1692   assert(compiler_objects != NULL, "must be initialized at this point");
       
  1693   CompileLog** logs = c1 ? _compiler1_logs : _compiler2_logs;
       
  1694   assert(logs != NULL, "must be initialized at this point");
       
  1695   int count = c1 ? _c1_count : _c2_count;
       
  1696 
       
  1697   // Find Compiler number by its threadObj.
       
  1698   oop compiler_obj = ct->threadObj();
       
  1699   int compiler_number = 0;
       
  1700   bool found = false;
       
  1701   for (; compiler_number < count; compiler_number++) {
       
  1702     if (oopDesc::equals(JNIHandles::resolve_non_null(compiler_objects[compiler_number]), compiler_obj)) {
       
  1703       found = true;
       
  1704       break;
       
  1705     }
       
  1706   }
       
  1707   assert(found, "Compiler must exist at this point");
       
  1708 
       
  1709   // Determine pointer for this thread's log.
       
  1710   CompileLog** log_ptr = &logs[compiler_number];
       
  1711 
       
  1712   // Return old one if it exists.
       
  1713   CompileLog* log = *log_ptr;
       
  1714   if (log != NULL) {
       
  1715     ct->init_log(log);
       
  1716     return log;
       
  1717   }
       
  1718 
       
  1719   // Create a new one and remember it.
       
  1720   init_compiler_thread_log();
       
  1721   log = ct->log();
       
  1722   *log_ptr = log;
       
  1723   return log;
       
  1724 }
       
  1725 
  1549 // ------------------------------------------------------------------
  1726 // ------------------------------------------------------------------
  1550 // CompileBroker::compiler_thread_loop
  1727 // CompileBroker::compiler_thread_loop
  1551 //
  1728 //
  1552 // The main loop run by a CompilerThread.
  1729 // The main loop run by a CompilerThread.
  1553 void CompileBroker::compiler_thread_loop() {
  1730 void CompileBroker::compiler_thread_loop() {
  1566       ciObjectFactory::initialize();
  1743       ciObjectFactory::initialize();
  1567     }
  1744     }
  1568   }
  1745   }
  1569 
  1746 
  1570   // Open a log.
  1747   // Open a log.
  1571   if (LogCompilation) {
  1748   CompileLog* log = get_log(thread);
  1572     init_compiler_thread_log();
       
  1573   }
       
  1574   CompileLog* log = thread->log();
       
  1575   if (log != NULL) {
  1749   if (log != NULL) {
  1576     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
  1750     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
  1577                     thread->name(),
  1751                     thread->name(),
  1578                     os::current_thread_id(),
  1752                     os::current_thread_id(),
  1579                     os::current_process_id());
  1753                     os::current_process_id());
  1583 
  1757 
  1584   // If compiler thread/runtime initialization fails, exit the compiler thread
  1758   // If compiler thread/runtime initialization fails, exit the compiler thread
  1585   if (!init_compiler_runtime()) {
  1759   if (!init_compiler_runtime()) {
  1586     return;
  1760     return;
  1587   }
  1761   }
       
  1762 
       
  1763   thread->start_idle_timer();
  1588 
  1764 
  1589   // Poll for new compilation tasks as long as the JVM runs. Compilation
  1765   // Poll for new compilation tasks as long as the JVM runs. Compilation
  1590   // should only be disabled if something went wrong while initializing the
  1766   // should only be disabled if something went wrong while initializing the
  1591   // compiler runtimes. This, in turn, should not happen. The only known case
  1767   // compiler runtimes. This, in turn, should not happen. The only known case
  1592   // when compiler runtime initialization fails is if there is not enough free
  1768   // when compiler runtime initialization fails is if there is not enough free
  1595     // We need this HandleMark to avoid leaking VM handles.
  1771     // We need this HandleMark to avoid leaking VM handles.
  1596     HandleMark hm(thread);
  1772     HandleMark hm(thread);
  1597 
  1773 
  1598     CompileTask* task = queue->get();
  1774     CompileTask* task = queue->get();
  1599     if (task == NULL) {
  1775     if (task == NULL) {
       
  1776       if (UseDynamicNumberOfCompilerThreads) {
       
  1777         // Access compiler_count under lock to enforce consistency.
       
  1778         MutexLocker only_one(CompileThread_lock);
       
  1779         if (can_remove(thread, true)) {
       
  1780           if (TraceCompilerThreads) {
       
  1781             tty->print_cr("Removing compiler thread %s after " JLONG_FORMAT " ms idle time",
       
  1782                           thread->name(), thread->idle_time_millis());
       
  1783           }
       
  1784           return; // Stop this thread.
       
  1785         }
       
  1786       }
  1600       continue;
  1787       continue;
       
  1788     }
       
  1789 
       
  1790     if (UseDynamicNumberOfCompilerThreads) {
       
  1791       possibly_add_compiler_threads();
  1601     }
  1792     }
  1602 
  1793 
  1603     // Give compiler threads an extra quanta.  They tend to be bursty and
  1794     // Give compiler threads an extra quanta.  They tend to be bursty and
  1604     // this helps the compiler to finish up the job.
  1795     // this helps the compiler to finish up the job.
  1605     if (CompilerThreadHintNoPreempt) {
  1796     if (CompilerThreadHintNoPreempt) {
  1616     // Never compile a method if breakpoints are present in it
  1807     // Never compile a method if breakpoints are present in it
  1617     if (method()->number_of_breakpoints() == 0) {
  1808     if (method()->number_of_breakpoints() == 0) {
  1618       // Compile the method.
  1809       // Compile the method.
  1619       if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
  1810       if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
  1620         invoke_compiler_on_method(task);
  1811         invoke_compiler_on_method(task);
       
  1812         thread->start_idle_timer();
  1621       } else {
  1813       } else {
  1622         // After compilation is disabled, remove remaining methods from queue
  1814         // After compilation is disabled, remove remaining methods from queue
  1623         method->clear_queued_for_compilation();
  1815         method->clear_queued_for_compilation();
  1624         task->set_failure_reason("compilation is disabled");
  1816         task->set_failure_reason("compilation is disabled");
  1625       }
  1817       }