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
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 */
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);
1643 } else {
1644 if (WhiteBoxAPI && WhiteBox::compilation_locked) {
1645 MonitorLockerEx locker(Compilation_lock, Mutex::_no_safepoint_check_flag);
1646 while (WhiteBox::compilation_locked) {
1647 locker.wait(Mutex::_no_safepoint_check_flag);
1648 }
1649 }
1650 comp->compile_method(&ci_env, target, osr_bci);
1651 }
1652
1653 if (!ci_env.failing() && task->code() == NULL) {
1654 //assert(false, "compiler should always document failure");
1655 // The compiler elected, without comment, not to register a result.
1656 // Do not attempt further compilations of this method.
1657 ci_env.record_method_not_compilable("compile failed", !TieredCompilation);
1658 }
1659
1660 // Copy this bit to the enclosing block:
1661 compilable = ci_env.compilable();
1662
1663 if (ci_env.failing()) {
1664 task->set_failure_reason(ci_env.failure_reason());
1665 ci_env.report_failure(ci_env.failure_reason());
1666 const char* retry_message = ci_env.retry_message();
1667 if (_compilation_log != NULL) {
1668 _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message);
1669 }
1670 if (PrintCompilation) {
1679 if (_compilation_log != NULL) {
1680 nmethod* code = task->code();
1681 if (code != NULL) {
1682 _compilation_log->log_nmethod(thread, code);
1683 }
1684 }
1685 }
1686 // simulate crash during compilation
1687 assert(task->compile_id() != CICrashAt, "just as planned");
1688 if (event.should_commit()) {
1689 event.set_method(target->get_Method());
1690 event.set_compileID(compile_id);
1691 event.set_compileLevel(task->comp_level());
1692 event.set_succeded(task->is_success());
1693 event.set_isOsr(is_osr);
1694 event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size());
1695 event.set_inlinedBytes(task->num_inlined_bytecodes());
1696 event.commit();
1697 }
1698 }
1699 pop_jni_handle_block();
1700
1701 methodHandle method(thread, task->method());
1702
1703 DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
1704
1705 collect_statistics(thread, time, task);
1706
1707 if (PrintCompilation && PrintCompilation2) {
1708 tty->print("%7d ", (int) tty->time_stamp().milliseconds()); // print timestamp
1709 tty->print("%4d ", compile_id); // print compilation number
1710 tty->print("%s ", (is_osr ? "%" : " "));
1711 if (task->code() != NULL) {
1712 tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size());
1713 }
1714 tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
1715 }
1716
1717 if (PrintCodeCacheOnCompilation)
1718 codecache_print(/* detailed= */ false);
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
181 StringLogMessage lm;
182 lm.print("%4d COMPILE SKIPPED: %s", task->compile_id(), reason);
183 if (retry_message != NULL) {
184 lm.append(" (%s)", retry_message);
185 }
186 lm.print("\n");
187 log(thread, "%s", (const char*)lm);
188 }
189
190 void log_metaspace_failure(const char* reason) {
191 ResourceMark rm;
192 StringLogMessage lm;
193 lm.print("%4d COMPILE PROFILING SKIPPED: %s", -1, reason);
194 lm.print("\n");
195 log(JavaThread::current(), "%s", (const char*)lm);
196 }
197 };
198
199 static CompilationLog* _compilation_log = NULL;
200
201 bool compileBroker_init() {
202 if (LogEvents) {
203 _compilation_log = new CompilationLog();
204 }
205
206 // init directives stack, adding default directive
207 DirectivesStack::init();
208
209 if (DirectivesParser::has_file()) {
210 return DirectivesParser::parse_from_flag();
211 }
212 return true;
213 }
214
215 CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
216 CompilerThread* thread = CompilerThread::current();
217 thread->set_task(task);
218 CompileLog* log = thread->log();
219 if (log != NULL) task->log_task_start(log);
220 }
221
222 CompileTaskWrapper::~CompileTaskWrapper() {
223 CompilerThread* thread = CompilerThread::current();
224 CompileTask* task = thread->task();
225 CompileLog* log = thread->log();
226 if (log != NULL) task->log_task_done(log);
227 thread->set_task(NULL);
228 task->set_code_handle(NULL);
229 thread->set_env(NULL);
230 if (task->is_blocking()) {
231 MutexLocker notifier(task->lock(), thread);
232 task->mark_complete();
1135 //
1136 // See if this compilation is not allowed.
1137 bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) {
1138 bool is_native = method->is_native();
1139 // Some compilers may not support the compilation of natives.
1140 AbstractCompiler *comp = compiler(comp_level);
1141 if (is_native &&
1142 (!CICompileNatives || comp == NULL || !comp->supports_native())) {
1143 method->set_not_compilable_quietly(comp_level);
1144 return true;
1145 }
1146
1147 bool is_osr = (osr_bci != standard_entry_bci);
1148 // Some compilers may not support on stack replacement.
1149 if (is_osr &&
1150 (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
1151 method->set_not_osr_compilable(comp_level);
1152 return true;
1153 }
1154
1155 // Breaking the abstraction - directives are only used inside a compilation otherwise.
1156 DirectiveSet* dirset = DirectivesStack::getMatchingDirective(method, comp);
1157 bool excluded = !dirset->EnabledOption;
1158 DirectivesStack::release(dirset);
1159
1160 // The method may be explicitly excluded by the user.
1161 bool quietly;
1162 double scale;
1163 if (excluded || (CompilerOracle::has_option_value(method, "CompileThresholdScaling", scale) && scale == 0)) {
1164 bool quietly = CompilerOracle::should_exclude_quietly();
1165 if (!quietly) {
1166 // This does not happen quietly...
1167 ResourceMark rm;
1168 tty->print("### Excluding %s:%s",
1169 method->is_native() ? "generation of native wrapper" : "compile",
1170 (method->is_static() ? " static" : ""));
1171 method->print_short_name(tty);
1172 tty->cr();
1173 }
1174 method->set_not_compilable(CompLevel_all, !quietly, "excluded by CompileCommand");
1175 }
1176
1177 return false;
1178 }
1179
1180 /**
1181 * Generate serialized IDs for compilation requests. If certain debugging flags are used
1182 * and the ID is not within the specified range, the method is not compiled and 0 is returned.
1183 * The function also allows to generate separate compilation IDs for OSR compilations.
1184 */
1308
1309 int system_dictionary_modification_counter;
1310 {
1311 MutexLocker locker(Compile_lock, thread);
1312 system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1313 }
1314
1315 {
1316 // Must switch to native to allocate ci_env
1317 ThreadToNativeFromVM ttn(thread);
1318 ciEnv ci_env(NULL, system_dictionary_modification_counter);
1319 // Cache Jvmti state
1320 ci_env.cache_jvmti_state();
1321 // Cache DTrace flags
1322 ci_env.cache_dtrace_flags();
1323
1324 // Switch back to VM state to do compiler initialization
1325 ThreadInVMfromNative tv(thread);
1326 ResetNoHandleMark rnhm;
1327
1328 if (!comp->is_shark()) {
1329 // Perform per-thread and global initializations
1330 comp->initialize();
1331 }
1332 }
1333
1334 if (comp->is_failed()) {
1335 disable_compilation_forever();
1336 // If compiler initialization failed, no compiler thread that is specific to a
1337 // particular compiler runtime will ever start to compile methods.
1338 shutdown_compiler_runtime(comp, thread);
1339 return false;
1340 }
1341
1342 // C1 specific check
1343 if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) {
1344 warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
1345 return false;
1346 }
1347
1554 tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
1555 #endif
1556 ThreadInVMfromNative tivfn(JavaThread::current());
1557 }
1558 }
1559
1560 // wrapper for CodeCache::print_summary()
1561 static void codecache_print(bool detailed)
1562 {
1563 ResourceMark rm;
1564 stringStream s;
1565 // Dump code cache into a buffer before locking the tty,
1566 {
1567 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1568 CodeCache::print_summary(&s, detailed);
1569 }
1570 ttyLocker ttyl;
1571 tty->print("%s", s.as_string());
1572 }
1573
1574 // Create a new dirstack and push a default directive
1575
1576 int DirectivesStack::_depth = 0;
1577 CompilerDirectives* DirectivesStack::_top = NULL;
1578 CompilerDirectives* DirectivesStack::_bottom = NULL;
1579
1580 void DirectivesStack::init() {
1581 CompilerDirectives* _default_directives = new CompilerDirectives();
1582 char str[] = "*.*";
1583 const char* error_msg = NULL;
1584 _default_directives->add_match(str, error_msg);
1585 assert(error_msg == NULL, "Must succeed.");
1586 push(_default_directives);
1587 }
1588
1589 DirectiveSet* DirectivesStack::getDefaultDirective(AbstractCompiler* comp) {
1590 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1591
1592 assert(_bottom != NULL, "Must never be empty");
1593 return _bottom->get_for(comp);
1594 }
1595
1596 void DirectivesStack::push(CompilerDirectives* directive) {
1597 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1598
1599 directive->inc_refcount();
1600 if (_top == NULL) {
1601 assert(_bottom == NULL, "There can only be one default directive");
1602 _bottom = directive; // default directive, can never be removed.
1603 }
1604
1605 directive->set_next(_top);
1606 _top = directive;
1607 _depth++;
1608 }
1609
1610 void DirectivesStack::pop() {
1611 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1612 pop_inner();
1613 }
1614
1615 void DirectivesStack::pop_inner() {
1616 assert(DirectivesStack_lock->owned_by_self(), "");
1617
1618 if (_top->next() == NULL) {
1619 return; // Do nothing - don't allow an empty stack
1620 }
1621 CompilerDirectives* tmp = _top;
1622 _top = _top->next();
1623 _depth--;
1624
1625 DirectivesStack::release(tmp);
1626 }
1627
1628 void DirectivesStack::clear() {
1629 // holding the lock during the whole operation ensuring consistent result
1630 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1631 while (_top->next() != NULL) {
1632 pop_inner();
1633 }
1634 }
1635
1636 void DirectivesStack::print(outputStream* st) {
1637 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1638 CompilerDirectives* tmp = _top;
1639 while (tmp != NULL) {
1640 tmp->print(st);
1641 tmp = tmp->next();
1642 }
1643 }
1644
1645 void DirectivesStack::release(DirectiveSet* set) {
1646 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1647 if (set->is_exclusive_copy()) {
1648 // Old CompilecCmmands forced us to create an exclusive copy
1649 delete set;
1650 } else {
1651 assert(set->directive() != NULL, "");
1652 release(set->directive());
1653 }
1654 }
1655
1656
1657 void DirectivesStack::release(CompilerDirectives* dir) {
1658 assert(DirectivesStack_lock->owned_by_self(), "");
1659 dir->dec_refcount();
1660 if (dir->refcount() == 0) {
1661 delete dir;
1662 }
1663 }
1664
1665 DirectiveSet* DirectivesStack::getMatchingDirective(methodHandle mh, AbstractCompiler *comp) {
1666 assert(_depth > 0, "Must never be empty");
1667 CompilerDirectives* dir = _top;
1668 assert(dir != NULL, "Must be initialized");
1669
1670 DirectiveSet* match = NULL;
1671 {
1672 MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
1673 while (dir != NULL) {
1674 if (dir->is_default_directive() || dir->match(mh)) {
1675 match = dir->get_for(comp);
1676 break;
1677 }
1678 dir = dir->next();
1679 }
1680 }
1681 guarantee(match != NULL, "There should always be a default directive that matches");
1682
1683 // Check for legacy compile commands update, without DirectivesStack_lock
1684 return match->compilecommand_compatibility_init(mh);
1685 }
1686
1687 // ------------------------------------------------------------------
1688 // CompileBroker::invoke_compiler_on_method
1689 //
1690 // Compile a method.
1691 //
1692 void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
1693 if (PrintCompilation) {
1694 ResourceMark rm;
1695 task->print_tty();
1696 }
1697 elapsedTimer time;
1698
1699 CompilerThread* thread = CompilerThread::current();
1700 ResourceMark rm(thread);
1701
1702 if (LogEvents) {
1703 _compilation_log->log_compile(thread, task);
1704 }
1705
1706 // Common flags.
1707 uint compile_id = task->compile_id();
1708 int osr_bci = task->osr_bci();
1709 bool is_osr = (osr_bci != standard_entry_bci);
1710 bool should_log = (thread->log() != NULL);
1711 bool should_break = false;
1712 int task_level = task->comp_level();
1713
1714 // Look up matching directives
1715 DirectiveSet* dirset = DirectivesStack::getMatchingDirective(task->method(), compiler(task_level));
1716
1717 should_break = dirset->BreakAtExecuteOption || task->check_break_at_flags();
1718 if (should_log && !dirset->LogOption) {
1719 int val = 0;
1720 dirset->set_Log((void*)&val);
1721 }
1722 {
1723 // create the handle inside it's own block so it can't
1724 // accidentally be referenced once the thread transitions to
1725 // native. The NoHandleMark before the transition should catch
1726 // any cases where this occurs in the future.
1727 methodHandle method(thread, task->method());
1728 assert(!method->is_native(), "no longer compile natives");
1729
1730 // Save information about this method in case of failure.
1731 set_last_compile(thread, method, is_osr, task_level);
1732
1733 DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
1734 }
1735
1736 // Allocate a new set of JNI handles.
1737 push_jni_handle_block();
1738 Method* target_handle = task->method();
1739 int compilable = ciEnv::MethodCompilable;
1740 {
1741 int system_dictionary_modification_counter;
1742 {
1743 MutexLocker locker(Compile_lock, thread);
1744 system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1745 }
1746
1747 NoHandleMark nhm;
1748 ThreadToNativeFromVM ttn(thread);
1749
1750 ciEnv ci_env(task, system_dictionary_modification_counter);
1751 if (should_log) {
1752 ci_env.set_log(thread->log());
1753 }
1754 assert(thread->env() == &ci_env, "set by ci_env");
1755 // The thread-env() field is cleared in ~CompileTaskWrapper.
1756
1757 // Cache Jvmti state
1758 ci_env.cache_jvmti_state();
1759
1760 // Cache DTrace flags
1761 ci_env.cache_dtrace_flags();
1762
1763 ciMethod* target = ci_env.get_method_from_handle(target_handle);
1764
1765 TraceTime t1("compilation", &time);
1766 EventCompilation event;
1767
1768 AbstractCompiler *comp = compiler(task_level);
1769 if (comp == NULL) {
1770 ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
1771 } else {
1772 if (WhiteBoxAPI && WhiteBox::compilation_locked) {
1773 MonitorLockerEx locker(Compilation_lock, Mutex::_no_safepoint_check_flag);
1774 while (WhiteBox::compilation_locked) {
1775 locker.wait(Mutex::_no_safepoint_check_flag);
1776 }
1777 }
1778 comp->compile_method(&ci_env, target, osr_bci, dirset);
1779 }
1780
1781 if (!ci_env.failing() && task->code() == NULL) {
1782 //assert(false, "compiler should always document failure");
1783 // The compiler elected, without comment, not to register a result.
1784 // Do not attempt further compilations of this method.
1785 ci_env.record_method_not_compilable("compile failed", !TieredCompilation);
1786 }
1787
1788 // Copy this bit to the enclosing block:
1789 compilable = ci_env.compilable();
1790
1791 if (ci_env.failing()) {
1792 task->set_failure_reason(ci_env.failure_reason());
1793 ci_env.report_failure(ci_env.failure_reason());
1794 const char* retry_message = ci_env.retry_message();
1795 if (_compilation_log != NULL) {
1796 _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message);
1797 }
1798 if (PrintCompilation) {
1807 if (_compilation_log != NULL) {
1808 nmethod* code = task->code();
1809 if (code != NULL) {
1810 _compilation_log->log_nmethod(thread, code);
1811 }
1812 }
1813 }
1814 // simulate crash during compilation
1815 assert(task->compile_id() != CICrashAt, "just as planned");
1816 if (event.should_commit()) {
1817 event.set_method(target->get_Method());
1818 event.set_compileID(compile_id);
1819 event.set_compileLevel(task->comp_level());
1820 event.set_succeded(task->is_success());
1821 event.set_isOsr(is_osr);
1822 event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size());
1823 event.set_inlinedBytes(task->num_inlined_bytecodes());
1824 event.commit();
1825 }
1826 }
1827 DirectivesStack::release(dirset);
1828 pop_jni_handle_block();
1829
1830 methodHandle method(thread, task->method());
1831
1832 DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
1833
1834 collect_statistics(thread, time, task);
1835
1836 if (PrintCompilation && PrintCompilation2) {
1837 tty->print("%7d ", (int) tty->time_stamp().milliseconds()); // print timestamp
1838 tty->print("%4d ", compile_id); // print compilation number
1839 tty->print("%s ", (is_osr ? "%" : " "));
1840 if (task->code() != NULL) {
1841 tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size());
1842 }
1843 tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
1844 }
1845
1846 if (PrintCodeCacheOnCompilation)
1847 codecache_print(/* detailed= */ false);
1993 compile_handles->set_pop_frame_link(java_handles); // make sure java handles get gc'd.
1994 thread->set_active_handles(compile_handles);
1995 }
1996
1997
1998 // ------------------------------------------------------------------
1999 // CompileBroker::pop_jni_handle_block
2000 //
2001 // Pop off the current block of JNI handles.
2002 void CompileBroker::pop_jni_handle_block() {
2003 JavaThread* thread = JavaThread::current();
2004
2005 // Release our JNI handle block
2006 JNIHandleBlock* compile_handles = thread->active_handles();
2007 JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
2008 thread->set_active_handles(java_handles);
2009 compile_handles->set_pop_frame_link(NULL);
2010 JNIHandleBlock::release_block(compile_handles, thread); // may block
2011 }
2012
2013 // ------------------------------------------------------------------
2014 // CompileBroker::collect_statistics
2015 //
2016 // Collect statistics about the compilation.
2017
2018 void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
2019 bool success = task->is_success();
2020 methodHandle method (thread, task->method());
2021 uint compile_id = task->compile_id();
2022 bool is_osr = (task->osr_bci() != standard_entry_bci);
2023 nmethod* code = task->code();
2024 CompilerCounters* counters = thread->counters();
2025
2026 assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker");
2027 MutexLocker locker(CompileStatistics_lock);
2028
2029 // _perf variables are production performance counters which are
2030 // updated regardless of the setting of the CITime and CITimeEach flags
2031 //
2032
2172 compiler(_last_compile_level) != NULL &&
2173 _last_method_compiled != NULL &&
2174 _last_compile_type != no_compile) {
2175 if (_last_compile_type == osr_compile) {
2176 tty->print_cr("Last parse: [osr]%d+++(%d) %s",
2177 _osr_compilation_id, _last_compile_level, _last_method_compiled);
2178 } else {
2179 tty->print_cr("Last parse: %d+++(%d) %s",
2180 _compilation_id, _last_compile_level, _last_method_compiled);
2181 }
2182 }
2183 }
2184
2185
2186 void CompileBroker::print_compiler_threads_on(outputStream* st) {
2187 #ifndef PRODUCT
2188 st->print_cr("Compiler thread printing unimplemented.");
2189 st->cr();
2190 #endif
2191 }
2192
|