12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "code/codeCache.hpp"
29 #include "compiler/compileBroker.hpp"
30 #include "compiler/compileLog.hpp"
31 #include "compiler/compilerOracle.hpp"
32 #include "interpreter/linkResolver.hpp"
33 #include "memory/allocation.inline.hpp"
34 #include "oops/methodData.hpp"
35 #include "oops/method.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "prims/nativeLookup.hpp"
38 #include "prims/whitebox.hpp"
39 #include "runtime/arguments.hpp"
40 #include "runtime/atomic.inline.hpp"
41 #include "runtime/compilationPolicy.hpp"
42 #include "runtime/init.hpp"
43 #include "runtime/interfaceSupport.hpp"
44 #include "runtime/javaCalls.hpp"
45 #include "runtime/os.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "runtime/sweeper.hpp"
48 #include "trace/tracing.hpp"
49 #include "utilities/dtrace.hpp"
50 #include "utilities/events.hpp"
51 #ifdef COMPILER1
83 (char *) comp_name, strlen(comp_name), \
84 (char *) klass_name->bytes(), klass_name->utf8_length(), \
85 (char *) name->bytes(), name->utf8_length(), \
86 (char *) signature->bytes(), signature->utf8_length(), (success)); \
87 }
88
89 #else // ndef DTRACE_ENABLED
90
91 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
92 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
93
94 #endif // ndef DTRACE_ENABLED
95
96 bool CompileBroker::_initialized = false;
97 volatile bool CompileBroker::_should_block = false;
98 volatile jint CompileBroker::_print_compilation_warning = 0;
99 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
100
101 // The installed compiler(s)
102 AbstractCompiler* CompileBroker::_compilers[2];
103
104 // These counters are used to assign an unique ID to each compilation.
105 volatile jint CompileBroker::_compilation_id = 0;
106 volatile jint CompileBroker::_osr_compilation_id = 0;
107
108 // Debugging information
109 int CompileBroker::_last_compile_type = no_compile;
110 int CompileBroker::_last_compile_level = CompLevel_none;
111 char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
112
113 // Performance counters
114 PerfCounter* CompileBroker::_perf_total_compilation = NULL;
115 PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
116 PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
117
118 PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
119 PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
120 PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
121 PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
122 PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
180 StringLogMessage lm;
181 lm.print("%4d COMPILE SKIPPED: %s", task->compile_id(), reason);
182 if (retry_message != NULL) {
183 lm.append(" (%s)", retry_message);
184 }
185 lm.print("\n");
186 log(thread, "%s", (const char*)lm);
187 }
188
189 void log_metaspace_failure(const char* reason) {
190 ResourceMark rm;
191 StringLogMessage lm;
192 lm.print("%4d COMPILE PROFILING SKIPPED: %s", -1, reason);
193 lm.print("\n");
194 log(JavaThread::current(), "%s", (const char*)lm);
195 }
196 };
197
198 static CompilationLog* _compilation_log = NULL;
199
200 void compileBroker_init() {
201 if (LogEvents) {
202 _compilation_log = new CompilationLog();
203 }
204 }
205
206 CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
207 CompilerThread* thread = CompilerThread::current();
208 thread->set_task(task);
209 CompileLog* log = thread->log();
210 if (log != NULL) task->log_task_start(log);
211 }
212
213 CompileTaskWrapper::~CompileTaskWrapper() {
214 CompilerThread* thread = CompilerThread::current();
215 CompileTask* task = thread->task();
216 CompileLog* log = thread->log();
217 if (log != NULL) task->log_task_done(log);
218 thread->set_task(NULL);
219 task->set_code_handle(NULL);
220 thread->set_env(NULL);
221 if (task->is_blocking()) {
222 MutexLocker notifier(task->lock(), thread);
223 task->mark_complete();
1126 //
1127 // See if this compilation is not allowed.
1128 bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) {
1129 bool is_native = method->is_native();
1130 // Some compilers may not support the compilation of natives.
1131 AbstractCompiler *comp = compiler(comp_level);
1132 if (is_native &&
1133 (!CICompileNatives || comp == NULL || !comp->supports_native())) {
1134 method->set_not_compilable_quietly(comp_level);
1135 return true;
1136 }
1137
1138 bool is_osr = (osr_bci != standard_entry_bci);
1139 // Some compilers may not support on stack replacement.
1140 if (is_osr &&
1141 (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
1142 method->set_not_osr_compilable(comp_level);
1143 return true;
1144 }
1145
1146 // The method may be explicitly excluded by the user.
1147 bool quietly;
1148 double scale;
1149 if (CompilerOracle::should_exclude(method, quietly)
1150 || (CompilerOracle::has_option_value(method, "CompileThresholdScaling", scale) && scale == 0)) {
1151 if (!quietly) {
1152 // This does not happen quietly...
1153 ResourceMark rm;
1154 tty->print("### Excluding %s:%s",
1155 method->is_native() ? "generation of native wrapper" : "compile",
1156 (method->is_static() ? " static" : ""));
1157 method->print_short_name(tty);
1158 tty->cr();
1159 }
1160 method->set_not_compilable(CompLevel_all, !quietly, "excluded by CompileCommand");
1161 }
1162
1163 return false;
1164 }
1165
1166 /**
1167 * Generate serialized IDs for compilation requests. If certain debugging flags are used
1168 * and the ID is not within the specified range, the method is not compiled and 0 is returned.
1169 * The function also allows to generate separate compilation IDs for OSR compilations.
1170 */
1282 }
1283
1284 /**
1285 * Initialize compiler thread(s) + compiler object(s). The postcondition
1286 * of this function is that the compiler runtimes are initialized and that
1287 * compiler threads can start compiling.
1288 */
1289 bool CompileBroker::init_compiler_runtime() {
1290 CompilerThread* thread = CompilerThread::current();
1291 AbstractCompiler* comp = thread->compiler();
1292 // Final sanity check - the compiler object must exist
1293 guarantee(comp != NULL, "Compiler object must exist");
1294
1295 int system_dictionary_modification_counter;
1296 {
1297 MutexLocker locker(Compile_lock, thread);
1298 system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1299 }
1300
1301 {
1302 // Must switch to native to allocate ci_env
1303 ThreadToNativeFromVM ttn(thread);
1304 ciEnv ci_env(NULL, system_dictionary_modification_counter);
1305 // Cache Jvmti state
1306 ci_env.cache_jvmti_state();
1307 // Cache DTrace flags
1308 ci_env.cache_dtrace_flags();
1309
1310 // Switch back to VM state to do compiler initialization
1311 ThreadInVMfromNative tv(thread);
1312 ResetNoHandleMark rnhm;
1313
1314
1315 if (!comp->is_shark()) {
1316 // Perform per-thread and global initializations
1317 comp->initialize();
1318 }
1319 }
1320
1321 if (comp->is_failed()) {
1322 disable_compilation_forever();
1323 // If compiler initialization failed, no compiler thread that is specific to a
1324 // particular compiler runtime will ever start to compile methods.
1325 shutdown_compiler_runtime(comp, thread);
1326 return false;
1327 }
1328
1329 // C1 specific check
1330 if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) {
1331 warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
1332 return false;
1333 }
1334
1541 tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
1542 #endif
1543 ThreadInVMfromNative tivfn(JavaThread::current());
1544 }
1545 }
1546
1547 // wrapper for CodeCache::print_summary()
1548 static void codecache_print(bool detailed)
1549 {
1550 ResourceMark rm;
1551 stringStream s;
1552 // Dump code cache into a buffer before locking the tty,
1553 {
1554 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1555 CodeCache::print_summary(&s, detailed);
1556 }
1557 ttyLocker ttyl;
1558 tty->print("%s", s.as_string());
1559 }
1560
1561 // ------------------------------------------------------------------
1562 // CompileBroker::invoke_compiler_on_method
1563 //
1564 // Compile a method.
1565 //
1566 void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
1567 if (PrintCompilation) {
1568 ResourceMark rm;
1569 task->print_tty();
1570 }
1571 elapsedTimer time;
1572
1573 CompilerThread* thread = CompilerThread::current();
1574 ResourceMark rm(thread);
1575
1576 if (LogEvents) {
1577 _compilation_log->log_compile(thread, task);
1578 }
1579
1580 // Common flags.
1581 uint compile_id = task->compile_id();
1582 int osr_bci = task->osr_bci();
1583 bool is_osr = (osr_bci != standard_entry_bci);
1584 bool should_log = (thread->log() != NULL);
1585 bool should_break = false;
1586 int task_level = task->comp_level();
1587 {
1588 // create the handle inside it's own block so it can't
1589 // accidentally be referenced once the thread transitions to
1590 // native. The NoHandleMark before the transition should catch
1591 // any cases where this occurs in the future.
1592 methodHandle method(thread, task->method());
1593 should_break = check_break_at(method, compile_id, is_osr);
1594 if (should_log && !CompilerOracle::should_log(method)) {
1595 should_log = false;
1596 }
1597 assert(!method->is_native(), "no longer compile natives");
1598
1599 // Save information about this method in case of failure.
1600 set_last_compile(thread, method, is_osr, task_level);
1601
1602 DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
1603 }
1604
1605 // Allocate a new set of JNI handles.
1606 push_jni_handle_block();
1607 Method* target_handle = task->method();
1608 int compilable = ciEnv::MethodCompilable;
1609 {
1610 int system_dictionary_modification_counter;
1611 {
1612 MutexLocker locker(Compile_lock, thread);
1613 system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1614 }
1615
1616 NoHandleMark nhm;
1617 ThreadToNativeFromVM ttn(thread);
1618
1619 ciEnv ci_env(task, system_dictionary_modification_counter);
1620 if (should_break) {
1621 ci_env.set_break_at_compile(true);
1622 }
1623 if (should_log) {
1624 ci_env.set_log(thread->log());
1625 }
1626 assert(thread->env() == &ci_env, "set by ci_env");
1627 // The thread-env() field is cleared in ~CompileTaskWrapper.
1628
1629 // Cache Jvmti state
1630 ci_env.cache_jvmti_state();
1631
1632 // Cache DTrace flags
1633 ci_env.cache_dtrace_flags();
1634
1635 ciMethod* target = ci_env.get_method_from_handle(target_handle);
1636
1637 TraceTime t1("compilation", &time);
1638 EventCompilation event;
1639
1640 AbstractCompiler *comp = compiler(task_level);
1641 if (comp == NULL) {
1642 ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
1864 compile_handles->set_pop_frame_link(java_handles); // make sure java handles get gc'd.
1865 thread->set_active_handles(compile_handles);
1866 }
1867
1868
1869 // ------------------------------------------------------------------
1870 // CompileBroker::pop_jni_handle_block
1871 //
1872 // Pop off the current block of JNI handles.
1873 void CompileBroker::pop_jni_handle_block() {
1874 JavaThread* thread = JavaThread::current();
1875
1876 // Release our JNI handle block
1877 JNIHandleBlock* compile_handles = thread->active_handles();
1878 JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
1879 thread->set_active_handles(java_handles);
1880 compile_handles->set_pop_frame_link(NULL);
1881 JNIHandleBlock::release_block(compile_handles, thread); // may block
1882 }
1883
1884
1885 // ------------------------------------------------------------------
1886 // CompileBroker::check_break_at
1887 //
1888 // Should the compilation break at the current compilation.
1889 bool CompileBroker::check_break_at(methodHandle method, int compile_id, bool is_osr) {
1890 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) {
1891 return true;
1892 } else if( CompilerOracle::should_break_at(method) ) { // break when compiling
1893 return true;
1894 } else {
1895 return (compile_id == CIBreakAt);
1896 }
1897 }
1898
1899 // ------------------------------------------------------------------
1900 // CompileBroker::collect_statistics
1901 //
1902 // Collect statistics about the compilation.
1903
1904 void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
1905 bool success = task->is_success();
1906 methodHandle method (thread, task->method());
1907 uint compile_id = task->compile_id();
1908 bool is_osr = (task->osr_bci() != standard_entry_bci);
1909 nmethod* code = task->code();
1910 CompilerCounters* counters = thread->counters();
1911
1912 assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker");
1913 MutexLocker locker(CompileStatistics_lock);
1914
1915 // _perf variables are production performance counters which are
1916 // updated regardless of the setting of the CITime and CITimeEach flags
1917 //
1918
2058 compiler(_last_compile_level) != NULL &&
2059 _last_method_compiled != NULL &&
2060 _last_compile_type != no_compile) {
2061 if (_last_compile_type == osr_compile) {
2062 tty->print_cr("Last parse: [osr]%d+++(%d) %s",
2063 _osr_compilation_id, _last_compile_level, _last_method_compiled);
2064 } else {
2065 tty->print_cr("Last parse: %d+++(%d) %s",
2066 _compilation_id, _last_compile_level, _last_method_compiled);
2067 }
2068 }
2069 }
2070
2071
2072 void CompileBroker::print_compiler_threads_on(outputStream* st) {
2073 #ifndef PRODUCT
2074 st->print_cr("Compiler thread printing unimplemented.");
2075 st->cr();
2076 #endif
2077 }
|
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "code/codeCache.hpp"
29 #include "compiler/compileBroker.hpp"
30 #include "compiler/compileLog.hpp"
31 #include "compiler/compilerOracle.hpp"
32 #include "compiler/directivesparser.hpp"
33 #include "interpreter/linkResolver.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "oops/methodData.hpp"
36 #include "oops/method.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "prims/nativeLookup.hpp"
39 #include "prims/whitebox.hpp"
40 #include "runtime/arguments.hpp"
41 #include "runtime/atomic.inline.hpp"
42 #include "runtime/compilationPolicy.hpp"
43 #include "runtime/init.hpp"
44 #include "runtime/interfaceSupport.hpp"
45 #include "runtime/javaCalls.hpp"
46 #include "runtime/os.hpp"
47 #include "runtime/sharedRuntime.hpp"
48 #include "runtime/sweeper.hpp"
49 #include "trace/tracing.hpp"
50 #include "utilities/dtrace.hpp"
51 #include "utilities/events.hpp"
52 #ifdef COMPILER1
84 (char *) comp_name, strlen(comp_name), \
85 (char *) klass_name->bytes(), klass_name->utf8_length(), \
86 (char *) name->bytes(), name->utf8_length(), \
87 (char *) signature->bytes(), signature->utf8_length(), (success)); \
88 }
89
90 #else // ndef DTRACE_ENABLED
91
92 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
93 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
94
95 #endif // ndef DTRACE_ENABLED
96
97 bool CompileBroker::_initialized = false;
98 volatile bool CompileBroker::_should_block = false;
99 volatile jint CompileBroker::_print_compilation_warning = 0;
100 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
101
102 // The installed compiler(s)
103 AbstractCompiler* CompileBroker::_compilers[2];
104 DirectivesStack* CompileBroker::_dirstack;
105
106 // These counters are used to assign an unique ID to each compilation.
107 volatile jint CompileBroker::_compilation_id = 0;
108 volatile jint CompileBroker::_osr_compilation_id = 0;
109
110 // Debugging information
111 int CompileBroker::_last_compile_type = no_compile;
112 int CompileBroker::_last_compile_level = CompLevel_none;
113 char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
114
115 // Performance counters
116 PerfCounter* CompileBroker::_perf_total_compilation = NULL;
117 PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
118 PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
119
120 PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
121 PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
122 PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
123 PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
124 PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
182 StringLogMessage lm;
183 lm.print("%4d COMPILE SKIPPED: %s", task->compile_id(), reason);
184 if (retry_message != NULL) {
185 lm.append(" (%s)", retry_message);
186 }
187 lm.print("\n");
188 log(thread, "%s", (const char*)lm);
189 }
190
191 void log_metaspace_failure(const char* reason) {
192 ResourceMark rm;
193 StringLogMessage lm;
194 lm.print("%4d COMPILE PROFILING SKIPPED: %s", -1, reason);
195 lm.print("\n");
196 log(JavaThread::current(), "%s", (const char*)lm);
197 }
198 };
199
200 static CompilationLog* _compilation_log = NULL;
201
202 bool compileBroker_init() {
203 if (LogEvents) {
204 _compilation_log = new CompilationLog();
205 }
206
207 // init directives stack and add default directives
208 CompileBroker::set_dirstack(new DirectivesStack());
209
210 /*CompilerDirectives* _default_directives = new CompilerDirectives();
211 char str[] = "*.*";
212 const char* error_msg = NULL;
213 _default_directives->add_match(str, error_msg);
214 assert(error_msg == NULL, "Must succeed.");
215 CompileBroker::dirstack()->push(_default_directives);*/
216
217 if (DirectivesParser::has_file()) {
218 DirectivesParser::parse_from_flag();
219 }
220 return true;
221 }
222
223 CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
224 CompilerThread* thread = CompilerThread::current();
225 thread->set_task(task);
226 CompileLog* log = thread->log();
227 if (log != NULL) task->log_task_start(log);
228 }
229
230 CompileTaskWrapper::~CompileTaskWrapper() {
231 CompilerThread* thread = CompilerThread::current();
232 CompileTask* task = thread->task();
233 CompileLog* log = thread->log();
234 if (log != NULL) task->log_task_done(log);
235 thread->set_task(NULL);
236 task->set_code_handle(NULL);
237 thread->set_env(NULL);
238 if (task->is_blocking()) {
239 MutexLocker notifier(task->lock(), thread);
240 task->mark_complete();
1143 //
1144 // See if this compilation is not allowed.
1145 bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) {
1146 bool is_native = method->is_native();
1147 // Some compilers may not support the compilation of natives.
1148 AbstractCompiler *comp = compiler(comp_level);
1149 if (is_native &&
1150 (!CICompileNatives || comp == NULL || !comp->supports_native())) {
1151 method->set_not_compilable_quietly(comp_level);
1152 return true;
1153 }
1154
1155 bool is_osr = (osr_bci != standard_entry_bci);
1156 // Some compilers may not support on stack replacement.
1157 if (is_osr &&
1158 (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
1159 method->set_not_osr_compilable(comp_level);
1160 return true;
1161 }
1162
1163 // Breaking the abstraction - directives are only used inside a compilation otherwise.
1164 DirectiveSet* dirset = dirstack()->getMatchingDirective(method, comp_level);
1165 bool excluded = !dirset->EnabledOption;
1166 {
1167 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1168 dirset->directive()->dec_refcount();
1169 }
1170
1171 // The method may be explicitly excluded by the user.
1172 bool quietly;
1173 double scale;
1174 if (excluded || (CompilerOracle::has_option_value(method, "CompileThresholdScaling", scale) && scale == 0)) {
1175 bool quietly = CompilerOracle::should_exclude_quietly();
1176 if (!quietly) {
1177 // This does not happen quietly...
1178 ResourceMark rm;
1179 tty->print("### Excluding %s:%s",
1180 method->is_native() ? "generation of native wrapper" : "compile",
1181 (method->is_static() ? " static" : ""));
1182 method->print_short_name(tty);
1183 tty->cr();
1184 }
1185 method->set_not_compilable(CompLevel_all, !quietly, "excluded by CompileCommand");
1186 }
1187
1188 return false;
1189 }
1190
1191 /**
1192 * Generate serialized IDs for compilation requests. If certain debugging flags are used
1193 * and the ID is not within the specified range, the method is not compiled and 0 is returned.
1194 * The function also allows to generate separate compilation IDs for OSR compilations.
1195 */
1307 }
1308
1309 /**
1310 * Initialize compiler thread(s) + compiler object(s). The postcondition
1311 * of this function is that the compiler runtimes are initialized and that
1312 * compiler threads can start compiling.
1313 */
1314 bool CompileBroker::init_compiler_runtime() {
1315 CompilerThread* thread = CompilerThread::current();
1316 AbstractCompiler* comp = thread->compiler();
1317 // Final sanity check - the compiler object must exist
1318 guarantee(comp != NULL, "Compiler object must exist");
1319
1320 int system_dictionary_modification_counter;
1321 {
1322 MutexLocker locker(Compile_lock, thread);
1323 system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1324 }
1325
1326 {
1327 DirectiveSet* dirset = _dirstack->peak(comp);
1328 assert(dirset->directive()->is_default_directive(), "Consistency");
1329
1330 // Must switch to native to allocate ci_env
1331 ThreadToNativeFromVM ttn(thread);
1332 ciEnv ci_env(NULL, dirset, system_dictionary_modification_counter);
1333 // Cache Jvmti state
1334 ci_env.cache_jvmti_state();
1335 // Cache DTrace flags
1336 ci_env.cache_dtrace_flags();
1337
1338 // Switch back to VM state to do compiler initialization
1339 ThreadInVMfromNative tv(thread);
1340 ResetNoHandleMark rnhm;
1341
1342 if (!comp->is_shark()) {
1343 // Perform per-thread and global initializations
1344 comp->initialize();
1345 }
1346 }
1347
1348 if (comp->is_failed()) {
1349 disable_compilation_forever();
1350 // If compiler initialization failed, no compiler thread that is specific to a
1351 // particular compiler runtime will ever start to compile methods.
1352 shutdown_compiler_runtime(comp, thread);
1353 return false;
1354 }
1355
1356 // C1 specific check
1357 if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) {
1358 warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
1359 return false;
1360 }
1361
1568 tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
1569 #endif
1570 ThreadInVMfromNative tivfn(JavaThread::current());
1571 }
1572 }
1573
1574 // wrapper for CodeCache::print_summary()
1575 static void codecache_print(bool detailed)
1576 {
1577 ResourceMark rm;
1578 stringStream s;
1579 // Dump code cache into a buffer before locking the tty,
1580 {
1581 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1582 CodeCache::print_summary(&s, detailed);
1583 }
1584 ttyLocker ttyl;
1585 tty->print("%s", s.as_string());
1586 }
1587
1588 void CompileBroker::print_directives(outputStream* st) {
1589 dirstack()->print(st);
1590 }
1591
1592 DirectivesStack* CompileBroker::dirstack() {
1593 return _dirstack;
1594 }
1595
1596 void CompileBroker::set_dirstack(DirectivesStack* stack) {
1597 _dirstack = stack;
1598 }
1599
1600 // Create a new dirstack and push a default directive
1601 DirectivesStack::DirectivesStack() : _depth(0), _top(NULL), _bottom(NULL)
1602 {
1603 CompilerDirectives* _default_directives = new CompilerDirectives();
1604 char str[] = "*.*";
1605 const char* error_msg = NULL;
1606 _default_directives->add_match(str, error_msg);
1607 assert(error_msg == NULL, "Must succeed.");
1608 push(_default_directives);
1609 }
1610
1611 DirectiveSet* DirectivesStack::peak(AbstractCompiler* comp) {
1612 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1613
1614 assert(_top != NULL, "Must never be empty");
1615 if (comp->is_c1()) {
1616 return _top->get_for(CompilerDirectives::TargetC1);
1617 } else {
1618 assert(comp->is_c2(), "Must be c2");
1619 return _top->get_for(CompilerDirectives::TargetC2);
1620 }
1621 // CMH handle shark and others.
1622 }
1623
1624 void DirectivesStack::push(CompilerDirectives* directive) {
1625 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1626
1627 directive->inc_refcount();
1628 if (_top == NULL) {
1629 assert(_bottom == NULL, "There can only be one default directive");
1630 _bottom = directive; // default directive, can never be removed.
1631 }
1632
1633 directive->set_next(_top);
1634 _top = directive;
1635 _depth++;
1636
1637 if (PrintCompilerDirectives) {
1638 directive->print(tty);
1639 }
1640 }
1641
1642 void DirectivesStack::pop() {
1643 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1644 pop_inner();
1645 }
1646
1647 void DirectivesStack::pop_inner() {
1648 assert(DirectivesStack_lock->owned_by_self(), "");
1649
1650 if (_top->next() == NULL) {
1651 return; // Do nothing - dont allow an empty stack
1652 }
1653 CompilerDirectives* tmp = _top;
1654 _top = _top->next();
1655 _depth--;
1656 tmp->dec_refcount();
1657 return;
1658 }
1659
1660 void DirectivesStack::clear() {
1661 // holding the lock during the whole operation ensuring consistent result
1662 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1663 while (_top->next() != NULL) {
1664 DirectivesStack::pop_inner();
1665 }
1666 }
1667
1668 void DirectivesStack::print(outputStream* st) {
1669 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1670 CompilerDirectives* tmp = _top;
1671 while (tmp != NULL) {
1672 tmp->print(st);
1673 tmp = tmp->next();
1674 }
1675 }
1676
1677 DirectiveSet* DirectivesStack::getMatchingDirective(methodHandle mh, int comp_level) {
1678 assert(_depth > 0, "Must never be empty");
1679 CompilerDirectives* dir = _top;
1680 assert(dir != NULL, "Must be initialized");
1681
1682 DirectiveSet* match = NULL;
1683 {
1684 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1685 while (dir != NULL) {
1686 if (dir->is_default_directive() || dir->match(mh, comp_level)) {
1687 match = dir->get_for(comp_level);
1688 break;
1689 }
1690 dir = dir->next();
1691 }
1692 }
1693 guarantee(match != NULL, "There should always be a default directive that matches");
1694
1695 // Check for legacy compile commands update, without DirectivesStack_lock
1696 return match->late_cc_init(mh);
1697 }
1698
1699 // ------------------------------------------------------------------
1700 // CompileBroker::invoke_compiler_on_method
1701 //
1702 // Compile a method.
1703 //
1704 void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
1705 if (PrintCompilation) {
1706 ResourceMark rm;
1707 task->print_tty();
1708 }
1709 elapsedTimer time;
1710
1711 CompilerThread* thread = CompilerThread::current();
1712 ResourceMark rm(thread);
1713
1714 if (LogEvents) {
1715 _compilation_log->log_compile(thread, task);
1716 }
1717
1718 // Common flags.
1719 uint compile_id = task->compile_id();
1720 int osr_bci = task->osr_bci();
1721 bool is_osr = (osr_bci != standard_entry_bci);
1722 bool should_log = (thread->log() != NULL);
1723 bool should_break = false;
1724 int task_level = task->comp_level();
1725
1726 // Look up matching directives
1727 DirectiveSet* dir = CompileBroker::_dirstack->getMatchingDirective(task->method(), task_level);
1728
1729 should_break = dir->BreakAtExecuteOption || task->check_break_at_flags();
1730 if (should_log && !dir->LogOption) {
1731 dir->set_Log((void*)false);
1732 }
1733 {
1734 // create the handle inside it's own block so it can't
1735 // accidentally be referenced once the thread transitions to
1736 // native. The NoHandleMark before the transition should catch
1737 // any cases where this occurs in the future.
1738 methodHandle method(thread, task->method());
1739 assert(!method->is_native(), "no longer compile natives");
1740
1741 // Save information about this method in case of failure.
1742 set_last_compile(thread, method, is_osr, task_level);
1743
1744 DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
1745 }
1746
1747 // Allocate a new set of JNI handles.
1748 push_jni_handle_block();
1749 Method* target_handle = task->method();
1750 int compilable = ciEnv::MethodCompilable;
1751 {
1752 int system_dictionary_modification_counter;
1753 {
1754 MutexLocker locker(Compile_lock, thread);
1755 system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1756 }
1757
1758 NoHandleMark nhm;
1759 ThreadToNativeFromVM ttn(thread);
1760
1761 ciEnv ci_env(task, dir, system_dictionary_modification_counter);
1762 if (should_log) {
1763 ci_env.set_log(thread->log());
1764 }
1765 assert(thread->env() == &ci_env, "set by ci_env");
1766 // The thread-env() field is cleared in ~CompileTaskWrapper.
1767
1768 // Cache Jvmti state
1769 ci_env.cache_jvmti_state();
1770
1771 // Cache DTrace flags
1772 ci_env.cache_dtrace_flags();
1773
1774 ciMethod* target = ci_env.get_method_from_handle(target_handle);
1775
1776 TraceTime t1("compilation", &time);
1777 EventCompilation event;
1778
1779 AbstractCompiler *comp = compiler(task_level);
1780 if (comp == NULL) {
1781 ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
2003 compile_handles->set_pop_frame_link(java_handles); // make sure java handles get gc'd.
2004 thread->set_active_handles(compile_handles);
2005 }
2006
2007
2008 // ------------------------------------------------------------------
2009 // CompileBroker::pop_jni_handle_block
2010 //
2011 // Pop off the current block of JNI handles.
2012 void CompileBroker::pop_jni_handle_block() {
2013 JavaThread* thread = JavaThread::current();
2014
2015 // Release our JNI handle block
2016 JNIHandleBlock* compile_handles = thread->active_handles();
2017 JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
2018 thread->set_active_handles(java_handles);
2019 compile_handles->set_pop_frame_link(NULL);
2020 JNIHandleBlock::release_block(compile_handles, thread); // may block
2021 }
2022
2023 // ------------------------------------------------------------------
2024 // CompileBroker::collect_statistics
2025 //
2026 // Collect statistics about the compilation.
2027
2028 void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
2029 bool success = task->is_success();
2030 methodHandle method (thread, task->method());
2031 uint compile_id = task->compile_id();
2032 bool is_osr = (task->osr_bci() != standard_entry_bci);
2033 nmethod* code = task->code();
2034 CompilerCounters* counters = thread->counters();
2035
2036 assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker");
2037 MutexLocker locker(CompileStatistics_lock);
2038
2039 // _perf variables are production performance counters which are
2040 // updated regardless of the setting of the CITime and CITimeEach flags
2041 //
2042
2182 compiler(_last_compile_level) != NULL &&
2183 _last_method_compiled != NULL &&
2184 _last_compile_type != no_compile) {
2185 if (_last_compile_type == osr_compile) {
2186 tty->print_cr("Last parse: [osr]%d+++(%d) %s",
2187 _osr_compilation_id, _last_compile_level, _last_method_compiled);
2188 } else {
2189 tty->print_cr("Last parse: %d+++(%d) %s",
2190 _compilation_id, _last_compile_level, _last_method_compiled);
2191 }
2192 }
2193 }
2194
2195
2196 void CompileBroker::print_compiler_threads_on(outputStream* st) {
2197 #ifndef PRODUCT
2198 st->print_cr("Compiler thread printing unimplemented.");
2199 st->cr();
2200 #endif
2201 }
2202
|