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