210 if (LogEvents) { 211 _compilation_log = new CompilationLog(); 212 } 213 214 // init directives stack, adding default directive 215 DirectivesStack::init(); 216 217 if (DirectivesParser::has_file()) { 218 return DirectivesParser::parse_from_flag(); 219 } else if (CompilerDirectivesPrint) { 220 // Print default directive even when no other was added 221 DirectivesStack::print(tty); 222 } 223 224 return true; 225 } 226 227 CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) { 228 CompilerThread* thread = CompilerThread::current(); 229 thread->set_task(task); 230 CompileLog* log = thread->log(); 231 if (log != NULL) task->log_task_start(log); 232 } 233 234 CompileTaskWrapper::~CompileTaskWrapper() { 235 CompilerThread* thread = CompilerThread::current(); 236 CompileTask* task = thread->task(); 237 CompileLog* log = thread->log(); 238 if (log != NULL) task->log_task_done(log); 239 thread->set_task(NULL); 240 task->set_code_handle(NULL); 241 thread->set_env(NULL); 242 if (task->is_blocking()) { 243 bool free_task = false; 244 { 245 MutexLocker notifier(task->lock(), thread); 246 task->mark_complete(); 247 #if INCLUDE_JVMCI 248 if (CompileBroker::compiler(task->comp_level())->is_jvmci() && 249 !task->has_waiter()) { 250 // The waiting thread timed out and thus did not free the task. 251 free_task = true; 252 } 253 #endif 254 if (!free_task) { 255 // Notify the waiting thread that the compilation has completed 256 // so that it can free the task. 257 task->lock()->notify_all(); 258 } 259 } 260 if (free_task) { 261 // The task can only be freed once the task lock is released. 262 CompileTask::free(task); 263 } 264 } else { 265 task->mark_complete(); 266 267 // By convention, the compiling thread is responsible for 268 // recycling a non-blocking CompileTask. 269 CompileTask::free(task); 270 } 271 } 272 1315 // 1316 // Create a CompileTask object representing the current request for 1317 // compilation. Add this task to the queue. 1318 CompileTask* CompileBroker::create_compile_task(CompileQueue* queue, 1319 int compile_id, 1320 const methodHandle& method, 1321 int osr_bci, 1322 int comp_level, 1323 const methodHandle& hot_method, 1324 int hot_count, 1325 const char* comment, 1326 bool blocking) { 1327 CompileTask* new_task = CompileTask::allocate(); 1328 new_task->initialize(compile_id, method, osr_bci, comp_level, 1329 hot_method, hot_count, comment, 1330 blocking); 1331 queue->add(new_task); 1332 return new_task; 1333 } 1334 1335 // 1 second should be long enough to complete most JVMCI compilations 1336 // and not too long to stall a blocking JVMCI compilation that 1337 // is trying to acquire a lock held by the app thread that submitted the 1338 // compilation. 1339 static const long BLOCKING_JVMCI_COMPILATION_TIMEOUT = 1000; 1340 1341 /** 1342 * Wait for the compilation task to complete. 1343 */ 1344 void CompileBroker::wait_for_completion(CompileTask* task) { 1345 if (CIPrintCompileQueue) { 1346 ttyLocker ttyl; 1347 tty->print_cr("BLOCKING FOR COMPILE"); 1348 } 1349 1350 assert(task->is_blocking(), "can only wait on blocking task"); 1351 1352 JavaThread* thread = JavaThread::current(); 1353 thread->set_blocked_on_compilation(true); 1354 1355 methodHandle method(thread, task->method()); 1356 bool free_task; 1357 #if INCLUDE_JVMCI 1358 if (compiler(task->comp_level())->is_jvmci()) { 1359 MutexLocker waiter(task->lock(), thread); 1360 // No need to check if compilation has completed - just 1361 // rely on the time out. The JVMCI compiler thread will 1362 // recycle the CompileTask. 1363 task->lock()->wait(!Mutex::_no_safepoint_check_flag, BLOCKING_JVMCI_COMPILATION_TIMEOUT); 1364 // If the compilation completes while has_waiter is true then 1365 // this thread is responsible for freeing the task. Otherwise 1366 // the compiler thread will free the task. 1367 task->clear_waiter(); 1368 free_task = task->is_complete(); 1369 } else 1370 #endif 1371 { 1372 MutexLocker waiter(task->lock(), thread); 1373 free_task = true; 1374 while (!task->is_complete() && !is_compilation_disabled_forever()) { 1375 task->lock()->wait(); 1376 } 1377 } 1378 1379 thread->set_blocked_on_compilation(false); 1380 if (free_task) { 1381 if (is_compilation_disabled_forever()) { 1382 CompileTask::free(task); 1383 return; 1384 } 1385 1386 // It is harmless to check this status without the lock, because 1387 // completion is a stable property (until the task object is recycled). 1388 assert(task->is_complete(), "Compilation should have completed"); | 210 if (LogEvents) { 211 _compilation_log = new CompilationLog(); 212 } 213 214 // init directives stack, adding default directive 215 DirectivesStack::init(); 216 217 if (DirectivesParser::has_file()) { 218 return DirectivesParser::parse_from_flag(); 219 } else if (CompilerDirectivesPrint) { 220 // Print default directive even when no other was added 221 DirectivesStack::print(tty); 222 } 223 224 return true; 225 } 226 227 CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) { 228 CompilerThread* thread = CompilerThread::current(); 229 thread->set_task(task); 230 #if INCLUDE_JVMCI 231 if (task->is_blocking() && CompileBroker::compiler(task->comp_level())->is_jvmci()) { 232 task->set_jvmci_compiler_thread(thread); 233 } 234 #endif 235 CompileLog* log = thread->log(); 236 if (log != NULL) task->log_task_start(log); 237 } 238 239 CompileTaskWrapper::~CompileTaskWrapper() { 240 CompilerThread* thread = CompilerThread::current(); 241 CompileTask* task = thread->task(); 242 CompileLog* log = thread->log(); 243 if (log != NULL) task->log_task_done(log); 244 thread->set_task(NULL); 245 task->set_code_handle(NULL); 246 thread->set_env(NULL); 247 if (task->is_blocking()) { 248 bool free_task = false; 249 { 250 MutexLocker notifier(task->lock(), thread); 251 task->mark_complete(); 252 #if INCLUDE_JVMCI 253 if (CompileBroker::compiler(task->comp_level())->is_jvmci()) { 254 if (!task->has_waiter()) { 255 // The waiting thread timed out and thus did not free the task. 256 free_task = true; 257 } 258 task->set_jvmci_compiler_thread(NULL); 259 } 260 #endif 261 if (!free_task) { 262 // Notify the waiting thread that the compilation has completed 263 // so that it can free the task. 264 task->lock()->notify_all(); 265 } 266 } 267 if (free_task) { 268 // The task can only be freed once the task lock is released. 269 CompileTask::free(task); 270 } 271 } else { 272 task->mark_complete(); 273 274 // By convention, the compiling thread is responsible for 275 // recycling a non-blocking CompileTask. 276 CompileTask::free(task); 277 } 278 } 279 1322 // 1323 // Create a CompileTask object representing the current request for 1324 // compilation. Add this task to the queue. 1325 CompileTask* CompileBroker::create_compile_task(CompileQueue* queue, 1326 int compile_id, 1327 const methodHandle& method, 1328 int osr_bci, 1329 int comp_level, 1330 const methodHandle& hot_method, 1331 int hot_count, 1332 const char* comment, 1333 bool blocking) { 1334 CompileTask* new_task = CompileTask::allocate(); 1335 new_task->initialize(compile_id, method, osr_bci, comp_level, 1336 hot_method, hot_count, comment, 1337 blocking); 1338 queue->add(new_task); 1339 return new_task; 1340 } 1341 1342 #if INCLUDE_JVMCI 1343 // The number of milliseconds to wait before checking if the 1344 // JVMCI compiler thread is blocked. 1345 static const long BLOCKING_JVMCI_COMPILATION_WAIT_TIMESLICE = 500; 1346 1347 // The number of successive times the above check is allowed to 1348 // see a blocked JVMCI compiler thread before unblocking the 1349 // thread waiting for the compilation to finish. 1350 static const int BLOCKING_JVMCI_COMPILATION_WAIT_TO_UNBLOCK_ATTEMPTS = 5; 1351 1352 /** 1353 * Waits for a JVMCI compiler to complete a given task. This thread 1354 * waits until either the task completes or it sees the JVMCI compiler 1355 * thread is blocked for N consecutive milliseconds where N is 1356 * BLOCKING_JVMCI_COMPILATION_WAIT_TIMESLICE * 1357 * BLOCKING_JVMCI_COMPILATION_WAIT_TO_UNBLOCK_ATTEMPTS. 1358 * 1359 * @return true if this thread needs to free/recycle the task 1360 */ 1361 bool CompileBroker::wait_for_jvmci_completion(CompileTask* task, JavaThread* thread) { 1362 MutexLocker waiter(task->lock(), thread); 1363 int consecutively_blocked = 0; 1364 while (task->lock()->wait(!Mutex::_no_safepoint_check_flag, BLOCKING_JVMCI_COMPILATION_WAIT_TIMESLICE)) { 1365 CompilerThread* jvmci_compiler_thread = task->jvmci_compiler_thread(); 1366 if (jvmci_compiler_thread != NULL) { 1367 JavaThreadState state; 1368 { 1369 // A JVMCI compiler thread should not disappear at this point 1370 // but let's be extra safe. 1371 MutexLocker mu(Threads_lock, thread); 1372 state = jvmci_compiler_thread->thread_state(); 1373 } 1374 if (state == _thread_blocked) { 1375 if (++consecutively_blocked == BLOCKING_JVMCI_COMPILATION_WAIT_TO_UNBLOCK_ATTEMPTS) { 1376 if (PrintCompilation) { 1377 task->print(tty, "wait for blocking compilation timed out"); 1378 } 1379 break; 1380 } 1381 } else { 1382 consecutively_blocked = 0; 1383 } 1384 } else { 1385 // Still waiting on JVMCI compiler queue 1386 } 1387 } 1388 task->clear_waiter(); 1389 return task->is_complete(); 1390 } 1391 #endif 1392 1393 /** 1394 * Wait for the compilation task to complete. 1395 */ 1396 void CompileBroker::wait_for_completion(CompileTask* task) { 1397 if (CIPrintCompileQueue) { 1398 ttyLocker ttyl; 1399 tty->print_cr("BLOCKING FOR COMPILE"); 1400 } 1401 1402 assert(task->is_blocking(), "can only wait on blocking task"); 1403 1404 JavaThread* thread = JavaThread::current(); 1405 thread->set_blocked_on_compilation(true); 1406 1407 methodHandle method(thread, task->method()); 1408 bool free_task; 1409 #if INCLUDE_JVMCI 1410 if (compiler(task->comp_level())->is_jvmci()) { 1411 free_task = wait_for_jvmci_completion(task, thread); 1412 } else 1413 #endif 1414 { 1415 MutexLocker waiter(task->lock(), thread); 1416 free_task = true; 1417 while (!task->is_complete() && !is_compilation_disabled_forever()) { 1418 task->lock()->wait(); 1419 } 1420 } 1421 1422 thread->set_blocked_on_compilation(false); 1423 if (free_task) { 1424 if (is_compilation_disabled_forever()) { 1425 CompileTask::free(task); 1426 return; 1427 } 1428 1429 // It is harmless to check this status without the lock, because 1430 // completion is a stable property (until the task object is recycled). 1431 assert(task->is_complete(), "Compilation should have completed"); |