src/share/vm/compiler/compileBroker.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/compiler

src/share/vm/compiler/compileBroker.cpp

Print this page
rev 10344 : 8150646: Add support for blocking compiles through whitebox API


 821   // Since we are at a safepoint, we do not need a lock to access
 822   // the compile queues.
 823   if (_c2_compile_queue != NULL) {
 824     _c2_compile_queue->mark_on_stack();
 825   }
 826   if (_c1_compile_queue != NULL) {
 827     _c1_compile_queue->mark_on_stack();
 828   }
 829 }
 830 
 831 // ------------------------------------------------------------------
 832 // CompileBroker::compile_method
 833 //
 834 // Request compilation of a method.
 835 void CompileBroker::compile_method_base(const methodHandle& method,
 836                                         int osr_bci,
 837                                         int comp_level,
 838                                         const methodHandle& hot_method,
 839                                         int hot_count,
 840                                         const char* comment,

 841                                         Thread* thread) {
 842   // do nothing if compiler thread(s) is not available
 843   if (!_initialized) {
 844     return;
 845   }
 846 
 847   guarantee(!method->is_abstract(), "cannot compile abstract methods");
 848   assert(method->method_holder()->is_instance_klass(),
 849          "sanity check");
 850   assert(!method->method_holder()->is_not_initialized(),
 851          "method holder must be initialized");
 852   assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
 853 
 854   if (CIPrintRequests) {
 855     tty->print("request: ");
 856     method->print_short_name(tty);
 857     if (osr_bci != InvocationEntryBci) {
 858       tty->print(" osr_bci: %d", osr_bci);
 859     }
 860     tty->print(" level: %d comment: %s count: %d", comp_level, comment, hot_count);


 899     return;
 900   }
 901 
 902   // If the requesting thread is holding the pending list lock
 903   // then we just return. We can't risk blocking while holding
 904   // the pending list lock or a 3-way deadlock may occur
 905   // between the reference handler thread, a GC (instigated
 906   // by a compiler thread), and compiled method registration.
 907   if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) {
 908     return;
 909   }
 910 
 911   if (TieredCompilation) {
 912     // Tiered policy requires MethodCounters to exist before adding a method to
 913     // the queue. Create if we don't have them yet.
 914     method->get_method_counters(thread);
 915   }
 916 
 917   // Outputs from the following MutexLocker block:
 918   CompileTask* task     = NULL;
 919   bool         blocking = false;
 920   CompileQueue* queue  = compile_queue(comp_level);
 921 
 922   // Acquire our lock.
 923   {
 924     MutexLocker locker(MethodCompileQueue_lock, thread);
 925 
 926     // Make sure the method has not slipped into the queues since
 927     // last we checked; note that those checks were "fast bail-outs".
 928     // Here we need to be more careful, see 14012000 below.
 929     if (compilation_is_in_queue(method)) {
 930       return;
 931     }
 932 
 933     // We need to check again to see if the compilation has
 934     // completed.  A previous compilation may have registered
 935     // some result.
 936     if (compilation_is_complete(method, osr_bci, comp_level)) {
 937       return;
 938     }
 939 
 940     // We now know that this compilation is not pending, complete,
 941     // or prohibited.  Assign a compile_id to this compilation
 942     // and check to see if it is in our [Start..Stop) range.
 943     int compile_id = assign_compile_id(method, osr_bci);
 944     if (compile_id == 0) {
 945       // The compilation falls outside the allowed range.
 946       return;
 947     }
 948 
 949     // Should this thread wait for completion of the compile?
 950     blocking = is_compile_blocking();
 951 
 952 #if INCLUDE_JVMCI
 953     if (UseJVMCICompiler) {
 954       if (blocking) {
 955         // Don't allow blocking compiles for requests triggered by JVMCI.
 956         if (thread->is_Compiler_thread()) {
 957           blocking = false;
 958         }
 959 
 960         // Don't allow blocking compiles if inside a class initializer or while performing class loading
 961         vframeStream vfst((JavaThread*) thread);
 962         for (; !vfst.at_end(); vfst.next()) {
 963           if (vfst.method()->is_static_initializer() ||
 964               (vfst.method()->method_holder()->is_subclass_of(SystemDictionary::ClassLoader_klass()) &&
 965                   vfst.method()->name() == vmSymbols::loadClass_name())) {
 966             blocking = false;
 967             break;
 968           }
 969         }
 970 
 971         // Don't allow blocking compilation requests to JVMCI


1017     // completed, and end up introducing a "duplicate" (redundant) task.
1018     // In that case, the compiler thread should first check if a method
1019     // has already been compiled before trying to compile it.
1020     // NOTE: in the event that there are multiple compiler threads and
1021     // there is de-optimization/recompilation, things will get hairy,
1022     // and in that case it's best to protect both the testing (here) of
1023     // these bits, and their updating (here and elsewhere) under a
1024     // common lock.
1025     task = create_compile_task(queue,
1026                                compile_id, method,
1027                                osr_bci, comp_level,
1028                                hot_method, hot_count, comment,
1029                                blocking);
1030   }
1031 
1032   if (blocking) {
1033     wait_for_completion(task);
1034   }
1035 }
1036 
1037 
1038 nmethod* CompileBroker::compile_method(const methodHandle& method, int osr_bci,
1039                                        int comp_level,
1040                                        const methodHandle& hot_method, int hot_count,
1041                                        const char* comment, Thread* THREAD) {













1042   // make sure arguments make sense
1043   assert(method->method_holder()->is_instance_klass(), "not an instance method");
1044   assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range");
1045   assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods");
1046   assert(!method->method_holder()->is_not_initialized(), "method holder must be initialized");
1047   // allow any levels for WhiteBox
1048   assert(WhiteBoxAPI || TieredCompilation || comp_level == CompLevel_highest_tier, "only CompLevel_highest_tier must be used in non-tiered");
1049   // return quickly if possible
1050 
1051   // lock, make sure that the compilation
1052   // isn't prohibited in a straightforward way.
1053   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
1054   if (comp == NULL || !comp->can_compile_method(method) ||
1055       compilation_is_prohibited(method, osr_bci, comp_level)) {
1056     return NULL;
1057   }
1058 
1059   if (osr_bci == InvocationEntryBci) {
1060     // standard compilation
1061     nmethod* method_code = method->code();
1062     if (method_code != NULL) {
1063       if (compilation_is_complete(method, osr_bci, comp_level)) {
1064         return method_code;
1065       }
1066     }
1067     if (method->is_not_compilable(comp_level)) {
1068       return NULL;
1069     }
1070   } else {
1071     // osr compilation
1072 #ifndef TIERED
1073     // seems like an assert of dubious value
1074     assert(comp_level == CompLevel_highest_tier,
1075            "all OSR compiles are assumed to be at a single compilation level");


1143             method->intrinsic_id() == vmIntrinsics::_doubleToRawLongBits))) {
1144         return NULL;
1145       }
1146 
1147       // To properly handle the appendix argument for out-of-line calls we are using a small trampoline that
1148       // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
1149       //
1150       // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
1151       // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
1152       AdapterHandlerLibrary::create_native_wrapper(method);
1153     } else {
1154       return NULL;
1155     }
1156   } else {
1157     // If the compiler is shut off due to code cache getting full
1158     // fail out now so blocking compiles dont hang the java thread
1159     if (!should_compile_new_jobs()) {
1160       CompilationPolicy::policy()->delay_compilation(method());
1161       return NULL;
1162     }
1163     compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, THREAD);
1164   }
1165 
1166   // return requested nmethod
1167   // We accept a higher level osr method
1168   if (osr_bci == InvocationEntryBci) {
1169     return method->code();
1170   }
1171   return method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
1172 }
1173 
1174 
1175 // ------------------------------------------------------------------
1176 // CompileBroker::compilation_is_complete
1177 //
1178 // See if compilation of this method is already complete.
1179 bool CompileBroker::compilation_is_complete(const methodHandle& method,
1180                                             int                 osr_bci,
1181                                             int                 comp_level) {
1182   bool is_osr = (osr_bci != standard_entry_bci);
1183   if (is_osr) {


1200 
1201 
1202 /**
1203  * See if this compilation is already requested.
1204  *
1205  * Implementation note: there is only a single "is in queue" bit
1206  * for each method.  This means that the check below is overly
1207  * conservative in the sense that an osr compilation in the queue
1208  * will block a normal compilation from entering the queue (and vice
1209  * versa).  This can be remedied by a full queue search to disambiguate
1210  * cases.  If it is deemed profitable, this may be done.
1211  */
1212 bool CompileBroker::compilation_is_in_queue(const methodHandle& method) {
1213   return method->queued_for_compilation();
1214 }
1215 
1216 // ------------------------------------------------------------------
1217 // CompileBroker::compilation_is_prohibited
1218 //
1219 // See if this compilation is not allowed.
1220 bool CompileBroker::compilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level) {
1221   bool is_native = method->is_native();
1222   // Some compilers may not support the compilation of natives.
1223   AbstractCompiler *comp = compiler(comp_level);
1224   if (is_native &&
1225       (!CICompileNatives || comp == NULL || !comp->supports_native())) {
1226     method->set_not_compilable_quietly(comp_level);
1227     return true;
1228   }
1229 
1230   bool is_osr = (osr_bci != standard_entry_bci);
1231   // Some compilers may not support on stack replacement.
1232   if (is_osr &&
1233       (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
1234     method->set_not_osr_compilable(comp_level);
1235     return true;
1236   }
1237 
1238   // Breaking the abstraction - directives are only used inside a compilation otherwise.
1239   DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, comp);
1240   bool excluded = directive->ExcludeOption;
1241   DirectivesStack::release(directive);
1242 
1243   // The method may be explicitly excluded by the user.
1244   double scale;
1245   if (excluded || (CompilerOracle::has_option_value(method, "CompileThresholdScaling", scale) && scale == 0)) {
1246     bool quietly = CompilerOracle::should_exclude_quietly();
1247     if (PrintCompilation && !quietly) {
1248       // This does not happen quietly...
1249       ResourceMark rm;
1250       tty->print("### Excluding %s:%s",
1251                  method->is_native() ? "generation of native wrapper" : "compile",
1252                  (method->is_static() ? " static" : ""));
1253       method->print_short_name(tty);
1254       tty->cr();
1255     }
1256     method->set_not_compilable(comp_level, !quietly, "excluded by CompileCommand");
1257   }
1258 
1259   return false;
1260 }
1261 




 821   // Since we are at a safepoint, we do not need a lock to access
 822   // the compile queues.
 823   if (_c2_compile_queue != NULL) {
 824     _c2_compile_queue->mark_on_stack();
 825   }
 826   if (_c1_compile_queue != NULL) {
 827     _c1_compile_queue->mark_on_stack();
 828   }
 829 }
 830 
 831 // ------------------------------------------------------------------
 832 // CompileBroker::compile_method
 833 //
 834 // Request compilation of a method.
 835 void CompileBroker::compile_method_base(const methodHandle& method,
 836                                         int osr_bci,
 837                                         int comp_level,
 838                                         const methodHandle& hot_method,
 839                                         int hot_count,
 840                                         const char* comment,
 841                                         bool blocking,
 842                                         Thread* thread) {
 843   // do nothing if compiler thread(s) is not available
 844   if (!_initialized) {
 845     return;
 846   }
 847 
 848   guarantee(!method->is_abstract(), "cannot compile abstract methods");
 849   assert(method->method_holder()->is_instance_klass(),
 850          "sanity check");
 851   assert(!method->method_holder()->is_not_initialized(),
 852          "method holder must be initialized");
 853   assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
 854 
 855   if (CIPrintRequests) {
 856     tty->print("request: ");
 857     method->print_short_name(tty);
 858     if (osr_bci != InvocationEntryBci) {
 859       tty->print(" osr_bci: %d", osr_bci);
 860     }
 861     tty->print(" level: %d comment: %s count: %d", comp_level, comment, hot_count);


 900     return;
 901   }
 902 
 903   // If the requesting thread is holding the pending list lock
 904   // then we just return. We can't risk blocking while holding
 905   // the pending list lock or a 3-way deadlock may occur
 906   // between the reference handler thread, a GC (instigated
 907   // by a compiler thread), and compiled method registration.
 908   if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) {
 909     return;
 910   }
 911 
 912   if (TieredCompilation) {
 913     // Tiered policy requires MethodCounters to exist before adding a method to
 914     // the queue. Create if we don't have them yet.
 915     method->get_method_counters(thread);
 916   }
 917 
 918   // Outputs from the following MutexLocker block:
 919   CompileTask* task     = NULL;

 920   CompileQueue* queue  = compile_queue(comp_level);
 921 
 922   // Acquire our lock.
 923   {
 924     MutexLocker locker(MethodCompileQueue_lock, thread);
 925 
 926     // Make sure the method has not slipped into the queues since
 927     // last we checked; note that those checks were "fast bail-outs".
 928     // Here we need to be more careful, see 14012000 below.
 929     if (compilation_is_in_queue(method)) {
 930       return;
 931     }
 932 
 933     // We need to check again to see if the compilation has
 934     // completed.  A previous compilation may have registered
 935     // some result.
 936     if (compilation_is_complete(method, osr_bci, comp_level)) {
 937       return;
 938     }
 939 
 940     // We now know that this compilation is not pending, complete,
 941     // or prohibited.  Assign a compile_id to this compilation
 942     // and check to see if it is in our [Start..Stop) range.
 943     int compile_id = assign_compile_id(method, osr_bci);
 944     if (compile_id == 0) {
 945       // The compilation falls outside the allowed range.
 946       return;
 947     }
 948 



 949 #if INCLUDE_JVMCI
 950     if (UseJVMCICompiler) {
 951       if (blocking) {
 952         // Don't allow blocking compiles for requests triggered by JVMCI.
 953         if (thread->is_Compiler_thread()) {
 954           blocking = false;
 955         }
 956 
 957         // Don't allow blocking compiles if inside a class initializer or while performing class loading
 958         vframeStream vfst((JavaThread*) thread);
 959         for (; !vfst.at_end(); vfst.next()) {
 960           if (vfst.method()->is_static_initializer() ||
 961               (vfst.method()->method_holder()->is_subclass_of(SystemDictionary::ClassLoader_klass()) &&
 962                   vfst.method()->name() == vmSymbols::loadClass_name())) {
 963             blocking = false;
 964             break;
 965           }
 966         }
 967 
 968         // Don't allow blocking compilation requests to JVMCI


1014     // completed, and end up introducing a "duplicate" (redundant) task.
1015     // In that case, the compiler thread should first check if a method
1016     // has already been compiled before trying to compile it.
1017     // NOTE: in the event that there are multiple compiler threads and
1018     // there is de-optimization/recompilation, things will get hairy,
1019     // and in that case it's best to protect both the testing (here) of
1020     // these bits, and their updating (here and elsewhere) under a
1021     // common lock.
1022     task = create_compile_task(queue,
1023                                compile_id, method,
1024                                osr_bci, comp_level,
1025                                hot_method, hot_count, comment,
1026                                blocking);
1027   }
1028 
1029   if (blocking) {
1030     wait_for_completion(task);
1031   }
1032 }
1033 

1034 nmethod* CompileBroker::compile_method(const methodHandle& method, int osr_bci,
1035                                        int comp_level,
1036                                        const methodHandle& hot_method, int hot_count,
1037                                        const char* comment, Thread* THREAD) {
1038   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
1039   DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, comp);
1040   nmethod* nm = CompileBroker::compile_method(method, osr_bci, comp_level, hot_method, hot_count, comment, directive, THREAD);
1041   DirectivesStack::release(directive);
1042   return nm;
1043 }
1044 
1045 nmethod* CompileBroker::compile_method(const methodHandle& method, int osr_bci,
1046                                          int comp_level,
1047                                          const methodHandle& hot_method, int hot_count,
1048                                          const char* comment, DirectiveSet* directive,
1049                                          Thread* THREAD) {
1050 
1051   // make sure arguments make sense
1052   assert(method->method_holder()->is_instance_klass(), "not an instance method");
1053   assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range");
1054   assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods");
1055   assert(!method->method_holder()->is_not_initialized(), "method holder must be initialized");
1056   // allow any levels for WhiteBox
1057   assert(WhiteBoxAPI || TieredCompilation || comp_level == CompLevel_highest_tier, "only CompLevel_highest_tier must be used in non-tiered");
1058   // return quickly if possible
1059 
1060   // lock, make sure that the compilation
1061   // isn't prohibited in a straightforward way.
1062   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
1063   if (comp == NULL || !comp->can_compile_method(method) ||
1064       compilation_is_prohibited(method, osr_bci, comp_level, directive->ExcludeOption)) {
1065     return NULL;
1066   }
1067 
1068   if (osr_bci == InvocationEntryBci) {
1069     // standard compilation
1070     nmethod* method_code = method->code();
1071     if (method_code != NULL) {
1072       if (compilation_is_complete(method, osr_bci, comp_level)) {
1073         return method_code;
1074       }
1075     }
1076     if (method->is_not_compilable(comp_level)) {
1077       return NULL;
1078     }
1079   } else {
1080     // osr compilation
1081 #ifndef TIERED
1082     // seems like an assert of dubious value
1083     assert(comp_level == CompLevel_highest_tier,
1084            "all OSR compiles are assumed to be at a single compilation level");


1152             method->intrinsic_id() == vmIntrinsics::_doubleToRawLongBits))) {
1153         return NULL;
1154       }
1155 
1156       // To properly handle the appendix argument for out-of-line calls we are using a small trampoline that
1157       // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
1158       //
1159       // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
1160       // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
1161       AdapterHandlerLibrary::create_native_wrapper(method);
1162     } else {
1163       return NULL;
1164     }
1165   } else {
1166     // If the compiler is shut off due to code cache getting full
1167     // fail out now so blocking compiles dont hang the java thread
1168     if (!should_compile_new_jobs()) {
1169       CompilationPolicy::policy()->delay_compilation(method());
1170       return NULL;
1171     }
1172     compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, !directive->BackgroundCompilationOption, THREAD);
1173   }
1174 
1175   // return requested nmethod
1176   // We accept a higher level osr method
1177   if (osr_bci == InvocationEntryBci) {
1178     return method->code();
1179   }
1180   return method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
1181 }
1182 
1183 
1184 // ------------------------------------------------------------------
1185 // CompileBroker::compilation_is_complete
1186 //
1187 // See if compilation of this method is already complete.
1188 bool CompileBroker::compilation_is_complete(const methodHandle& method,
1189                                             int                 osr_bci,
1190                                             int                 comp_level) {
1191   bool is_osr = (osr_bci != standard_entry_bci);
1192   if (is_osr) {


1209 
1210 
1211 /**
1212  * See if this compilation is already requested.
1213  *
1214  * Implementation note: there is only a single "is in queue" bit
1215  * for each method.  This means that the check below is overly
1216  * conservative in the sense that an osr compilation in the queue
1217  * will block a normal compilation from entering the queue (and vice
1218  * versa).  This can be remedied by a full queue search to disambiguate
1219  * cases.  If it is deemed profitable, this may be done.
1220  */
1221 bool CompileBroker::compilation_is_in_queue(const methodHandle& method) {
1222   return method->queued_for_compilation();
1223 }
1224 
1225 // ------------------------------------------------------------------
1226 // CompileBroker::compilation_is_prohibited
1227 //
1228 // See if this compilation is not allowed.
1229 bool CompileBroker::compilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level, bool excluded) {
1230   bool is_native = method->is_native();
1231   // Some compilers may not support the compilation of natives.
1232   AbstractCompiler *comp = compiler(comp_level);
1233   if (is_native &&
1234       (!CICompileNatives || comp == NULL || !comp->supports_native())) {
1235     method->set_not_compilable_quietly(comp_level);
1236     return true;
1237   }
1238 
1239   bool is_osr = (osr_bci != standard_entry_bci);
1240   // Some compilers may not support on stack replacement.
1241   if (is_osr &&
1242       (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
1243     method->set_not_osr_compilable(comp_level);
1244     return true;
1245   }





1246 
1247   // The method may be explicitly excluded by the user.
1248   double scale;
1249   if (excluded || (CompilerOracle::has_option_value(method, "CompileThresholdScaling", scale) && scale == 0)) {
1250     bool quietly = CompilerOracle::should_exclude_quietly();
1251     if (PrintCompilation && !quietly) {
1252       // This does not happen quietly...
1253       ResourceMark rm;
1254       tty->print("### Excluding %s:%s",
1255                  method->is_native() ? "generation of native wrapper" : "compile",
1256                  (method->is_static() ? " static" : ""));
1257       method->print_short_name(tty);
1258       tty->cr();
1259     }
1260     method->set_not_compilable(comp_level, !quietly, "excluded by CompileCommand");
1261   }
1262 
1263   return false;
1264 }
1265 


src/share/vm/compiler/compileBroker.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File