1 /* 2 * Copyright (c) 1999, 2015, 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_VM_COMPILER_COMPILEBROKER_HPP 26 #define SHARE_VM_COMPILER_COMPILEBROKER_HPP 27 28 #include "ci/compilerInterface.hpp" 29 #include "compiler/abstractCompiler.hpp" 30 #include "runtime/perfData.hpp" 31 #include "trace/tracing.hpp" 32 33 class nmethod; 34 class nmethodLocker; 35 36 // CompileTask 37 // 38 // An entry in the compile queue. It represents a pending or current 39 // compilation. 40 class CompileTask : public CHeapObj<mtCompiler> { 41 friend class VMStructs; 42 43 private: 44 static CompileTask* _task_free_list; 45 #ifdef ASSERT 46 static int _num_allocated_tasks; 47 #endif 48 49 Monitor* _lock; 50 uint _compile_id; 51 Method* _method; 52 jobject _method_holder; 53 int _osr_bci; 54 bool _is_complete; 55 bool _is_success; 56 bool _is_blocking; 57 int _comp_level; 58 int _num_inlined_bytecodes; 59 nmethodLocker* _code_handle; // holder of eventual result 60 CompileTask* _next, *_prev; 61 bool _is_free; 62 // Fields used for logging why the compilation was initiated: 63 jlong _time_queued; // in units of os::elapsed_counter() 64 Method* _hot_method; // which method actually triggered this task 65 jobject _hot_method_holder; 66 int _hot_count; // information about its invocation counter 67 const char* _comment; // more info about the task 68 const char* _failure_reason; 69 70 public: 71 CompileTask() { 72 _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock"); 73 } 74 75 void initialize(int compile_id, methodHandle method, int osr_bci, int comp_level, 76 methodHandle hot_method, int hot_count, const char* comment, 77 bool is_blocking); 78 79 static CompileTask* allocate(); 80 static void free(CompileTask* task); 81 82 int compile_id() const { return _compile_id; } 83 Method* method() const { return _method; } 84 Method* hot_method() const { return _hot_method; } 85 int osr_bci() const { return _osr_bci; } 86 bool is_complete() const { return _is_complete; } 87 bool is_blocking() const { return _is_blocking; } 88 bool is_success() const { return _is_success; } 89 90 nmethodLocker* code_handle() const { return _code_handle; } 91 void set_code_handle(nmethodLocker* l) { _code_handle = l; } 92 nmethod* code() const; // _code_handle->code() 93 void set_code(nmethod* nm); // _code_handle->set_code(nm) 94 95 Monitor* lock() const { return _lock; } 96 97 void mark_complete() { _is_complete = true; } 98 void mark_success() { _is_success = true; } 99 100 int comp_level() { return _comp_level;} 101 void set_comp_level(int comp_level) { _comp_level = comp_level;} 102 103 int num_inlined_bytecodes() const { return _num_inlined_bytecodes; } 104 void set_num_inlined_bytecodes(int n) { _num_inlined_bytecodes = n; } 105 106 CompileTask* next() const { return _next; } 107 void set_next(CompileTask* next) { _next = next; } 108 CompileTask* prev() const { return _prev; } 109 void set_prev(CompileTask* prev) { _prev = prev; } 110 bool is_free() const { return _is_free; } 111 void set_is_free(bool val) { _is_free = val; } 112 113 // RedefineClasses support 114 void metadata_do(void f(Metadata*)); 115 116 private: 117 static void print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level, 118 bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false, 119 const char* msg = NULL, bool short_form = false, bool cr = true); 120 121 public: 122 void print_compilation(outputStream* st = tty, const char* msg = NULL, bool short_form = false, bool cr = true); 123 static void print_compilation(outputStream* st, const nmethod* nm, const char* msg = NULL, bool short_form = false, bool cr = true) { 124 print_compilation_impl(st, nm->method(), nm->compile_id(), nm->comp_level(), 125 nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false, 126 msg, short_form, cr); 127 } 128 129 static void print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL); 130 static void print_inlining(ciMethod* method, int inline_level, int bci, const char* msg = NULL) { 131 print_inlining(tty, method, inline_level, bci, msg); 132 } 133 134 // Redefine Classes support 135 void mark_on_stack(); 136 137 static void print_inline_indent(int inline_level, outputStream* st = tty); 138 139 void print_tty(); 140 void print_line_on_error(outputStream* st, char* buf, int buflen); 141 142 void log_task(xmlStream* log); 143 void log_task_queued(); 144 void log_task_dequeued(const char* comment); 145 void log_task_start(CompileLog* log); 146 void log_task_done(CompileLog* log); 147 148 void set_failure_reason(const char* reason) { 149 _failure_reason = reason; 150 } 151 }; 152 153 // CompilerCounters 154 // 155 // Per Compiler Performance Counters. 156 // 157 class CompilerCounters : public CHeapObj<mtCompiler> { 158 159 public: 160 enum { 161 cmname_buffer_length = 160 162 }; 163 164 private: 165 166 char _current_method[cmname_buffer_length]; 167 PerfStringVariable* _perf_current_method; 168 169 int _compile_type; 170 PerfVariable* _perf_compile_type; 171 172 PerfCounter* _perf_time; 173 PerfCounter* _perf_compiles; 174 175 public: 176 CompilerCounters(const char* name, int instance, TRAPS); 177 178 // these methods should be called in a thread safe context 179 180 void set_current_method(const char* method) { 181 strncpy(_current_method, method, (size_t)cmname_buffer_length-1); 182 _current_method[cmname_buffer_length-1] = '\0'; 183 if (UsePerfData) _perf_current_method->set_value(method); 184 } 185 186 char* current_method() { return _current_method; } 187 188 void set_compile_type(int compile_type) { 189 _compile_type = compile_type; 190 if (UsePerfData) _perf_compile_type->set_value((jlong)compile_type); 191 } 192 193 int compile_type() { return _compile_type; } 194 195 PerfCounter* time_counter() { return _perf_time; } 196 PerfCounter* compile_counter() { return _perf_compiles; } 197 }; 198 199 // CompileQueue 200 // 201 // A list of CompileTasks. 202 class CompileQueue : public CHeapObj<mtCompiler> { 203 private: 204 const char* _name; 205 206 CompileTask* _first; 207 CompileTask* _last; 208 209 CompileTask* _first_stale; 210 211 int _size; 212 213 void purge_stale_tasks(); 214 public: 215 CompileQueue(const char* name) { 216 _name = name; 217 _first = NULL; 218 _last = NULL; 219 _size = 0; 220 _first_stale = NULL; 221 } 222 223 const char* name() const { return _name; } 224 225 void add(CompileTask* task); 226 void remove(CompileTask* task); 227 void remove_and_mark_stale(CompileTask* task); 228 CompileTask* first() { return _first; } 229 CompileTask* last() { return _last; } 230 231 CompileTask* get(); 232 233 bool is_empty() const { return _first == NULL; } 234 int size() const { return _size; } 235 236 237 // Redefine Classes support 238 void mark_on_stack(); 239 void free_all(); 240 void print_tty(); 241 void print(outputStream* st = tty); 242 243 ~CompileQueue() { 244 assert (is_empty(), " Compile Queue must be empty"); 245 } 246 }; 247 248 // CompileTaskWrapper 249 // 250 // Assign this task to the current thread. Deallocate the task 251 // when the compilation is complete. 252 class CompileTaskWrapper : StackObj { 253 public: 254 CompileTaskWrapper(CompileTask* task); 255 ~CompileTaskWrapper(); 256 }; 257 258 259 // Compilation 260 // 261 // The broker for all compilation requests. 262 class CompileBroker: AllStatic { 263 friend class Threads; 264 friend class CompileTaskWrapper; 265 266 public: 267 enum { 268 name_buffer_length = 100 269 }; 270 271 // Compile type Information for print_last_compile() and CompilerCounters 272 enum { no_compile, normal_compile, osr_compile, native_compile }; 273 static int assign_compile_id (methodHandle method, int osr_bci); 274 275 276 private: 277 static bool _initialized; 278 static volatile bool _should_block; 279 280 // This flag can be used to stop compilation or turn it back on 281 static volatile jint _should_compile_new_jobs; 282 283 // The installed compiler(s) 284 static AbstractCompiler* _compilers[2]; 285 286 // These counters are used for assigning id's to each compilation 287 static volatile jint _compilation_id; 288 static volatile jint _osr_compilation_id; 289 290 static int _last_compile_type; 291 static int _last_compile_level; 292 static char _last_method_compiled[name_buffer_length]; 293 294 static CompileQueue* _c2_compile_queue; 295 static CompileQueue* _c1_compile_queue; 296 297 // performance counters 298 static PerfCounter* _perf_total_compilation; 299 static PerfCounter* _perf_native_compilation; 300 static PerfCounter* _perf_osr_compilation; 301 static PerfCounter* _perf_standard_compilation; 302 303 static PerfCounter* _perf_total_bailout_count; 304 static PerfCounter* _perf_total_invalidated_count; 305 static PerfCounter* _perf_total_compile_count; 306 static PerfCounter* _perf_total_native_compile_count; 307 static PerfCounter* _perf_total_osr_compile_count; 308 static PerfCounter* _perf_total_standard_compile_count; 309 310 static PerfCounter* _perf_sum_osr_bytes_compiled; 311 static PerfCounter* _perf_sum_standard_bytes_compiled; 312 static PerfCounter* _perf_sum_nmethod_size; 313 static PerfCounter* _perf_sum_nmethod_code_size; 314 315 static PerfStringVariable* _perf_last_method; 316 static PerfStringVariable* _perf_last_failed_method; 317 static PerfStringVariable* _perf_last_invalidated_method; 318 static PerfVariable* _perf_last_compile_type; 319 static PerfVariable* _perf_last_compile_size; 320 static PerfVariable* _perf_last_failed_type; 321 static PerfVariable* _perf_last_invalidated_type; 322 323 // Timers and counters for generating statistics 324 static elapsedTimer _t_total_compilation; 325 static elapsedTimer _t_osr_compilation; 326 static elapsedTimer _t_standard_compilation; 327 static elapsedTimer _t_invalidated_compilation; 328 static elapsedTimer _t_bailedout_compilation; 329 330 static int _total_compile_count; 331 static int _total_bailout_count; 332 static int _total_invalidated_count; 333 static int _total_native_compile_count; 334 static int _total_osr_compile_count; 335 static int _total_standard_compile_count; 336 static int _sum_osr_bytes_compiled; 337 static int _sum_standard_bytes_compiled; 338 static int _sum_nmethod_size; 339 static int _sum_nmethod_code_size; 340 static long _peak_compilation_time; 341 342 static volatile jint _print_compilation_warning; 343 344 static JavaThread* make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, AbstractCompiler* comp, bool compiler_thread, TRAPS); 345 static void init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count); 346 static bool compilation_is_complete (methodHandle method, int osr_bci, int comp_level); 347 static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level); 348 static bool is_compile_blocking(); 349 static void preload_classes (methodHandle method, TRAPS); 350 351 static CompileTask* create_compile_task(CompileQueue* queue, 352 int compile_id, 353 methodHandle method, 354 int osr_bci, 355 int comp_level, 356 methodHandle hot_method, 357 int hot_count, 358 const char* comment, 359 bool blocking); 360 static void wait_for_completion(CompileTask* task); 361 362 static void invoke_compiler_on_method(CompileTask* task); 363 static void post_compile(CompilerThread* thread, CompileTask* task, EventCompilation& event, bool success, ciEnv* ci_env); 364 static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level); 365 static void push_jni_handle_block(); 366 static void pop_jni_handle_block(); 367 static bool check_break_at(methodHandle method, int compile_id, bool is_osr); 368 static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task); 369 370 static void compile_method_base(methodHandle method, 371 int osr_bci, 372 int comp_level, 373 methodHandle hot_method, 374 int hot_count, 375 const char* comment, 376 Thread* thread); 377 378 static CompileQueue* compile_queue(int comp_level); 379 static bool init_compiler_runtime(); 380 static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread); 381 382 public: 383 enum { 384 // The entry bci used for non-OSR compilations. 385 standard_entry_bci = InvocationEntryBci 386 }; 387 388 static AbstractCompiler* compiler(int comp_level) { 389 if (is_c2_compile(comp_level)) return _compilers[1]; // C2 390 if (is_c1_compile(comp_level)) return _compilers[0]; // C1 391 return NULL; 392 } 393 394 static bool compilation_is_in_queue(methodHandle method); 395 static void print_compile_queues(outputStream* st); 396 static int queue_size(int comp_level) { 397 CompileQueue *q = compile_queue(comp_level); 398 return q != NULL ? q->size() : 0; 399 } 400 static void compilation_init(); 401 static void init_compiler_thread_log(); 402 static nmethod* compile_method(methodHandle method, 403 int osr_bci, 404 int comp_level, 405 methodHandle hot_method, 406 int hot_count, 407 const char* comment, Thread* thread); 408 409 // Acquire any needed locks and assign a compile id 410 static uint assign_compile_id_unlocked(Thread* thread, methodHandle method, int osr_bci); 411 412 static void compiler_thread_loop(); 413 static uint get_compilation_id() { return _compilation_id; } 414 415 // Set _should_block. 416 // Call this from the VM, with Threads_lock held and a safepoint requested. 417 static void set_should_block(); 418 419 // Call this from the compiler at convenient points, to poll for _should_block. 420 static void maybe_block(); 421 422 enum { 423 // Flags for toggling compiler activity 424 stop_compilation = 0, 425 run_compilation = 1, 426 shutdown_compilaton = 2 427 }; 428 429 static jint get_compilation_activity_mode() { return _should_compile_new_jobs; } 430 static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); } 431 static bool set_should_compile_new_jobs(jint new_state) { 432 // Return success if the current caller set it 433 jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state); 434 return (old == (1-new_state)); 435 } 436 437 static void disable_compilation_forever() { 438 UseCompiler = false; 439 AlwaysCompileLoopMethods = false; 440 Atomic::xchg(shutdown_compilaton, &_should_compile_new_jobs); 441 } 442 443 static bool is_compilation_disabled_forever() { 444 return _should_compile_new_jobs == shutdown_compilaton; 445 } 446 static void handle_full_code_cache(int code_blob_type); 447 // Ensures that warning is only printed once. 448 static bool should_print_compiler_warning() { 449 jint old = Atomic::cmpxchg(1, &_print_compilation_warning, 0); 450 return old == 0; 451 } 452 // Return total compilation ticks 453 static jlong total_compilation_ticks() { 454 return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0; 455 } 456 457 // Redefine Classes support 458 static void mark_on_stack(); 459 460 #if INCLUDE_JVMCI 461 // Print curent compilation time stats for a given compiler 462 static void print_times(AbstractCompiler* comp); 463 #endif 464 465 // Print a detailed accounting of compilation time 466 static void print_times(bool per_compiler = true, bool aggregate = true); 467 468 // Debugging output for failure 469 static void print_last_compile(); 470 471 static void print_compiler_threads_on(outputStream* st); 472 473 // compiler name for debugging 474 static const char* compiler_name(int comp_level); 475 476 static int get_total_compile_count() { return _total_compile_count; } 477 static int get_total_bailout_count() { return _total_bailout_count; } 478 static int get_total_invalidated_count() { return _total_invalidated_count; } 479 static int get_total_native_compile_count() { return _total_native_compile_count; } 480 static int get_total_osr_compile_count() { return _total_osr_compile_count; } 481 static int get_total_standard_compile_count() { return _total_standard_compile_count; } 482 static int get_sum_osr_bytes_compiled() { return _sum_osr_bytes_compiled; } 483 static int get_sum_standard_bytes_compiled() { return _sum_standard_bytes_compiled; } 484 static int get_sum_nmethod_size() { return _sum_nmethod_size;} 485 static int get_sum_nmethod_code_size() { return _sum_nmethod_code_size; } 486 static long get_peak_compilation_time() { return _peak_compilation_time; } 487 static long get_total_compilation_time() { return _t_total_compilation.milliseconds(); } 488 489 // Log that compilation profiling is skipped because metaspace is full. 490 static void log_metaspace_failure(); 491 }; 492 493 #endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP