1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
  26 
  27 #include "code/nmethod.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.hpp"
  29 #include "gc/shenandoah/shenandoahLock.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "utilities/growableArray.hpp"
  32 
  33 // ShenandoahNMethod tuple records the internal locations of oop slots within reclocation stream in
  34 // the nmethod. This allows us to quickly scan the oops without doing the nmethod-internal scans,
  35 // that sometimes involves parsing the machine code. Note it does not record the oops themselves,
  36 // because it would then require handling these tuples as the new class of roots.
  37 class ShenandoahNMethod : public CHeapObj<mtGC> {
  38 private:
  39   nmethod* const          _nm;
  40   oop**                   _oops;
  41   int                     _oops_count;
  42   bool                    _has_non_immed_oops;
  43   bool                    _unregistered;
  44   ShenandoahReentrantLock _lock;
  45 
  46 public:
  47   ShenandoahNMethod(nmethod *nm, GrowableArray<oop*>& oops, bool has_non_immed_oops);
  48   ~ShenandoahNMethod();
  49 
  50   inline nmethod* nm() const;
  51   inline ShenandoahReentrantLock* lock();
  52   void oops_do(OopClosure* oops, bool fix_relocations = false);
  53   // Update oops when the nmethod is re-registered
  54   void update();
  55 
  56   bool has_cset_oops(ShenandoahHeap* heap);
  57 
  58   inline int oop_count() const;
  59   inline bool has_oops() const;
  60 
  61   inline void mark_unregistered();
  62   inline bool is_unregistered() const;
  63 
  64   static ShenandoahNMethod* for_nmethod(nmethod* nm);
  65   static inline ShenandoahReentrantLock* lock_for_nmethod(nmethod* nm);
  66 
  67   static void heal_nmethod(nmethod* nm);
  68   static inline void disarm_nmethod(nmethod* nm);
  69 
  70   static inline ShenandoahNMethod* gc_data(nmethod* nm);
  71   static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
  72 
  73   void assert_alive_and_correct() NOT_DEBUG_RETURN;
  74   void assert_same_oops(bool allow_dead = false) NOT_DEBUG_RETURN;
  75   static void assert_no_oops(nmethod* nm, bool allow_dea = false) NOT_DEBUG_RETURN;
  76 
  77 private:
  78   bool has_non_immed_oops() const { return _has_non_immed_oops; }
  79   static void detect_reloc_oops(nmethod* nm, GrowableArray<oop*>& oops, bool& _has_non_immed_oops);
  80 };
  81 
  82 class ShenandoahNMethodTable;
  83 
  84 // An opaque snapshot of current nmethod table for iteration
  85 class ShenandoahNMethodTableSnapshot : public CHeapObj<mtGC> {
  86   friend class ShenandoahNMethodTable;
  87 private:
  88   ShenandoahHeap* const       _heap;
  89   ShenandoahNMethodTable*     _table;
  90   ShenandoahNMethod** const   _array;
  91   const int                   _length;
  92 
  93   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
  94   volatile size_t       _claimed;
  95   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
  96 
  97 public:
  98   ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
  99 
 100   template<bool CSET_FILTER>
 101   void parallel_blobs_do(CodeBlobClosure *f);
 102 
 103   void concurrent_nmethods_do(NMethodClosure* cl);
 104 };
 105 
 106 class ShenandoahNMethodTable : public CHeapObj<mtGC> {
 107   friend class ShenandoahNMethodTableSnapshot;
 108 private:
 109   enum {
 110     minSize = 1024
 111   };
 112 
 113   ShenandoahHeap* const _heap;
 114   ShenandoahNMethod**   _array;
 115   int                   _size;
 116   int                   _index;
 117   ShenandoahLock        _lock;
 118   bool                  _iteration_in_progress;
 119 
 120 public:
 121   ShenandoahNMethodTable();
 122   ~ShenandoahNMethodTable();
 123 
 124   void register_nmethod(nmethod* nm);
 125   void unregister_nmethod(nmethod* nm);
 126   void flush_nmethod(nmethod* nm);
 127 
 128   bool contain(nmethod* nm) const;
 129   int length() const { return _index; }
 130 
 131   // Table iteration support
 132   ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
 133   void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
 134 
 135   void assert_nmethods_alive_and_correct() NOT_DEBUG_RETURN;
 136 private:
 137   // Rebuild table and replace current one
 138   void rebuild(int size);
 139 
 140   bool is_full() const {
 141     assert(_index <= _size, "Sanity");
 142     return _index == _size;
 143   }
 144 
 145   ShenandoahNMethod* at(int index) const;
 146   int  index_of(nmethod* nm) const;
 147   void remove(int index);
 148   void append(ShenandoahNMethod* snm);
 149 
 150   inline bool iteration_in_progress() const;
 151   void wait_until_concurrent_iteration_done();
 152 
 153   // Logging support
 154   void log_register_nmethod(nmethod* nm);
 155   void log_unregister_nmethod(nmethod* nm);
 156   void log_flush_nmethod(nmethod* nm);
 157 };
 158 
 159 class ShenandoahConcurrentNMethodIterator {
 160 private:
 161   ShenandoahNMethodTable*         const _table;
 162   ShenandoahNMethodTableSnapshot*       _table_snapshot;
 163 
 164 public:
 165   ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
 166 
 167   void nmethods_do_begin();
 168   void nmethods_do(NMethodClosure* cl);
 169   void nmethods_do_end();
 170 };
 171 
 172 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP