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 "runtime/atomic.hpp"
 34 #include "utilities/growableArray.hpp"
 35 
 36 // Use ShenandoahReentrantLock as ShenandoahNMethodLock
 37 typedef ShenandoahReentrantLock<ShenandoahSimpleLock> ShenandoahNMethodLock;
 38 typedef ShenandoahLocker<ShenandoahNMethodLock>       ShenandoahNMethodLocker;
 39 
 40 // ShenandoahNMethod tuple records the internal locations of oop slots within reclocation stream in
 41 // the nmethod. This allows us to quickly scan the oops without doing the nmethod-internal scans,
 42 // that sometimes involves parsing the machine code. Note it does not record the oops themselves,
 43 // because it would then require handling these tuples as the new class of roots.
 44 class ShenandoahNMethod : public CHeapObj<mtGC> {
 45 private:
 46   nmethod* const          _nm;
 47   oop**                   _oops;
 48   int                     _oops_count;
 49   bool                    _has_non_immed_oops;
 50   bool                    _unregistered;
 51   ShenandoahNMethodLock   _lock;
 52   ShenandoahNMethodLock   _ic_lock;
 53 
 54 public:
 55   ShenandoahNMethod(nmethod *nm, GrowableArray<oop*>& oops, bool has_non_immed_oops);
 56   ~ShenandoahNMethod();
 57 
 58   inline nmethod* nm() const;
 59   inline ShenandoahNMethodLock* lock();
 60   inline ShenandoahNMethodLock* ic_lock();
 61   inline void oops_do(OopClosure* oops, bool fix_relocations = false);
 62   // Update oops when the nmethod is re-registered
 63   void update();
 64 
 65   inline bool is_unregistered() const;
 66 
 67   static ShenandoahNMethod* for_nmethod(nmethod* nm);
 68   static inline ShenandoahNMethodLock* lock_for_nmethod(nmethod* nm);
 69   static inline ShenandoahNMethodLock* ic_lock_for_nmethod(nmethod* nm);
 70 
 71   static void heal_nmethod(nmethod* nm);
 72   static inline void heal_nmethod_metadata(ShenandoahNMethod* nmethod_data);
 73   static inline void disarm_nmethod(nmethod* nm);
 74 
 75   static inline ShenandoahNMethod* gc_data(nmethod* nm);
 76   static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
 77 
 78   void assert_correct() NOT_DEBUG_RETURN;
 79   void assert_same_oops() NOT_DEBUG_RETURN;
 80 
 81 private:
 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   Atomic<size_t>            _claimed;
124   shenandoah_padding(1);
125 
126 public:
127   ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
128   ~ShenandoahNMethodTableSnapshot();
129 
130   void parallel_nmethods_do(NMethodClosure *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 
155   bool contain(nmethod* nm) const;
156   int length() const { return _index; }
157 
158   // Table iteration support
159   ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
160   void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
161 
162   void assert_nmethods_correct() NOT_DEBUG_RETURN;
163 private:
164   // Rebuild table and replace current one
165   void rebuild(int size);
166 
167   bool is_full() const {
168     assert(_index <= _list->size(), "Sanity");
169     return _index == _list->size();
170   }
171 
172   ShenandoahNMethod* at(int index) const;
173   int  index_of(nmethod* nm) const;
174   void remove(int index);
175   void append(ShenandoahNMethod* snm);
176 
177   inline bool iteration_in_progress() const;
178   void wait_until_concurrent_iteration_done();
179 
180   // Logging support
181   void log_register_nmethod(nmethod* nm);
182   void log_unregister_nmethod(nmethod* nm);
183 };
184 
185 class ShenandoahConcurrentNMethodIterator {
186 private:
187   ShenandoahNMethodTable*         const _table;
188   ShenandoahNMethodTableSnapshot*       _table_snapshot;
189   uint                                  _started_workers;
190   uint                                  _finished_workers;
191 
192 public:
193   ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
194 
195   void nmethods_do(NMethodClosure* cl);
196 };
197 
198 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP