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 "compiler/compileTask.hpp" 31 #include "runtime/perfData.hpp" 32 #include "utilities/stack.hpp" 33 34 class nmethod; 35 class nmethodLocker; 36 37 // CompilerCounters 38 // 39 // Per Compiler Performance Counters. 40 // 41 class CompilerCounters : public CHeapObj<mtCompiler> { 42 43 public: 44 enum { 45 cmname_buffer_length = 160 46 }; 47 48 private: 49 50 char _current_method[cmname_buffer_length]; 51 PerfStringVariable* _perf_current_method; 52 53 int _compile_type; 54 PerfVariable* _perf_compile_type; 55 56 PerfCounter* _perf_time; 57 PerfCounter* _perf_compiles; 58 59 public: 60 CompilerCounters(const char* name, int instance, TRAPS); 61 62 // these methods should be called in a thread safe context 63 64 void set_current_method(const char* method) { 65 strncpy(_current_method, method, (size_t)cmname_buffer_length-1); 66 _current_method[cmname_buffer_length-1] = '\0'; 67 if (UsePerfData) _perf_current_method->set_value(method); 68 } 69 70 char* current_method() { return _current_method; } 71 72 void set_compile_type(int compile_type) { 73 _compile_type = compile_type; 74 if (UsePerfData) _perf_compile_type->set_value((jlong)compile_type); 75 } 76 77 int compile_type() { return _compile_type; } 78 79 PerfCounter* time_counter() { return _perf_time; } 80 PerfCounter* compile_counter() { return _perf_compiles; } 81 }; 82 83 // CompileQueue 84 // 85 // A list of CompileTasks. 86 class CompileQueue : public CHeapObj<mtCompiler> { 87 private: 88 const char* _name; 89 90 CompileTask* _first; 91 CompileTask* _last; 92 93 CompileTask* _first_stale; 94 95 int _size; 96 97 void purge_stale_tasks(); 98 public: 99 CompileQueue(const char* name) { 100 _name = name; 101 _first = NULL; 102 _last = NULL; 103 _size = 0; 104 _first_stale = NULL; 105 } 106 107 const char* name() const { return _name; } 108 109 void add(CompileTask* task); 110 void remove(CompileTask* task); 111 void remove_and_mark_stale(CompileTask* task); 112 CompileTask* first() { return _first; } 113 CompileTask* last() { return _last; } 114 115 CompileTask* get(); 116 117 bool is_empty() const { return _first == NULL; } 118 int size() const { return _size; } 119 120 121 // Redefine Classes support 122 void mark_on_stack(); 123 void free_all(); 124 void print_tty(); 125 void print(outputStream* st = tty); 126 127 ~CompileQueue() { 128 assert (is_empty(), " Compile Queue must be empty"); 129 } 130 }; 131 132 // CompileTaskWrapper 133 // 134 // Assign this task to the current thread. Deallocate the task 135 // when the compilation is complete. 136 class CompileTaskWrapper : StackObj { 137 public: 138 CompileTaskWrapper(CompileTask* task); 139 ~CompileTaskWrapper(); 140 }; 141 142 class DirectivesStack : public CHeapObj<mtCompiler> { 143 private: 144 CompilerDirectives* _top; 145 CompilerDirectives* _bottom; 146 int _depth; 147 148 void pop_inner(); // no lock version of pop 149 public: 150 DirectivesStack(); 151 DirectiveSet* peak(AbstractCompiler* comp); 152 void push(CompilerDirectives* directive); 153 void pop(); 154 void clear(); 155 DirectiveSet* getMatchingDirective(methodHandle mh, int comp_level); 156 void print(outputStream* st); 157 }; 158 159 // Compilation 160 // 161 // The broker for all compilation requests. 162 class CompileBroker: AllStatic { 163 friend class Threads; 164 friend class CompileTaskWrapper; 165 166 public: 167 enum { 168 name_buffer_length = 100 169 }; 170 171 // Compile type Information for print_last_compile() and CompilerCounters 172 enum { no_compile, normal_compile, osr_compile, native_compile }; 173 static int assign_compile_id (methodHandle method, int osr_bci); 174 175 176 private: 177 static bool _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[2]; 185 186 // The directives stack 187 static DirectivesStack* _dirstack; 188 189 // These counters are used for assigning id's to each compilation 190 static volatile jint _compilation_id; 191 static volatile jint _osr_compilation_id; 192 193 static int _last_compile_type; 194 static int _last_compile_level; 195 static char _last_method_compiled[name_buffer_length]; 196 197 static CompileQueue* _c2_compile_queue; 198 static CompileQueue* _c1_compile_queue; 199 200 // performance counters 201 static PerfCounter* _perf_total_compilation; 202 static PerfCounter* _perf_native_compilation; 203 static PerfCounter* _perf_osr_compilation; 204 static PerfCounter* _perf_standard_compilation; 205 206 static PerfCounter* _perf_total_bailout_count; 207 static PerfCounter* _perf_total_invalidated_count; 208 static PerfCounter* _perf_total_compile_count; 209 static PerfCounter* _perf_total_native_compile_count; 210 static PerfCounter* _perf_total_osr_compile_count; 211 static PerfCounter* _perf_total_standard_compile_count; 212 213 static PerfCounter* _perf_sum_osr_bytes_compiled; 214 static PerfCounter* _perf_sum_standard_bytes_compiled; 215 static PerfCounter* _perf_sum_nmethod_size; 216 static PerfCounter* _perf_sum_nmethod_code_size; 217 218 static PerfStringVariable* _perf_last_method; 219 static PerfStringVariable* _perf_last_failed_method; 220 static PerfStringVariable* _perf_last_invalidated_method; 221 static PerfVariable* _perf_last_compile_type; 222 static PerfVariable* _perf_last_compile_size; 223 static PerfVariable* _perf_last_failed_type; 224 static PerfVariable* _perf_last_invalidated_type; 225 226 // Timers and counters for generating statistics 227 static elapsedTimer _t_total_compilation; 228 static elapsedTimer _t_osr_compilation; 229 static elapsedTimer _t_standard_compilation; 230 static elapsedTimer _t_invalidated_compilation; 231 static elapsedTimer _t_bailedout_compilation; 232 233 static int _total_compile_count; 234 static int _total_bailout_count; 235 static int _total_invalidated_count; 236 static int _total_native_compile_count; 237 static int _total_osr_compile_count; 238 static int _total_standard_compile_count; 239 static int _sum_osr_bytes_compiled; 240 static int _sum_standard_bytes_compiled; 241 static int _sum_nmethod_size; 242 static int _sum_nmethod_code_size; 243 static long _peak_compilation_time; 244 245 static volatile jint _print_compilation_warning; 246 247 static JavaThread* make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, AbstractCompiler* comp, bool compiler_thread, TRAPS); 248 static void init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count); 249 static bool compilation_is_complete (methodHandle method, int osr_bci, int comp_level); 250 static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level); 251 static bool is_compile_blocking(); 252 static void preload_classes (methodHandle method, TRAPS); 253 254 static CompileTask* create_compile_task(CompileQueue* queue, 255 int compile_id, 256 methodHandle method, 257 int osr_bci, 258 int comp_level, 259 methodHandle hot_method, 260 int hot_count, 261 const char* comment, 262 bool blocking); 263 static void wait_for_completion(CompileTask* task); 264 265 static void invoke_compiler_on_method(CompileTask* task); 266 static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level); 267 static void push_jni_handle_block(); 268 static void pop_jni_handle_block(); 269 static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task); 270 271 static void compile_method_base(methodHandle method, 272 int osr_bci, 273 int comp_level, 274 methodHandle hot_method, 275 int hot_count, 276 const char* comment, 277 Thread* thread); 278 279 static CompileQueue* compile_queue(int comp_level); 280 static bool init_compiler_runtime(); 281 static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread); 282 283 public: 284 285 static DirectivesStack* dirstack(); 286 static void set_dirstack(DirectivesStack* stack); 287 288 enum { 289 // The entry bci used for non-OSR compilations. 290 standard_entry_bci = InvocationEntryBci 291 }; 292 293 static AbstractCompiler* compiler(int comp_level) { 294 if (is_c2_compile(comp_level)) return _compilers[1]; // C2 295 if (is_c1_compile(comp_level)) return _compilers[0]; // C1 296 return NULL; 297 } 298 299 static bool compilation_is_in_queue(methodHandle method); 300 static void print_compile_queues(outputStream* st); 301 static void print_directives(outputStream* st); 302 static int queue_size(int comp_level) { 303 CompileQueue *q = compile_queue(comp_level); 304 return q != NULL ? q->size() : 0; 305 } 306 static void compilation_init(); 307 static void init_compiler_thread_log(); 308 static nmethod* compile_method(methodHandle method, 309 int osr_bci, 310 int comp_level, 311 methodHandle hot_method, 312 int hot_count, 313 const char* comment, Thread* thread); 314 315 static void compiler_thread_loop(); 316 static uint get_compilation_id() { return _compilation_id; } 317 318 // Set _should_block. 319 // Call this from the VM, with Threads_lock held and a safepoint requested. 320 static void set_should_block(); 321 322 // Call this from the compiler at convenient points, to poll for _should_block. 323 static void maybe_block(); 324 325 enum { 326 // Flags for toggling compiler activity 327 stop_compilation = 0, 328 run_compilation = 1, 329 shutdown_compilaton = 2 330 }; 331 332 static jint get_compilation_activity_mode() { return _should_compile_new_jobs; } 333 static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); } 334 static bool set_should_compile_new_jobs(jint new_state) { 335 // Return success if the current caller set it 336 jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state); 337 return (old == (1-new_state)); 338 } 339 340 static void disable_compilation_forever() { 341 UseCompiler = false; 342 AlwaysCompileLoopMethods = false; 343 Atomic::xchg(shutdown_compilaton, &_should_compile_new_jobs); 344 } 345 346 static bool is_compilation_disabled_forever() { 347 return _should_compile_new_jobs == shutdown_compilaton; 348 } 349 static void handle_full_code_cache(int code_blob_type); 350 // Ensures that warning is only printed once. 351 static bool should_print_compiler_warning() { 352 jint old = Atomic::cmpxchg(1, &_print_compilation_warning, 0); 353 return old == 0; 354 } 355 // Return total compilation ticks 356 static jlong total_compilation_ticks() { 357 return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0; 358 } 359 360 // Redefine Classes support 361 static void mark_on_stack(); 362 363 // Print a detailed accounting of compilation time 364 static void print_times(); 365 366 // Debugging output for failure 367 static void print_last_compile(); 368 369 static void print_compiler_threads_on(outputStream* st); 370 371 // compiler name for debugging 372 static const char* compiler_name(int comp_level); 373 374 static int get_total_compile_count() { return _total_compile_count; } 375 static int get_total_bailout_count() { return _total_bailout_count; } 376 static int get_total_invalidated_count() { return _total_invalidated_count; } 377 static int get_total_native_compile_count() { return _total_native_compile_count; } 378 static int get_total_osr_compile_count() { return _total_osr_compile_count; } 379 static int get_total_standard_compile_count() { return _total_standard_compile_count; } 380 static int get_sum_osr_bytes_compiled() { return _sum_osr_bytes_compiled; } 381 static int get_sum_standard_bytes_compiled() { return _sum_standard_bytes_compiled; } 382 static int get_sum_nmethod_size() { return _sum_nmethod_size;} 383 static int get_sum_nmethod_code_size() { return _sum_nmethod_code_size; } 384 static long get_peak_compilation_time() { return _peak_compilation_time; } 385 static long get_total_compilation_time() { return _t_total_compilation.milliseconds(); } 386 387 // Log that compilation profiling is skipped because metaspace is full. 388 static void log_metaspace_failure(); 389 }; 390 391 #endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP