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