src/hotspot/share/compiler/compileBroker.cpp
changeset 59050 7bbaa3c416e7
parent 58545 725244418646
child 59056 15936b142f86
equal deleted inserted replaced
59049:dc1899bb84c0 59050:7bbaa3c416e7
   297 }
   297 }
   298 
   298 
   299 /**
   299 /**
   300  * Check if a CompilerThread can be removed and update count if requested.
   300  * Check if a CompilerThread can be removed and update count if requested.
   301  */
   301  */
   302 static bool can_remove(CompilerThread *ct, bool do_it) {
   302 bool CompileBroker::can_remove(CompilerThread *ct, bool do_it) {
   303   assert(UseDynamicNumberOfCompilerThreads, "or shouldn't be here");
   303   assert(UseDynamicNumberOfCompilerThreads, "or shouldn't be here");
   304   if (!ReduceNumberOfCompilerThreads) return false;
   304   if (!ReduceNumberOfCompilerThreads) return false;
   305 
   305 
   306   AbstractCompiler *compiler = ct->compiler();
   306   AbstractCompiler *compiler = ct->compiler();
   307   int compiler_count = compiler->num_compiler_threads();
   307   int compiler_count = compiler->num_compiler_threads();
   311   if (compiler_count < 2) return false;
   311   if (compiler_count < 2) return false;
   312 
   312 
   313   // Keep thread alive for at least some time.
   313   // Keep thread alive for at least some time.
   314   if (ct->idle_time_millis() < (c1 ? 500 : 100)) return false;
   314   if (ct->idle_time_millis() < (c1 ? 500 : 100)) return false;
   315 
   315 
       
   316 #if INCLUDE_JVMCI
       
   317   if (compiler->is_jvmci()) {
       
   318     // Handles for JVMCI thread objects may get released concurrently.
       
   319     if (do_it) {
       
   320       assert(CompileThread_lock->owner() == ct, "must be holding lock");
       
   321     } else {
       
   322       // Skip check if it's the last thread and let caller check again.
       
   323       return true;
       
   324     }
       
   325   }
       
   326 #endif
       
   327 
   316   // We only allow the last compiler thread of each type to get removed.
   328   // We only allow the last compiler thread of each type to get removed.
   317   jobject last_compiler = c1 ? CompileBroker::compiler1_object(compiler_count - 1)
   329   jobject last_compiler = c1 ? compiler1_object(compiler_count - 1)
   318                              : CompileBroker::compiler2_object(compiler_count - 1);
   330                              : compiler2_object(compiler_count - 1);
   319   if (ct->threadObj() == JNIHandles::resolve_non_null(last_compiler)) {
   331   if (ct->threadObj() == JNIHandles::resolve_non_null(last_compiler)) {
   320     if (do_it) {
   332     if (do_it) {
   321       assert_locked_or_safepoint(CompileThread_lock); // Update must be consistent.
   333       assert_locked_or_safepoint(CompileThread_lock); // Update must be consistent.
   322       compiler->set_num_compiler_threads(compiler_count - 1);
   334       compiler->set_num_compiler_threads(compiler_count - 1);
       
   335 #if INCLUDE_JVMCI
       
   336       if (compiler->is_jvmci()) {
       
   337         // Old j.l.Thread object can die when no longer referenced elsewhere.
       
   338         JNIHandles::destroy_global(compiler2_object(compiler_count - 1));
       
   339         _compiler2_objects[compiler_count - 1] = NULL;
       
   340       }
       
   341 #endif
   323     }
   342     }
   324     return true;
   343     return true;
   325   }
   344   }
   326   return false;
   345   return false;
   327 }
   346 }
   424     // is not critical and we do not want idle compiler threads to wake up too often.
   443     // is not critical and we do not want idle compiler threads to wake up too often.
   425     locker.wait(5*1000);
   444     locker.wait(5*1000);
   426 
   445 
   427     if (UseDynamicNumberOfCompilerThreads && _first == NULL) {
   446     if (UseDynamicNumberOfCompilerThreads && _first == NULL) {
   428       // Still nothing to compile. Give caller a chance to stop this thread.
   447       // Still nothing to compile. Give caller a chance to stop this thread.
   429       if (can_remove(CompilerThread::current(), false)) return NULL;
   448       if (CompileBroker::can_remove(CompilerThread::current(), false)) return NULL;
   430     }
   449     }
   431   }
   450   }
   432 
   451 
   433   if (CompileBroker::is_compilation_disabled_forever()) {
   452   if (CompileBroker::is_compilation_disabled_forever()) {
   434     return NULL;
   453     return NULL;
   840   }
   859   }
   841 
   860 
   842   char name_buffer[256];
   861   char name_buffer[256];
   843 
   862 
   844   for (int i = 0; i < _c2_count; i++) {
   863   for (int i = 0; i < _c2_count; i++) {
       
   864     jobject thread_handle = NULL;
       
   865     // Create all j.l.Thread objects for C1 and C2 threads here, but only one
       
   866     // for JVMCI compiler which can create further ones on demand.
       
   867     JVMCI_ONLY(if (!UseJVMCICompiler || !UseDynamicNumberOfCompilerThreads || i == 0) {)
   845     // Create a name for our thread.
   868     // Create a name for our thread.
   846     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
   869     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
   847     Handle thread_oop = create_thread_oop(name_buffer, CHECK);
   870     Handle thread_oop = create_thread_oop(name_buffer, CHECK);
   848     jobject thread_handle = JNIHandles::make_global(thread_oop);
   871     thread_handle = JNIHandles::make_global(thread_oop);
       
   872     JVMCI_ONLY(})
   849     _compiler2_objects[i] = thread_handle;
   873     _compiler2_objects[i] = thread_handle;
   850     _compiler2_logs[i] = NULL;
   874     _compiler2_logs[i] = NULL;
   851 
   875 
   852     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
   876     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
   853       JavaThread *ct = make_thread(thread_handle, _c2_compile_queue, _compilers[1], CHECK);
   877       JavaThread *ct = make_thread(thread_handle, _c2_compile_queue, _compilers[1], CHECK);
   910         _c2_compile_queue->size() / 2,
   934         _c2_compile_queue->size() / 2,
   911         (int)(available_memory / (200*M)),
   935         (int)(available_memory / (200*M)),
   912         (int)(available_cc_np / (128*K)));
   936         (int)(available_cc_np / (128*K)));
   913 
   937 
   914     for (int i = old_c2_count; i < new_c2_count; i++) {
   938     for (int i = old_c2_count; i < new_c2_count; i++) {
       
   939 #if INCLUDE_JVMCI
       
   940       if (UseJVMCICompiler) {
       
   941         // Native compiler threads as used in C1/C2 can reuse the j.l.Thread
       
   942         // objects as their existence is completely hidden from the rest of
       
   943         // the VM (and those compiler threads can't call Java code to do the
       
   944         // creation anyway). For JVMCI we have to create new j.l.Thread objects
       
   945         // as they are visible and we can see unexpected thread lifecycle
       
   946         // transitions if we bind them to new JavaThreads.
       
   947         if (!THREAD->can_call_java()) break;
       
   948         char name_buffer[256];
       
   949         sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
       
   950         Handle thread_oop;
       
   951         {
       
   952           // We have to give up the lock temporarily for the Java calls.
       
   953           MutexUnlocker mu(CompileThread_lock);
       
   954           thread_oop = create_thread_oop(name_buffer, THREAD);
       
   955         }
       
   956         if (HAS_PENDING_EXCEPTION) {
       
   957           if (TraceCompilerThreads) {
       
   958             ResourceMark rm;
       
   959             tty->print_cr("JVMCI compiler thread creation failed:");
       
   960             PENDING_EXCEPTION->print();
       
   961           }
       
   962           CLEAR_PENDING_EXCEPTION;
       
   963           break;
       
   964         }
       
   965         // Check if another thread has beaten us during the Java calls.
       
   966         if (_compilers[1]->num_compiler_threads() != i) break;
       
   967         jobject thread_handle = JNIHandles::make_global(thread_oop);
       
   968         assert(compiler2_object(i) == NULL, "Old one must be released!");
       
   969         _compiler2_objects[i] = thread_handle;
       
   970       }
       
   971 #endif
   915       JavaThread *ct = make_thread(compiler2_object(i), _c2_compile_queue, _compilers[1], CHECK);
   972       JavaThread *ct = make_thread(compiler2_object(i), _c2_compile_queue, _compilers[1], CHECK);
   916       if (ct == NULL) break;
   973       if (ct == NULL) break;
   917       _compilers[1]->set_num_compiler_threads(i + 1);
   974       _compilers[1]->set_num_compiler_threads(i + 1);
   918       if (TraceCompilerThreads) {
   975       if (TraceCompilerThreads) {
   919         ResourceMark rm;
   976         ResourceMark rm;