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