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