1 /*
2 * Copyright (c) 1998, 2025, 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 "runtime/mutexLocker.hpp"
33 #include "utilities/xmlstream.hpp"
34
35 class CompileTrainingData;
36 class DirectiveSet;
37
38 JVMCI_ONLY(class JVMCICompileState;)
39
40 enum class InliningResult { SUCCESS, FAILURE };
41
42 inline InliningResult inlining_result_of(bool success) {
43 return success ? InliningResult::SUCCESS : InliningResult::FAILURE;
44 }
45
46 // CompileTask
47 //
48 // An entry in the compile queue. It represents a pending or current
49 // compilation.
50
51 class CompileTask : public CHeapObj<mtCompiler> {
52 friend class VMStructs;
53 friend class JVMCIVMStructs;
54
55 public:
56 // Different reasons for a compilation
57 // The order is important - mapped to reason_names[]
58 enum CompileReason {
59 Reason_None,
60 Reason_InvocationCount, // Simple/StackWalk-policy
61 Reason_BackedgeCount, // Simple/StackWalk-policy
62 Reason_Tiered, // Tiered-policy
63 Reason_Replay, // ciReplay
64 Reason_Whitebox, // Whitebox API
65 Reason_MustBeCompiled, // Used for -Xcomp or AlwaysCompileLoopMethods (see CompilationPolicy::must_be_compiled())
66 Reason_Bootstrap, // JVMCI bootstrap
67 Reason_Count
68 };
69
70 static const char* reason_name(CompileTask::CompileReason compile_reason) {
71 static const char* reason_names[] = {
72 "no_reason",
73 "count",
74 "backedge_count",
75 "tiered",
76 "replay",
77 "whitebox",
78 "must_be_compiled",
79 "bootstrap"
80 };
81 return reason_names[compile_reason];
82 }
83
84 private:
85 static int _active_tasks;
86 int _compile_id;
87 Method* _method;
88 jobject _method_holder;
89 int _osr_bci;
90 bool _is_complete;
91 bool _is_success;
92 bool _is_blocking;
93 CodeSection::csize_t _nm_content_size;
94 CodeSection::csize_t _nm_total_size;
95 CodeSection::csize_t _nm_insts_size;
96 DirectiveSet* _directive;
97 #if INCLUDE_JVMCI
98 bool _has_waiter;
99 // Compilation state for a blocking JVMCI compilation
100 JVMCICompileState* _blocking_jvmci_compile_state;
101 #endif
102 int _comp_level;
103 int _num_inlined_bytecodes;
104 CompileTask* _next;
105 CompileTask* _prev;
106 // Fields used for logging why the compilation was initiated:
107 jlong _time_queued; // time when task was enqueued
108 jlong _time_started; // time when compilation started
109 int _hot_count; // information about its invocation counter
110 CompileReason _compile_reason; // more info about the task
111 const char* _failure_reason;
112 // Specifies if _failure_reason is on the C heap.
113 bool _failure_reason_on_C_heap;
114 CompileTrainingData* _training_data;
115 size_t _arena_bytes; // peak size of temporary memory during compilation (e.g. node arenas)
116
117 public:
118 CompileTask(int compile_id, const methodHandle& method, int osr_bci, int comp_level,
119 int hot_count, CompileReason compile_reason, bool is_blocking);
120 ~CompileTask();
121 static void wait_for_no_active_tasks();
122
123 int compile_id() const { return _compile_id; }
124 Method* method() const { return _method; }
125 int osr_bci() const { return _osr_bci; }
126 bool is_complete() const { return _is_complete; }
127 bool is_blocking() const { return _is_blocking; }
128 bool is_success() const { return _is_success; }
129 DirectiveSet* directive() const { return _directive; }
130 CodeSection::csize_t nm_content_size() { return _nm_content_size; }
131 void set_nm_content_size(CodeSection::csize_t size) { _nm_content_size = size; }
132 CodeSection::csize_t nm_insts_size() { return _nm_insts_size; }
133 void set_nm_insts_size(CodeSection::csize_t size) { _nm_insts_size = size; }
134 CodeSection::csize_t nm_total_size() { return _nm_total_size; }
135 void set_nm_total_size(CodeSection::csize_t size) { _nm_total_size = size; }
136 bool can_become_stale() const {
137 switch (_compile_reason) {
138 case Reason_BackedgeCount:
139 case Reason_InvocationCount:
140 case Reason_Tiered:
141 return !_is_blocking;
142 default:
143 return false;
144 }
145 }
146 #if INCLUDE_JVMCI
147 bool should_wait_for_compilation() const {
148 // Wait for blocking compilation to finish.
149 switch (_compile_reason) {
150 case Reason_Replay:
151 case Reason_Whitebox:
152 case Reason_Bootstrap:
153 return _is_blocking;
154 default:
155 return false;
156 }
157 }
158
159 bool has_waiter() const { return _has_waiter; }
160 void clear_waiter() { _has_waiter = false; }
161 JVMCICompileState* blocking_jvmci_compile_state() const { return _blocking_jvmci_compile_state; }
162 void set_blocking_jvmci_compile_state(JVMCICompileState* state) {
163 _blocking_jvmci_compile_state = state;
164 }
165 #endif
166
167 void mark_complete() { _is_complete = true; }
168 void mark_success() { _is_success = true; }
169 void mark_started(jlong time) { _time_started = time; }
170
171 int comp_level() { return _comp_level;}
172 void set_comp_level(int comp_level) { _comp_level = comp_level;}
173
174 CompileReason compile_reason() { return _compile_reason; }
175
176 AbstractCompiler* compiler() const;
177 CompileTask* select_for_compilation();
178
179 int num_inlined_bytecodes() const { return _num_inlined_bytecodes; }
180 void set_num_inlined_bytecodes(int n) { _num_inlined_bytecodes = n; }
181
182 CompileTask* next() const { return _next; }
183 void set_next(CompileTask* next) { _next = next; }
184 CompileTask* prev() const { return _prev; }
185 void set_prev(CompileTask* prev) { _prev = prev; }
186 bool is_unloaded() const;
187
188 CompileTrainingData* training_data() const { return _training_data; }
189 void set_training_data(CompileTrainingData* td) { _training_data = td; }
190
191 // RedefineClasses support
192 void metadata_do(MetadataClosure* f);
193 void mark_on_stack();
194
195 void set_arena_bytes(size_t s) { _arena_bytes = s; }
196 size_t arena_bytes() const { return _arena_bytes; }
197
198 private:
199 static void print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
200 bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
201 const char* msg = nullptr, bool short_form = false, bool cr = true,
202 jlong time_queued = 0, jlong time_started = 0);
203
204 public:
205 void print(outputStream* st = tty, const char* msg = nullptr, bool short_form = false, bool cr = true);
206 void print_ul(const char* msg = nullptr);
207 static void print(outputStream* st, const nmethod* nm, const char* msg = nullptr, bool short_form = false, bool cr = true) {
208 print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
209 nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
210 msg, short_form, cr);
211 }
212 static void print_ul(const nmethod* nm, const char* msg = nullptr);
213
214 /**
215 * @deprecated Please rely on Compile::inline_printer. Do not directly write inlining information to tty.
216 */
217 static void print_inline_indent(int inline_level, outputStream* st = tty);
218
219 void print_tty();
220 void print_line_on_error(outputStream* st, char* buf, int buflen);
221
222 void log_task(xmlStream* log);
223 void log_task_queued();
224 void log_task_start(CompileLog* log);
225 void log_task_done(CompileLog* log);
226
227 void set_failure_reason(const char* reason, bool on_C_heap = false) {
228 _failure_reason = reason;
229 _failure_reason_on_C_heap = on_C_heap;
230 }
231
232 bool check_break_at_flags();
233
234 static void print_inlining_header(outputStream* st, ciMethod* method, int inline_level, int bci);
235 static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr);
236 static void print_inline_inner_method_info(outputStream* st, ciMethod* method);
237 static void print_inlining_inner_message(outputStream* st, InliningResult result, const char* msg);
238
239 static void print_inlining_tty(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr) {
240 print_inlining_inner(tty, method, inline_level, bci, result, msg);
241 }
242 static void print_inlining_ul(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr);
243 };
244
245 #endif // SHARE_COMPILER_COMPILETASK_HPP