638 // having no compile jobs: First, we compiled everything we wanted. Second, |
645 // having no compile jobs: First, we compiled everything we wanted. Second, |
639 // we ran out of code cache so compilation has been disabled. In the latter |
646 // we ran out of code cache so compilation has been disabled. In the latter |
640 // case we perform code cache sweeps to free memory such that we can re-enable |
647 // case we perform code cache sweeps to free memory such that we can re-enable |
641 // compilation. |
648 // compilation. |
642 while (_first == NULL) { |
649 while (_first == NULL) { |
|
650 // Exit loop if compilation is disabled forever |
|
651 if (CompileBroker::is_compilation_disabled_forever()) { |
|
652 return NULL; |
|
653 } |
|
654 |
643 if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) { |
655 if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) { |
644 // Wait a certain amount of time to possibly do another sweep. |
656 // Wait a certain amount of time to possibly do another sweep. |
645 // We must wait until stack scanning has happened so that we can |
657 // We must wait until stack scanning has happened so that we can |
646 // transition a method's state from 'not_entrant' to 'zombie'. |
658 // transition a method's state from 'not_entrant' to 'zombie'. |
647 long wait_time = NmethodSweepCheckInterval * 1000; |
659 long wait_time = NmethodSweepCheckInterval * 1000; |
662 // (i.e., there is enough free space in the code cache) there is |
674 // (i.e., there is enough free space in the code cache) there is |
663 // no need to invoke the sweeper. As a result, the hotness of methods |
675 // no need to invoke the sweeper. As a result, the hotness of methods |
664 // remains unchanged. This behavior is desired, since we want to keep |
676 // remains unchanged. This behavior is desired, since we want to keep |
665 // the stable state, i.e., we do not want to evict methods from the |
677 // the stable state, i.e., we do not want to evict methods from the |
666 // code cache if it is unnecessary. |
678 // code cache if it is unnecessary. |
667 lock()->wait(); |
679 // We need a timed wait here, since compiler threads can exit if compilation |
668 } |
680 // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads |
669 } |
681 // is not critical and we do not want idle compiler threads to wake up too often. |
|
682 lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000); |
|
683 } |
|
684 } |
|
685 |
|
686 if (CompileBroker::is_compilation_disabled_forever()) { |
|
687 return NULL; |
|
688 } |
|
689 |
670 CompileTask* task = CompilationPolicy::policy()->select_task(this); |
690 CompileTask* task = CompilationPolicy::policy()->select_task(this); |
671 remove(task); |
691 remove(task); |
672 return task; |
692 return task; |
673 } |
693 } |
674 |
694 |
970 |
989 |
971 return compiler_thread; |
990 return compiler_thread; |
972 } |
991 } |
973 |
992 |
974 |
993 |
975 // ------------------------------------------------------------------ |
|
976 // CompileBroker::init_compiler_threads |
|
977 // |
|
978 // Initialize the compilation queue |
|
979 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) { |
994 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) { |
980 EXCEPTION_MARK; |
995 EXCEPTION_MARK; |
981 #if !defined(ZERO) && !defined(SHARK) |
996 #if !defined(ZERO) && !defined(SHARK) |
982 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); |
997 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); |
983 #endif // !ZERO && !SHARK |
998 #endif // !ZERO && !SHARK |
|
999 // Initialize the compilation queue |
984 if (c2_compiler_count > 0) { |
1000 if (c2_compiler_count > 0) { |
985 _c2_method_queue = new CompileQueue("C2MethodQueue", MethodCompileQueue_lock); |
1001 _c2_method_queue = new CompileQueue("C2MethodQueue", MethodCompileQueue_lock); |
|
1002 _compilers[1]->set_num_compiler_threads(c2_compiler_count); |
986 } |
1003 } |
987 if (c1_compiler_count > 0) { |
1004 if (c1_compiler_count > 0) { |
988 _c1_method_queue = new CompileQueue("C1MethodQueue", MethodCompileQueue_lock); |
1005 _c1_method_queue = new CompileQueue("C1MethodQueue", MethodCompileQueue_lock); |
|
1006 _compilers[0]->set_num_compiler_threads(c1_compiler_count); |
989 } |
1007 } |
990 |
1008 |
991 int compiler_count = c1_compiler_count + c2_compiler_count; |
1009 int compiler_count = c1_compiler_count + c2_compiler_count; |
992 |
1010 |
993 _method_threads = |
1011 _compiler_threads = |
994 new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true); |
1012 new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true); |
995 |
1013 |
996 char name_buffer[256]; |
1014 char name_buffer[256]; |
997 for (int i = 0; i < c2_compiler_count; i++) { |
1015 for (int i = 0; i < c2_compiler_count; i++) { |
998 // Create a name for our thread. |
1016 // Create a name for our thread. |
999 sprintf(name_buffer, "C2 CompilerThread%d", i); |
1017 sprintf(name_buffer, "C2 CompilerThread%d", i); |
1000 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1018 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1001 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, CHECK); |
1019 // Shark and C2 |
1002 _method_threads->append(new_thread); |
1020 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK); |
|
1021 _compiler_threads->append(new_thread); |
1003 } |
1022 } |
1004 |
1023 |
1005 for (int i = c2_compiler_count; i < compiler_count; i++) { |
1024 for (int i = c2_compiler_count; i < compiler_count; i++) { |
1006 // Create a name for our thread. |
1025 // Create a name for our thread. |
1007 sprintf(name_buffer, "C1 CompilerThread%d", i); |
1026 sprintf(name_buffer, "C1 CompilerThread%d", i); |
1008 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1027 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1009 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, CHECK); |
1028 // C1 |
1010 _method_threads->append(new_thread); |
1029 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK); |
|
1030 _compiler_threads->append(new_thread); |
1011 } |
1031 } |
1012 |
1032 |
1013 if (UsePerfData) { |
1033 if (UsePerfData) { |
1014 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, |
1034 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK); |
1015 compiler_count, CHECK); |
|
1016 } |
1035 } |
1017 } |
1036 } |
1018 |
1037 |
1019 |
1038 |
1020 // Set the methods on the stack as on_stack so that redefine classes doesn't |
1039 // Set the methods on the stack as on_stack so that redefine classes doesn't |
1549 // waiting on a CompileTask, we know that no one else will |
1547 // waiting on a CompileTask, we know that no one else will |
1550 // be using this CompileTask; we can free it. |
1548 // be using this CompileTask; we can free it. |
1551 free_task(task); |
1549 free_task(task); |
1552 } |
1550 } |
1553 |
1551 |
|
1552 // Initialize compiler thread(s) + compiler object(s). The postcondition |
|
1553 // of this function is that the compiler runtimes are initialized and that |
|
1554 //compiler threads can start compiling. |
|
1555 bool CompileBroker::init_compiler_runtime() { |
|
1556 CompilerThread* thread = CompilerThread::current(); |
|
1557 AbstractCompiler* comp = thread->compiler(); |
|
1558 // Final sanity check - the compiler object must exist |
|
1559 guarantee(comp != NULL, "Compiler object must exist"); |
|
1560 |
|
1561 int system_dictionary_modification_counter; |
|
1562 { |
|
1563 MutexLocker locker(Compile_lock, thread); |
|
1564 system_dictionary_modification_counter = SystemDictionary::number_of_modifications(); |
|
1565 } |
|
1566 |
|
1567 { |
|
1568 // Must switch to native to allocate ci_env |
|
1569 ThreadToNativeFromVM ttn(thread); |
|
1570 ciEnv ci_env(NULL, system_dictionary_modification_counter); |
|
1571 // Cache Jvmti state |
|
1572 ci_env.cache_jvmti_state(); |
|
1573 // Cache DTrace flags |
|
1574 ci_env.cache_dtrace_flags(); |
|
1575 |
|
1576 // Switch back to VM state to do compiler initialization |
|
1577 ThreadInVMfromNative tv(thread); |
|
1578 ResetNoHandleMark rnhm; |
|
1579 |
|
1580 |
|
1581 if (!comp->is_shark()) { |
|
1582 // Perform per-thread and global initializations |
|
1583 comp->initialize(); |
|
1584 } |
|
1585 } |
|
1586 |
|
1587 if (comp->is_failed()) { |
|
1588 disable_compilation_forever(); |
|
1589 // If compiler initialization failed, no compiler thread that is specific to a |
|
1590 // particular compiler runtime will ever start to compile methods. |
|
1591 |
|
1592 shutdown_compiler_runtime(comp, thread); |
|
1593 return false; |
|
1594 } |
|
1595 |
|
1596 // C1 specific check |
|
1597 if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) { |
|
1598 warning("Initialization of %s thread failed (no space to run compilers)", thread->name()); |
|
1599 return false; |
|
1600 } |
|
1601 |
|
1602 return true; |
|
1603 } |
|
1604 |
|
1605 // If C1 and/or C2 initialization failed, we shut down all compilation. |
|
1606 // We do this to keep things simple. This can be changed if it ever turns out to be |
|
1607 // a problem. |
|
1608 void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) { |
|
1609 // Free buffer blob, if allocated |
|
1610 if (thread->get_buffer_blob() != NULL) { |
|
1611 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
|
1612 CodeCache::free(thread->get_buffer_blob()); |
|
1613 } |
|
1614 |
|
1615 if (comp->should_perform_shutdown()) { |
|
1616 // There are two reasons for shutting down the compiler |
|
1617 // 1) compiler runtime initialization failed |
|
1618 // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing |
|
1619 warning("Shutting down compiler %s (no space to run compilers)", comp->name()); |
|
1620 |
|
1621 // Only one thread per compiler runtime object enters here |
|
1622 // Set state to shut down |
|
1623 comp->set_shut_down(); |
|
1624 |
|
1625 MutexLocker mu(MethodCompileQueue_lock, thread); |
|
1626 CompileQueue* queue; |
|
1627 if (_c1_method_queue != NULL) { |
|
1628 _c1_method_queue->delete_all(); |
|
1629 queue = _c1_method_queue; |
|
1630 _c1_method_queue = NULL; |
|
1631 delete _c1_method_queue; |
|
1632 } |
|
1633 |
|
1634 if (_c2_method_queue != NULL) { |
|
1635 _c2_method_queue->delete_all(); |
|
1636 queue = _c2_method_queue; |
|
1637 _c2_method_queue = NULL; |
|
1638 delete _c2_method_queue; |
|
1639 } |
|
1640 |
|
1641 // We could delete compiler runtimes also. However, there are references to |
|
1642 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then |
|
1643 // fail. This can be done later if necessary. |
|
1644 } |
|
1645 } |
|
1646 |
1554 // ------------------------------------------------------------------ |
1647 // ------------------------------------------------------------------ |
1555 // CompileBroker::compiler_thread_loop |
1648 // CompileBroker::compiler_thread_loop |
1556 // |
1649 // |
1557 // The main loop run by a CompilerThread. |
1650 // The main loop run by a CompilerThread. |
1558 void CompileBroker::compiler_thread_loop() { |
1651 void CompileBroker::compiler_thread_loop() { |
1559 CompilerThread* thread = CompilerThread::current(); |
1652 CompilerThread* thread = CompilerThread::current(); |
1560 CompileQueue* queue = thread->queue(); |
1653 CompileQueue* queue = thread->queue(); |
1561 |
|
1562 // For the thread that initializes the ciObjectFactory |
1654 // For the thread that initializes the ciObjectFactory |
1563 // this resource mark holds all the shared objects |
1655 // this resource mark holds all the shared objects |
1564 ResourceMark rm; |
1656 ResourceMark rm; |
1565 |
1657 |
1566 // First thread to get here will initialize the compiler interface |
1658 // First thread to get here will initialize the compiler interface |
1585 os::current_process_id()); |
1677 os::current_process_id()); |
1586 log->stamp(); |
1678 log->stamp(); |
1587 log->end_elem(); |
1679 log->end_elem(); |
1588 } |
1680 } |
1589 |
1681 |
1590 while (true) { |
1682 // If compiler thread/runtime initialization fails, exit the compiler thread |
1591 { |
1683 if (!init_compiler_runtime()) { |
1592 // We need this HandleMark to avoid leaking VM handles. |
1684 return; |
1593 HandleMark hm(thread); |
1685 } |
1594 |
1686 |
1595 if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) { |
1687 // Poll for new compilation tasks as long as the JVM runs. Compilation |
1596 // the code cache is really full |
1688 // should only be disabled if something went wrong while initializing the |
1597 handle_full_code_cache(); |
1689 // compiler runtimes. This, in turn, should not happen. The only known case |
|
1690 // when compiler runtime initialization fails is if there is not enough free |
|
1691 // space in the code cache to generate the necessary stubs, etc. |
|
1692 while (!is_compilation_disabled_forever()) { |
|
1693 // We need this HandleMark to avoid leaking VM handles. |
|
1694 HandleMark hm(thread); |
|
1695 |
|
1696 if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) { |
|
1697 // the code cache is really full |
|
1698 handle_full_code_cache(); |
|
1699 } |
|
1700 |
|
1701 CompileTask* task = queue->get(); |
|
1702 if (task == NULL) { |
|
1703 continue; |
|
1704 } |
|
1705 |
|
1706 // Give compiler threads an extra quanta. They tend to be bursty and |
|
1707 // this helps the compiler to finish up the job. |
|
1708 if( CompilerThreadHintNoPreempt ) |
|
1709 os::hint_no_preempt(); |
|
1710 |
|
1711 // trace per thread time and compile statistics |
|
1712 CompilerCounters* counters = ((CompilerThread*)thread)->counters(); |
|
1713 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); |
|
1714 |
|
1715 // Assign the task to the current thread. Mark this compilation |
|
1716 // thread as active for the profiler. |
|
1717 CompileTaskWrapper ctw(task); |
|
1718 nmethodLocker result_handle; // (handle for the nmethod produced by this task) |
|
1719 task->set_code_handle(&result_handle); |
|
1720 methodHandle method(thread, task->method()); |
|
1721 |
|
1722 // Never compile a method if breakpoints are present in it |
|
1723 if (method()->number_of_breakpoints() == 0) { |
|
1724 // Compile the method. |
|
1725 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { |
|
1726 #ifdef COMPILER1 |
|
1727 // Allow repeating compilations for the purpose of benchmarking |
|
1728 // compile speed. This is not useful for customers. |
|
1729 if (CompilationRepeat != 0) { |
|
1730 int compile_count = CompilationRepeat; |
|
1731 while (compile_count > 0) { |
|
1732 invoke_compiler_on_method(task); |
|
1733 nmethod* nm = method->code(); |
|
1734 if (nm != NULL) { |
|
1735 nm->make_zombie(); |
|
1736 method->clear_code(); |
|
1737 } |
|
1738 compile_count--; |
|
1739 } |
|
1740 } |
|
1741 #endif /* COMPILER1 */ |
|
1742 invoke_compiler_on_method(task); |
|
1743 } else { |
|
1744 // After compilation is disabled, remove remaining methods from queue |
|
1745 method->clear_queued_for_compilation(); |
1598 } |
1746 } |
1599 |
1747 } |
1600 CompileTask* task = queue->get(); |
1748 } |
1601 |
1749 |
1602 // Give compiler threads an extra quanta. They tend to be bursty and |
1750 // Shut down compiler runtime |
1603 // this helps the compiler to finish up the job. |
1751 shutdown_compiler_runtime(thread->compiler(), thread); |
1604 if( CompilerThreadHintNoPreempt ) |
1752 } |
1605 os::hint_no_preempt(); |
|
1606 |
|
1607 // trace per thread time and compile statistics |
|
1608 CompilerCounters* counters = ((CompilerThread*)thread)->counters(); |
|
1609 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); |
|
1610 |
|
1611 // Assign the task to the current thread. Mark this compilation |
|
1612 // thread as active for the profiler. |
|
1613 CompileTaskWrapper ctw(task); |
|
1614 nmethodLocker result_handle; // (handle for the nmethod produced by this task) |
|
1615 task->set_code_handle(&result_handle); |
|
1616 methodHandle method(thread, task->method()); |
|
1617 |
|
1618 // Never compile a method if breakpoints are present in it |
|
1619 if (method()->number_of_breakpoints() == 0) { |
|
1620 // Compile the method. |
|
1621 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { |
|
1622 #ifdef COMPILER1 |
|
1623 // Allow repeating compilations for the purpose of benchmarking |
|
1624 // compile speed. This is not useful for customers. |
|
1625 if (CompilationRepeat != 0) { |
|
1626 int compile_count = CompilationRepeat; |
|
1627 while (compile_count > 0) { |
|
1628 invoke_compiler_on_method(task); |
|
1629 nmethod* nm = method->code(); |
|
1630 if (nm != NULL) { |
|
1631 nm->make_zombie(); |
|
1632 method->clear_code(); |
|
1633 } |
|
1634 compile_count--; |
|
1635 } |
|
1636 } |
|
1637 #endif /* COMPILER1 */ |
|
1638 invoke_compiler_on_method(task); |
|
1639 } else { |
|
1640 // After compilation is disabled, remove remaining methods from queue |
|
1641 method->clear_queued_for_compilation(); |
|
1642 } |
|
1643 } |
|
1644 } |
|
1645 } |
|
1646 } |
|
1647 |
|
1648 |
1753 |
1649 // ------------------------------------------------------------------ |
1754 // ------------------------------------------------------------------ |
1650 // CompileBroker::init_compiler_thread_log |
1755 // CompileBroker::init_compiler_thread_log |
1651 // |
1756 // |
1652 // Set up state required by +LogCompilation. |
1757 // Set up state required by +LogCompilation. |