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