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