1.1 --- a/src/share/vm/compiler/compileBroker.cpp Thu Oct 30 10:51:06 2014 +0100 1.2 +++ b/src/share/vm/compiler/compileBroker.cpp Tue Apr 29 07:59:22 2014 +0200 1.3 @@ -183,9 +183,8 @@ 1.4 1.5 long CompileBroker::_peak_compilation_time = 0; 1.6 1.7 -CompileQueue* CompileBroker::_c2_method_queue = NULL; 1.8 -CompileQueue* CompileBroker::_c1_method_queue = NULL; 1.9 -CompileTask* CompileBroker::_task_free_list = NULL; 1.10 +CompileQueue* CompileBroker::_c2_compile_queue = NULL; 1.11 +CompileQueue* CompileBroker::_c1_compile_queue = NULL; 1.12 1.13 GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL; 1.14 1.15 @@ -253,13 +252,56 @@ 1.16 1.17 // By convention, the compiling thread is responsible for 1.18 // recycling a non-blocking CompileTask. 1.19 - CompileBroker::free_task(task); 1.20 + CompileTask::free(task); 1.21 } 1.22 } 1.23 1.24 1.25 -// ------------------------------------------------------------------ 1.26 -// CompileTask::initialize 1.27 +CompileTask* CompileTask::_task_free_list = NULL; 1.28 +#ifdef ASSERT 1.29 +int CompileTask::_num_allocated_tasks = 0; 1.30 +#endif 1.31 +/** 1.32 + * Allocate a CompileTask, from the free list if possible. 1.33 + */ 1.34 +CompileTask* CompileTask::allocate() { 1.35 + MutexLocker locker(CompileTaskAlloc_lock); 1.36 + CompileTask* task = NULL; 1.37 + 1.38 + if (_task_free_list != NULL) { 1.39 + task = _task_free_list; 1.40 + _task_free_list = task->next(); 1.41 + task->set_next(NULL); 1.42 + } else { 1.43 + task = new CompileTask(); 1.44 + DEBUG_ONLY(_num_allocated_tasks++;) 1.45 + assert (_num_allocated_tasks < 10000, "Leaking compilation tasks?"); 1.46 + task->set_next(NULL); 1.47 + task->set_is_free(true); 1.48 + } 1.49 + assert(task->is_free(), "Task must be free."); 1.50 + task->set_is_free(false); 1.51 + return task; 1.52 +} 1.53 + 1.54 + 1.55 +/** 1.56 + * Add a task to the free list. 1.57 + */ 1.58 +void CompileTask::free(CompileTask* task) { 1.59 + MutexLocker locker(CompileTaskAlloc_lock); 1.60 + if (!task->is_free()) { 1.61 + task->set_code(NULL); 1.62 + assert(!task->lock()->is_locked(), "Should not be locked when freed"); 1.63 + JNIHandles::destroy_global(task->_method_holder); 1.64 + JNIHandles::destroy_global(task->_hot_method_holder); 1.65 + 1.66 + task->set_is_free(true); 1.67 + task->set_next(_task_free_list); 1.68 + _task_free_list = task; 1.69 + } 1.70 +} 1.71 + 1.72 void CompileTask::initialize(int compile_id, 1.73 methodHandle method, 1.74 int osr_bci, 1.75 @@ -318,15 +360,6 @@ 1.76 if (nm == NULL) _code_handle = NULL; // drop the handle also 1.77 } 1.78 1.79 -// ------------------------------------------------------------------ 1.80 -// CompileTask::free 1.81 -void CompileTask::free() { 1.82 - set_code(NULL); 1.83 - assert(!_lock->is_locked(), "Should not be locked when freed"); 1.84 - JNIHandles::destroy_global(_method_holder); 1.85 - JNIHandles::destroy_global(_hot_method_holder); 1.86 -} 1.87 - 1.88 1.89 void CompileTask::mark_on_stack() { 1.90 // Mark these methods as something redefine classes cannot remove. 1.91 @@ -594,9 +627,12 @@ 1.92 1.93 1.94 1.95 -// Add a CompileTask to a CompileQueue 1.96 +/** 1.97 + * Add a CompileTask to a CompileQueue 1.98 + */ 1.99 void CompileQueue::add(CompileTask* task) { 1.100 assert(lock()->owned_by_self(), "must own lock"); 1.101 + assert(!CompileBroker::is_compilation_disabled_forever(), "Do not add task if compilation is turned off forever"); 1.102 1.103 task->set_next(NULL); 1.104 task->set_prev(NULL); 1.105 @@ -618,9 +654,7 @@ 1.106 // Mark the method as being in the compile queue. 1.107 task->method()->set_queued_for_compilation(); 1.108 1.109 - if (CIPrintCompileQueue) { 1.110 - print(); 1.111 - } 1.112 + NOT_PRODUCT(print();) 1.113 1.114 if (LogCompilation && xtty != NULL) { 1.115 task->log_task_queued(); 1.116 @@ -630,14 +664,19 @@ 1.117 lock()->notify_all(); 1.118 } 1.119 1.120 -void CompileQueue::delete_all() { 1.121 - assert(lock()->owned_by_self(), "must own lock"); 1.122 +void CompileQueue::free_all() { 1.123 + MutexLocker mu(lock()); 1.124 if (_first != NULL) { 1.125 for (CompileTask* task = _first; task != NULL; task = task->next()) { 1.126 - delete task; 1.127 + // Wake up thread that blocks on the compile task. 1.128 + task->lock()->notify(); 1.129 + // Puts task back on the freelist. 1.130 + CompileTask::free(task); 1.131 } 1.132 _first = NULL; 1.133 } 1.134 + // Wake up all threads that block on the queue. 1.135 + lock()->notify_all(); 1.136 } 1.137 1.138 // ------------------------------------------------------------------ 1.139 @@ -767,18 +806,24 @@ 1.140 } 1.141 } 1.142 1.143 -// ------------------------------------------------------------------ 1.144 -// CompileQueue::print 1.145 +#ifndef PRODUCT 1.146 +/** 1.147 + * Print entire compilation queue. 1.148 + */ 1.149 void CompileQueue::print() { 1.150 - tty->print_cr("Contents of %s", name()); 1.151 - tty->print_cr("----------------------"); 1.152 - CompileTask* task = _first; 1.153 - while (task != NULL) { 1.154 - task->print_line(); 1.155 - task = task->next(); 1.156 + if (CIPrintCompileQueue) { 1.157 + ttyLocker ttyl; 1.158 + tty->print_cr("Contents of %s", name()); 1.159 + tty->print_cr("----------------------"); 1.160 + CompileTask* task = _first; 1.161 + while (task != NULL) { 1.162 + task->print_line(); 1.163 + task = task->next(); 1.164 + } 1.165 + tty->print_cr("----------------------"); 1.166 } 1.167 - tty->print_cr("----------------------"); 1.168 } 1.169 +#endif // PRODUCT 1.170 1.171 CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) { 1.172 1.173 @@ -851,9 +896,6 @@ 1.174 _compilers[1] = new SharkCompiler(); 1.175 #endif // SHARK 1.176 1.177 - // Initialize the CompileTask free list 1.178 - _task_free_list = NULL; 1.179 - 1.180 // Start the CompilerThreads 1.181 init_compiler_threads(c1_count, c2_count); 1.182 // totalTime performance counter is always created as it is required 1.183 @@ -1046,11 +1088,11 @@ 1.184 #endif // !ZERO && !SHARK 1.185 // Initialize the compilation queue 1.186 if (c2_compiler_count > 0) { 1.187 - _c2_method_queue = new CompileQueue("C2MethodQueue", MethodCompileQueue_lock); 1.188 + _c2_compile_queue = new CompileQueue("C2 CompileQueue", MethodCompileQueue_lock); 1.189 _compilers[1]->set_num_compiler_threads(c2_compiler_count); 1.190 } 1.191 if (c1_compiler_count > 0) { 1.192 - _c1_method_queue = new CompileQueue("C1MethodQueue", MethodCompileQueue_lock); 1.193 + _c1_compile_queue = new CompileQueue("C1 CompileQueue", MethodCompileQueue_lock); 1.194 _compilers[0]->set_num_compiler_threads(c1_compiler_count); 1.195 } 1.196 1.197 @@ -1065,7 +1107,7 @@ 1.198 sprintf(name_buffer, "C2 CompilerThread%d", i); 1.199 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1.200 // Shark and C2 1.201 - CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK); 1.202 + CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], CHECK); 1.203 _compiler_threads->append(new_thread); 1.204 } 1.205 1.206 @@ -1074,7 +1116,7 @@ 1.207 sprintf(name_buffer, "C1 CompilerThread%d", i); 1.208 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1.209 // C1 1.210 - CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK); 1.211 + CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], CHECK); 1.212 _compiler_threads->append(new_thread); 1.213 } 1.214 1.215 @@ -1084,14 +1126,18 @@ 1.216 } 1.217 1.218 1.219 -// Set the methods on the stack as on_stack so that redefine classes doesn't 1.220 -// reclaim them 1.221 +/** 1.222 + * Set the methods on the stack as on_stack so that redefine classes doesn't 1.223 + * reclaim them 1.224 + */ 1.225 void CompileBroker::mark_on_stack() { 1.226 - if (_c2_method_queue != NULL) { 1.227 - _c2_method_queue->mark_on_stack(); 1.228 + if (_c2_compile_queue != NULL) { 1.229 + MutexLocker locker(_c2_compile_queue->lock()); 1.230 + _c2_compile_queue->mark_on_stack(); 1.231 } 1.232 - if (_c1_method_queue != NULL) { 1.233 - _c1_method_queue->mark_on_stack(); 1.234 + if (_c1_compile_queue != NULL) { 1.235 + MutexLocker locker(_c1_compile_queue->lock()); 1.236 + _c1_compile_queue->mark_on_stack(); 1.237 } 1.238 } 1.239 1.240 @@ -1107,7 +1153,7 @@ 1.241 const char* comment, 1.242 Thread* thread) { 1.243 // do nothing if compiler thread(s) is not available 1.244 - if (!_initialized ) { 1.245 + if (!_initialized) { 1.246 return; 1.247 } 1.248 1.249 @@ -1154,7 +1200,7 @@ 1.250 1.251 // If this method is already in the compile queue, then 1.252 // we do not block the current thread. 1.253 - if (compilation_is_in_queue(method, osr_bci)) { 1.254 + if (compilation_is_in_queue(method)) { 1.255 // We may want to decay our counter a bit here to prevent 1.256 // multiple denied requests for compilation. This is an 1.257 // open compilation policy issue. Note: The other possibility, 1.258 @@ -1193,7 +1239,7 @@ 1.259 // Make sure the method has not slipped into the queues since 1.260 // last we checked; note that those checks were "fast bail-outs". 1.261 // Here we need to be more careful, see 14012000 below. 1.262 - if (compilation_is_in_queue(method, osr_bci)) { 1.263 + if (compilation_is_in_queue(method)) { 1.264 return; 1.265 } 1.266 1.267 @@ -1214,7 +1260,7 @@ 1.268 } 1.269 1.270 // Should this thread wait for completion of the compile? 1.271 - blocking = is_compile_blocking(method, osr_bci); 1.272 + blocking = is_compile_blocking(); 1.273 1.274 // We will enter the compilation in the queue. 1.275 // 14012000: Note that this sets the queued_for_compile bits in 1.276 @@ -1406,19 +1452,17 @@ 1.277 } 1.278 1.279 1.280 -// ------------------------------------------------------------------ 1.281 -// CompileBroker::compilation_is_in_queue 1.282 -// 1.283 -// See if this compilation is already requested. 1.284 -// 1.285 -// Implementation note: there is only a single "is in queue" bit 1.286 -// for each method. This means that the check below is overly 1.287 -// conservative in the sense that an osr compilation in the queue 1.288 -// will block a normal compilation from entering the queue (and vice 1.289 -// versa). This can be remedied by a full queue search to disambiguate 1.290 -// cases. If it is deemed profitible, this may be done. 1.291 -bool CompileBroker::compilation_is_in_queue(methodHandle method, 1.292 - int osr_bci) { 1.293 +/** 1.294 + * See if this compilation is already requested. 1.295 + * 1.296 + * Implementation note: there is only a single "is in queue" bit 1.297 + * for each method. This means that the check below is overly 1.298 + * conservative in the sense that an osr compilation in the queue 1.299 + * will block a normal compilation from entering the queue (and vice 1.300 + * versa). This can be remedied by a full queue search to disambiguate 1.301 + * cases. If it is deemed profitable, this may be done. 1.302 + */ 1.303 +bool CompileBroker::compilation_is_in_queue(methodHandle method) { 1.304 return method->queued_for_compilation(); 1.305 } 1.306 1.307 @@ -1498,13 +1542,11 @@ 1.308 #endif 1.309 } 1.310 1.311 - 1.312 -// ------------------------------------------------------------------ 1.313 -// CompileBroker::is_compile_blocking 1.314 -// 1.315 -// Should the current thread be blocked until this compilation request 1.316 -// has been fulfilled? 1.317 -bool CompileBroker::is_compile_blocking(methodHandle method, int osr_bci) { 1.318 +/** 1.319 + * Should the current thread block until this compilation request 1.320 + * has been fulfilled? 1.321 + */ 1.322 +bool CompileBroker::is_compile_blocking() { 1.323 assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock"); 1.324 return !BackgroundCompilation; 1.325 } 1.326 @@ -1532,7 +1574,7 @@ 1.327 int hot_count, 1.328 const char* comment, 1.329 bool blocking) { 1.330 - CompileTask* new_task = allocate_task(); 1.331 + CompileTask* new_task = CompileTask::allocate(); 1.332 new_task->initialize(compile_id, method, osr_bci, comp_level, 1.333 hot_method, hot_count, comment, 1.334 blocking); 1.335 @@ -1541,75 +1583,52 @@ 1.336 } 1.337 1.338 1.339 -// ------------------------------------------------------------------ 1.340 -// CompileBroker::allocate_task 1.341 -// 1.342 -// Allocate a CompileTask, from the free list if possible. 1.343 -CompileTask* CompileBroker::allocate_task() { 1.344 - MutexLocker locker(CompileTaskAlloc_lock); 1.345 - CompileTask* task = NULL; 1.346 - if (_task_free_list != NULL) { 1.347 - task = _task_free_list; 1.348 - _task_free_list = task->next(); 1.349 - task->set_next(NULL); 1.350 - } else { 1.351 - task = new CompileTask(); 1.352 - task->set_next(NULL); 1.353 - } 1.354 - return task; 1.355 -} 1.356 - 1.357 - 1.358 -// ------------------------------------------------------------------ 1.359 -// CompileBroker::free_task 1.360 -// 1.361 -// Add a task to the free list. 1.362 -void CompileBroker::free_task(CompileTask* task) { 1.363 - MutexLocker locker(CompileTaskAlloc_lock); 1.364 - task->free(); 1.365 - task->set_next(_task_free_list); 1.366 - _task_free_list = task; 1.367 -} 1.368 - 1.369 - 1.370 -// ------------------------------------------------------------------ 1.371 -// CompileBroker::wait_for_completion 1.372 -// 1.373 -// Wait for the given method CompileTask to complete. 1.374 +/** 1.375 + * Wait for the compilation task to complete. 1.376 + */ 1.377 void CompileBroker::wait_for_completion(CompileTask* task) { 1.378 if (CIPrintCompileQueue) { 1.379 + ttyLocker ttyl; 1.380 tty->print_cr("BLOCKING FOR COMPILE"); 1.381 } 1.382 1.383 assert(task->is_blocking(), "can only wait on blocking task"); 1.384 1.385 - JavaThread *thread = JavaThread::current(); 1.386 + JavaThread* thread = JavaThread::current(); 1.387 thread->set_blocked_on_compilation(true); 1.388 1.389 methodHandle method(thread, task->method()); 1.390 { 1.391 MutexLocker waiter(task->lock(), thread); 1.392 1.393 - while (!task->is_complete()) 1.394 + while (!task->is_complete() && !is_compilation_disabled_forever()) { 1.395 task->lock()->wait(); 1.396 + } 1.397 } 1.398 + 1.399 + thread->set_blocked_on_compilation(false); 1.400 + if (is_compilation_disabled_forever()) { 1.401 + CompileTask::free(task); 1.402 + return; 1.403 + } 1.404 + 1.405 // It is harmless to check this status without the lock, because 1.406 // completion is a stable property (until the task object is recycled). 1.407 assert(task->is_complete(), "Compilation should have completed"); 1.408 assert(task->code_handle() == NULL, "must be reset"); 1.409 1.410 - thread->set_blocked_on_compilation(false); 1.411 - 1.412 // By convention, the waiter is responsible for recycling a 1.413 // blocking CompileTask. Since there is only one waiter ever 1.414 // waiting on a CompileTask, we know that no one else will 1.415 // be using this CompileTask; we can free it. 1.416 - free_task(task); 1.417 + CompileTask::free(task); 1.418 } 1.419 1.420 -// Initialize compiler thread(s) + compiler object(s). The postcondition 1.421 -// of this function is that the compiler runtimes are initialized and that 1.422 -//compiler threads can start compiling. 1.423 +/** 1.424 + * Initialize compiler thread(s) + compiler object(s). The postcondition 1.425 + * of this function is that the compiler runtimes are initialized and that 1.426 + * compiler threads can start compiling. 1.427 + */ 1.428 bool CompileBroker::init_compiler_runtime() { 1.429 CompilerThread* thread = CompilerThread::current(); 1.430 AbstractCompiler* comp = thread->compiler(); 1.431 @@ -1646,7 +1665,6 @@ 1.432 disable_compilation_forever(); 1.433 // If compiler initialization failed, no compiler thread that is specific to a 1.434 // particular compiler runtime will ever start to compile methods. 1.435 - 1.436 shutdown_compiler_runtime(comp, thread); 1.437 return false; 1.438 } 1.439 @@ -1660,9 +1678,11 @@ 1.440 return true; 1.441 } 1.442 1.443 -// If C1 and/or C2 initialization failed, we shut down all compilation. 1.444 -// We do this to keep things simple. This can be changed if it ever turns out to be 1.445 -// a problem. 1.446 +/** 1.447 + * If C1 and/or C2 initialization failed, we shut down all compilation. 1.448 + * We do this to keep things simple. This can be changed if it ever turns 1.449 + * out to be a problem. 1.450 + */ 1.451 void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) { 1.452 // Free buffer blob, if allocated 1.453 if (thread->get_buffer_blob() != NULL) { 1.454 @@ -1674,28 +1694,25 @@ 1.455 // There are two reasons for shutting down the compiler 1.456 // 1) compiler runtime initialization failed 1.457 // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing 1.458 - warning("Shutting down compiler %s (no space to run compilers)", comp->name()); 1.459 + warning("%s initialization failed. Shutting down all compilers", comp->name()); 1.460 1.461 // Only one thread per compiler runtime object enters here 1.462 // Set state to shut down 1.463 comp->set_shut_down(); 1.464 1.465 - MutexLocker mu(MethodCompileQueue_lock, thread); 1.466 - CompileQueue* queue; 1.467 - if (_c1_method_queue != NULL) { 1.468 - _c1_method_queue->delete_all(); 1.469 - queue = _c1_method_queue; 1.470 - _c1_method_queue = NULL; 1.471 - delete _c1_method_queue; 1.472 + // Delete all queued compilation tasks to make compiler threads exit faster. 1.473 + if (_c1_compile_queue != NULL) { 1.474 + _c1_compile_queue->free_all(); 1.475 } 1.476 1.477 - if (_c2_method_queue != NULL) { 1.478 - _c2_method_queue->delete_all(); 1.479 - queue = _c2_method_queue; 1.480 - _c2_method_queue = NULL; 1.481 - delete _c2_method_queue; 1.482 + if (_c2_compile_queue != NULL) { 1.483 + _c2_compile_queue->free_all(); 1.484 } 1.485 1.486 + // Set flags so that we continue execution with using interpreter only. 1.487 + UseCompiler = false; 1.488 + UseInterpreter = true; 1.489 + 1.490 // We could delete compiler runtimes also. However, there are references to 1.491 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then 1.492 // fail. This can be done later if necessary.