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