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 : AllStatic { //public CHeapObj<mtCompiler> {
 143 private:
 144   static CompilerDirectives* _top;
 145   static CompilerDirectives* _bottom;
 146   static int _depth;
 147 
 148   static void pop_inner(); // no lock version of pop
 149 public:
 150   static void init();
 151   static DirectiveSet* getDefaultDirective(AbstractCompiler* comp);
 152   static void push(CompilerDirectives* directive);
 153   static void pop();
 154   static void clear();
 155   static DirectiveSet* getMatchingDirective(methodHandle mh, AbstractCompiler* comp);
 156   static void print(outputStream* st);
 157   static void release(DirectiveSet* set);
 158   static void release(CompilerDirectives* dir);
 159 };
 160 
 161 // Compilation
 162 //
 163 // The broker for all compilation requests.
 164 class CompileBroker: AllStatic {
 165  friend class Threads;
 166  friend class CompileTaskWrapper;
 167 
 168  public:
 169   enum {
 170     name_buffer_length = 100
 171   };
 172 
 173   // Compile type Information for print_last_compile() and CompilerCounters
 174   enum { no_compile, normal_compile, osr_compile, native_compile };
 175   static int assign_compile_id (methodHandle method, int osr_bci);
 176 
 177 
 178  private:
 179   static bool _initialized;
 180   static volatile bool _should_block;
 181 
 182   // This flag can be used to stop compilation or turn it back on
 183   static volatile jint _should_compile_new_jobs;
 184 
 185   // The installed compiler(s)
 186   static AbstractCompiler* _compilers[2];
 187 
 188   // These counters are used for assigning id's to each compilation
 189   static volatile jint _compilation_id;
 190   static volatile jint _osr_compilation_id;
 191 
 192   static int  _last_compile_type;
 193   static int  _last_compile_level;
 194   static char _last_method_compiled[name_buffer_length];
 195 
 196   static CompileQueue* _c2_compile_queue;
 197   static CompileQueue* _c1_compile_queue;
 198 
 199   // performance counters
 200   static PerfCounter* _perf_total_compilation;
 201   static PerfCounter* _perf_native_compilation;
 202   static PerfCounter* _perf_osr_compilation;
 203   static PerfCounter* _perf_standard_compilation;
 204 
 205   static PerfCounter* _perf_total_bailout_count;
 206   static PerfCounter* _perf_total_invalidated_count;
 207   static PerfCounter* _perf_total_compile_count;
 208   static PerfCounter* _perf_total_native_compile_count;
 209   static PerfCounter* _perf_total_osr_compile_count;
 210   static PerfCounter* _perf_total_standard_compile_count;
 211 
 212   static PerfCounter* _perf_sum_osr_bytes_compiled;
 213   static PerfCounter* _perf_sum_standard_bytes_compiled;
 214   static PerfCounter* _perf_sum_nmethod_size;
 215   static PerfCounter* _perf_sum_nmethod_code_size;
 216 
 217   static PerfStringVariable* _perf_last_method;
 218   static PerfStringVariable* _perf_last_failed_method;
 219   static PerfStringVariable* _perf_last_invalidated_method;
 220   static PerfVariable*       _perf_last_compile_type;
 221   static PerfVariable*       _perf_last_compile_size;
 222   static PerfVariable*       _perf_last_failed_type;
 223   static PerfVariable*       _perf_last_invalidated_type;
 224 
 225   // Timers and counters for generating statistics
 226   static elapsedTimer _t_total_compilation;
 227   static elapsedTimer _t_osr_compilation;
 228   static elapsedTimer _t_standard_compilation;
 229   static elapsedTimer _t_invalidated_compilation;
 230   static elapsedTimer _t_bailedout_compilation;
 231 
 232   static int _total_compile_count;
 233   static int _total_bailout_count;
 234   static int _total_invalidated_count;
 235   static int _total_native_compile_count;
 236   static int _total_osr_compile_count;
 237   static int _total_standard_compile_count;
 238   static int _sum_osr_bytes_compiled;
 239   static int _sum_standard_bytes_compiled;
 240   static int _sum_nmethod_size;
 241   static int _sum_nmethod_code_size;
 242   static long _peak_compilation_time;
 243 
 244   static volatile jint _print_compilation_warning;
 245 
 246   static JavaThread* make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, AbstractCompiler* comp, bool compiler_thread, TRAPS);
 247   static void init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count);
 248   static bool compilation_is_complete  (methodHandle method, int osr_bci, int comp_level);
 249   static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level);
 250   static bool is_compile_blocking();
 251   static void preload_classes          (methodHandle method, TRAPS);
 252 
 253   static CompileTask* create_compile_task(CompileQueue* queue,
 254                                           int           compile_id,
 255                                           methodHandle  method,
 256                                           int           osr_bci,
 257                                           int           comp_level,
 258                                           methodHandle  hot_method,
 259                                           int           hot_count,
 260                                           const char*   comment,
 261                                           bool          blocking);
 262   static void wait_for_completion(CompileTask* task);
 263 
 264   static void invoke_compiler_on_method(CompileTask* task);
 265   static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level);
 266   static void push_jni_handle_block();
 267   static void pop_jni_handle_block();
 268   static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
 269 
 270   static void compile_method_base(methodHandle method,
 271                                   int osr_bci,
 272                                   int comp_level,
 273                                   methodHandle hot_method,
 274                                   int hot_count,
 275                                   const char* comment,
 276                                   Thread* thread);
 277 
 278   static CompileQueue* compile_queue(int comp_level);
 279   static bool init_compiler_runtime();
 280   static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread);
 281 
 282 public:
 283 
 284   static DirectivesStack* dirstack();
 285   static void set_dirstack(DirectivesStack* stack);
 286 
 287   enum {
 288     // The entry bci used for non-OSR compilations.
 289     standard_entry_bci = InvocationEntryBci
 290   };
 291 
 292   static AbstractCompiler* compiler(int comp_level) {
 293     if (is_c2_compile(comp_level)) return _compilers[1]; // C2
 294     if (is_c1_compile(comp_level)) return _compilers[0]; // C1
 295     return NULL;
 296   }
 297 
 298   static bool compilation_is_in_queue(methodHandle method);
 299   static void print_compile_queues(outputStream* st);
 300   static void print_directives(outputStream* st);
 301   static int queue_size(int comp_level) {
 302     CompileQueue *q = compile_queue(comp_level);
 303     return q != NULL ? q->size() : 0;
 304   }
 305   static void compilation_init();
 306   static void init_compiler_thread_log();
 307   static nmethod* compile_method(methodHandle method,
 308                                  int osr_bci,
 309                                  int comp_level,
 310                                  methodHandle hot_method,
 311                                  int hot_count,
 312                                  const char* comment, Thread* thread);
 313 
 314   static void compiler_thread_loop();
 315   static uint get_compilation_id() { return _compilation_id; }
 316 
 317   // Set _should_block.
 318   // Call this from the VM, with Threads_lock held and a safepoint requested.
 319   static void set_should_block();
 320 
 321   // Call this from the compiler at convenient points, to poll for _should_block.
 322   static void maybe_block();
 323 
 324   enum {
 325     // Flags for toggling compiler activity
 326     stop_compilation    = 0,
 327     run_compilation     = 1,
 328     shutdown_compilaton = 2
 329   };
 330 
 331   static jint get_compilation_activity_mode() { return _should_compile_new_jobs; }
 332   static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
 333   static bool set_should_compile_new_jobs(jint new_state) {
 334     // Return success if the current caller set it
 335     jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state);
 336     return (old == (1-new_state));
 337   }
 338 
 339   static void disable_compilation_forever() {
 340     UseCompiler               = false;
 341     AlwaysCompileLoopMethods  = false;
 342     Atomic::xchg(shutdown_compilaton, &_should_compile_new_jobs);
 343   }
 344 
 345   static bool is_compilation_disabled_forever() {
 346     return _should_compile_new_jobs == shutdown_compilaton;
 347   }
 348   static void handle_full_code_cache(int code_blob_type);
 349   // Ensures that warning is only printed once.
 350   static bool should_print_compiler_warning() {
 351     jint old = Atomic::cmpxchg(1, &_print_compilation_warning, 0);
 352     return old == 0;
 353   }
 354   // Return total compilation ticks
 355   static jlong total_compilation_ticks() {
 356     return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0;
 357   }
 358 
 359   // Redefine Classes support
 360   static void mark_on_stack();
 361 
 362   // Print a detailed accounting of compilation time
 363   static void print_times();
 364 
 365   // Debugging output for failure
 366   static void print_last_compile();
 367 
 368   static void print_compiler_threads_on(outputStream* st);
 369 
 370   // compiler name for debugging
 371   static const char* compiler_name(int comp_level);
 372 
 373   static int get_total_compile_count() {          return _total_compile_count; }
 374   static int get_total_bailout_count() {          return _total_bailout_count; }
 375   static int get_total_invalidated_count() {      return _total_invalidated_count; }
 376   static int get_total_native_compile_count() {   return _total_native_compile_count; }
 377   static int get_total_osr_compile_count() {      return _total_osr_compile_count; }
 378   static int get_total_standard_compile_count() { return _total_standard_compile_count; }
 379   static int get_sum_osr_bytes_compiled() {       return _sum_osr_bytes_compiled; }
 380   static int get_sum_standard_bytes_compiled() {  return _sum_standard_bytes_compiled; }
 381   static int get_sum_nmethod_size() {             return _sum_nmethod_size;}
 382   static int get_sum_nmethod_code_size() {        return _sum_nmethod_code_size; }
 383   static long get_peak_compilation_time() {       return _peak_compilation_time; }
 384   static long get_total_compilation_time() {      return _t_total_compilation.milliseconds(); }
 385 
 386   // Log that compilation profiling is skipped because metaspace is full.
 387   static void log_metaspace_failure();
 388 };
 389 
 390 #endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP