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 class nmethod;
  29 class DepChange;
  30 
  31 //
  32 // nmethodBucket is used to record dependent nmethods for
  33 // deoptimization.  nmethod dependencies are actually <klass, method>
  34 // pairs but we really only care about the klass part for purposes of
  35 // finding nmethods which might need to be deoptimized.  Instead of
  36 // recording the method, a count of how many times a particular nmethod
  37 // was recorded is kept.  This ensures that any recording errors are
  38 // noticed since an nmethod should be removed as many times are it's
  39 // added.
  40 //
  41 class nmethodBucket: public CHeapObj<mtClass> {
  42   friend class VMStructs;
  43  private:
  44   nmethod*       _nmethod;
  45   int            _count;
  46   nmethodBucket* _next;
  47 
  48  public:
  49   nmethodBucket(nmethod* nmethod, nmethodBucket* next) :
  50    _nmethod(nmethod), _next(next), _count(1) {}
  51 
  52   int count()                             { return _count; }
  53   int increment()                         { _count += 1; return _count; }
  54   int decrement();
  55   nmethodBucket* next()                   { return _next; }
  56   void set_next(nmethodBucket* b)         { _next = b; }
  57   nmethod* get_nmethod()                  { return _nmethod; }
  58 };
  59 
  60 //
  61 // Utility class to manipulate nmethod dependency context.
  62 // The context consists of nmethodBucket* (a head of a linked list)
  63 // and a boolean flag (does the list contains stale entries). The structure is
  64 // encoded as an intptr_t: lower bit is used for the flag. It is possible since
  65 // nmethodBucket* is aligned - the structure is malloc'ed in C heap.
  66 // Dependency context can be attached either to an InstanceKlass (_dep_context field)
  67 // or CallSiteContext oop for call_site_target dependencies (see javaClasses.hpp).
  68 // DependencyContext class operates on some location which holds a intptr_t value.
  69 //
  70 class DependencyContext : public StackObj {
  71   friend class VMStructs;
  72   friend class TestDependencyContext;
  73  private:
  74   enum TagBits { _has_stale_entries_bit = 1, _has_stale_entries_mask = 1 };
  75 
  76   intptr_t* _dependency_context_addr;
  77 
  78   void set_dependencies(nmethodBucket* b) {
  79     assert((intptr_t(b) & _has_stale_entries_mask) == 0, "should be aligned");
  80     if (has_stale_entries()) {
  81       *_dependency_context_addr = intptr_t(b) | _has_stale_entries_mask;
  82     } else {
  83       *_dependency_context_addr = intptr_t(b);
  84     }
  85   }
  86 
  87   void clear_has_stale_entries() {
  88     *_dependency_context_addr &= ~_has_stale_entries_mask;
  89   }
  90 
  91   nmethodBucket* dependencies() {
  92     intptr_t value = *_dependency_context_addr;
  93     return (nmethodBucket*) (value & ~_has_stale_entries_mask);
  94   }
  95 
  96   bool has_stale_entries() const {
  97     intptr_t value = *_dependency_context_addr;
  98     return (value & _has_stale_entries_mask) != 0;
  99   };
 100 
 101  public:
 102   DependencyContext(intptr_t* addr) : _dependency_context_addr(addr) {}
 103 
 104   static const intptr_t EMPTY = 0; // dependencies = NULL, has_stale_entries = false
 105 
 106   int  mark_dependent_nmethods(DepChange& changes);
 107   void add_dependent_nmethod(nmethod* nm, bool expunge_stale_entries = false);
 108   void remove_dependent_nmethod(nmethod* nm, bool expunge_stale_entries = false);
 109   int  remove_all_dependents();
 110 
 111   void expunge_stale_entries();
 112 
 113 #ifndef PRODUCT
 114   void print_dependent_nmethods(bool verbose);
 115   bool is_dependent_nmethod(nmethod* nm);
 116   bool find_stale_entries();
 117 #endif //PRODUCT
 118 };
 119 
 120 #endif // SHARE_VM_CODE_DEPENDENCYCONTEXT_HPP