1 /*
2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
25 #define SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
26 
27 #include "code/nmethod.hpp"
28 #include "gc/shenandoah/shenandoahHeap.hpp"
29 #include "gc/shenandoah/shenandoahLock.hpp"
30 #include "gc/shenandoah/shenandoahPadding.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   static void heal_nmethod(nmethod* nm);
69   static inline void disarm_nmethod(nmethod* nm);
70 
71   static inline ShenandoahNMethod* gc_data(nmethod* nm);
72   static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
73 
74   void assert_alive_and_correct() NOT_DEBUG_RETURN;
75   void assert_same_oops(bool allow_dead = false) NOT_DEBUG_RETURN;
76   static void assert_no_oops(nmethod* nm, bool allow_dea = false) NOT_DEBUG_RETURN;
77 
78 private:
has_non_immed_oops() const79   bool has_non_immed_oops() const { return _has_non_immed_oops; }
80   static void detect_reloc_oops(nmethod* nm, GrowableArray<oop*>& oops, bool& _has_non_immed_oops);
81 };
82 
83 class ShenandoahNMethodTable;
84 
85 // An opaque snapshot of current nmethod table for iteration
86 class ShenandoahNMethodTableSnapshot : public CHeapObj<mtGC> {
87   friend class ShenandoahNMethodTable;
88 private:
89   ShenandoahHeap* const       _heap;
90   ShenandoahNMethodTable*     _table;
91   ShenandoahNMethod** const   _array;
92   const int                   _length;
93 
94   shenandoah_padding(0);
95   volatile size_t       _claimed;
96   shenandoah_padding(1);
97 
98 public:
99   ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
100 
101   template<bool CSET_FILTER>
102   void parallel_blobs_do(CodeBlobClosure *f);
103 
104   void concurrent_nmethods_do(NMethodClosure* cl);
105 };
106 
107 class ShenandoahNMethodTable : public CHeapObj<mtGC> {
108   friend class ShenandoahNMethodTableSnapshot;
109 private:
110   enum {
111     minSize = 1024
112   };
113 
114   ShenandoahHeap* const _heap;
115   ShenandoahNMethod**   _array;
116   int                   _size;
117   int                   _index;
118   ShenandoahLock        _lock;
119   bool                  _iteration_in_progress;
120 
121 public:
122   ShenandoahNMethodTable();
123   ~ShenandoahNMethodTable();
124 
125   void register_nmethod(nmethod* nm);
126   void unregister_nmethod(nmethod* nm);
127   void flush_nmethod(nmethod* nm);
128 
129   bool contain(nmethod* nm) const;
length() const130   int length() const { return _index; }
131 
132   // Table iteration support
133   ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
134   void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
135 
136   void assert_nmethods_alive_and_correct() NOT_DEBUG_RETURN;
137 private:
138   // Rebuild table and replace current one
139   void rebuild(int size);
140 
is_full() const141   bool is_full() const {
142     assert(_index <= _size, "Sanity");
143     return _index == _size;
144   }
145 
146   ShenandoahNMethod* at(int index) const;
147   int  index_of(nmethod* nm) const;
148   void remove(int index);
149   void append(ShenandoahNMethod* snm);
150 
151   inline bool iteration_in_progress() const;
152   void wait_until_concurrent_iteration_done();
153 
154   // Logging support
155   void log_register_nmethod(nmethod* nm);
156   void log_unregister_nmethod(nmethod* nm);
157   void log_flush_nmethod(nmethod* nm);
158 };
159 
160 class ShenandoahConcurrentNMethodIterator {
161 private:
162   ShenandoahNMethodTable*         const _table;
163   ShenandoahNMethodTableSnapshot*       _table_snapshot;
164 
165 public:
166   ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
167 
168   void nmethods_do_begin();
169   void nmethods_do(NMethodClosure* cl);
170   void nmethods_do_end();
171 };
172 
173 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
174