33 #include "compiler/compileBroker.hpp"
34 #include "compiler/compileLog.hpp"
35 #include "compiler/compilerOracle.hpp"
36 #include "compiler/directivesParser.hpp"
37 #include "interpreter/linkResolver.hpp"
38 #include "logging/log.hpp"
39 #include "logging/logStream.hpp"
40 #include "memory/allocation.inline.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "oops/methodData.hpp"
43 #include "oops/method.inline.hpp"
44 #include "oops/oop.inline.hpp"
45 #include "prims/nativeLookup.hpp"
46 #include "prims/whitebox.hpp"
47 #include "runtime/arguments.hpp"
48 #include "runtime/atomic.hpp"
49 #include "runtime/compilationPolicy.hpp"
50 #include "runtime/init.hpp"
51 #include "runtime/interfaceSupport.inline.hpp"
52 #include "runtime/javaCalls.hpp"
53 #include "runtime/os.hpp"
54 #include "runtime/safepointVerifiers.hpp"
55 #include "runtime/sharedRuntime.hpp"
56 #include "runtime/sweeper.hpp"
57 #include "runtime/timerTrace.hpp"
58 #include "runtime/vframe.inline.hpp"
59 #include "trace/tracing.hpp"
60 #include "utilities/debug.hpp"
61 #include "utilities/dtrace.hpp"
62 #include "utilities/events.hpp"
63 #include "utilities/formatBuffer.hpp"
64 #ifdef COMPILER1
65 #include "c1/c1_Compiler.hpp"
66 #endif
67 #if INCLUDE_JVMCI
68 #include "jvmci/jvmciCompiler.hpp"
69 #include "jvmci/jvmciRuntime.hpp"
70 #include "jvmci/jvmciJavaClasses.hpp"
71 #include "runtime/vframe.hpp"
72 #endif
100 (char *) klass_name->bytes(), klass_name->utf8_length(), \
101 (char *) name->bytes(), name->utf8_length(), \
102 (char *) signature->bytes(), signature->utf8_length(), (success)); \
103 }
104
105 #else // ndef DTRACE_ENABLED
106
107 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
108 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
109
110 #endif // ndef DTRACE_ENABLED
111
112 bool CompileBroker::_initialized = false;
113 volatile bool CompileBroker::_should_block = false;
114 volatile int CompileBroker::_print_compilation_warning = 0;
115 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
116
117 // The installed compiler(s)
118 AbstractCompiler* CompileBroker::_compilers[2];
119
120 // These counters are used to assign an unique ID to each compilation.
121 volatile jint CompileBroker::_compilation_id = 0;
122 volatile jint CompileBroker::_osr_compilation_id = 0;
123
124 // Debugging information
125 int CompileBroker::_last_compile_type = no_compile;
126 int CompileBroker::_last_compile_level = CompLevel_none;
127 char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
128
129 // Performance counters
130 PerfCounter* CompileBroker::_perf_total_compilation = NULL;
131 PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
132 PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
133
134 PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
135 PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
136 PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
137 PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
138 PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
139
270 if (!free_task) {
271 // Notify the waiting thread that the compilation has completed
272 // so that it can free the task.
273 task->lock()->notify_all();
274 }
275 }
276 if (free_task) {
277 // The task can only be freed once the task lock is released.
278 CompileTask::free(task);
279 }
280 } else {
281 task->mark_complete();
282
283 // By convention, the compiling thread is responsible for
284 // recycling a non-blocking CompileTask.
285 CompileTask::free(task);
286 }
287 }
288
289 /**
290 * Add a CompileTask to a CompileQueue.
291 */
292 void CompileQueue::add(CompileTask* task) {
293 assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
294
295 task->set_next(NULL);
296 task->set_prev(NULL);
297
298 if (_last == NULL) {
299 // The compile queue is empty.
300 assert(_first == NULL, "queue is empty");
301 _first = task;
302 _last = task;
303 } else {
304 // Append the task to the queue.
305 assert(_last->next() == NULL, "not last");
306 _last->set_next(task);
307 task->set_prev(_last);
308 _last = task;
309 }
366 // having no compile jobs: First, we compiled everything we wanted. Second,
367 // we ran out of code cache so compilation has been disabled. In the latter
368 // case we perform code cache sweeps to free memory such that we can re-enable
369 // compilation.
370 while (_first == NULL) {
371 // Exit loop if compilation is disabled forever
372 if (CompileBroker::is_compilation_disabled_forever()) {
373 return NULL;
374 }
375
376 // If there are no compilation tasks and we can compile new jobs
377 // (i.e., there is enough free space in the code cache) there is
378 // no need to invoke the sweeper. As a result, the hotness of methods
379 // remains unchanged. This behavior is desired, since we want to keep
380 // the stable state, i.e., we do not want to evict methods from the
381 // code cache if it is unnecessary.
382 // 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
384 // 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);
386 }
387
388 if (CompileBroker::is_compilation_disabled_forever()) {
389 return NULL;
390 }
391
392 CompileTask* task;
393 {
394 NoSafepointVerifier nsv;
395 task = CompilationPolicy::policy()->select_task(this);
396 }
397
398 if (task != NULL) {
399 // Save method pointers across unlock safepoint. The task is removed from
400 // the compilation queue, which is walked during RedefineClasses.
401 save_method = methodHandle(task->method());
402 save_hot_method = methodHandle(task->hot_method());
403
404 remove(task);
405 purge_stale_tasks(); // may temporarily release MCQ lock
515 print(tty);
516 }
517
518 CompilerCounters::CompilerCounters() {
519 _current_method[0] = '\0';
520 _compile_type = CompileBroker::no_compile;
521 }
522
523 // ------------------------------------------------------------------
524 // CompileBroker::compilation_init
525 //
526 // Initialize the Compilation object
527 void CompileBroker::compilation_init_phase1(TRAPS) {
528 _last_method_compiled[0] = '\0';
529
530 // No need to initialize compilation system if we do not use it.
531 if (!UseCompiler) {
532 return;
533 }
534 // Set the interface to the current compiler(s).
535 int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
536 int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
537
538 #if INCLUDE_JVMCI
539 if (EnableJVMCI) {
540 // This is creating a JVMCICompiler singleton.
541 JVMCICompiler* jvmci = new JVMCICompiler();
542
543 if (UseJVMCICompiler) {
544 _compilers[1] = jvmci;
545 if (FLAG_IS_DEFAULT(JVMCIThreads)) {
546 if (BootstrapJVMCI) {
547 // JVMCI will bootstrap so give it more threads
548 c2_count = MIN2(32, os::active_processor_count());
549 }
550 } else {
551 c2_count = JVMCIThreads;
552 }
553 if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
554 } else {
555 c1_count = JVMCIHostThreads;
556 }
557 }
558 }
559 #endif // INCLUDE_JVMCI
560
561 #ifdef COMPILER1
562 if (c1_count > 0) {
563 _compilers[0] = new Compiler();
564 }
565 #endif // COMPILER1
566
567 #ifdef COMPILER2
568 if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
569 if (c2_count > 0) {
570 _compilers[1] = new C2Compiler();
571 }
572 }
573 #endif // COMPILER2
574
575 // Start the compiler thread(s) and the sweeper thread
576 init_compiler_sweeper_threads(c1_count, c2_count);
577 // totalTime performance counter is always created as it is required
578 // by the implementation of java.lang.management.CompilationMBean.
579 {
580 EXCEPTION_MARK;
581 _perf_total_compilation =
582 PerfDataManager::create_counter(JAVA_CI, "totalTime",
583 PerfData::U_Ticks, CHECK);
584 }
585
586 if (UsePerfData) {
587
588 EXCEPTION_MARK;
589
590 // create the jvmstat performance counters
591 _perf_osr_compilation =
592 PerfDataManager::create_counter(SUN_CI, "osrTime",
593 PerfData::U_Ticks, CHECK);
594
595 _perf_standard_compilation =
596 PerfDataManager::create_counter(SUN_CI, "standardTime",
662 _perf_last_failed_type =
663 PerfDataManager::create_variable(SUN_CI, "lastFailedType",
664 PerfData::U_None,
665 (jlong)CompileBroker::no_compile,
666 CHECK);
667
668 _perf_last_invalidated_type =
669 PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
670 PerfData::U_None,
671 (jlong)CompileBroker::no_compile,
672 CHECK);
673 }
674 }
675
676 // Completes compiler initialization. Compilation requests submitted
677 // prior to this will be silently ignored.
678 void CompileBroker::compilation_init_phase2() {
679 _initialized = true;
680 }
681
682 JavaThread* CompileBroker::make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
683 AbstractCompiler* comp, bool compiler_thread, TRAPS) {
684 JavaThread* thread = NULL;
685 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_0);
686 InstanceKlass* klass = InstanceKlass::cast(k);
687 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
688 Handle string = java_lang_String::create_from_str(name, CHECK_0);
689
690 // Initialize thread_oop to put it into the system threadGroup
691 Handle thread_group (THREAD, Universe::system_thread_group());
692 JavaValue result(T_VOID);
693 JavaCalls::call_special(&result, thread_oop,
694 klass,
695 vmSymbols::object_initializer_name(),
696 vmSymbols::threadgroup_string_void_signature(),
697 thread_group,
698 string,
699 CHECK_0);
700
701 {
702 MutexLocker mu(Threads_lock, THREAD);
703 if (compiler_thread) {
704 thread = new CompilerThread(queue, counters);
705 } else {
706 thread = new CodeCacheSweeperThread();
707 }
708 // At this point the new CompilerThread data-races with this startup
709 // thread (which I believe is the primoridal thread and NOT the VM
710 // thread). This means Java bytecodes being executed at startup can
711 // queue compile jobs which will run at whatever default priority the
712 // newly created CompilerThread runs at.
713
714
715 // At this point it may be possible that no osthread was created for the
716 // JavaThread due to lack of memory. We would have to throw an exception
717 // in that case. However, since this must work and we do not allow
718 // exceptions anyway, check and abort if this fails. But first release the
719 // lock.
720
721 if (thread != NULL && thread->osthread() != NULL) {
722
723 java_lang_Thread::set_thread(thread_oop(), thread);
724
725 // Note that this only sets the JavaThread _priority field, which by
726 // definition is limited to Java priorities and not OS priorities.
727 // The os-priority is set in the CompilerThread startup code itself
728
729 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
730
731 // 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
733 // possible to set the compiler thread priority higher than any Java
734 // thread.
735
736 int native_prio = CompilerThreadPriority;
737 if (native_prio == -1) {
738 if (UseCriticalCompilerThreadPriority) {
739 native_prio = os::java_to_os_priority[CriticalPriority];
740 } else {
741 native_prio = os::java_to_os_priority[NearMaxPriority];
742 }
743 }
744 os::set_native_priority(thread, native_prio);
745
746 java_lang_Thread::set_daemon(thread_oop());
747
748 thread->set_threadObj(thread_oop());
749 if (compiler_thread) {
750 thread->as_CompilerThread()->set_compiler(comp);
751 }
752 Threads::add(thread);
753 Thread::start(thread);
754 }
755 }
756
757 // First release lock before aborting VM.
758 if (thread == NULL || thread->osthread() == NULL) {
759 vm_exit_during_initialization("java.lang.OutOfMemoryError",
760 os::native_thread_creation_failed_msg());
761 }
762
763 // Let go of Threads_lock before yielding
764 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
765
766 return thread;
767 }
768
769
770 void CompileBroker::init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count) {
771 EXCEPTION_MARK;
772 #if !defined(ZERO)
773 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
774 #endif // !ZERO
775 // Initialize the compilation queue
776 if (c2_compiler_count > 0) {
777 const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
778 _c2_compile_queue = new CompileQueue(name);
779 _compilers[1]->set_num_compiler_threads(c2_compiler_count);
780 }
781 if (c1_compiler_count > 0) {
782 _c1_compile_queue = new CompileQueue("C1 compile queue");
783 _compilers[0]->set_num_compiler_threads(c1_compiler_count);
784 }
785
786 int compiler_count = c1_compiler_count + c2_compiler_count;
787
788 char name_buffer[256];
789 const bool compiler_thread = true;
790 for (int i = 0; i < c2_compiler_count; i++) {
791 // Create a name for our thread.
792 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
793 CompilerCounters* counters = new CompilerCounters();
794 make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK);
795 }
796
797 for (int i = c2_compiler_count; i < compiler_count; i++) {
798 // Create a name for our thread.
799 sprintf(name_buffer, "C1 CompilerThread%d", i);
800 CompilerCounters* counters = new CompilerCounters();
801 // C1
802 make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK);
803 }
804
805 if (UsePerfData) {
806 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
807 }
808
809 if (MethodFlushing) {
810 // Initialize the sweeper thread
811 make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK);
812 }
813 }
814
815
816 /**
817 * Set the methods on the stack as on_stack so that redefine classes doesn't
818 * reclaim them. This method is executed at a safepoint.
819 */
820 void CompileBroker::mark_on_stack() {
821 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
822 // Since we are at a safepoint, we do not need a lock to access
823 // the compile queues.
824 if (_c2_compile_queue != NULL) {
825 _c2_compile_queue->mark_on_stack();
826 }
827 if (_c1_compile_queue != NULL) {
828 _c1_compile_queue->mark_on_stack();
829 }
830 }
831
832 // ------------------------------------------------------------------
1529
1530 // Delete all queued compilation tasks to make compiler threads exit faster.
1531 if (_c1_compile_queue != NULL) {
1532 _c1_compile_queue->free_all();
1533 }
1534
1535 if (_c2_compile_queue != NULL) {
1536 _c2_compile_queue->free_all();
1537 }
1538
1539 // Set flags so that we continue execution with using interpreter only.
1540 UseCompiler = false;
1541 UseInterpreter = true;
1542
1543 // We could delete compiler runtimes also. However, there are references to
1544 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then
1545 // fail. This can be done later if necessary.
1546 }
1547 }
1548
1549 // ------------------------------------------------------------------
1550 // CompileBroker::compiler_thread_loop
1551 //
1552 // The main loop run by a CompilerThread.
1553 void CompileBroker::compiler_thread_loop() {
1554 CompilerThread* thread = CompilerThread::current();
1555 CompileQueue* queue = thread->queue();
1556 // For the thread that initializes the ciObjectFactory
1557 // this resource mark holds all the shared objects
1558 ResourceMark rm;
1559
1560 // First thread to get here will initialize the compiler interface
1561
1562 {
1563 ASSERT_IN_VM;
1564 MutexLocker only_one (CompileThread_lock, thread);
1565 if (!ciObjectFactory::is_initialized()) {
1566 ciObjectFactory::initialize();
1567 }
1568 }
1569
1570 // Open a log.
1571 if (LogCompilation) {
1572 init_compiler_thread_log();
1573 }
1574 CompileLog* log = thread->log();
1575 if (log != NULL) {
1576 log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1577 thread->name(),
1578 os::current_thread_id(),
1579 os::current_process_id());
1580 log->stamp();
1581 log->end_elem();
1582 }
1583
1584 // If compiler thread/runtime initialization fails, exit the compiler thread
1585 if (!init_compiler_runtime()) {
1586 return;
1587 }
1588
1589 // Poll for new compilation tasks as long as the JVM runs. Compilation
1590 // should only be disabled if something went wrong while initializing the
1591 // 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
1593 // space in the code cache to generate the necessary stubs, etc.
1594 while (!is_compilation_disabled_forever()) {
1595 // We need this HandleMark to avoid leaking VM handles.
1596 HandleMark hm(thread);
1597
1598 CompileTask* task = queue->get();
1599 if (task == NULL) {
1600 continue;
1601 }
1602
1603 // Give compiler threads an extra quanta. They tend to be bursty and
1604 // this helps the compiler to finish up the job.
1605 if (CompilerThreadHintNoPreempt) {
1606 os::hint_no_preempt();
1607 }
1608
1609 // Assign the task to the current thread. Mark this compilation
1610 // thread as active for the profiler.
1611 CompileTaskWrapper ctw(task);
1612 nmethodLocker result_handle; // (handle for the nmethod produced by this task)
1613 task->set_code_handle(&result_handle);
1614 methodHandle method(thread, task->method());
1615
1616 // Never compile a method if breakpoints are present in it
1617 if (method()->number_of_breakpoints() == 0) {
1618 // Compile the method.
1619 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
1620 invoke_compiler_on_method(task);
1621 } else {
1622 // After compilation is disabled, remove remaining methods from queue
1623 method->clear_queued_for_compilation();
1624 task->set_failure_reason("compilation is disabled");
1625 }
1626 }
1627 }
1628
1629 // Shut down compiler runtime
1630 shutdown_compiler_runtime(thread->compiler(), thread);
1631 }
1632
1633 // ------------------------------------------------------------------
1634 // CompileBroker::init_compiler_thread_log
1635 //
1636 // Set up state required by +LogCompilation.
1637 void CompileBroker::init_compiler_thread_log() {
1638 CompilerThread* thread = CompilerThread::current();
1639 char file_name[4*K];
1640 FILE* fp = NULL;
|
33 #include "compiler/compileBroker.hpp"
34 #include "compiler/compileLog.hpp"
35 #include "compiler/compilerOracle.hpp"
36 #include "compiler/directivesParser.hpp"
37 #include "interpreter/linkResolver.hpp"
38 #include "logging/log.hpp"
39 #include "logging/logStream.hpp"
40 #include "memory/allocation.inline.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "oops/methodData.hpp"
43 #include "oops/method.inline.hpp"
44 #include "oops/oop.inline.hpp"
45 #include "prims/nativeLookup.hpp"
46 #include "prims/whitebox.hpp"
47 #include "runtime/arguments.hpp"
48 #include "runtime/atomic.hpp"
49 #include "runtime/compilationPolicy.hpp"
50 #include "runtime/init.hpp"
51 #include "runtime/interfaceSupport.inline.hpp"
52 #include "runtime/javaCalls.hpp"
53 #include "runtime/jniHandles.inline.hpp"
54 #include "runtime/os.hpp"
55 #include "runtime/safepointVerifiers.hpp"
56 #include "runtime/sharedRuntime.hpp"
57 #include "runtime/sweeper.hpp"
58 #include "runtime/timerTrace.hpp"
59 #include "runtime/vframe.inline.hpp"
60 #include "trace/tracing.hpp"
61 #include "utilities/debug.hpp"
62 #include "utilities/dtrace.hpp"
63 #include "utilities/events.hpp"
64 #include "utilities/formatBuffer.hpp"
65 #ifdef COMPILER1
66 #include "c1/c1_Compiler.hpp"
67 #endif
68 #if INCLUDE_JVMCI
69 #include "jvmci/jvmciCompiler.hpp"
70 #include "jvmci/jvmciRuntime.hpp"
71 #include "jvmci/jvmciJavaClasses.hpp"
72 #include "runtime/vframe.hpp"
73 #endif
101 (char *) klass_name->bytes(), klass_name->utf8_length(), \
102 (char *) name->bytes(), name->utf8_length(), \
103 (char *) signature->bytes(), signature->utf8_length(), (success)); \
104 }
105
106 #else // ndef DTRACE_ENABLED
107
108 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
109 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
110
111 #endif // ndef DTRACE_ENABLED
112
113 bool CompileBroker::_initialized = false;
114 volatile bool CompileBroker::_should_block = false;
115 volatile int CompileBroker::_print_compilation_warning = 0;
116 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
117
118 // The installed compiler(s)
119 AbstractCompiler* CompileBroker::_compilers[2];
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
132 // These counters are used to assign an unique ID to each compilation.
133 volatile jint CompileBroker::_compilation_id = 0;
134 volatile jint CompileBroker::_osr_compilation_id = 0;
135
136 // Debugging information
137 int CompileBroker::_last_compile_type = no_compile;
138 int CompileBroker::_last_compile_level = CompLevel_none;
139 char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
140
141 // Performance counters
142 PerfCounter* CompileBroker::_perf_total_compilation = NULL;
143 PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
144 PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
145
146 PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
147 PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
148 PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
149 PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
150 PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
151
282 if (!free_task) {
283 // Notify the waiting thread that the compilation has completed
284 // so that it can free the task.
285 task->lock()->notify_all();
286 }
287 }
288 if (free_task) {
289 // The task can only be freed once the task lock is released.
290 CompileTask::free(task);
291 }
292 } else {
293 task->mark_complete();
294
295 // By convention, the compiling thread is responsible for
296 // recycling a non-blocking CompileTask.
297 CompileTask::free(task);
298 }
299 }
300
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 /**
332 * Add a CompileTask to a CompileQueue.
333 */
334 void CompileQueue::add(CompileTask* task) {
335 assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
336
337 task->set_next(NULL);
338 task->set_prev(NULL);
339
340 if (_last == NULL) {
341 // The compile queue is empty.
342 assert(_first == NULL, "queue is empty");
343 _first = task;
344 _last = task;
345 } else {
346 // Append the task to the queue.
347 assert(_last->next() == NULL, "not last");
348 _last->set_next(task);
349 task->set_prev(_last);
350 _last = task;
351 }
408 // having no compile jobs: First, we compiled everything we wanted. Second,
409 // we ran out of code cache so compilation has been disabled. In the latter
410 // case we perform code cache sweeps to free memory such that we can re-enable
411 // compilation.
412 while (_first == NULL) {
413 // Exit loop if compilation is disabled forever
414 if (CompileBroker::is_compilation_disabled_forever()) {
415 return NULL;
416 }
417
418 // If there are no compilation tasks and we can compile new jobs
419 // (i.e., there is enough free space in the code cache) there is
420 // no need to invoke the sweeper. As a result, the hotness of methods
421 // remains unchanged. This behavior is desired, since we want to keep
422 // the stable state, i.e., we do not want to evict methods from the
423 // code cache if it is unnecessary.
424 // We need a timed wait here, since compiler threads can exit if compilation
425 // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
426 // is not critical and we do not want idle compiler threads to wake up too often.
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 }
433 }
434
435 if (CompileBroker::is_compilation_disabled_forever()) {
436 return NULL;
437 }
438
439 CompileTask* task;
440 {
441 NoSafepointVerifier nsv;
442 task = CompilationPolicy::policy()->select_task(this);
443 }
444
445 if (task != NULL) {
446 // Save method pointers across unlock safepoint. The task is removed from
447 // the compilation queue, which is walked during RedefineClasses.
448 save_method = methodHandle(task->method());
449 save_hot_method = methodHandle(task->hot_method());
450
451 remove(task);
452 purge_stale_tasks(); // may temporarily release MCQ lock
562 print(tty);
563 }
564
565 CompilerCounters::CompilerCounters() {
566 _current_method[0] = '\0';
567 _compile_type = CompileBroker::no_compile;
568 }
569
570 // ------------------------------------------------------------------
571 // CompileBroker::compilation_init
572 //
573 // Initialize the Compilation object
574 void CompileBroker::compilation_init_phase1(TRAPS) {
575 _last_method_compiled[0] = '\0';
576
577 // No need to initialize compilation system if we do not use it.
578 if (!UseCompiler) {
579 return;
580 }
581 // Set the interface to the current compiler(s).
582 _c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
583 _c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
584
585 #if INCLUDE_JVMCI
586 if (EnableJVMCI) {
587 // This is creating a JVMCICompiler singleton.
588 JVMCICompiler* jvmci = new JVMCICompiler();
589
590 if (UseJVMCICompiler) {
591 _compilers[1] = jvmci;
592 if (FLAG_IS_DEFAULT(JVMCIThreads)) {
593 if (BootstrapJVMCI) {
594 // JVMCI will bootstrap so give it more threads
595 _c2_count = MIN2(32, os::active_processor_count());
596 }
597 } else {
598 _c2_count = JVMCIThreads;
599 }
600 if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
601 } else {
602 _c1_count = JVMCIHostThreads;
603 }
604 }
605 }
606 #endif // INCLUDE_JVMCI
607
608 #ifdef COMPILER1
609 if (_c1_count > 0) {
610 _compilers[0] = new Compiler();
611 }
612 #endif // COMPILER1
613
614 #ifdef COMPILER2
615 if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
616 if (_c2_count > 0) {
617 _compilers[1] = new C2Compiler();
618 }
619 }
620 #endif // COMPILER2
621
622 // Start the compiler thread(s) and the sweeper thread
623 init_compiler_sweeper_threads();
624 // totalTime performance counter is always created as it is required
625 // by the implementation of java.lang.management.CompilationMBean.
626 {
627 EXCEPTION_MARK;
628 _perf_total_compilation =
629 PerfDataManager::create_counter(JAVA_CI, "totalTime",
630 PerfData::U_Ticks, CHECK);
631 }
632
633 if (UsePerfData) {
634
635 EXCEPTION_MARK;
636
637 // create the jvmstat performance counters
638 _perf_osr_compilation =
639 PerfDataManager::create_counter(SUN_CI, "osrTime",
640 PerfData::U_Ticks, CHECK);
641
642 _perf_standard_compilation =
643 PerfDataManager::create_counter(SUN_CI, "standardTime",
709 _perf_last_failed_type =
710 PerfDataManager::create_variable(SUN_CI, "lastFailedType",
711 PerfData::U_None,
712 (jlong)CompileBroker::no_compile,
713 CHECK);
714
715 _perf_last_invalidated_type =
716 PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
717 PerfData::U_None,
718 (jlong)CompileBroker::no_compile,
719 CHECK);
720 }
721 }
722
723 // Completes compiler initialization. Compilation requests submitted
724 // prior to this will be silently ignored.
725 void CompileBroker::compilation_init_phase2() {
726 _initialized = true;
727 }
728
729 Handle CompileBroker::create_thread_oop(const char* name, TRAPS) {
730 Klass* k = SystemDictionary::find(vmSymbols::java_lang_Thread(), Handle(), Handle(), CHECK_NH);
731 assert(k != NULL, "must be initialized");
732 InstanceKlass* klass = InstanceKlass::cast(k);
733 instanceHandle thread_handle = klass->allocate_instance_handle(CHECK_NH);
734 Handle string = java_lang_String::create_from_str(name, CHECK_NH);
735
736 // Initialize thread_oop to put it into the system threadGroup
737 Handle thread_group(THREAD, Universe::system_thread_group());
738 JavaValue result(T_VOID);
739 JavaCalls::call_special(&result, thread_handle,
740 klass,
741 vmSymbols::object_initializer_name(),
742 vmSymbols::threadgroup_string_void_signature(),
743 thread_group,
744 string,
745 CHECK_NH);
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;
754 {
755 MutexLocker mu(Threads_lock, THREAD);
756 if (compiler_thread) {
757 if (!InjectCompilerCreationFailure || comp->num_compiler_threads() == 0) {
758 CompilerCounters* counters = new CompilerCounters();
759 thread = new CompilerThread(queue, counters);
760 }
761 } else {
762 thread = new CodeCacheSweeperThread();
763 }
764 // At this point the new CompilerThread data-races with this startup
765 // thread (which I believe is the primoridal thread and NOT the VM
766 // thread). This means Java bytecodes being executed at startup can
767 // queue compile jobs which will run at whatever default priority the
768 // newly created CompilerThread runs at.
769
770
771 // At this point it may be possible that no osthread was created for the
772 // JavaThread due to lack of memory. We would have to throw an exception
773 // in that case. However, since this must work and we do not allow
774 // exceptions anyway, check and abort if this fails. But first release the
775 // lock.
776
777 if (thread != NULL && thread->osthread() != NULL) {
778
779 java_lang_Thread::set_thread(JNIHandles::resolve_non_null(thread_handle), thread);
780
781 // Note that this only sets the JavaThread _priority field, which by
782 // definition is limited to Java priorities and not OS priorities.
783 // The os-priority is set in the CompilerThread startup code itself
784
785 java_lang_Thread::set_priority(JNIHandles::resolve_non_null(thread_handle), NearMaxPriority);
786
787 // Note that we cannot call os::set_priority because it expects Java
788 // priorities and we are *explicitly* using OS priorities so that it's
789 // possible to set the compiler thread priority higher than any Java
790 // thread.
791
792 int native_prio = CompilerThreadPriority;
793 if (native_prio == -1) {
794 if (UseCriticalCompilerThreadPriority) {
795 native_prio = os::java_to_os_priority[CriticalPriority];
796 } else {
797 native_prio = os::java_to_os_priority[NearMaxPriority];
798 }
799 }
800 os::set_native_priority(thread, native_prio);
801
802 java_lang_Thread::set_daemon(JNIHandles::resolve_non_null(thread_handle));
803
804 thread->set_threadObj(JNIHandles::resolve_non_null(thread_handle));
805 if (compiler_thread) {
806 thread->as_CompilerThread()->set_compiler(comp);
807 }
808 Threads::add(thread);
809 Thread::start(thread);
810 }
811 }
812
813 // First release lock before aborting VM.
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 }
821 vm_exit_during_initialization("java.lang.OutOfMemoryError",
822 os::native_thread_creation_failed_msg());
823 }
824
825 // Let go of Threads_lock before yielding
826 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
827
828 return thread;
829 }
830
831
832 void CompileBroker::init_compiler_sweeper_threads() {
833 EXCEPTION_MARK;
834 #if !defined(ZERO)
835 assert(_c2_count > 0 || _c1_count > 0, "No compilers?");
836 #endif // !ZERO
837 // Initialize the compilation queue
838 if (_c2_count > 0) {
839 const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
840 _c2_compile_queue = new CompileQueue(name);
841 _compiler2_objects = NEW_C_HEAP_ARRAY(jobject, _c2_count, mtCompiler);
842 _compiler2_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c2_count, mtCompiler);
843 }
844 if (_c1_count > 0) {
845 _c1_compile_queue = new CompileQueue("C1 compile queue");
846 _compiler1_objects = NEW_C_HEAP_ARRAY(jobject, _c1_count, mtCompiler);
847 _compiler1_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c1_count, mtCompiler);
848 }
849
850 char name_buffer[256];
851
852 for (int i = 0; i < _c2_count; i++) {
853 // Create a name for our thread.
854 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
855 jobject thread_handle = JNIHandles::make_global(create_thread_oop(name_buffer, THREAD));
856 _compiler2_objects[i] = thread_handle;
857 _compiler2_logs[i] = NULL;
858
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++) {
872 // Create a name for our thread.
873 sprintf(name_buffer, "C1 CompilerThread%d", i);
874 jobject thread_handle = JNIHandles::make_global(create_thread_oop(name_buffer, THREAD));
875 _compiler1_objects[i] = thread_handle;
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 }
888 }
889
890 if (UsePerfData) {
891 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, _c1_count + _c2_count, CHECK);
892 }
893
894 if (MethodFlushing) {
895 // Initialize the sweeper thread
896 jobject thread_handle = JNIHandles::make_local(THREAD, create_thread_oop("Sweeper thread", THREAD)());
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();
947 }
948
949
950 /**
951 * Set the methods on the stack as on_stack so that redefine classes doesn't
952 * reclaim them. This method is executed at a safepoint.
953 */
954 void CompileBroker::mark_on_stack() {
955 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
956 // Since we are at a safepoint, we do not need a lock to access
957 // the compile queues.
958 if (_c2_compile_queue != NULL) {
959 _c2_compile_queue->mark_on_stack();
960 }
961 if (_c1_compile_queue != NULL) {
962 _c1_compile_queue->mark_on_stack();
963 }
964 }
965
966 // ------------------------------------------------------------------
1663
1664 // Delete all queued compilation tasks to make compiler threads exit faster.
1665 if (_c1_compile_queue != NULL) {
1666 _c1_compile_queue->free_all();
1667 }
1668
1669 if (_c2_compile_queue != NULL) {
1670 _c2_compile_queue->free_all();
1671 }
1672
1673 // Set flags so that we continue execution with using interpreter only.
1674 UseCompiler = false;
1675 UseInterpreter = true;
1676
1677 // We could delete compiler runtimes also. However, there are references to
1678 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then
1679 // fail. This can be done later if necessary.
1680 }
1681 }
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
1726 // ------------------------------------------------------------------
1727 // CompileBroker::compiler_thread_loop
1728 //
1729 // The main loop run by a CompilerThread.
1730 void CompileBroker::compiler_thread_loop() {
1731 CompilerThread* thread = CompilerThread::current();
1732 CompileQueue* queue = thread->queue();
1733 // For the thread that initializes the ciObjectFactory
1734 // this resource mark holds all the shared objects
1735 ResourceMark rm;
1736
1737 // First thread to get here will initialize the compiler interface
1738
1739 {
1740 ASSERT_IN_VM;
1741 MutexLocker only_one (CompileThread_lock, thread);
1742 if (!ciObjectFactory::is_initialized()) {
1743 ciObjectFactory::initialize();
1744 }
1745 }
1746
1747 // Open a log.
1748 CompileLog* log = get_log(thread);
1749 if (log != NULL) {
1750 log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1751 thread->name(),
1752 os::current_thread_id(),
1753 os::current_process_id());
1754 log->stamp();
1755 log->end_elem();
1756 }
1757
1758 // If compiler thread/runtime initialization fails, exit the compiler thread
1759 if (!init_compiler_runtime()) {
1760 return;
1761 }
1762
1763 thread->start_idle_timer();
1764
1765 // Poll for new compilation tasks as long as the JVM runs. Compilation
1766 // should only be disabled if something went wrong while initializing the
1767 // compiler runtimes. This, in turn, should not happen. The only known case
1768 // when compiler runtime initialization fails is if there is not enough free
1769 // space in the code cache to generate the necessary stubs, etc.
1770 while (!is_compilation_disabled_forever()) {
1771 // We need this HandleMark to avoid leaking VM handles.
1772 HandleMark hm(thread);
1773
1774 CompileTask* task = queue->get();
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 }
1787 continue;
1788 }
1789
1790 if (UseDynamicNumberOfCompilerThreads) {
1791 possibly_add_compiler_threads();
1792 }
1793
1794 // Give compiler threads an extra quanta. They tend to be bursty and
1795 // this helps the compiler to finish up the job.
1796 if (CompilerThreadHintNoPreempt) {
1797 os::hint_no_preempt();
1798 }
1799
1800 // Assign the task to the current thread. Mark this compilation
1801 // thread as active for the profiler.
1802 CompileTaskWrapper ctw(task);
1803 nmethodLocker result_handle; // (handle for the nmethod produced by this task)
1804 task->set_code_handle(&result_handle);
1805 methodHandle method(thread, task->method());
1806
1807 // Never compile a method if breakpoints are present in it
1808 if (method()->number_of_breakpoints() == 0) {
1809 // Compile the method.
1810 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
1811 invoke_compiler_on_method(task);
1812 thread->start_idle_timer();
1813 } else {
1814 // After compilation is disabled, remove remaining methods from queue
1815 method->clear_queued_for_compilation();
1816 task->set_failure_reason("compilation is disabled");
1817 }
1818 }
1819 }
1820
1821 // Shut down compiler runtime
1822 shutdown_compiler_runtime(thread->compiler(), thread);
1823 }
1824
1825 // ------------------------------------------------------------------
1826 // CompileBroker::init_compiler_thread_log
1827 //
1828 // Set up state required by +LogCompilation.
1829 void CompileBroker::init_compiler_thread_log() {
1830 CompilerThread* thread = CompilerThread::current();
1831 char file_name[4*K];
1832 FILE* fp = NULL;
|