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