1 /*
   2  * Copyright (c) 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_CODE_DEPENDENCYCONTEXT_HPP
  26 #define SHARE_VM_CODE_DEPENDENCYCONTEXT_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "oops/oop.hpp"
  30 #include "runtime/handles.hpp"
  31 #include "runtime/perfData.hpp"
  32 
  33 class nmethod;
  34 class DepChange;
  35 
  36 //
  37 // nmethodBucket is used to record dependent nmethods for
  38 // deoptimization.  nmethod dependencies are actually <klass, method>
  39 // pairs but we really only care about the klass part for purposes of
  40 // finding nmethods which might need to be deoptimized.  Instead of
  41 // recording the method, a count of how many times a particular nmethod
  42 // was recorded is kept.  This ensures that any recording errors are
  43 // noticed since an nmethod should be removed as many times are it's
  44 // added.
  45 //
  46 class nmethodBucket: public CHeapObj<mtClass> {
  47   friend class VMStructs;
  48  private:
  49   nmethod*       _nmethod;
  50   int            _count;
  51   nmethodBucket* _next;
  52 
  53  public:
  54   nmethodBucket(nmethod* nmethod, nmethodBucket* next) :
  55    _nmethod(nmethod), _next(next), _count(1) {}
  56 
  57   int count()                             { return _count; }
  58   int increment()                         { _count += 1; return _count; }
  59   int decrement();
  60   nmethodBucket* next()                   { return _next; }
  61   void set_next(nmethodBucket* b)         { _next = b; }
  62   nmethod* get_nmethod()                  { return _nmethod; }
  63 };
  64 
  65 //
  66 // Utility class to manipulate nmethod dependency context.
  67 // The context consists of nmethodBucket* (a head of a linked list)
  68 // and a boolean flag (does the list contains stale entries). The structure is
  69 // encoded as an intptr_t: lower bit is used for the flag. It is possible since
  70 // nmethodBucket* is aligned - the structure is malloc'ed in C heap.
  71 // Dependency context can be attached either to an InstanceKlass (_dep_context field)
  72 // or CallSiteContext oop for call_site_target dependencies (see javaClasses.hpp).
  73 // DependencyContext class operates on some location which holds a intptr_t value.
  74 //
  75 class DependencyContext : public StackObj {
  76   friend class VMStructs;
  77   friend class TestDependencyContext;
  78  private:
  79   enum TagBits { _has_stale_entries_bit = 1, _has_stale_entries_mask = 1 };
  80 
  81   intptr_t* _dependency_context_addr;
  82 
  83   void set_dependencies(nmethodBucket* b) {
  84     assert((intptr_t(b) & _has_stale_entries_mask) == 0, "should be aligned");
  85     if (has_stale_entries()) {
  86       *_dependency_context_addr = intptr_t(b) | _has_stale_entries_mask;
  87     } else {
  88       *_dependency_context_addr = intptr_t(b);
  89     }
  90   }
  91 
  92   void set_has_stale_entries(bool x) {
  93     if (x) {
  94       *_dependency_context_addr |= _has_stale_entries_mask;
  95     } else {
  96       *_dependency_context_addr &= ~_has_stale_entries_mask;
  97     }
  98   }
  99 
 100   nmethodBucket* dependencies() {
 101     intptr_t value = *_dependency_context_addr;
 102     return (nmethodBucket*) (value & ~_has_stale_entries_mask);
 103   }
 104 
 105   bool has_stale_entries() const {
 106     intptr_t value = *_dependency_context_addr;
 107     return (value & _has_stale_entries_mask) != 0;
 108   }
 109 
 110   static PerfCounter* _perf_total_buckets_allocated_count;
 111   static PerfCounter* _perf_total_buckets_deallocated_count;
 112   static PerfCounter* _perf_total_buckets_stale_count;
 113   static PerfCounter* _perf_total_buckets_stale_acc_count;
 114 
 115  public:
 116 #ifdef ASSERT
 117   // Verification for dependency contexts rooted at Java objects.
 118   Handle _base; // non-NULL if dependency context resides in an oop (e.g. CallSite).
 119   oop _base_oop;
 120 
 121   DependencyContext(intptr_t* addr, Handle base = Handle())
 122     : _dependency_context_addr(addr), _base(base)
 123   {
 124       _base_oop = _base();
 125   }
 126 
 127   ~DependencyContext() {
 128     // Base oop relocation invalidates _dependency_context_addr.
 129     assert(_base_oop == _base(), "base oop relocation is forbidden");
 130   }
 131 #else
 132     DependencyContext(intptr_t* addr) : _dependency_context_addr(addr) {}
 133 #endif // ASSERT
 134 
 135   static const intptr_t EMPTY = 0; // dependencies = NULL, has_stale_entries = false
 136 
 137   static void init();
 138 
 139   int  mark_dependent_nmethods(DepChange& changes);
 140   void add_dependent_nmethod(nmethod* nm, bool expunge_stale_entries = false);
 141   void remove_dependent_nmethod(nmethod* nm, bool expunge_stale_entries = false);
 142   int  remove_all_dependents();
 143 
 144   void expunge_stale_entries();
 145 
 146   // Unsafe deallocation of nmethodBuckets. Used in IK::release_C_heap_structures
 147   // to clean up the context possibly containing live entries pointing to unloaded nmethods.
 148   void wipe();
 149 
 150 #ifndef PRODUCT
 151   void print_dependent_nmethods(bool verbose);
 152   bool is_dependent_nmethod(nmethod* nm);
 153   bool find_stale_entries();
 154 #endif //PRODUCT
 155 };
 156 #endif // SHARE_VM_CODE_DEPENDENCYCONTEXT_HPP