compileBroker.hpp 17.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31
#ifndef SHARE_VM_COMPILER_COMPILEBROKER_HPP
#define SHARE_VM_COMPILER_COMPILEBROKER_HPP

#include "ci/compilerInterface.hpp"
#include "compiler/abstractCompiler.hpp"
#include "runtime/perfData.hpp"

D
duke 已提交
32 33 34 35 36 37 38
class nmethod;
class nmethodLocker;

// CompileTask
//
// An entry in the compile queue.  It represents a pending or current
// compilation.
Z
zgu 已提交
39
class CompileTask : public CHeapObj<mtCompiler> {
N
never 已提交
40 41
  friend class VMStructs;

D
duke 已提交
42
 private:
43 44 45 46 47
  static CompileTask* _task_free_list;
#ifdef ASSERT
  static int          _num_allocated_tasks;
#endif

D
duke 已提交
48 49
  Monitor*     _lock;
  uint         _compile_id;
50
  Method*      _method;
51
  jobject      _method_holder;
D
duke 已提交
52 53 54 55 56 57 58
  int          _osr_bci;
  bool         _is_complete;
  bool         _is_success;
  bool         _is_blocking;
  int          _comp_level;
  int          _num_inlined_bytecodes;
  nmethodLocker* _code_handle;  // holder of eventual result
I
iveresov 已提交
59
  CompileTask* _next, *_prev;
60
  bool         _is_free;
D
duke 已提交
61 62
  // Fields used for logging why the compilation was initiated:
  jlong        _time_queued;  // in units of os::elapsed_counter()
63
  Method*      _hot_method;   // which method actually triggered this task
64
  jobject      _hot_method_holder;
D
duke 已提交
65 66
  int          _hot_count;    // information about its invocation counter
  const char*  _comment;      // more info about the task
67
  const char*  _failure_reason;
D
duke 已提交
68 69 70 71 72 73 74 75 76 77

 public:
  CompileTask() {
    _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock");
  }

  void initialize(int compile_id, methodHandle method, int osr_bci, int comp_level,
                  methodHandle hot_method, int hot_count, const char* comment,
                  bool is_blocking);

78 79
  static CompileTask* allocate();
  static void         free(CompileTask* task);
D
duke 已提交
80 81

  int          compile_id() const                { return _compile_id; }
82
  Method*      method() const                    { return _method; }
D
duke 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  int          osr_bci() const                   { return _osr_bci; }
  bool         is_complete() const               { return _is_complete; }
  bool         is_blocking() const               { return _is_blocking; }
  bool         is_success() const                { return _is_success; }

  nmethodLocker* code_handle() const             { return _code_handle; }
  void         set_code_handle(nmethodLocker* l) { _code_handle = l; }
  nmethod*     code() const;                     // _code_handle->code()
  void         set_code(nmethod* nm);            // _code_handle->set_code(nm)

  Monitor*     lock() const                      { return _lock; }

  void         mark_complete()                   { _is_complete = true; }
  void         mark_success()                    { _is_success = true; }

  int          comp_level()                      { return _comp_level;}
  void         set_comp_level(int comp_level)    { _comp_level = comp_level;}

  int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
  void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }

  CompileTask* next() const                      { return _next; }
  void         set_next(CompileTask* next)       { _next = next; }
I
iveresov 已提交
106 107
  CompileTask* prev() const                      { return _prev; }
  void         set_prev(CompileTask* prev)       { _prev = prev; }
108 109
  bool         is_free() const                   { return _is_free; }
  void         set_is_free(bool val)             { _is_free = val; }
D
duke 已提交
110

111
private:
112
  static void  print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level,
113 114
                                      bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
                                      const char* msg = NULL, bool short_form = false);
115 116

public:
117
  void         print_compilation(outputStream* st = tty, const char* msg = NULL, bool short_form = false);
118
  static void  print_compilation(outputStream* st, const nmethod* nm, const char* msg = NULL, bool short_form = false) {
119 120
    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,
121
                           msg, short_form);
122 123 124 125 126 127 128
  }

  static void  print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL);
  static void  print_inlining(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
    print_inlining(tty, method, inline_level, bci, msg);
  }

129 130 131
  // Redefine Classes support
  void mark_on_stack();

132 133
  static void  print_inline_indent(int inline_level, outputStream* st = tty);

D
duke 已提交
134 135 136
  void         print();
  void         print_line();
  void         print_line_on_error(outputStream* st, char* buf, int buflen);
137

D
duke 已提交
138 139 140 141
  void         log_task(xmlStream* log);
  void         log_task_queued();
  void         log_task_start(CompileLog* log);
  void         log_task_done(CompileLog* log);
142 143 144 145

  void         set_failure_reason(const char* reason) {
    _failure_reason = reason;
  }
D
duke 已提交
146 147 148 149 150 151
};

// CompilerCounters
//
// Per Compiler Performance Counters.
//
Z
zgu 已提交
152
class CompilerCounters : public CHeapObj<mtCompiler> {
D
duke 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

  public:
    enum {
      cmname_buffer_length = 160
    };

  private:

    char _current_method[cmname_buffer_length];
    PerfStringVariable* _perf_current_method;

    int  _compile_type;
    PerfVariable* _perf_compile_type;

    PerfCounter* _perf_time;
    PerfCounter* _perf_compiles;

  public:
    CompilerCounters(const char* name, int instance, TRAPS);

    // these methods should be called in a thread safe context

    void set_current_method(const char* method) {
      strncpy(_current_method, method, (size_t)cmname_buffer_length);
      if (UsePerfData) _perf_current_method->set_value(method);
    }

    char* current_method()                  { return _current_method; }

    void set_compile_type(int compile_type) {
      _compile_type = compile_type;
      if (UsePerfData) _perf_compile_type->set_value((jlong)compile_type);
    }

    int compile_type()                       { return _compile_type; }

    PerfCounter* time_counter()              { return _perf_time; }
    PerfCounter* compile_counter()           { return _perf_compiles; }
};

// CompileQueue
//
// A list of CompileTasks.
Z
zgu 已提交
196
class CompileQueue : public CHeapObj<mtCompiler> {
D
duke 已提交
197 198 199 200 201 202 203
 private:
  const char* _name;
  Monitor*    _lock;

  CompileTask* _first;
  CompileTask* _last;

204 205
  CompileTask* _first_stale;

I
iveresov 已提交
206
  int _size;
207 208

  void purge_stale_tasks();
D
duke 已提交
209 210 211 212 213 214
 public:
  CompileQueue(const char* name, Monitor* lock) {
    _name = name;
    _lock = lock;
    _first = NULL;
    _last = NULL;
I
iveresov 已提交
215
    _size = 0;
216
    _first_stale = NULL;
D
duke 已提交
217 218 219 220 221 222
  }

  const char*  name() const                      { return _name; }
  Monitor*     lock() const                      { return _lock; }

  void         add(CompileTask* task);
I
iveresov 已提交
223
  void         remove(CompileTask* task);
224
  void         remove_and_mark_stale(CompileTask* task);
I
iveresov 已提交
225 226
  CompileTask* first()                           { return _first; }
  CompileTask* last()                            { return _last;  }
D
duke 已提交
227 228 229 230

  CompileTask* get();

  bool         is_empty() const                  { return _first == NULL; }
I
iveresov 已提交
231
  int          size()     const                  { return _size;          }
D
duke 已提交
232

233

234 235
  // Redefine Classes support
  void mark_on_stack();
236 237
  void free_all();
  NOT_PRODUCT (void print();)
238 239 240 241

  ~CompileQueue() {
    assert (is_empty(), " Compile Queue must be empty");
  }
D
duke 已提交
242 243
};

I
iveresov 已提交
244 245 246 247 248 249 250 251 252 253
// CompileTaskWrapper
//
// Assign this task to the current thread.  Deallocate the task
// when the compilation is complete.
class CompileTaskWrapper : StackObj {
public:
  CompileTaskWrapper(CompileTask* task);
  ~CompileTaskWrapper();
};

D
duke 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

// Compilation
//
// The broker for all compilation requests.
class CompileBroker: AllStatic {
 friend class Threads;
  friend class CompileTaskWrapper;

 public:
  enum {
    name_buffer_length = 100
  };

  // Compile type Information for print_last_compile() and CompilerCounters
  enum { no_compile, normal_compile, osr_compile, native_compile };
A
anoll 已提交
269 270
  static int assign_compile_id (methodHandle method, int osr_bci);

D
duke 已提交
271 272 273 274 275

 private:
  static bool _initialized;
  static volatile bool _should_block;

276 277 278
  // This flag can be used to stop compilation or turn it back on
  static volatile jint _should_compile_new_jobs;

D
duke 已提交
279 280 281 282
  // The installed compiler(s)
  static AbstractCompiler* _compilers[2];

  // These counters are used for assigning id's to each compilation
A
anoll 已提交
283 284
  static volatile jint _compilation_id;
  static volatile jint _osr_compilation_id;
D
duke 已提交
285 286 287 288 289

  static int  _last_compile_type;
  static int  _last_compile_level;
  static char _last_method_compiled[name_buffer_length];

290 291
  static CompileQueue* _c2_compile_queue;
  static CompileQueue* _c1_compile_queue;
D
duke 已提交
292

293
  static GrowableArray<CompilerThread*>* _compiler_threads;
D
duke 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

  // performance counters
  static PerfCounter* _perf_total_compilation;
  static PerfCounter* _perf_native_compilation;
  static PerfCounter* _perf_osr_compilation;
  static PerfCounter* _perf_standard_compilation;

  static PerfCounter* _perf_total_bailout_count;
  static PerfCounter* _perf_total_invalidated_count;
  static PerfCounter* _perf_total_compile_count;
  static PerfCounter* _perf_total_native_compile_count;
  static PerfCounter* _perf_total_osr_compile_count;
  static PerfCounter* _perf_total_standard_compile_count;

  static PerfCounter* _perf_sum_osr_bytes_compiled;
  static PerfCounter* _perf_sum_standard_bytes_compiled;
  static PerfCounter* _perf_sum_nmethod_size;
  static PerfCounter* _perf_sum_nmethod_code_size;

  static PerfStringVariable* _perf_last_method;
  static PerfStringVariable* _perf_last_failed_method;
  static PerfStringVariable* _perf_last_invalidated_method;
  static PerfVariable*       _perf_last_compile_type;
  static PerfVariable*       _perf_last_compile_size;
  static PerfVariable*       _perf_last_failed_type;
  static PerfVariable*       _perf_last_invalidated_type;

  // Timers and counters for generating statistics
  static elapsedTimer _t_total_compilation;
  static elapsedTimer _t_osr_compilation;
  static elapsedTimer _t_standard_compilation;

S
sla 已提交
326
  static int _total_compile_count;
D
duke 已提交
327 328 329 330 331 332 333 334 335
  static int _total_bailout_count;
  static int _total_invalidated_count;
  static int _total_native_compile_count;
  static int _total_osr_compile_count;
  static int _total_standard_compile_count;
  static int _sum_osr_bytes_compiled;
  static int _sum_standard_bytes_compiled;
  static int _sum_nmethod_size;
  static int _sum_nmethod_code_size;
S
sla 已提交
336
  static long _peak_compilation_time;
D
duke 已提交
337

338 339
  static volatile jint _print_compilation_warning;

340
  static CompilerThread* make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, AbstractCompiler* comp, TRAPS);
I
iveresov 已提交
341
  static void init_compiler_threads(int c1_compiler_count, int c2_compiler_count);
D
duke 已提交
342
  static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level);
343
  static bool is_compile_blocking      ();
D
duke 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  static void preload_classes          (methodHandle method, TRAPS);

  static CompileTask* create_compile_task(CompileQueue* queue,
                                          int           compile_id,
                                          methodHandle  method,
                                          int           osr_bci,
                                          int           comp_level,
                                          methodHandle  hot_method,
                                          int           hot_count,
                                          const char*   comment,
                                          bool          blocking);
  static void wait_for_completion(CompileTask* task);

  static void invoke_compiler_on_method(CompileTask* task);
  static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level);
  static void push_jni_handle_block();
  static void pop_jni_handle_block();
  static bool check_break_at(methodHandle method, int compile_id, bool is_osr);
  static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);

  static void compile_method_base(methodHandle method,
                                  int osr_bci,
                                  int comp_level,
                                  methodHandle hot_method,
                                  int hot_count,
                                  const char* comment,
370
                                  Thread* thread);
I
iveresov 已提交
371
  static CompileQueue* compile_queue(int comp_level) {
372 373
    if (is_c2_compile(comp_level)) return _c2_compile_queue;
    if (is_c1_compile(comp_level)) return _c1_compile_queue;
I
iveresov 已提交
374 375
    return NULL;
  }
376 377 378
  static bool init_compiler_runtime();
  static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread);

D
duke 已提交
379 380 381 382 383 384
 public:
  enum {
    // The entry bci used for non-OSR compilations.
    standard_entry_bci = InvocationEntryBci
  };

I
iveresov 已提交
385 386 387 388
  static AbstractCompiler* compiler(int comp_level) {
    if (is_c2_compile(comp_level)) return _compilers[1]; // C2
    if (is_c1_compile(comp_level)) return _compilers[0]; // C1
    return NULL;
D
duke 已提交
389 390
  }

391
  static bool compilation_is_complete(methodHandle method, int osr_bci, int comp_level);
392
  static bool compilation_is_in_queue(methodHandle method);
I
iveresov 已提交
393 394 395 396
  static int queue_size(int comp_level) {
    CompileQueue *q = compile_queue(comp_level);
    return q != NULL ? q->size() : 0;
  }
D
duke 已提交
397 398
  static void compilation_init();
  static void init_compiler_thread_log();
I
iveresov 已提交
399 400 401 402 403
  static nmethod* compile_method(methodHandle method,
                                 int osr_bci,
                                 int comp_level,
                                 methodHandle hot_method,
                                 int hot_count,
404
                                 const char* comment, Thread* thread);
D
duke 已提交
405 406

  static void compiler_thread_loop();
407
  static uint get_compilation_id() { return _compilation_id; }
D
duke 已提交
408 409 410 411 412 413 414 415

  // Set _should_block.
  // Call this from the VM, with Threads_lock held and a safepoint requested.
  static void set_should_block();

  // Call this from the compiler at convenient points, to poll for _should_block.
  static void maybe_block();

416 417
  enum {
    // Flags for toggling compiler activity
418 419 420
    stop_compilation    = 0,
    run_compilation     = 1,
    shutdown_compilaton = 2
421 422 423 424 425 426 427 428
  };

  static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
  static bool set_should_compile_new_jobs(jint new_state) {
    // Return success if the current caller set it
    jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state);
    return (old == (1-new_state));
  }
429 430 431 432 433 434 435 436 437 438

  static void disable_compilation_forever() {
    UseCompiler               = false;
    AlwaysCompileLoopMethods  = false;
    Atomic::xchg(shutdown_compilaton, &_should_compile_new_jobs);
  }

  static bool is_compilation_disabled_forever() {
    return _should_compile_new_jobs == shutdown_compilaton;
  }
439
  static void handle_full_code_cache();
440 441 442 443 444
  // Ensures that warning is only printed once.
  static bool should_print_compiler_warning() {
    jint old = Atomic::cmpxchg(1, &_print_compilation_warning, 0);
    return old == 0;
  }
D
duke 已提交
445 446 447 448 449
  // Return total compilation ticks
  static jlong total_compilation_ticks() {
    return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0;
  }

450 451 452
  // Redefine Classes support
  static void mark_on_stack();

D
duke 已提交
453 454 455 456 457 458 459
  // Print a detailed accounting of compilation time
  static void print_times();

  // Debugging output for failure
  static void print_last_compile();

  static void print_compiler_threads_on(outputStream* st);
460 461 462

  // compiler name for debugging
  static const char* compiler_name(int comp_level);
S
sla 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475

  static int get_total_compile_count() {          return _total_compile_count; }
  static int get_total_bailout_count() {          return _total_bailout_count; }
  static int get_total_invalidated_count() {      return _total_invalidated_count; }
  static int get_total_native_compile_count() {   return _total_native_compile_count; }
  static int get_total_osr_compile_count() {      return _total_osr_compile_count; }
  static int get_total_standard_compile_count() { return _total_standard_compile_count; }
  static int get_sum_osr_bytes_compiled() {       return _sum_osr_bytes_compiled; }
  static int get_sum_standard_bytes_compiled() {  return _sum_standard_bytes_compiled; }
  static int get_sum_nmethod_size() {             return _sum_nmethod_size;}
  static int get_sum_nmethod_code_size() {        return _sum_nmethod_code_size; }
  static long get_peak_compilation_time() {       return _peak_compilation_time; }
  static long get_total_compilation_time() {      return _t_total_compilation.milliseconds(); }
D
duke 已提交
476
};
477 478

#endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP