1 /*
   2  * Copyright (c) 1999, 2011, 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 "runtime/perfData.hpp"
  31 
  32 class nmethod;
  33 class nmethodLocker;
  34 
  35 // CompileTask
  36 //
  37 // An entry in the compile queue.  It represents a pending or current
  38 // compilation.
  39 class CompileTask : public CHeapObj {
  40  private:
  41   Monitor*     _lock;
  42   uint         _compile_id;
  43   jobject      _method;
  44   int          _osr_bci;
  45   bool         _is_complete;
  46   bool         _is_success;
  47   bool         _is_blocking;
  48   int          _comp_level;
  49   int          _num_inlined_bytecodes;
  50   nmethodLocker* _code_handle;  // holder of eventual result
  51   CompileTask* _next, *_prev;
  52 
  53   // Fields used for logging why the compilation was initiated:
  54   jlong        _time_queued;  // in units of os::elapsed_counter()
  55   jobject      _hot_method;   // which method actually triggered this task
  56   int          _hot_count;    // information about its invocation counter
  57   const char*  _comment;      // more info about the task
  58 
  59  public:
  60   CompileTask() {
  61     _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock");
  62   }
  63 
  64   void initialize(int compile_id, methodHandle method, int osr_bci, int comp_level,
  65                   methodHandle hot_method, int hot_count, const char* comment,
  66                   bool is_blocking);
  67 
  68   void free();
  69 
  70   int          compile_id() const                { return _compile_id; }
  71   jobject      method_handle() const             { return _method; }
  72   int          osr_bci() const                   { return _osr_bci; }
  73   bool         is_complete() const               { return _is_complete; }
  74   bool         is_blocking() const               { return _is_blocking; }
  75   bool         is_success() const                { return _is_success; }
  76 
  77   nmethodLocker* code_handle() const             { return _code_handle; }
  78   void         set_code_handle(nmethodLocker* l) { _code_handle = l; }
  79   nmethod*     code() const;                     // _code_handle->code()
  80   void         set_code(nmethod* nm);            // _code_handle->set_code(nm)
  81 
  82   Monitor*     lock() const                      { return _lock; }
  83 
  84   void         mark_complete()                   { _is_complete = true; }
  85   void         mark_success()                    { _is_success = true; }
  86 
  87   int          comp_level()                      { return _comp_level;}
  88   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
  89 
  90   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
  91   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
  92 
  93   CompileTask* next() const                      { return _next; }
  94   void         set_next(CompileTask* next)       { _next = next; }
  95   CompileTask* prev() const                      { return _prev; }
  96   void         set_prev(CompileTask* prev)       { _prev = prev; }
  97 
  98 private:
  99   static void  print_compilation_impl(outputStream* st, methodOop method, int compile_id, int comp_level, bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false, const char* msg = NULL);
 100 
 101 public:
 102   void         print_compilation(outputStream* st = tty);
 103   static void  print_compilation(outputStream* st, const nmethod* nm, const char* msg = NULL) {
 104     print_compilation_impl(st, nm->method(), nm->compile_id(), nm->comp_level(), nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false, msg);
 105   }
 106 
 107   static void  print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL);
 108   static void  print_inlining(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
 109     print_inlining(tty, method, inline_level, bci, msg);
 110   }
 111 
 112   static void  print_inline_indent(int inline_level, outputStream* st = tty);
 113 
 114   void         print();
 115   void         print_line();
 116   void         print_line_on_error(outputStream* st, char* buf, int buflen);
 117 
 118   void         log_task(xmlStream* log);
 119   void         log_task_queued();
 120   void         log_task_start(CompileLog* log);
 121   void         log_task_done(CompileLog* log);
 122 };
 123 
 124 // CompilerCounters
 125 //
 126 // Per Compiler Performance Counters.
 127 //
 128 class CompilerCounters : public CHeapObj {
 129 
 130   public:
 131     enum {
 132       cmname_buffer_length = 160
 133     };
 134 
 135   private:
 136 
 137     char _current_method[cmname_buffer_length];
 138     PerfStringVariable* _perf_current_method;
 139 
 140     int  _compile_type;
 141     PerfVariable* _perf_compile_type;
 142 
 143     PerfCounter* _perf_time;
 144     PerfCounter* _perf_compiles;
 145 
 146   public:
 147     CompilerCounters(const char* name, int instance, TRAPS);
 148 
 149     // these methods should be called in a thread safe context
 150 
 151     void set_current_method(const char* method) {
 152       strncpy(_current_method, method, (size_t)cmname_buffer_length);
 153       if (UsePerfData) _perf_current_method->set_value(method);
 154     }
 155 
 156     char* current_method()                  { return _current_method; }
 157 
 158     void set_compile_type(int compile_type) {
 159       _compile_type = compile_type;
 160       if (UsePerfData) _perf_compile_type->set_value((jlong)compile_type);
 161     }
 162 
 163     int compile_type()                       { return _compile_type; }
 164 
 165     PerfCounter* time_counter()              { return _perf_time; }
 166     PerfCounter* compile_counter()           { return _perf_compiles; }
 167 };
 168 
 169 // CompileQueue
 170 //
 171 // A list of CompileTasks.
 172 class CompileQueue : public CHeapObj {
 173  private:
 174   const char* _name;
 175   Monitor*    _lock;
 176 
 177   CompileTask* _first;
 178   CompileTask* _last;
 179 
 180   int _size;
 181  public:
 182   CompileQueue(const char* name, Monitor* lock) {
 183     _name = name;
 184     _lock = lock;
 185     _first = NULL;
 186     _last = NULL;
 187     _size = 0;
 188   }
 189 
 190   const char*  name() const                      { return _name; }
 191   Monitor*     lock() const                      { return _lock; }
 192 
 193   void         add(CompileTask* task);
 194   void         remove(CompileTask* task);
 195   CompileTask* first()                           { return _first; }
 196   CompileTask* last()                            { return _last;  }
 197 
 198   CompileTask* get();
 199 
 200   bool         is_empty() const                  { return _first == NULL; }
 201   int          size()     const                  { return _size;          }
 202 
 203   void         print();
 204 };
 205 
 206 // CompileTaskWrapper
 207 //
 208 // Assign this task to the current thread.  Deallocate the task
 209 // when the compilation is complete.
 210 class CompileTaskWrapper : StackObj {
 211 public:
 212   CompileTaskWrapper(CompileTask* task);
 213   ~CompileTaskWrapper();
 214 };
 215 
 216 
 217 // Compilation
 218 //
 219 // The broker for all compilation requests.
 220 class CompileBroker: AllStatic {
 221  friend class Threads;
 222   friend class CompileTaskWrapper;
 223 
 224  public:
 225   enum {
 226     name_buffer_length = 100
 227   };
 228 
 229   // Compile type Information for print_last_compile() and CompilerCounters
 230   enum { no_compile, normal_compile, osr_compile, native_compile };
 231 
 232  private:
 233   static bool _initialized;
 234   static volatile bool _should_block;
 235 
 236   // This flag can be used to stop compilation or turn it back on
 237   static volatile jint _should_compile_new_jobs;
 238 
 239   // The installed compiler(s)
 240   static AbstractCompiler* _compilers[2];
 241 
 242   // These counters are used for assigning id's to each compilation
 243   static uint _compilation_id;
 244   static uint _osr_compilation_id;
 245   static uint _native_compilation_id;
 246 
 247   static int  _last_compile_type;
 248   static int  _last_compile_level;
 249   static char _last_method_compiled[name_buffer_length];
 250 
 251   static CompileQueue* _c2_method_queue;
 252   static CompileQueue* _c1_method_queue;
 253   static CompileTask* _task_free_list;
 254 
 255   static GrowableArray<CompilerThread*>* _method_threads;
 256 
 257   // performance counters
 258   static PerfCounter* _perf_total_compilation;
 259   static PerfCounter* _perf_native_compilation;
 260   static PerfCounter* _perf_osr_compilation;
 261   static PerfCounter* _perf_standard_compilation;
 262 
 263   static PerfCounter* _perf_total_bailout_count;
 264   static PerfCounter* _perf_total_invalidated_count;
 265   static PerfCounter* _perf_total_compile_count;
 266   static PerfCounter* _perf_total_native_compile_count;
 267   static PerfCounter* _perf_total_osr_compile_count;
 268   static PerfCounter* _perf_total_standard_compile_count;
 269 
 270   static PerfCounter* _perf_sum_osr_bytes_compiled;
 271   static PerfCounter* _perf_sum_standard_bytes_compiled;
 272   static PerfCounter* _perf_sum_nmethod_size;
 273   static PerfCounter* _perf_sum_nmethod_code_size;
 274 
 275   static PerfStringVariable* _perf_last_method;
 276   static PerfStringVariable* _perf_last_failed_method;
 277   static PerfStringVariable* _perf_last_invalidated_method;
 278   static PerfVariable*       _perf_last_compile_type;
 279   static PerfVariable*       _perf_last_compile_size;
 280   static PerfVariable*       _perf_last_failed_type;
 281   static PerfVariable*       _perf_last_invalidated_type;
 282 
 283   // Timers and counters for generating statistics
 284   static elapsedTimer _t_total_compilation;
 285   static elapsedTimer _t_osr_compilation;
 286   static elapsedTimer _t_standard_compilation;
 287 
 288   static int _total_bailout_count;
 289   static int _total_invalidated_count;
 290   static int _total_compile_count;
 291   static int _total_native_compile_count;
 292   static int _total_osr_compile_count;
 293   static int _total_standard_compile_count;
 294 
 295   static int _sum_osr_bytes_compiled;
 296   static int _sum_standard_bytes_compiled;
 297   static int _sum_nmethod_size;
 298   static int _sum_nmethod_code_size;
 299 
 300   static CompilerThread* make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS);
 301   static void init_compiler_threads(int c1_compiler_count, int c2_compiler_count);
 302   static bool compilation_is_complete  (methodHandle method, int osr_bci, int comp_level);
 303   static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level);
 304   static uint assign_compile_id        (methodHandle method, int osr_bci);
 305   static bool is_compile_blocking      (methodHandle method, int osr_bci);
 306   static void preload_classes          (methodHandle method, TRAPS);
 307 
 308   static CompileTask* create_compile_task(CompileQueue* queue,
 309                                           int           compile_id,
 310                                           methodHandle  method,
 311                                           int           osr_bci,
 312                                           int           comp_level,
 313                                           methodHandle  hot_method,
 314                                           int           hot_count,
 315                                           const char*   comment,
 316                                           bool          blocking);
 317   static CompileTask* allocate_task();
 318   static void free_task(CompileTask* task);
 319   static void wait_for_completion(CompileTask* task);
 320 
 321   static void invoke_compiler_on_method(CompileTask* task);
 322   static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level);
 323   static void push_jni_handle_block();
 324   static void pop_jni_handle_block();
 325   static bool check_break_at(methodHandle method, int compile_id, bool is_osr);
 326   static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
 327 
 328   static void compile_method_base(methodHandle method,
 329                                   int osr_bci,
 330                                   int comp_level,
 331                                   methodHandle hot_method,
 332                                   int hot_count,
 333                                   const char* comment,
 334                                   TRAPS);
 335   static CompileQueue* compile_queue(int comp_level) {
 336     if (is_c2_compile(comp_level)) return _c2_method_queue;
 337     if (is_c1_compile(comp_level)) return _c1_method_queue;
 338     return NULL;
 339   }
 340  public:
 341   enum {
 342     // The entry bci used for non-OSR compilations.
 343     standard_entry_bci = InvocationEntryBci
 344   };
 345 
 346   static AbstractCompiler* compiler(int comp_level) {
 347     if (is_c2_compile(comp_level)) return _compilers[1]; // C2
 348     if (is_c1_compile(comp_level)) return _compilers[0]; // C1
 349     return NULL;
 350   }
 351 
 352   static bool compilation_is_in_queue(methodHandle method, int osr_bci);
 353   static int queue_size(int comp_level) {
 354     CompileQueue *q = compile_queue(comp_level);
 355     return q != NULL ? q->size() : 0;
 356   }
 357   static void compilation_init();
 358   static void init_compiler_thread_log();
 359   static nmethod* compile_method(methodHandle method,
 360                                  int osr_bci,
 361                                  int comp_level,
 362                                  methodHandle hot_method,
 363                                  int hot_count,
 364                                  const char* comment, TRAPS);
 365 
 366   static void compiler_thread_loop();
 367 
 368   static uint get_compilation_id() { return _compilation_id; }
 369   static bool is_idle();
 370 
 371   // Set _should_block.
 372   // Call this from the VM, with Threads_lock held and a safepoint requested.
 373   static void set_should_block();
 374 
 375   // Call this from the compiler at convenient points, to poll for _should_block.
 376   static void maybe_block();
 377 
 378   enum {
 379     // Flags for toggling compiler activity
 380     stop_compilation = 0,
 381     run_compilation  = 1
 382   };
 383 
 384   static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
 385   static bool set_should_compile_new_jobs(jint new_state) {
 386     // Return success if the current caller set it
 387     jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state);
 388     return (old == (1-new_state));
 389   }
 390   static void handle_full_code_cache();
 391 
 392   // Return total compilation ticks
 393   static jlong total_compilation_ticks() {
 394     return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0;
 395   }
 396 
 397   // Print a detailed accounting of compilation time
 398   static void print_times();
 399 
 400   // Debugging output for failure
 401   static void print_last_compile();
 402 
 403   static void print_compiler_threads_on(outputStream* st);
 404 };
 405 
 406 #endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP