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 SCCEntry; 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 SC 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 SCCEntry* _scc_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 Method* _hot_method; // which method actually triggered this task 133 jobject _hot_method_holder; 134 int _hot_count; // information about its invocation counter 135 CompileReason _compile_reason; // more info about the task 136 const char* _failure_reason; 137 // Specifies if _failure_reason is on the C heap. 138 bool _failure_reason_on_C_heap; 139 CompileTrainingData* _training_data; 140 CompileQueue* _compile_queue; 141 size_t _arena_bytes; // peak size of temporary memory during compilation (e.g. node arenas) 142 143 public: 144 CompileTask() : _failure_reason(nullptr), _failure_reason_on_C_heap(false) { 145 // May hold MethodCompileQueue_lock 146 _lock = new Monitor(Mutex::safepoint-1, "CompileTask_lock"); 147 } 148 149 void initialize(int compile_id, const methodHandle& method, int osr_bci, int comp_level, 150 const methodHandle& hot_method, int hot_count, SCCEntry* scc_entry, 151 CompileTask::CompileReason compile_reason, 152 CompileQueue* compile_queue, 153 bool requires_online_compilation, bool is_blocking); 154 155 static CompileTask* allocate(); 156 static void free(CompileTask* task); 157 static void wait_for_no_active_tasks(); 158 159 int compile_id() const { return _compile_id; } 160 Method* method() const { return _method; } 161 Method* hot_method() const { return _hot_method; } 162 int osr_bci() const { return _osr_bci; } 163 bool is_complete() const { return _is_complete; } 164 bool is_blocking() const { return _is_blocking; } 165 bool is_success() const { return _is_success; } 166 bool is_scc() const { return _scc_entry != nullptr; } 167 void clear_scc() { _scc_entry = nullptr; } 168 SCCEntry* scc_entry() { return _scc_entry; } 169 bool requires_online_compilation() const { return _requires_online_compilation; } 170 DirectiveSet* directive() const { return _directive; } 171 CompileReason compile_reason() const { return _compile_reason; } 172 CodeSection::csize_t nm_content_size() { return _nm_content_size; } 173 void set_nm_content_size(CodeSection::csize_t size) { _nm_content_size = size; } 174 CodeSection::csize_t nm_insts_size() { return _nm_insts_size; } 175 void set_nm_insts_size(CodeSection::csize_t size) { _nm_insts_size = size; } 176 CodeSection::csize_t nm_total_size() { return _nm_total_size; } 177 void set_nm_total_size(CodeSection::csize_t size) { _nm_total_size = size; } 178 bool preload() const { return (_compile_reason == Reason_Preload); } 179 bool can_become_stale() const { 180 switch (_compile_reason) { 181 case Reason_BackedgeCount: 182 case Reason_InvocationCount: 183 case Reason_Tiered: 184 return !_is_blocking; 185 default: 186 return false; 187 } 188 } 189 #if INCLUDE_JVMCI 190 bool should_wait_for_compilation() const { 191 // Wait for blocking compilation to finish. 192 switch (_compile_reason) { 193 case Reason_Replay: 194 case Reason_Whitebox: 195 case Reason_Bootstrap: 196 return _is_blocking; 197 default: 198 return false; 199 } 200 } 201 202 bool has_waiter() const { return _has_waiter; } 203 void clear_waiter() { _has_waiter = false; } 204 JVMCICompileState* blocking_jvmci_compile_state() const { return _blocking_jvmci_compile_state; } 205 void set_blocking_jvmci_compile_state(JVMCICompileState* state) { 206 _blocking_jvmci_compile_state = state; 207 } 208 #endif 209 210 bool is_precompiled() { 211 return reason_is_precompiled(compile_reason()); 212 } 213 214 Monitor* lock() const { return _lock; } 215 CompileQueue* compile_queue() const { return _compile_queue; } 216 217 // See how many threads are waiting for this task. Must have lock to read this. 218 int waiting_for_completion_count() { 219 assert(_lock->owned_by_self(), "must have lock to use waiting_for_completion_count()"); 220 return _waiting_count; 221 } 222 // Indicates that a thread is waiting for this task to complete. Must have lock to use this. 223 void inc_waiting_for_completion() { 224 assert(_lock->owned_by_self(), "must have lock to use inc_waiting_for_completion()"); 225 _waiting_count++; 226 } 227 // Indicates that a thread stopped waiting for this task to complete. Must have lock to use this. 228 void dec_waiting_for_completion() { 229 assert(_lock->owned_by_self(), "must have lock to use dec_waiting_for_completion()"); 230 assert(_waiting_count > 0, "waiting count is not positive"); 231 _waiting_count--; 232 } 233 234 void mark_complete() { _is_complete = true; } 235 void mark_success() { _is_success = true; } 236 void mark_queued(jlong time) { _time_queued = time; } 237 void mark_started(jlong time) { _time_started = time; } 238 void mark_finished(jlong time) { _time_finished = time; } 239 void mark_aot_load_start(jlong time) { _aot_load_start = time; } 240 void mark_aot_load_finish(jlong time) { _aot_load_finish = time; } 241 int comp_level() { return _comp_level;} 242 void set_comp_level(int comp_level) { _comp_level = comp_level;} 243 244 CompileReason compile_reason() { return _compile_reason; } 245 246 AbstractCompiler* compiler() const; 247 CompileTask* select_for_compilation(); 248 249 int num_inlined_bytecodes() const { return _num_inlined_bytecodes; } 250 void set_num_inlined_bytecodes(int n) { _num_inlined_bytecodes = n; } 251 252 static CompileTask* volatile* next_ptr(CompileTask& task) { return &task._next; } 253 254 CompileTask* next() const { return _next; } 255 void set_next(CompileTask* next) { _next = next; } 256 CompileTask* prev() const { return _prev; } 257 void set_prev(CompileTask* prev) { _prev = prev; } 258 bool is_free() const { return _is_free; } 259 void set_is_free(bool val) { _is_free = val; } 260 bool is_unloaded() const; 261 262 CompileTrainingData* training_data() const { return _training_data; } 263 void set_training_data(CompileTrainingData*td) { _training_data = td; } 264 265 // RedefineClasses support 266 void metadata_do(MetadataClosure* f); 267 void mark_on_stack(); 268 269 void set_arena_bytes(size_t s) { _arena_bytes = s; } 270 size_t arena_bytes() const { return _arena_bytes; } 271 272 private: 273 static void print_impl(outputStream* st, Method* method, int compile_id, int comp_level, 274 bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false, 275 bool is_scc = false, bool is_preload = false, 276 const char* compiler_name = nullptr, 277 const char* msg = nullptr, bool short_form = false, bool cr = true, 278 jlong time_created = 0, jlong time_queued = 0, jlong time_started = 0, jlong time_finished = 0, 279 jlong aot_load_start = 0, jlong aot_load_finish = 0); 280 281 public: 282 void print(outputStream* st = tty, const char* msg = nullptr, bool short_form = false, bool cr = true); 283 void print_ul(const char* msg = nullptr); 284 static void print(outputStream* st, const nmethod* nm, const char* msg = nullptr, bool short_form = false, bool cr = true) { 285 print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(), 286 nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false, 287 nm->scc_entry() != nullptr, nm->preloaded(), 288 nm->compiler_name(), msg, short_form, cr); 289 } 290 static void print_ul(const nmethod* nm, const char* msg = nullptr); 291 292 /** 293 * @deprecated Please rely on Compile::inline_printer. Do not directly write inlining information to tty. 294 */ 295 static void print_inline_indent(int inline_level, outputStream* st = tty); 296 297 void print_tty(); 298 void print_line_on_error(outputStream* st, char* buf, int buflen); 299 300 void log_task(xmlStream* log); 301 void log_task_queued(); 302 void log_task_start(CompileLog* log); 303 void log_task_done(CompileLog* log); 304 305 void set_failure_reason(const char* reason, bool on_C_heap = false) { 306 _failure_reason = reason; 307 _failure_reason_on_C_heap = on_C_heap; 308 } 309 310 bool check_break_at_flags(); 311 312 static void print_inlining_header(outputStream* st, ciMethod* method, int inline_level, int bci); 313 static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr); 314 static void print_inline_inner_method_info(outputStream* st, ciMethod* method); 315 static void print_inlining_inner_message(outputStream* st, InliningResult result, const char* msg); 316 317 static void print_inlining_tty(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr) { 318 print_inlining_inner(tty, method, inline_level, bci, result, msg); 319 } 320 static void print_inlining_ul(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr); 321 }; 322 323 #endif // SHARE_COMPILER_COMPILETASK_HPP