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