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