1 /*
   2  * Copyright (c) 2019, 2020, Red Hat, Inc. 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_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
  27 
  28 #include "code/nmethod.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "gc/shenandoah/shenandoahLock.hpp"
  31 #include "gc/shenandoah/shenandoahPadding.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "utilities/growableArray.hpp"
  34 
  35 // ShenandoahNMethod tuple records the internal locations of oop slots within reclocation stream in
  36 // the nmethod. This allows us to quickly scan the oops without doing the nmethod-internal scans,
  37 // that sometimes involves parsing the machine code. Note it does not record the oops themselves,
  38 // because it would then require handling these tuples as the new class of roots.
  39 class ShenandoahNMethod : public CHeapObj<mtGC> {
  40 private:
  41   nmethod* const          _nm;
  42   oop**                   _oops;
  43   int                     _oops_count;
  44   bool                    _has_non_immed_oops;
  45   bool                    _unregistered;
  46   ShenandoahReentrantLock _lock;
  47 
  48 public:
  49   ShenandoahNMethod(nmethod *nm, GrowableArray<oop*>& oops, bool has_non_immed_oops);
  50   ~ShenandoahNMethod();
  51 
  52   inline nmethod* nm() const;
  53   inline ShenandoahReentrantLock* lock();
  54   inline void oops_do(OopClosure* oops, bool fix_relocations = false);
  55   // Update oops when the nmethod is re-registered
  56   void update();
  57 
  58   bool has_cset_oops(ShenandoahHeap* heap);
  59 
  60   inline int oop_count() const;
  61   inline bool has_oops() const;
  62 
  63   inline void mark_unregistered();
  64   inline bool is_unregistered() const;
  65 
  66   static ShenandoahNMethod* for_nmethod(nmethod* nm);
  67   static inline ShenandoahReentrantLock* lock_for_nmethod(nmethod* nm);
  68 
  69   static void heal_nmethod(nmethod* nm);
  70   static inline void heal_nmethod_metadata(ShenandoahNMethod* nmethod_data);
  71   static inline void disarm_nmethod(nmethod* nm);
  72 
  73   static inline ShenandoahNMethod* gc_data(nmethod* nm);
  74   static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
  75 
  76   void assert_alive_and_correct() NOT_DEBUG_RETURN;
  77   void assert_same_oops(bool allow_dead = false) NOT_DEBUG_RETURN;
  78   static void assert_no_oops(nmethod* nm, bool allow_dea = false) NOT_DEBUG_RETURN;
  79 
  80 private:
  81   bool has_non_immed_oops() const { return _has_non_immed_oops; }
  82   static void detect_reloc_oops(nmethod* nm, GrowableArray<oop*>& oops, bool& _has_non_immed_oops);
  83 };
  84 
  85 class ShenandoahNMethodTable;
  86 
  87 // ShenandoahNMethodList holds registered nmethod data. The list is reference counted.
  88 class ShenandoahNMethodList : public CHeapObj<mtGC> {
  89 private:
  90   ShenandoahNMethod** _list;
  91   const int           _size;
  92   uint                _ref_count;
  93 
  94 private:
  95   ~ShenandoahNMethodList();
  96 
  97 public:
  98   ShenandoahNMethodList(int size);
  99 
 100   // Reference counting with CoceCache_lock held
 101   ShenandoahNMethodList* acquire();
 102   void release();
 103 
 104   // Transfer content from other list to 'this' list, up to the limit
 105   void transfer(ShenandoahNMethodList* const other, int limit);
 106 
 107   inline int size() const;
 108   inline ShenandoahNMethod** list() const;
 109   inline ShenandoahNMethod* at(int index) const;
 110   inline void set(int index, ShenandoahNMethod* snm);
 111 };
 112 
 113 // An opaque snapshot of current nmethod table for iteration
 114 class ShenandoahNMethodTableSnapshot : public CHeapObj<mtGC> {
 115   friend class ShenandoahNMethodTable;
 116 private:
 117   ShenandoahHeap* const       _heap;
 118   ShenandoahNMethodList*      _list;
 119   /* snapshot iteration limit */
 120   int                         _limit;
 121 
 122   shenandoah_padding(0);
 123   volatile size_t       _claimed;
 124   shenandoah_padding(1);
 125 
 126 public:
 127   ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
 128   ~ShenandoahNMethodTableSnapshot();
 129 
 130   void parallel_blobs_do(CodeBlobClosure *f);
 131   void concurrent_nmethods_do(NMethodClosure* cl);
 132 };
 133 
 134 class ShenandoahNMethodTable : public CHeapObj<mtGC> {
 135   friend class ShenandoahNMethodTableSnapshot;
 136 private:
 137   enum {
 138     minSize = 1024
 139   };
 140 
 141   ShenandoahHeap* const  _heap;
 142   ShenandoahNMethodList* _list;
 143 
 144   int                    _index;
 145   ShenandoahLock         _lock;
 146   int                    _itr_cnt;
 147 
 148 public:
 149   ShenandoahNMethodTable();
 150   ~ShenandoahNMethodTable();
 151 
 152   void register_nmethod(nmethod* nm);
 153   void unregister_nmethod(nmethod* nm);
 154   void flush_nmethod(nmethod* nm);
 155 
 156   bool contain(nmethod* nm) const;
 157   int length() const { return _index; }
 158 
 159   // Table iteration support
 160   ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
 161   void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
 162 
 163   void assert_nmethods_alive_and_correct() NOT_DEBUG_RETURN;
 164 private:
 165   // Rebuild table and replace current one
 166   void rebuild(int size);
 167 
 168   bool is_full() const {
 169     assert(_index <= _list->size(), "Sanity");
 170     return _index == _list->size();
 171   }
 172 
 173   ShenandoahNMethod* at(int index) const;
 174   int  index_of(nmethod* nm) const;
 175   void remove(int index);
 176   void append(ShenandoahNMethod* snm);
 177 
 178   inline bool iteration_in_progress() const;
 179   void wait_until_concurrent_iteration_done();
 180 
 181   // Logging support
 182   void log_register_nmethod(nmethod* nm);
 183   void log_unregister_nmethod(nmethod* nm);
 184   void log_flush_nmethod(nmethod* nm);
 185 };
 186 
 187 class ShenandoahConcurrentNMethodIterator {
 188 private:
 189   ShenandoahNMethodTable*         const _table;
 190   ShenandoahNMethodTableSnapshot*       _table_snapshot;
 191 
 192 public:
 193   ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
 194 
 195   void nmethods_do_begin();
 196   void nmethods_do(NMethodClosure* cl);
 197   void nmethods_do_end();
 198 };
 199 
 200 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP