Path: blob/master/src/hotspot/share/compiler/compileTask.cpp
40930 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "compiler/compilationPolicy.hpp"26#include "compiler/compileTask.hpp"27#include "compiler/compileLog.hpp"28#include "compiler/compileBroker.hpp"29#include "compiler/compilerDirectives.hpp"30#include "logging/log.hpp"31#include "logging/logStream.hpp"32#include "memory/resourceArea.hpp"33#include "oops/klass.inline.hpp"34#include "runtime/handles.inline.hpp"35#include "runtime/jniHandles.hpp"3637CompileTask* CompileTask::_task_free_list = NULL;3839/**40* Allocate a CompileTask, from the free list if possible.41*/42CompileTask* CompileTask::allocate() {43MutexLocker locker(CompileTaskAlloc_lock);44CompileTask* task = NULL;4546if (_task_free_list != NULL) {47task = _task_free_list;48_task_free_list = task->next();49task->set_next(NULL);50} else {51task = new CompileTask();52task->set_next(NULL);53task->set_is_free(true);54}55assert(task->is_free(), "Task must be free.");56task->set_is_free(false);57return task;58}5960/**61* Add a task to the free list.62*/63void CompileTask::free(CompileTask* task) {64MutexLocker locker(CompileTaskAlloc_lock);65if (!task->is_free()) {66task->set_code(NULL);67assert(!task->lock()->is_locked(), "Should not be locked when freed");68if ((task->_method_holder != NULL && JNIHandles::is_weak_global_handle(task->_method_holder)) ||69(task->_hot_method_holder != NULL && JNIHandles::is_weak_global_handle(task->_hot_method_holder))) {70JNIHandles::destroy_weak_global(task->_method_holder);71JNIHandles::destroy_weak_global(task->_hot_method_holder);72} else {73JNIHandles::destroy_global(task->_method_holder);74JNIHandles::destroy_global(task->_hot_method_holder);75}76if (task->_failure_reason_on_C_heap && task->_failure_reason != NULL) {77os::free((void*) task->_failure_reason);78}79task->_failure_reason = NULL;80task->_failure_reason_on_C_heap = false;8182task->set_is_free(true);83task->set_next(_task_free_list);84_task_free_list = task;85}86}8788void CompileTask::initialize(int compile_id,89const methodHandle& method,90int osr_bci,91int comp_level,92const methodHandle& hot_method,93int hot_count,94CompileTask::CompileReason compile_reason,95bool is_blocking) {96assert(!_lock->is_locked(), "bad locking");9798Thread* thread = Thread::current();99_compile_id = compile_id;100_method = method();101_method_holder = JNIHandles::make_weak_global(Handle(thread, method->method_holder()->klass_holder()));102_osr_bci = osr_bci;103_is_blocking = is_blocking;104JVMCI_ONLY(_has_waiter = CompileBroker::compiler(comp_level)->is_jvmci();)105JVMCI_ONLY(_blocking_jvmci_compile_state = NULL;)106_comp_level = comp_level;107_num_inlined_bytecodes = 0;108109_is_complete = false;110_is_success = false;111_code_handle = NULL;112113_hot_method = NULL;114_hot_method_holder = NULL;115_hot_count = hot_count;116_time_queued = os::elapsed_counter();117_time_started = 0;118_compile_reason = compile_reason;119_failure_reason = NULL;120_failure_reason_on_C_heap = false;121122if (LogCompilation) {123if (hot_method.not_null()) {124if (hot_method == method) {125_hot_method = _method;126} else {127_hot_method = hot_method();128// only add loader or mirror if different from _method_holder129_hot_method_holder = JNIHandles::make_weak_global(Handle(thread, hot_method->method_holder()->klass_holder()));130}131}132}133134_next = NULL;135}136137/**138* Returns the compiler for this task.139*/140AbstractCompiler* CompileTask::compiler() {141return CompileBroker::compiler(_comp_level);142}143144// Replace weak handles by strong handles to avoid unloading during compilation.145CompileTask* CompileTask::select_for_compilation() {146if (is_unloaded()) {147// Guard against concurrent class unloading148return NULL;149}150Thread* thread = Thread::current();151assert(_method->method_holder()->is_loader_alive(), "should be alive");152Handle method_holder(thread, _method->method_holder()->klass_holder());153JNIHandles::destroy_weak_global(_method_holder);154JNIHandles::destroy_weak_global(_hot_method_holder);155_method_holder = JNIHandles::make_global(method_holder);156if (_hot_method != NULL) {157_hot_method_holder = JNIHandles::make_global(Handle(thread, _hot_method->method_holder()->klass_holder()));158}159return this;160}161162// ------------------------------------------------------------------163// CompileTask::code/set_code164//165nmethod* CompileTask::code() const {166if (_code_handle == NULL) return NULL;167CodeBlob *blob = _code_handle->code();168if (blob != NULL) {169return blob->as_nmethod();170}171return NULL;172}173174void CompileTask::set_code(nmethod* nm) {175if (_code_handle == NULL && nm == NULL) return;176guarantee(_code_handle != NULL, "");177_code_handle->set_code(nm);178if (nm == NULL) _code_handle = NULL; // drop the handle also179}180181void CompileTask::mark_on_stack() {182if (is_unloaded()) {183return;184}185// Mark these methods as something redefine classes cannot remove.186_method->set_on_stack(true);187if (_hot_method != NULL) {188_hot_method->set_on_stack(true);189}190}191192bool CompileTask::is_unloaded() const {193return _method_holder != NULL && JNIHandles::is_weak_global_handle(_method_holder) && JNIHandles::is_global_weak_cleared(_method_holder);194}195196// RedefineClasses support197void CompileTask::metadata_do(MetadataClosure* f) {198if (is_unloaded()) {199return;200}201f->do_metadata(method());202if (hot_method() != NULL && hot_method() != method()) {203f->do_metadata(hot_method());204}205}206207// ------------------------------------------------------------------208// CompileTask::print_line_on_error209//210// This function is called by fatal error handler when the thread211// causing troubles is a compiler thread.212//213// Do not grab any lock, do not allocate memory.214//215// Otherwise it's the same as CompileTask::print_line()216//217void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) {218// print compiler name219st->print("%s:", CompileBroker::compiler_name(comp_level()));220print(st);221}222223// ------------------------------------------------------------------224// CompileTask::print_tty225void CompileTask::print_tty() {226ttyLocker ttyl; // keep the following output all in one block227// print compiler name if requested228if (CIPrintCompilerName) {229tty->print("%s:", CompileBroker::compiler_name(comp_level()));230}231print(tty);232}233234// ------------------------------------------------------------------235// CompileTask::print_impl236void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level,237bool is_osr_method, int osr_bci, bool is_blocking,238const char* msg, bool short_form, bool cr,239jlong time_queued, jlong time_started) {240if (!short_form) {241// Print current time242st->print("%7d ", (int)tty->time_stamp().milliseconds());243if (Verbose && time_queued != 0) {244// Print time in queue and time being processed by compiler thread245jlong now = os::elapsed_counter();246st->print("%d ", (int)TimeHelper::counter_to_millis(now-time_queued));247if (time_started != 0) {248st->print("%d ", (int)TimeHelper::counter_to_millis(now-time_started));249}250}251}252// print compiler name if requested253if (CIPrintCompilerName) {254st->print("%s:", CompileBroker::compiler_name(comp_level));255}256st->print("%4d ", compile_id); // print compilation number257258// For unloaded methods the transition to zombie occurs after the259// method is cleared so it's impossible to report accurate260// information for that case.261bool is_synchronized = false;262bool has_exception_handler = false;263bool is_native = false;264if (method != NULL) {265is_synchronized = method->is_synchronized();266has_exception_handler = method->has_exception_handler();267is_native = method->is_native();268}269// method attributes270const char compile_type = is_osr_method ? '%' : ' ';271const char sync_char = is_synchronized ? 's' : ' ';272const char exception_char = has_exception_handler ? '!' : ' ';273const char blocking_char = is_blocking ? 'b' : ' ';274const char native_char = is_native ? 'n' : ' ';275276// print method attributes277st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char);278279if (TieredCompilation) {280if (comp_level != -1) st->print("%d ", comp_level);281else st->print("- ");282}283st->print(" "); // more indent284285if (method == NULL) {286st->print("(method)");287} else {288method->print_short_name(st);289if (is_osr_method) {290st->print(" @ %d", osr_bci);291}292if (method->is_native())293st->print(" (native)");294else295st->print(" (%d bytes)", method->code_size());296}297298if (msg != NULL) {299st->print(" %s", msg);300}301if (cr) {302st->cr();303}304}305306void CompileTask::print_inline_indent(int inline_level, outputStream* st) {307// 1234567308st->print(" "); // print timestamp309// 1234310st->print(" "); // print compilation number311// %s!bn312st->print(" "); // print method attributes313if (TieredCompilation) {314st->print(" ");315}316st->print(" "); // more indent317st->print(" "); // initial inlining indent318for (int i = 0; i < inline_level; i++) st->print(" ");319}320321// ------------------------------------------------------------------322// CompileTask::print_compilation323void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) {324bool is_osr_method = osr_bci() != InvocationEntryBci;325print_impl(st, is_unloaded() ? NULL : method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr, _time_queued, _time_started);326}327328// ------------------------------------------------------------------329// CompileTask::log_task330void CompileTask::log_task(xmlStream* log) {331Thread* thread = Thread::current();332methodHandle method(thread, this->method());333ResourceMark rm(thread);334335// <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>336log->print(" compile_id='%d'", _compile_id);337if (_osr_bci != CompileBroker::standard_entry_bci) {338log->print(" compile_kind='osr'"); // same as nmethod::compile_kind339} // else compile_kind='c2c'340if (!method.is_null()) log->method(method());341if (_osr_bci != CompileBroker::standard_entry_bci) {342log->print(" osr_bci='%d'", _osr_bci);343}344if (_comp_level != CompilationPolicy::highest_compile_level()) {345log->print(" level='%d'", _comp_level);346}347if (_is_blocking) {348log->print(" blocking='1'");349}350log->stamp();351}352353// ------------------------------------------------------------------354// CompileTask::log_task_queued355void CompileTask::log_task_queued() {356ttyLocker ttyl;357ResourceMark rm;358359xtty->begin_elem("task_queued");360log_task(xtty);361assert(_compile_reason > CompileTask::Reason_None && _compile_reason < CompileTask::Reason_Count, "Valid values");362xtty->print(" comment='%s'", reason_name(_compile_reason));363364if (_hot_method != NULL && _hot_method != _method) {365xtty->method(_hot_method);366}367if (_hot_count != 0) {368xtty->print(" hot_count='%d'", _hot_count);369}370xtty->end_elem();371}372373374// ------------------------------------------------------------------375// CompileTask::log_task_start376void CompileTask::log_task_start(CompileLog* log) {377log->begin_head("task");378log_task(log);379log->end_head();380}381382383// ------------------------------------------------------------------384// CompileTask::log_task_done385void CompileTask::log_task_done(CompileLog* log) {386Thread* thread = Thread::current();387methodHandle method(thread, this->method());388ResourceMark rm(thread);389390if (!_is_success) {391assert(_failure_reason != NULL, "missing");392const char* reason = _failure_reason != NULL ? _failure_reason : "unknown";393log->begin_elem("failure reason='");394log->text("%s", reason);395log->print("'");396log->end_elem();397}398399// <task_done ... stamp='1.234'> </task>400nmethod* nm = code();401log->begin_elem("task_done success='%d' nmsize='%d' count='%d'",402_is_success, nm == NULL ? 0 : nm->content_size(),403method->invocation_count());404int bec = method->backedge_count();405if (bec != 0) log->print(" backedge_count='%d'", bec);406// Note: "_is_complete" is about to be set, but is not.407if (_num_inlined_bytecodes != 0) {408log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);409}410log->stamp();411log->end_elem();412log->clear_identities(); // next task will have different CI413log->tail("task");414log->flush();415log->mark_file_end();416}417418// ------------------------------------------------------------------419// CompileTask::check_break_at_flags420bool CompileTask::check_break_at_flags() {421int compile_id = this->_compile_id;422bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci);423424if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) {425return true;426} else {427return (compile_id == CIBreakAt);428}429}430431// ------------------------------------------------------------------432// CompileTask::print_inlining433void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) {434// 1234567435st->print(" "); // print timestamp436// 1234437st->print(" "); // print compilation number438439// method attributes440if (method->is_loaded()) {441const char sync_char = method->is_synchronized() ? 's' : ' ';442const char exception_char = method->has_exception_handlers() ? '!' : ' ';443const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' ';444445// print method attributes446st->print(" %c%c%c ", sync_char, exception_char, monitors_char);447} else {448// %s!bn449st->print(" "); // print method attributes450}451452if (TieredCompilation) {453st->print(" ");454}455st->print(" "); // more indent456st->print(" "); // initial inlining indent457458for (int i = 0; i < inline_level; i++) st->print(" ");459460st->print("@ %d ", bci); // print bci461method->print_short_name(st);462if (method->is_loaded())463st->print(" (%d bytes)", method->code_size());464else465st->print(" (not loaded)");466467if (msg != NULL) {468st->print(" %s", msg);469}470st->cr();471}472473void CompileTask::print_ul(const char* msg){474LogTarget(Debug, jit, compilation) lt;475if (lt.is_enabled()) {476LogStream ls(lt);477print(&ls, msg, /* short form */ true, /* cr */ true);478}479}480481void CompileTask::print_ul(const nmethod* nm, const char* msg) {482LogTarget(Debug, jit, compilation) lt;483if (lt.is_enabled()) {484LogStream ls(lt);485print_impl(&ls, nm->method(), nm->compile_id(),486nm->comp_level(), nm->is_osr_method(),487nm->is_osr_method() ? nm->osr_entry_bci() : -1,488/*is_blocking*/ false,489msg, /* short form */ true, /* cr */ true);490}491}492493void CompileTask::print_inlining_ul(ciMethod* method, int inline_level, int bci, const char* msg) {494LogTarget(Debug, jit, inlining) lt;495if (lt.is_enabled()) {496LogStream ls(lt);497print_inlining_inner(&ls, method, inline_level, bci, msg);498}499}500501502503