1 /*
   2  * Copyright (c) 1998, 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_COMPILETASK_HPP
  26 #define SHARE_VM_COMPILER_COMPILETASK_HPP
  27 
  28 #include "code/nmethod.hpp"
  29 #include "ci/ciMethod.hpp"
  30 #include "compiler/compileLog.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "utilities/xmlstream.hpp"
  33 
  34 // CompileTask
  35 //
  36 // An entry in the compile queue.  It represents a pending or current
  37 // compilation.
  38 
  39 class CompileTask : public CHeapObj<mtCompiler> {
  40   friend class VMStructs;
  41 
  42  private:
  43   static CompileTask* _task_free_list;
  44 #ifdef ASSERT
  45   static int          _num_allocated_tasks;
  46 #endif
  47 
  48   Monitor*     _lock;
  49   uint         _compile_id;
  50   Method*      _method;
  51   jobject      _method_holder;
  52   int          _osr_bci;
  53   bool         _is_complete;
  54   bool         _is_success;
  55   bool         _is_blocking;
  56   int          _comp_level;
  57   int          _num_inlined_bytecodes;
  58   nmethodLocker* _code_handle;  // holder of eventual result
  59   CompileTask* _next, *_prev;
  60   bool         _is_free;
  61   // Fields used for logging why the compilation was initiated:
  62   jlong        _time_queued;  // in units of os::elapsed_counter()
  63   Method*      _hot_method;   // which method actually triggered this task
  64   jobject      _hot_method_holder;
  65   int          _hot_count;    // information about its invocation counter
  66   const char*  _comment;      // more info about the task
  67   const char*  _failure_reason;
  68 
  69  public:
  70   CompileTask() {
  71     _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock");
  72   }
  73 
  74   void initialize(int compile_id, methodHandle method, int osr_bci, int comp_level,
  75                   methodHandle hot_method, int hot_count, const char* comment,
  76                   bool is_blocking);
  77 
  78   static CompileTask* allocate();
  79   static void         free(CompileTask* task);
  80 
  81   int          compile_id() const                { return _compile_id; }
  82   Method*      method() const                    { return _method; }
  83   Method*      hot_method() const                { return _hot_method; }
  84   int          osr_bci() const                   { return _osr_bci; }
  85   bool         is_complete() const               { return _is_complete; }
  86   bool         is_blocking() const               { return _is_blocking; }
  87   bool         is_success() const                { return _is_success; }
  88 
  89   nmethodLocker* code_handle() const             { return _code_handle; }
  90   void         set_code_handle(nmethodLocker* l) { _code_handle = l; }
  91   nmethod*     code() const;                     // _code_handle->code()
  92   void         set_code(nmethod* nm);            // _code_handle->set_code(nm)
  93 
  94   Monitor*     lock() const                      { return _lock; }
  95 
  96   void         mark_complete()                   { _is_complete = true; }
  97   void         mark_success()                    { _is_success = true; }
  98 
  99   int          comp_level()                      { return _comp_level;}
 100   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
 101 
 102   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
 103   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
 104 
 105   CompileTask* next() const                      { return _next; }
 106   void         set_next(CompileTask* next)       { _next = next; }
 107   CompileTask* prev() const                      { return _prev; }
 108   void         set_prev(CompileTask* prev)       { _prev = prev; }
 109   bool         is_free() const                   { return _is_free; }
 110   void         set_is_free(bool val)             { _is_free = val; }
 111 
 112   // RedefineClasses support
 113   void         metadata_do(void f(Metadata*));
 114   void         mark_on_stack();
 115 
 116 private:
 117   static void  print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
 118                                       bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
 119                                       const char* msg = NULL, bool short_form = false, bool cr = true);
 120 
 121 public:
 122   void         print(outputStream* st = tty, const char* msg = NULL, bool short_form = false, bool cr = true);
 123   static void  print(outputStream* st, const nmethod* nm, const char* msg = NULL, bool short_form = false, bool cr = true) {
 124     print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
 125                            nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
 126                            msg, short_form, cr);
 127   }
 128 
 129   static void  print_inline_indent(int inline_level, outputStream* st = tty);
 130 
 131   void         print_tty();
 132   void         print_line_on_error(outputStream* st, char* buf, int buflen);
 133 
 134   void         log_task(xmlStream* log);
 135   void         log_task_queued();
 136   void         log_task_dequeued(const char* comment);
 137   void         log_task_start(CompileLog* log);
 138   void         log_task_done(CompileLog* log);
 139 
 140   void         set_failure_reason(const char* reason) {
 141     _failure_reason = reason;
 142   }
 143 
 144   bool         check_break_at_flags();
 145 
 146   static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL);
 147   static void print_inlining_tty(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
 148     print_inlining_inner(tty, method, inline_level, bci, msg);
 149   }
 150 };
 151 
 152 #endif // SHARE_VM_COMPILER_COMPILETASK_HPP