1 /*
2 * Copyright (c) 1999, 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_COMPILEBROKER_HPP
26 #define SHARE_COMPILER_COMPILEBROKER_HPP
27
28 #include "ci/compilerInterface.hpp"
29 #include "compiler/abstractCompiler.hpp"
30 #include "compiler/compilerDirectives.hpp"
31 #include "compiler/compilerThread.hpp"
32 #include "compiler/compileTask.hpp"
33 #include "runtime/atomicAccess.hpp"
34 #include "runtime/perfDataTypes.hpp"
35 #include "utilities/stack.hpp"
36 #if INCLUDE_JVMCI
37 #include "jvmci/jvmciCompiler.hpp"
38 #endif
39
40 class nmethod;
41
42 #if defined(ASSERT) && COMPILER2_OR_JVMCI
43 // Stress testing. Dedicated threads revert optimizations based on escape analysis concurrently to
44 // the running java application. Configured with vm options DeoptimizeObjectsALot*.
45 class DeoptimizeObjectsALotThread : public JavaThread {
46
47 static void deopt_objs_alot_thread_entry(JavaThread* thread, TRAPS);
48 void deoptimize_objects_alot_loop_single();
49 void deoptimize_objects_alot_loop_all();
50
51 public:
52 DeoptimizeObjectsALotThread() : JavaThread(&deopt_objs_alot_thread_entry) { }
53
54 bool is_hidden_from_external_view() const { return true; }
55 };
56 #endif
57
58 // CompilerCounters
59 //
60 // Per Compiler Performance Counters.
61 //
62 class CompilerCounters : public CHeapObj<mtCompiler> {
63
64 public:
65 enum {
66 cmname_buffer_length = 160
67 };
68
69 private:
70
71 char _current_method[cmname_buffer_length];
72 int _compile_type;
73
74 public:
75 CompilerCounters();
76
77 // these methods should be called in a thread safe context
78
79 void set_current_method(const char* method) {
80 strncpy(_current_method, method, (size_t)cmname_buffer_length-1);
81 _current_method[cmname_buffer_length-1] = '\0';
82 }
83
84 char* current_method() { return _current_method; }
85
86 void set_compile_type(int compile_type) {
87 _compile_type = compile_type;
88 }
89
90 int compile_type() { return _compile_type; }
91
92 };
93
94 // CompileQueue
95 //
96 // A list of CompileTasks.
97 class CompileQueue : public CHeapObj<mtCompiler> {
98 private:
99 const char* _name;
100
101 CompileTask* _first;
102 CompileTask* _last;
103
104 CompileTask* _first_stale;
105
106 volatile int _size;
107 int _peak_size;
108 uint _total_added;
109 uint _total_removed;
110
111 void purge_stale_tasks();
112 public:
113 CompileQueue(const char* name) {
114 _name = name;
115 _first = nullptr;
116 _last = nullptr;
117 _size = 0;
118 _total_added = 0;
119 _total_removed = 0;
120 _peak_size = 0;
121 _first_stale = nullptr;
122 }
123
124 const char* name() const { return _name; }
125
126 void add(CompileTask* task);
127 void remove(CompileTask* task);
128 void remove_and_mark_stale(CompileTask* task);
129 CompileTask* first() { return _first; }
130 CompileTask* last() { return _last; }
131
132 CompileTask* get(CompilerThread* thread);
133
134 bool is_empty() const { return _first == nullptr; }
135 int size() const { return _size; }
136
137 int get_peak_size() const { return _peak_size; }
138 uint get_total_added() const { return _total_added; }
139 uint get_total_removed() const { return _total_removed; }
140
141 // Redefine Classes support
142 void mark_on_stack();
143 void delete_all();
144 void print_tty();
145 void print(outputStream* st = tty);
146
147 ~CompileQueue() {
148 assert (is_empty(), " Compile Queue must be empty");
149 }
150 };
151
152 // CompileTaskWrapper
153 //
154 // Assign this task to the current thread. Deallocate the task
155 // when the compilation is complete.
156 class CompileTaskWrapper : StackObj {
157 public:
158 CompileTaskWrapper(CompileTask* task);
159 ~CompileTaskWrapper();
160 };
161
162 // Compilation
163 //
164 // The broker for all compilation requests.
165 class CompileBroker: AllStatic {
166 friend class Threads;
167 friend class CompileTaskWrapper;
168
169 public:
170 enum {
171 name_buffer_length = 100
172 };
173
174 // Compile type Information for print_last_compile() and CompilerCounters
175 enum { no_compile, normal_compile, osr_compile, native_compile };
176 static int assign_compile_id (const methodHandle& method, int osr_bci);
177
178
179 private:
180 static bool _initialized;
181 static volatile bool _should_block;
182
183 // This flag can be used to stop compilation or turn it back on
184 static volatile jint _should_compile_new_jobs;
185
186 // The installed compiler(s)
187 static AbstractCompiler* _compilers[2];
188
189 // The maximum numbers of compiler threads to be determined during startup.
190 static int _c1_count, _c2_count;
191
192 // An array of compiler thread Java objects
193 static jobject *_compiler1_objects, *_compiler2_objects;
194
195 // An array of compiler logs
196 static CompileLog **_compiler1_logs, **_compiler2_logs;
197
198 // These counters are used for assigning id's to each compilation
199 static volatile jint _compilation_id;
200 static volatile jint _osr_compilation_id;
201 static volatile jint _native_compilation_id;
202
203 static CompileQueue* _c2_compile_queue;
204 static CompileQueue* _c1_compile_queue;
205
206 // performance counters
207 static PerfCounter* _perf_total_compilation;
208 static PerfCounter* _perf_osr_compilation;
209 static PerfCounter* _perf_standard_compilation;
210
211 static PerfCounter* _perf_total_bailout_count;
212 static PerfCounter* _perf_total_invalidated_count;
213 static PerfCounter* _perf_total_compile_count;
214 static PerfCounter* _perf_total_osr_compile_count;
215 static PerfCounter* _perf_total_standard_compile_count;
216
217 static PerfCounter* _perf_sum_osr_bytes_compiled;
218 static PerfCounter* _perf_sum_standard_bytes_compiled;
219 static PerfCounter* _perf_sum_nmethod_size;
220 static PerfCounter* _perf_sum_nmethod_code_size;
221
222 static PerfStringVariable* _perf_last_method;
223 static PerfStringVariable* _perf_last_failed_method;
224 static PerfStringVariable* _perf_last_invalidated_method;
225 static PerfVariable* _perf_last_compile_type;
226 static PerfVariable* _perf_last_compile_size;
227 static PerfVariable* _perf_last_failed_type;
228 static PerfVariable* _perf_last_invalidated_type;
229
230 // Timers and counters for generating statistics
231 static elapsedTimer _t_total_compilation;
232 static elapsedTimer _t_osr_compilation;
233 static elapsedTimer _t_standard_compilation;
234 static elapsedTimer _t_invalidated_compilation;
235 static elapsedTimer _t_bailedout_compilation;
236
237 static uint _total_compile_count;
238 static uint _total_bailout_count;
239 static uint _total_invalidated_count;
240 static uint _total_native_compile_count;
241 static uint _total_osr_compile_count;
242 static uint _total_standard_compile_count;
243 static uint _total_compiler_stopped_count;
244 static uint _total_compiler_restarted_count;
245 static uint _sum_osr_bytes_compiled;
246 static uint _sum_standard_bytes_compiled;
247 static uint _sum_nmethod_size;
248 static uint _sum_nmethod_code_size;
249 static uint _largest_nmethod_code_size;
250 static jlong _peak_compilation_time;
251
252 static CompilerStatistics _stats_per_level[];
253
254 static volatile int _print_compilation_warning;
255
256 enum ThreadType {
257 compiler_t,
258 deoptimizer_t,
259 training_replay_t
260 };
261
262 static JavaThread* make_thread(ThreadType type, jobject thread_oop, CompileQueue* queue, AbstractCompiler* comp, JavaThread* THREAD);
263 static void init_compiler_threads();
264 static void init_training_replay();
265 static void possibly_add_compiler_threads(JavaThread* THREAD);
266 static bool compilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level, bool excluded);
267
268 static CompileTask* create_compile_task(CompileQueue* queue,
269 int compile_id,
270 const methodHandle& method,
271 int osr_bci,
272 int comp_level,
273 int hot_count,
274 CompileTask::CompileReason compile_reason,
275 bool blocking);
276 static void wait_for_completion(CompileTask* task);
277 #if INCLUDE_JVMCI
278 static bool wait_for_jvmci_completion(JVMCICompiler* comp, CompileTask* task, JavaThread* thread);
279 #endif
280
281 static void free_buffer_blob_if_allocated(CompilerThread* thread);
282
283 static void invoke_compiler_on_method(CompileTask* task);
284 static void handle_compile_error(CompilerThread* thread, CompileTask* task, ciEnv* ci_env,
285 int compilable, const char* failure_reason);
286 static void update_compile_perf_data(CompilerThread *thread, const methodHandle& method, bool is_osr);
287
288 static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
289
290 static void compile_method_base(const methodHandle& method,
291 int osr_bci,
292 int comp_level,
293 int hot_count,
294 CompileTask::CompileReason compile_reason,
295 bool blocking,
296 Thread* thread);
297
298 static CompileQueue* compile_queue(int comp_level);
299 static bool init_compiler_runtime();
300 static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread);
301
302 public:
303 enum {
304 // The entry bci used for non-OSR compilations.
305 standard_entry_bci = InvocationEntryBci
306 };
307
308 static AbstractCompiler* compiler(int comp_level) {
309 if (is_c2_compile(comp_level)) return _compilers[1]; // C2
310 if (is_c1_compile(comp_level)) return _compilers[0]; // C1
311 return nullptr;
312 }
313
314 static bool compilation_is_complete(const methodHandle& method, int osr_bci, int comp_level);
315 static bool compilation_is_in_queue(const methodHandle& method);
316 static void print_compile_queues(outputStream* st);
317 static int queue_size(int comp_level) {
318 CompileQueue *q = compile_queue(comp_level);
319 return q != nullptr ? q->size() : 0;
320 }
321 static void compilation_init(JavaThread* THREAD);
322 static void init_compiler_thread_log();
323 static nmethod* compile_method(const methodHandle& method,
324 int osr_bci,
325 int comp_level,
326 int hot_count,
327 CompileTask::CompileReason compile_reason,
328 TRAPS);
329 static CompileQueue* c1_compile_queue();
330 static CompileQueue* c2_compile_queue();
331
332 private:
333 static nmethod* compile_method(const methodHandle& method,
334 int osr_bci,
335 int comp_level,
336 int hot_count,
337 CompileTask::CompileReason compile_reason,
338 DirectiveSet* directive,
339 TRAPS);
340
341 public:
342 // Acquire any needed locks and assign a compile id
343 static int assign_compile_id_unlocked(Thread* thread, const methodHandle& method, int osr_bci);
344
345 static void compiler_thread_loop();
346 static int get_compilation_id() { return _compilation_id; }
347
348 // Set _should_block.
349 // Call this from the VM, with Threads_lock held and a safepoint requested.
350 static void set_should_block();
351
352 // Call this from the compiler at convenient points, to poll for _should_block.
353 static void maybe_block();
354
355 enum CompilerActivity {
356 // Flags for toggling compiler activity
357 stop_compilation = 0,
358 run_compilation = 1,
359 shutdown_compilation = 2
360 };
361
362 static inline jint get_compilation_activity_mode() { return _should_compile_new_jobs; }
363 static inline bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
364 static bool set_should_compile_new_jobs(jint new_state) {
365 // Return success if the current caller set it
366 jint old = AtomicAccess::cmpxchg(&_should_compile_new_jobs, 1-new_state, new_state);
367 bool success = (old == (1-new_state));
368 if (success) {
369 if (new_state == run_compilation) {
370 _total_compiler_restarted_count++;
371 } else {
372 _total_compiler_stopped_count++;
373 }
374 }
375 return success;
376 }
377
378 static void disable_compilation_forever() {
379 UseCompiler = false;
380 AlwaysCompileLoopMethods = false;
381 AtomicAccess::xchg(&_should_compile_new_jobs, jint(shutdown_compilation));
382 }
383
384 static bool is_compilation_disabled_forever() {
385 return AtomicAccess::load(&_should_compile_new_jobs) == shutdown_compilation;
386 }
387
388 static void wait_for_no_active_tasks();
389
390 static void handle_full_code_cache(CodeBlobType code_blob_type);
391 // Ensures that warning is only printed once.
392 static bool should_print_compiler_warning() {
393 jint old = AtomicAccess::cmpxchg(&_print_compilation_warning, 0, 1);
394 return old == 0;
395 }
396 // Return total compilation ticks
397 static jlong total_compilation_ticks();
398
399 // Redefine Classes support
400 static void mark_on_stack();
401
402 // Print current compilation time stats for a given compiler
403 static void print_times(const char* name, CompilerStatistics* stats);
404
405 // Print a detailed accounting of compilation time
406 static void print_times(bool per_compiler = true, bool aggregate = true);
407
408 // compiler name for debugging
409 static const char* compiler_name(int comp_level);
410
411 // Provide access to compiler thread Java objects
412 static jobject compiler1_object(int idx) {
413 assert(_compiler1_objects != nullptr, "must be initialized");
414 assert(idx < _c1_count, "oob");
415 return _compiler1_objects[idx];
416 }
417
418 static jobject compiler2_object(int idx) {
419 assert(_compiler2_objects != nullptr, "must be initialized");
420 assert(idx < _c2_count, "oob");
421 return _compiler2_objects[idx];
422 }
423
424 static AbstractCompiler* compiler1() { return _compilers[0]; }
425 static AbstractCompiler* compiler2() { return _compilers[1]; }
426
427 static bool can_remove(CompilerThread *ct, bool do_it);
428
429 static CompileLog* get_log(CompilerThread* ct);
430
431 static int get_c1_thread_count() { return _compilers[0]->num_compiler_threads(); }
432 static int get_c2_thread_count() { return _compilers[1]->num_compiler_threads(); }
433 static int get_total_compile_count() { return _total_compile_count; }
434 static int get_total_bailout_count() { return _total_bailout_count; }
435 static int get_total_invalidated_count() { return _total_invalidated_count; }
436 static int get_total_native_compile_count() { return _total_native_compile_count; }
437 static int get_total_osr_compile_count() { return _total_osr_compile_count; }
438 static int get_total_standard_compile_count() { return _total_standard_compile_count; }
439 static int get_total_compiler_stopped_count() { return _total_compiler_stopped_count; }
440 static int get_total_compiler_restarted_count() { return _total_compiler_restarted_count; }
441 static int get_sum_osr_bytes_compiled() { return _sum_osr_bytes_compiled; }
442 static int get_sum_standard_bytes_compiled() { return _sum_standard_bytes_compiled; }
443 static int get_sum_nmethod_size() { return _sum_nmethod_size;}
444 static int get_sum_nmethod_code_size() { return _sum_nmethod_code_size; }
445 static jlong get_peak_compilation_time() { return _peak_compilation_time; }
446 static jlong get_total_compilation_time() { return _t_total_compilation.milliseconds(); }
447
448 // Log that compilation profiling is skipped because metaspace is full.
449 static void log_metaspace_failure();
450
451 // CodeHeap State Analytics.
452 static void print_info(outputStream *out);
453 static void print_heapinfo(outputStream *out, const char* function, size_t granularity);
454 };
455
456 // In order to achiveve a maximally fast warmup we attempt to compile important methods as soon as all
457 // the classes that they depend on are initialized. TrainingReplayThread processes a queue of InstanceKlass*
458 // that have just finished running their static initializers. We find all the methods that depend on the given class
459 // and for which the number of remaining dependencies is now zero, and eagerly compile them.
460 class TrainingReplayThread : public JavaThread {
461 static void training_replay_thread_entry(JavaThread* thread, TRAPS);
462 public:
463 TrainingReplayThread() : JavaThread(&training_replay_thread_entry) { }
464
465 bool is_hidden_from_external_view() const { return true; }
466 };
467
468 #endif // SHARE_COMPILER_COMPILEBROKER_HPP