23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderDataGraph.inline.hpp"
27 #include "code/compiledIC.hpp"
28 #include "code/nmethod.hpp"
29 #include "code/scopeDesc.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/methodData.hpp"
33 #include "oops/method.inline.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "prims/nativeLookup.hpp"
36 #include "runtime/compilationPolicy.hpp"
37 #include "runtime/frame.hpp"
38 #include "runtime/handles.inline.hpp"
39 #include "runtime/rframe.hpp"
40 #include "runtime/stubRoutines.hpp"
41 #include "runtime/thread.hpp"
42 #include "runtime/tieredThresholdPolicy.hpp"
43 #include "runtime/timer.hpp"
44 #include "runtime/vframe.hpp"
45 #include "runtime/vmOperations.hpp"
46 #include "utilities/events.hpp"
47 #include "utilities/globalDefinitions.hpp"
48
49 #ifdef COMPILER1
50 #include "c1/c1_Compiler.hpp"
51 #endif
52 #ifdef COMPILER2
53 #include "opto/c2compiler.hpp"
54 #endif
55
56 CompilationPolicy* CompilationPolicy::_policy;
57 elapsedTimer CompilationPolicy::_accumulated_time;
58
59 // Determine compilation policy based on command line argument
60 void compilationPolicy_init() {
61 switch(CompilationPolicyChoice) {
62 case 0:
63 CompilationPolicy::set_policy(new SimpleCompPolicy());
64 break;
65
66 case 1:
67 #ifdef COMPILER2
68 CompilationPolicy::set_policy(new StackWalkCompPolicy());
69 #else
70 Unimplemented();
71 #endif
72 break;
73 case 2:
74 #ifdef TIERED
75 CompilationPolicy::set_policy(new TieredThresholdPolicy());
76 #else
77 Unimplemented();
181 #if INCLUDE_JVMCI
182 if (UseJVMCICompiler && !BackgroundCompilation) {
183 /*
184 * In blocking compilation mode, the CompileBroker will make
185 * compilations submitted by a JVMCI compiler thread non-blocking. These
186 * compilations should be scheduled after all blocking compilations
187 * to service non-compiler related compilations sooner and reduce the
188 * chance of such compilations timing out.
189 */
190 for (CompileTask* task = compile_queue->first(); task != NULL; task = task->next()) {
191 if (task->is_blocking()) {
192 return task;
193 }
194 }
195 }
196 #endif
197 return compile_queue->first();
198 }
199
200 #ifndef PRODUCT
201 void CompilationPolicy::print_time() {
202 tty->print_cr ("Accumulated compilationPolicy times:");
203 tty->print_cr ("---------------------------");
204 tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds());
205 }
206
207 void NonTieredCompPolicy::trace_osr_completion(nmethod* osr_nm) {
208 if (TraceOnStackReplacement) {
209 if (osr_nm == NULL) tty->print_cr("compilation failed");
210 else tty->print_cr("nmethod " INTPTR_FORMAT, p2i(osr_nm));
211 }
212 }
213 #endif // !PRODUCT
214
215 void NonTieredCompPolicy::initialize() {
216 // Setup the compiler thread numbers
217 if (CICompilerCountPerCPU) {
218 // Example: if CICompilerCountPerCPU is true, then we get
219 // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine.
220 // May help big-app startup time.
221 _compiler_count = MAX2(log2_int(os::active_processor_count())-1,1);
222 // Make sure there is enough space in the code cache to hold all the compiler buffers
223 size_t buffer_size = 1;
224 #ifdef COMPILER1
225 buffer_size = is_client_compilation_mode_vm() ? Compiler::code_buffer_size() : buffer_size;
226 #endif
520 }
521 }
522 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
523
524 #ifdef COMPILER2
525 const char* StackWalkCompPolicy::_msg = NULL;
526
527
528 // Consider m for compilation
529 void StackWalkCompPolicy::method_invocation_event(const methodHandle& m, JavaThread* thread) {
530 const int comp_level = CompLevel_highest_tier;
531 const int hot_count = m->invocation_count();
532 reset_counter_for_invocation_event(m);
533
534 if (is_compilation_enabled() && m->code() == NULL && can_be_compiled(m, comp_level)) {
535 ResourceMark rm(thread);
536 frame fr = thread->last_frame();
537 assert(fr.is_interpreted_frame(), "must be interpreted");
538 assert(fr.interpreter_frame_method() == m(), "bad method");
539
540 if (TraceCompilationPolicy) {
541 tty->print("method invocation trigger: ");
542 m->print_short_name(tty);
543 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)m()), m->code_size());
544 }
545 RegisterMap reg_map(thread, false);
546 javaVFrame* triggerVF = thread->last_java_vframe(®_map);
547 // triggerVF is the frame that triggered its counter
548 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m());
549
550 if (first->top_method()->code() != NULL) {
551 // called obsolete method/nmethod -- no need to recompile
552 if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, p2i(first->top_method()->code()));
553 } else {
554 if (TimeCompilationPolicy) accumulated_time()->start();
555 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
556 stack->push(first);
557 RFrame* top = findTopInlinableFrame(stack);
558 if (TimeCompilationPolicy) accumulated_time()->stop();
559 assert(top != NULL, "findTopInlinableFrame returned null");
560 if (TraceCompilationPolicy) top->print();
561 CompileBroker::compile_method(top->top_method(), InvocationEntryBci, comp_level,
562 m, hot_count, CompileTask::Reason_InvocationCount, thread);
563 }
564 }
565 }
566
567 void StackWalkCompPolicy::method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread) {
568 const int comp_level = CompLevel_highest_tier;
569 const int hot_count = m->backedge_count();
570
571 if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
572 CompileBroker::compile_method(m, bci, comp_level, m, hot_count, CompileTask::Reason_BackedgeCount, thread);
573 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
574 }
575 }
576
577 RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
578 // go up the stack until finding a frame that (probably) won't be inlined
579 // into its caller
580 RFrame* current = stack->at(0); // current choice for stopping
581 assert( current && !current->is_compiled(), "" );
582 const char* msg = NULL;
583
584 while (1) {
585
586 // before going up the stack further, check if doing so would get us into
587 // compiled code
588 RFrame* next = senderOf(current, stack);
589 if( !next ) // No next frame up the stack?
590 break; // Then compile with current frame
591
592 Method* m = current->top_method();
593 Method* next_m = next->top_method();
594
595 if (TraceCompilationPolicy && Verbose) {
596 tty->print("[caller: ");
597 next_m->print_short_name(tty);
598 tty->print("] ");
599 }
600
601 if( !Inline ) { // Inlining turned off
602 msg = "Inlining turned off";
603 break;
604 }
605 if (next_m->is_not_compilable()) { // Did fail to compile this before/
606 msg = "caller not compilable";
607 break;
608 }
609 if (next->num() > MaxRecompilationSearchLength) {
610 // don't go up too high when searching for recompilees
611 msg = "don't go up any further: > MaxRecompilationSearchLength";
612 break;
613 }
614 if (next->distance() > MaxInterpretedSearchLength) {
615 // don't go up too high when searching for recompilees
616 msg = "don't go up any further: next > MaxInterpretedSearchLength";
617 break;
618 }
619 // Compiled frame above already decided not to inline;
620 // do not recompile him.
656 break;
657 }
658 // Check inlining negative tests
659 if ((msg = shouldNotInline(m)) != NULL) {
660 break;
661 }
662
663
664 // If the caller method is too big or something then we do not want to
665 // compile it just to inline a method
666 if (!can_be_compiled(next_m, CompLevel_any)) {
667 msg = "caller cannot be compiled";
668 break;
669 }
670
671 if( next_m->name() == vmSymbols::class_initializer_name() ) {
672 msg = "do not compile class initializer (OSR ok)";
673 break;
674 }
675
676 if (TraceCompilationPolicy && Verbose) {
677 tty->print("\n\t check caller: ");
678 next_m->print_short_name(tty);
679 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)next_m), next_m->code_size());
680 }
681
682 current = next;
683 }
684
685 assert( !current || !current->is_compiled(), "" );
686
687 if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg);
688
689 return current;
690 }
691
692 RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
693 RFrame* sender = rf->caller();
694 if (sender && sender->num() == stack->length()) stack->push(sender);
695 return sender;
696 }
697
698
699 const char* StackWalkCompPolicy::shouldInline(const methodHandle& m, float freq, int cnt) {
700 // Allows targeted inlining
701 // positive filter: should send be inlined? returns NULL (--> yes)
702 // or rejection msg
703 int max_size = MaxInlineSize;
704 int cost = m->code_size();
705
706 // Check for too many throws (and not too huge)
707 if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
|
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderDataGraph.inline.hpp"
27 #include "code/compiledIC.hpp"
28 #include "code/nmethod.hpp"
29 #include "code/scopeDesc.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/methodData.hpp"
33 #include "oops/method.inline.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "prims/nativeLookup.hpp"
36 #include "runtime/compilationPolicy.hpp"
37 #include "runtime/frame.hpp"
38 #include "runtime/handles.inline.hpp"
39 #include "runtime/rframe.hpp"
40 #include "runtime/stubRoutines.hpp"
41 #include "runtime/thread.hpp"
42 #include "runtime/tieredThresholdPolicy.hpp"
43 #include "runtime/vframe.hpp"
44 #include "runtime/vmOperations.hpp"
45 #include "utilities/events.hpp"
46 #include "utilities/globalDefinitions.hpp"
47
48 #ifdef COMPILER1
49 #include "c1/c1_Compiler.hpp"
50 #endif
51 #ifdef COMPILER2
52 #include "opto/c2compiler.hpp"
53 #endif
54
55 CompilationPolicy* CompilationPolicy::_policy;
56
57 // Determine compilation policy based on command line argument
58 void compilationPolicy_init() {
59 switch(CompilationPolicyChoice) {
60 case 0:
61 CompilationPolicy::set_policy(new SimpleCompPolicy());
62 break;
63
64 case 1:
65 #ifdef COMPILER2
66 CompilationPolicy::set_policy(new StackWalkCompPolicy());
67 #else
68 Unimplemented();
69 #endif
70 break;
71 case 2:
72 #ifdef TIERED
73 CompilationPolicy::set_policy(new TieredThresholdPolicy());
74 #else
75 Unimplemented();
179 #if INCLUDE_JVMCI
180 if (UseJVMCICompiler && !BackgroundCompilation) {
181 /*
182 * In blocking compilation mode, the CompileBroker will make
183 * compilations submitted by a JVMCI compiler thread non-blocking. These
184 * compilations should be scheduled after all blocking compilations
185 * to service non-compiler related compilations sooner and reduce the
186 * chance of such compilations timing out.
187 */
188 for (CompileTask* task = compile_queue->first(); task != NULL; task = task->next()) {
189 if (task->is_blocking()) {
190 return task;
191 }
192 }
193 }
194 #endif
195 return compile_queue->first();
196 }
197
198 #ifndef PRODUCT
199 void NonTieredCompPolicy::trace_osr_completion(nmethod* osr_nm) {
200 if (TraceOnStackReplacement) {
201 if (osr_nm == NULL) tty->print_cr("compilation failed");
202 else tty->print_cr("nmethod " INTPTR_FORMAT, p2i(osr_nm));
203 }
204 }
205 #endif // !PRODUCT
206
207 void NonTieredCompPolicy::initialize() {
208 // Setup the compiler thread numbers
209 if (CICompilerCountPerCPU) {
210 // Example: if CICompilerCountPerCPU is true, then we get
211 // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine.
212 // May help big-app startup time.
213 _compiler_count = MAX2(log2_int(os::active_processor_count())-1,1);
214 // Make sure there is enough space in the code cache to hold all the compiler buffers
215 size_t buffer_size = 1;
216 #ifdef COMPILER1
217 buffer_size = is_client_compilation_mode_vm() ? Compiler::code_buffer_size() : buffer_size;
218 #endif
512 }
513 }
514 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
515
516 #ifdef COMPILER2
517 const char* StackWalkCompPolicy::_msg = NULL;
518
519
520 // Consider m for compilation
521 void StackWalkCompPolicy::method_invocation_event(const methodHandle& m, JavaThread* thread) {
522 const int comp_level = CompLevel_highest_tier;
523 const int hot_count = m->invocation_count();
524 reset_counter_for_invocation_event(m);
525
526 if (is_compilation_enabled() && m->code() == NULL && can_be_compiled(m, comp_level)) {
527 ResourceMark rm(thread);
528 frame fr = thread->last_frame();
529 assert(fr.is_interpreted_frame(), "must be interpreted");
530 assert(fr.interpreter_frame_method() == m(), "bad method");
531
532 RegisterMap reg_map(thread, false);
533 javaVFrame* triggerVF = thread->last_java_vframe(®_map);
534 // triggerVF is the frame that triggered its counter
535 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m());
536
537 if (first->top_method()->code() != NULL) {
538 // called obsolete method/nmethod -- no need to recompile
539 } else {
540 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
541 stack->push(first);
542 RFrame* top = findTopInlinableFrame(stack);
543 assert(top != NULL, "findTopInlinableFrame returned null");
544 CompileBroker::compile_method(top->top_method(), InvocationEntryBci, comp_level,
545 m, hot_count, CompileTask::Reason_InvocationCount, thread);
546 }
547 }
548 }
549
550 void StackWalkCompPolicy::method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread) {
551 const int comp_level = CompLevel_highest_tier;
552 const int hot_count = m->backedge_count();
553
554 if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
555 CompileBroker::compile_method(m, bci, comp_level, m, hot_count, CompileTask::Reason_BackedgeCount, thread);
556 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
557 }
558 }
559
560 RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
561 // go up the stack until finding a frame that (probably) won't be inlined
562 // into its caller
563 RFrame* current = stack->at(0); // current choice for stopping
564 assert( current && !current->is_compiled(), "" );
565 const char* msg = NULL;
566
567 while (1) {
568
569 // before going up the stack further, check if doing so would get us into
570 // compiled code
571 RFrame* next = senderOf(current, stack);
572 if( !next ) // No next frame up the stack?
573 break; // Then compile with current frame
574
575 Method* m = current->top_method();
576 Method* next_m = next->top_method();
577
578 if( !Inline ) { // Inlining turned off
579 msg = "Inlining turned off";
580 break;
581 }
582 if (next_m->is_not_compilable()) { // Did fail to compile this before/
583 msg = "caller not compilable";
584 break;
585 }
586 if (next->num() > MaxRecompilationSearchLength) {
587 // don't go up too high when searching for recompilees
588 msg = "don't go up any further: > MaxRecompilationSearchLength";
589 break;
590 }
591 if (next->distance() > MaxInterpretedSearchLength) {
592 // don't go up too high when searching for recompilees
593 msg = "don't go up any further: next > MaxInterpretedSearchLength";
594 break;
595 }
596 // Compiled frame above already decided not to inline;
597 // do not recompile him.
633 break;
634 }
635 // Check inlining negative tests
636 if ((msg = shouldNotInline(m)) != NULL) {
637 break;
638 }
639
640
641 // If the caller method is too big or something then we do not want to
642 // compile it just to inline a method
643 if (!can_be_compiled(next_m, CompLevel_any)) {
644 msg = "caller cannot be compiled";
645 break;
646 }
647
648 if( next_m->name() == vmSymbols::class_initializer_name() ) {
649 msg = "do not compile class initializer (OSR ok)";
650 break;
651 }
652
653 current = next;
654 }
655
656 assert( !current || !current->is_compiled(), "" );
657
658 return current;
659 }
660
661 RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
662 RFrame* sender = rf->caller();
663 if (sender && sender->num() == stack->length()) stack->push(sender);
664 return sender;
665 }
666
667
668 const char* StackWalkCompPolicy::shouldInline(const methodHandle& m, float freq, int cnt) {
669 // Allows targeted inlining
670 // positive filter: should send be inlined? returns NULL (--> yes)
671 // or rejection msg
672 int max_size = MaxInlineSize;
673 int cost = m->code_size();
674
675 // Check for too many throws (and not too huge)
676 if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
|