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 "memory/allocation.hpp"
  32 #include "utilities/growableArray.hpp"
  33 
  34 // ShenandoahNMethod tuple records the internal locations of oop slots within reclocation stream in
  35 // the nmethod. This allows us to quickly scan the oops without doing the nmethod-internal scans,
  36 // that sometimes involves parsing the machine code. Note it does not record the oops themselves,
  37 // because it would then require handling these tuples as the new class of roots.
  38 class ShenandoahNMethod : public CHeapObj<mtGC> {
  39 private:
  40   nmethod* const          _nm;
  41   oop**                   _oops;
  42   int                     _oops_count;
  43   bool                    _has_non_immed_oops;
  44   bool                    _unregistered;
  45   ShenandoahReentrantLock _lock;
  46 
  47 public:
  48   ShenandoahNMethod(nmethod *nm, GrowableArray<oop*>& oops, bool has_non_immed_oops);
  49   ~ShenandoahNMethod();
  50 
  51   inline nmethod* nm() const;
  52   inline ShenandoahReentrantLock* lock();
  53   void oops_do(OopClosure* oops, bool fix_relocations = false);
  54   // Update oops when the nmethod is re-registered
  55   void update();
  56 
  57   bool has_cset_oops(ShenandoahHeap* heap);
  58 
  59   inline int oop_count() const;
  60   inline bool has_oops() const;
  61 
  62   inline void mark_unregistered();
  63   inline bool is_unregistered() const;
  64 
  65   static ShenandoahNMethod* for_nmethod(nmethod* nm);
  66   static inline ShenandoahReentrantLock* lock_for_nmethod(nmethod* nm);
  67 
  68   // Keep nmethod's metadata alive
  69   static void keep_metadata_alive(nmethod* nm);
  70 
  71   static void heal_nmethod(nmethod* nm);
  72   static inline void disarm_nmethod(nmethod* nm);
  73 
  74   static inline ShenandoahNMethod* gc_data(nmethod* nm);
  75   static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
  76 
  77   void assert_alive_and_correct() NOT_DEBUG_RETURN;
  78   void assert_same_oops(bool allow_dead = false) NOT_DEBUG_RETURN;
  79   static void assert_no_oops(nmethod* nm, bool allow_dea = false) NOT_DEBUG_RETURN;
  80 
  81 private:
  82   bool has_non_immed_oops() const { return _has_non_immed_oops; }
  83   static void detect_reloc_oops(nmethod* nm, GrowableArray<oop*>& oops, bool& _has_non_immed_oops);
  84 };
  85 
  86 class ShenandoahNMethodTable;
  87 
  88 // An opaque snapshot of current nmethod table for iteration
  89 class ShenandoahNMethodTableSnapshot : public CHeapObj<mtGC> {
  90   friend class ShenandoahNMethodTable;
  91 private:
  92   ShenandoahHeap* const       _heap;
  93   ShenandoahNMethodTable*     _table;
  94   ShenandoahNMethod** const   _array;
  95   const int                   _length;
  96 
  97   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
  98   volatile size_t       _claimed;
  99   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
 100 
 101 public:
 102   ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
 103 
 104   template<bool CSET_FILTER>
 105   void parallel_blobs_do(CodeBlobClosure *f);
 106 
 107   void concurrent_nmethods_do(NMethodClosure* cl);
 108 };
 109 
 110 class ShenandoahNMethodTable : public CHeapObj<mtGC> {
 111   friend class ShenandoahNMethodTableSnapshot;
 112 private:
 113   enum {
 114     minSize = 1024
 115   };
 116 
 117   ShenandoahHeap* const _heap;
 118   ShenandoahNMethod**   _array;
 119   int                   _size;
 120   int                   _index;
 121   ShenandoahLock        _lock;
 122   bool                  _iteration_in_progress;
 123 
 124 public:
 125   ShenandoahNMethodTable();
 126   ~ShenandoahNMethodTable();
 127 
 128   void register_nmethod(nmethod* nm);
 129   void unregister_nmethod(nmethod* nm);
 130   void flush_nmethod(nmethod* nm);
 131 
 132   bool contain(nmethod* nm) const;
 133   int length() const { return _index; }
 134 
 135   // Table iteration support
 136   ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
 137   void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
 138 
 139   void assert_nmethods_alive_and_correct() NOT_DEBUG_RETURN;
 140 private:
 141   // Rebuild table and replace current one
 142   void rebuild(int size);
 143 
 144   bool is_full() const {
 145     assert(_index <= _size, "Sanity");
 146     return _index == _size;
 147   }
 148 
 149   ShenandoahNMethod* at(int index) const;
 150   int  index_of(nmethod* nm) const;
 151   void remove(int index);
 152   void append(ShenandoahNMethod* snm);
 153 
 154   inline bool iteration_in_progress() const;
 155   void wait_until_concurrent_iteration_done();
 156 
 157   // Logging support
 158   void log_register_nmethod(nmethod* nm);
 159   void log_unregister_nmethod(nmethod* nm);
 160   void log_flush_nmethod(nmethod* nm);
 161 };
 162 
 163 class ShenandoahConcurrentNMethodIterator {
 164 private:
 165   ShenandoahNMethodTable*         const _table;
 166   ShenandoahNMethodTableSnapshot*       _table_snapshot;
 167 
 168 public:
 169   ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
 170 
 171   void nmethods_do_begin();
 172   void nmethods_do(NMethodClosure* cl);
 173   void nmethods_do_end();
 174 };
 175 
 176 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP