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