1 /* 2 * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 #ifndef SHARE_COMPILER_COMPILETASK_HPP 26 #define SHARE_COMPILER_COMPILETASK_HPP 27 28 #include "ci/ciMethod.hpp" 29 #include "code/nmethod.hpp" 30 #include "compiler/compileLog.hpp" 31 #include "memory/allocation.hpp" 32 #include "utilities/xmlstream.hpp" 33 34 class CompileQueue; 35 class CompileTrainingData; 36 class DirectiveSet; 37 class AOTCodeEntry; 38 39 JVMCI_ONLY(class JVMCICompileState;) 40 41 enum class InliningResult { SUCCESS, FAILURE }; 42 43 inline InliningResult inlining_result_of(bool success) { 44 return success ? InliningResult::SUCCESS : InliningResult::FAILURE; 45 } 46 47 // CompileTask 48 // 49 // An entry in the compile queue. It represents a pending or current 50 // compilation. 51 52 class CompileTask : public CHeapObj<mtCompiler> { 53 friend class VMStructs; 54 friend class JVMCIVMStructs; 55 56 public: 57 // Different reasons for a compilation 58 // The order is important - mapped to reason_names[] 59 enum CompileReason { 60 Reason_None, 61 Reason_InvocationCount, // Simple/StackWalk-policy 62 Reason_BackedgeCount, // Simple/StackWalk-policy 63 Reason_Tiered, // Tiered-policy 64 Reason_Replay, // ciReplay 65 Reason_Whitebox, // Whitebox API 66 Reason_MustBeCompiled, // Used for -Xcomp or AlwaysCompileLoopMethods (see CompilationPolicy::must_be_compiled()) 67 Reason_Bootstrap, // JVMCI bootstrap 68 Reason_Preload, // pre-load AOT code 69 Reason_Precompile, 70 Reason_PrecompileForPreload, 71 Reason_Count 72 }; 73 74 static const char* reason_name(CompileTask::CompileReason compile_reason) { 75 static const char* reason_names[] = { 76 "no_reason", 77 "count", 78 "backedge_count", 79 "tiered", 80 "replay", 81 "whitebox", 82 "must_be_compiled", 83 "bootstrap", 84 "preload", 85 "precompile", 86 "precompile_for_preload", 87 }; 88 return reason_names[compile_reason]; 89 } 90 91 static bool reason_is_precompiled(CompileTask::CompileReason compile_reason) { 92 return (compile_reason == CompileTask::Reason_Precompile) || 93 (compile_reason == CompileTask::Reason_PrecompileForPreload); 94 } 95 96 private: 97 static CompileTask* _task_free_list; 98 static int _active_tasks; 99 Monitor* _lock; 100 int _compile_id; 101 Method* _method; 102 jobject _method_holder; 103 int _osr_bci; 104 bool _is_complete; 105 bool _is_success; 106 bool _requires_online_compilation; 107 bool _is_blocking; 108 CodeSection::csize_t _nm_content_size; 109 CodeSection::csize_t _nm_total_size; 110 CodeSection::csize_t _nm_insts_size; 111 DirectiveSet* _directive; 112 AbstractCompiler* _compiler; 113 AOTCodeEntry* _aot_code_entry; 114 #if INCLUDE_JVMCI 115 bool _has_waiter; 116 // Compilation state for a blocking JVMCI compilation 117 JVMCICompileState* _blocking_jvmci_compile_state; 118 #endif 119 int _waiting_count; // See waiting_for_completion_count() 120 int _comp_level; 121 int _num_inlined_bytecodes; 122 CompileTask* _next; 123 CompileTask* _prev; 124 bool _is_free; 125 // Fields used for logging why the compilation was initiated: 126 jlong _time_created; // time when task was created 127 jlong _time_queued; // time when task was enqueued 128 jlong _time_started; // time when compilation started 129 jlong _time_finished; // time when compilation finished 130 jlong _aot_load_start; 131 jlong _aot_load_finish; 132 int _hot_count; // information about its invocation counter 133 CompileReason _compile_reason; // more info about the task 134 const char* _failure_reason; 135 // Specifies if _failure_reason is on the C heap. 136 bool _failure_reason_on_C_heap; 137 CompileTrainingData* _training_data; 138 CompileQueue* _compile_queue; 139 size_t _arena_bytes; // peak size of temporary memory during compilation (e.g. node arenas) 140 141 public: 142 CompileTask() : _failure_reason(nullptr), _failure_reason_on_C_heap(false) { 143 // May hold MethodCompileQueue_lock 144 _lock = new Monitor(Mutex::safepoint-1, "CompileTask_lock"); 145 } 146 147 void initialize(int compile_id, const methodHandle& method, int osr_bci, int comp_level, 148 int hot_count, AOTCodeEntry* aot_code_entry, 149 CompileTask::CompileReason compile_reason, 150 CompileQueue* compile_queue, 151 bool requires_online_compilation, bool is_blocking); 152 153 static CompileTask* allocate(); 154 static void free(CompileTask* task); 155 static void wait_for_no_active_tasks(); 156 157 int compile_id() const { return _compile_id; } 158 Method* method() const { return _method; } 159 int osr_bci() const { return _osr_bci; } 160 bool is_complete() const { return _is_complete; } 161 bool is_blocking() const { return _is_blocking; } 162 bool is_success() const { return _is_success; } 163 bool is_aot() const { return _aot_code_entry != nullptr; } 164 void clear_aot() { _aot_code_entry = nullptr; } 165 AOTCodeEntry* aot_code_entry() { return _aot_code_entry; } 166 bool requires_online_compilation() const { return _requires_online_compilation; } 167 DirectiveSet* directive() const { return _directive; } 168 CompileReason compile_reason() const { return _compile_reason; } 169 CodeSection::csize_t nm_content_size() { return _nm_content_size; } 170 void set_nm_content_size(CodeSection::csize_t size) { _nm_content_size = size; } 171 CodeSection::csize_t nm_insts_size() { return _nm_insts_size; } 172 void set_nm_insts_size(CodeSection::csize_t size) { _nm_insts_size = size; } 173 CodeSection::csize_t nm_total_size() { return _nm_total_size; } 174 void set_nm_total_size(CodeSection::csize_t size) { _nm_total_size = size; } 175 bool preload() const { return (_compile_reason == Reason_Preload); } 176 bool can_become_stale() const { 177 switch (_compile_reason) { 178 case Reason_BackedgeCount: 179 case Reason_InvocationCount: 180 case Reason_Tiered: 181 return !_is_blocking; 182 default: 183 return false; 184 } 185 } 186 #if INCLUDE_JVMCI 187 bool should_wait_for_compilation() const { 188 // Wait for blocking compilation to finish. 189 switch (_compile_reason) { 190 case Reason_Replay: 191 case Reason_Whitebox: 192 case Reason_Bootstrap: 193 return _is_blocking; 194 default: 195 return false; 196 } 197 } 198 199 bool has_waiter() const { return _has_waiter; } 200 void clear_waiter() { _has_waiter = false; } 201 JVMCICompileState* blocking_jvmci_compile_state() const { return _blocking_jvmci_compile_state; } 202 void set_blocking_jvmci_compile_state(JVMCICompileState* state) { 203 _blocking_jvmci_compile_state = state; 204 } 205 #endif 206 207 bool is_precompiled() { 208 return reason_is_precompiled(compile_reason()); 209 } 210 211 Monitor* lock() const { return _lock; } 212 CompileQueue* compile_queue() const { return _compile_queue; } 213 214 // See how many threads are waiting for this task. Must have lock to read this. 215 int waiting_for_completion_count() { 216 assert(_lock->owned_by_self(), "must have lock to use waiting_for_completion_count()"); 217 return _waiting_count; 218 } 219 // Indicates that a thread is waiting for this task to complete. Must have lock to use this. 220 void inc_waiting_for_completion() { 221 assert(_lock->owned_by_self(), "must have lock to use inc_waiting_for_completion()"); 222 _waiting_count++; 223 } 224 // Indicates that a thread stopped waiting for this task to complete. Must have lock to use this. 225 void dec_waiting_for_completion() { 226 assert(_lock->owned_by_self(), "must have lock to use dec_waiting_for_completion()"); 227 assert(_waiting_count > 0, "waiting count is not positive"); 228 _waiting_count--; 229 } 230 231 void mark_complete() { _is_complete = true; } 232 void mark_success() { _is_success = true; } 233 void mark_queued(jlong time) { _time_queued = time; } 234 void mark_started(jlong time) { _time_started = time; } 235 void mark_finished(jlong time) { _time_finished = time; } 236 void mark_aot_load_start(jlong time) { _aot_load_start = time; } 237 void mark_aot_load_finish(jlong time) { _aot_load_finish = time; } 238 int comp_level() { return _comp_level;} 239 void set_comp_level(int comp_level) { _comp_level = comp_level;} 240 241 CompileReason compile_reason() { return _compile_reason; } 242 243 AbstractCompiler* compiler() const; 244 CompileTask* select_for_compilation(); 245 246 int num_inlined_bytecodes() const { return _num_inlined_bytecodes; } 247 void set_num_inlined_bytecodes(int n) { _num_inlined_bytecodes = n; } 248 249 static CompileTask* volatile* next_ptr(CompileTask& task) { return &task._next; } 250 251 CompileTask* next() const { return _next; } 252 void set_next(CompileTask* next) { _next = next; } 253 CompileTask* prev() const { return _prev; } 254 void set_prev(CompileTask* prev) { _prev = prev; } 255 bool is_free() const { return _is_free; } 256 void set_is_free(bool val) { _is_free = val; } 257 bool is_unloaded() const; 258 259 CompileTrainingData* training_data() const { return _training_data; } 260 void set_training_data(CompileTrainingData* td) { _training_data = td; } 261 262 // RedefineClasses support 263 void metadata_do(MetadataClosure* f); 264 void mark_on_stack(); 265 266 void set_arena_bytes(size_t s) { _arena_bytes = s; } 267 size_t arena_bytes() const { return _arena_bytes; } 268 269 private: 270 static void print_impl(outputStream* st, Method* method, int compile_id, int comp_level, 271 bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false, 272 bool is_aot = false, bool is_preload = false, 273 const char* compiler_name = nullptr, 274 const char* msg = nullptr, bool short_form = false, bool cr = true, 275 jlong time_created = 0, jlong time_queued = 0, jlong time_started = 0, jlong time_finished = 0, 276 jlong aot_load_start = 0, jlong aot_load_finish = 0); 277 278 public: 279 void print(outputStream* st = tty, const char* msg = nullptr, bool short_form = false, bool cr = true); 280 void print_ul(const char* msg = nullptr); 281 static void print(outputStream* st, const nmethod* nm, const char* msg = nullptr, bool short_form = false, bool cr = true) { 282 print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(), 283 nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false, 284 nm->aot_code_entry() != nullptr, nm->preloaded(), 285 nm->compiler_name(), msg, short_form, cr); 286 } 287 static void print_ul(const nmethod* nm, const char* msg = nullptr); 288 289 /** 290 * @deprecated Please rely on Compile::inline_printer. Do not directly write inlining information to tty. 291 */ 292 static void print_inline_indent(int inline_level, outputStream* st = tty); 293 294 void print_tty(); 295 void print_line_on_error(outputStream* st, char* buf, int buflen); 296 297 void log_task(xmlStream* log); 298 void log_task_queued(); 299 void log_task_start(CompileLog* log); 300 void log_task_done(CompileLog* log); 301 302 void set_failure_reason(const char* reason, bool on_C_heap = false) { 303 _failure_reason = reason; 304 _failure_reason_on_C_heap = on_C_heap; 305 } 306 307 bool check_break_at_flags(); 308 309 static void print_inlining_header(outputStream* st, ciMethod* method, int inline_level, int bci); 310 static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr); 311 static void print_inline_inner_method_info(outputStream* st, ciMethod* method); 312 static void print_inlining_inner_message(outputStream* st, InliningResult result, const char* msg); 313 314 static void print_inlining_tty(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr) { 315 print_inlining_inner(tty, method, inline_level, bci, result, msg); 316 } 317 static void print_inlining_ul(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr); 318 }; 319 320 #endif // SHARE_COMPILER_COMPILETASK_HPP