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