7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26
27 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
29 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "runtime/continuation.hpp"
32 #include "runtime/safepointVerifiers.hpp"
33
34 ShenandoahNMethod::ShenandoahNMethod(nmethod* nm, GrowableArray<oop*>& oops, bool non_immediate_oops) :
35 _nm(nm), _oops(nullptr), _oops_count(0), _unregistered(false), _lock(), _ic_lock() {
36
37 if (!oops.is_empty()) {
38 _oops_count = oops.length();
39 _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
40 for (int c = 0; c < _oops_count; c++) {
41 _oops[c] = oops.at(c);
42 }
43 }
44 _has_non_immed_oops = non_immediate_oops;
45
46 assert_same_oops();
47 }
48
49 ShenandoahNMethod::~ShenandoahNMethod() {
50 if (_oops != nullptr) {
51 FREE_C_HEAP_ARRAY(oop*, _oops);
52 }
53 }
54
55 void ShenandoahNMethod::update() {
56 ResourceMark rm;
57 bool non_immediate_oops = false;
58 GrowableArray<oop*> oops;
59
60 detect_reloc_oops(nm(), oops, non_immediate_oops);
61 if (oops.length() != _oops_count) {
62 if (_oops != nullptr) {
63 FREE_C_HEAP_ARRAY(oop*, _oops);
64 _oops = nullptr;
65 }
66
67 _oops_count = oops.length();
68 if (_oops_count > 0) {
69 _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
70 }
71 }
72
73 for (int index = 0; index < _oops_count; index ++) {
74 _oops[index] = oops.at(index);
75 }
76 _has_non_immed_oops = non_immediate_oops;
77
78 assert_same_oops();
79 }
80
81 void ShenandoahNMethod::detect_reloc_oops(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops) {
82 has_non_immed_oops = false;
83 // Find all oops relocations
84 RelocIterator iter(nm);
85 while (iter.next()) {
86 if (iter.type() != relocInfo::oop_type) {
87 // Not an oop
88 continue;
89 }
90
91 oop_Relocation* r = iter.oop_reloc();
92 if (!r->oop_is_immediate()) {
93 // Non-immediate oop found
94 has_non_immed_oops = true;
95 continue;
96 }
97
98 oop value = r->oop_value();
99 if (value != nullptr) {
100 oop* addr = r->oop_addr();
101 shenandoah_assert_correct(addr, value);
102 shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc());
103 shenandoah_assert_not_forwarded(addr, value);
104 // Non-null immediate oop found. null oops can safely be
105 // ignored since the method will be re-registered if they
106 // are later patched to be non-null.
107 oops.push(addr);
108 }
109 }
110 }
111
112 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
113 ResourceMark rm;
114 bool non_immediate_oops = false;
115 GrowableArray<oop*> oops;
116
117 detect_reloc_oops(nm, oops, non_immediate_oops);
118 return new ShenandoahNMethod(nm, oops, non_immediate_oops);
119 }
120
121 void ShenandoahNMethod::heal_nmethod(nmethod* nm) {
122 ShenandoahNMethod* data = gc_data(nm);
123 assert(data != nullptr, "Sanity");
124 assert(data->lock()->owned_by_self(), "Must hold the lock");
125
126 ShenandoahHeap* const heap = ShenandoahHeap::heap();
127 if (heap->is_concurrent_weak_root_in_progress() ||
128 heap->is_concurrent_strong_root_in_progress()) {
129 ShenandoahEvacOOMScope evac_scope;
130 heal_nmethod_metadata(data);
131 } else if (heap->is_concurrent_mark_in_progress()) {
132 ShenandoahKeepAliveClosure cl;
133 data->oops_do(&cl);
134 } else {
135 // There is possibility that GC is cancelled when it arrives final mark.
136 // In this case, concurrent root phase is skipped and degenerated GC should be
137 // followed, where nmethods are disarmed.
138 }
139 }
140
141 #ifdef ASSERT
142 void ShenandoahNMethod::assert_correct() {
143 ShenandoahHeap* heap = ShenandoahHeap::heap();
144 for (int c = 0; c < _oops_count; c++) {
145 oop *loc = _oops[c];
146 assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
147 oop o = RawAccess<>::oop_load(loc);
148 shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
149 }
150
151 oop* const begin = _nm->oops_begin();
152 oop* const end = _nm->oops_end();
153 for (oop* p = begin; p < end; p++) {
154 if (*p != Universe::non_oop_word()) {
155 oop o = RawAccess<>::oop_load(p);
156 shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
157 }
158 }
159 }
160
189 assert(oops->contains(_oops[index]), "Must contain this oop");
190 }
191
192 for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) {
193 if (*p == Universe::non_oop_word()) continue;
194 count++;
195 assert(oops->contains(p), "Must contain this oop");
196 }
197
198 if (oops->length() < count) {
199 stringStream debug_stream;
200 debug_stream.print_cr("detected locs: %d", oops->length());
201 for (int i = 0; i < oops->length(); i++) {
202 debug_stream.print_cr("-> " PTR_FORMAT, p2i(oops->at(i)));
203 }
204 debug_stream.print_cr("recorded oops: %d", _oops_count);
205 for (int i = 0; i < _oops_count; i++) {
206 debug_stream.print_cr("-> " PTR_FORMAT, p2i(_oops[i]));
207 }
208 GrowableArray<oop*> check;
209 bool non_immed;
210 detect_reloc_oops(nm(), check, non_immed);
211 debug_stream.print_cr("check oops: %d", check.length());
212 for (int i = 0; i < check.length(); i++) {
213 debug_stream.print_cr("-> " PTR_FORMAT, p2i(check.at(i)));
214 }
215 fatal("Must match #detected: %d, #recorded: %d, #total: %d, begin: " PTR_FORMAT ", end: " PTR_FORMAT "\n%s",
216 oops->length(), _oops_count, count, p2i(nm()->oops_begin()), p2i(nm()->oops_end()), debug_stream.freeze());
217 }
218 }
219 #endif
220
221 ShenandoahNMethodTable::ShenandoahNMethodTable() :
222 _heap(ShenandoahHeap::heap()),
223 _index(0),
224 _itr_cnt(0) {
225 _list = new ShenandoahNMethodList(minSize);
226 }
227
228 ShenandoahNMethodTable::~ShenandoahNMethodTable() {
229 assert(_list != nullptr, "Sanity");
230 _list->release();
231 }
232
233 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) {
234 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
235 assert(_index >= 0 && _index <= _list->size(), "Sanity");
236
237 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
238
239 if (data != nullptr) {
240 assert(contain(nm), "Must have been registered");
241 assert(nm == data->nm(), "Must be same nmethod");
242 // Prevent updating a nmethod while concurrent iteration is in progress.
243 wait_until_concurrent_iteration_done();
244 ShenandoahNMethodLocker data_locker(data->lock());
245 data->update();
246 } else {
247 // For a new nmethod, we can safely append it to the list, because
248 // concurrent iteration will not touch it.
249 data = ShenandoahNMethod::for_nmethod(nm);
250 assert(data != nullptr, "Sanity");
251 ShenandoahNMethod::attach_gc_data(nm, data);
252 ShenandoahLocker locker(&_lock);
253 log_register_nmethod(nm);
254 append(data);
255 }
256 // Disarm new nmethod
257 ShenandoahNMethod::disarm_nmethod(nm);
258 }
259
260 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) {
261 assert_locked_or_safepoint(CodeCache_lock);
262
263 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
264 assert(data != nullptr, "Sanity");
265 log_unregister_nmethod(nm);
266 ShenandoahLocker locker(&_lock);
267 assert(contain(nm), "Must have been registered");
268
269 int idx = index_of(nm);
270 assert(idx >= 0 && idx < _index, "Invalid index");
271 ShenandoahNMethod::attach_gc_data(nm, nullptr);
272 remove(idx);
273 }
274
|
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26
27 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
28 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
30 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "runtime/continuation.hpp"
33 #include "runtime/safepointVerifiers.hpp"
34
35 ShenandoahNMethod::ShenandoahNMethod(nmethod* nm, GrowableArray<oop*>& oops, bool non_immediate_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers) :
36 _nm(nm), _oops(nullptr), _oops_count(0), _barriers(nullptr), _barriers_count(0), _unregistered(false), _lock(), _ic_lock() {
37
38 if (!oops.is_empty()) {
39 _oops_count = oops.length();
40 _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
41 for (int c = 0; c < _oops_count; c++) {
42 _oops[c] = oops.at(c);
43 }
44 }
45 _has_non_immed_oops = non_immediate_oops;
46
47 assert_same_oops();
48
49 if (!barriers.is_empty()) {
50 _barriers_count = barriers.length();
51 _barriers = NEW_C_HEAP_ARRAY(ShenandoahNMethodBarrier, _barriers_count, mtGC);
52 for (int c = 0; c < _barriers_count; c++) {
53 _barriers[c] = barriers.at(c);
54 }
55 }
56 }
57
58 ShenandoahNMethod::~ShenandoahNMethod() {
59 if (_oops != nullptr) {
60 FREE_C_HEAP_ARRAY(oop*, _oops);
61 }
62 if (_barriers != nullptr) {
63 FREE_C_HEAP_ARRAY(ShenandoahNMethodBarrier, _barriers);
64 }
65 }
66
67 void ShenandoahNMethod::update() {
68 ResourceMark rm;
69 bool non_immediate_oops = false;
70 GrowableArray<oop*> oops;
71 GrowableArray<ShenandoahNMethodBarrier> barriers;
72
73 parse(nm(), oops, non_immediate_oops, barriers);
74 if (oops.length() != _oops_count) {
75 if (_oops != nullptr) {
76 FREE_C_HEAP_ARRAY(oop*, _oops);
77 _oops = nullptr;
78 }
79
80 _oops_count = oops.length();
81 if (_oops_count > 0) {
82 _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
83 }
84 }
85
86 for (int index = 0; index < _oops_count; index ++) {
87 _oops[index] = oops.at(index);
88 }
89 _has_non_immed_oops = non_immediate_oops;
90
91 assert_same_oops();
92 }
93
94 void ShenandoahNMethod::parse(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers) {
95 has_non_immed_oops = false;
96 RelocIterator iter(nm);
97 while (iter.next()) {
98 switch (iter.type()) {
99 case relocInfo::oop_type: {
100 oop_Relocation* r = iter.oop_reloc();
101 if (!r->oop_is_immediate()) {
102 // Non-immediate oop found
103 has_non_immed_oops = true;
104 break;
105 }
106
107 oop value = r->oop_value();
108 if (value != nullptr) {
109 oop* addr = r->oop_addr();
110 shenandoah_assert_correct(addr, value);
111 shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc());
112 shenandoah_assert_not_forwarded(addr, value);
113 // Non-null immediate oop found. null oops can safely be
114 // ignored since the method will be re-registered if they
115 // are later patched to be non-null.
116 oops.push(addr);
117 }
118 break;
119 }
120 #ifdef COMPILER2
121 case relocInfo::barrier_type: {
122 assert(ShenandoahGCStateCheckHotpatch, "Who emits these?");
123 barrier_Relocation* r = iter.barrier_reloc();
124
125 ShenandoahNMethodBarrier b;
126 b._pc = r->addr();
127 b._stub_addr = ShenandoahBarrierSetAssembler::parse_stub_address(b._pc);
128 // TODO: Can technically figure out which GC state we care about in this reloc.
129 // b._gc_state_fast_bit = r->format();
130 barriers.push(b);
131 break;
132 }
133 #endif
134 default:
135 // We do not care about other relocations.
136 break;
137 }
138 }
139 }
140
141 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
142 ResourceMark rm;
143 bool non_immediate_oops = false;
144 GrowableArray<oop*> oops;
145 GrowableArray<ShenandoahNMethodBarrier> barriers;
146
147 parse(nm, oops, non_immediate_oops, barriers);
148 return new ShenandoahNMethod(nm, oops, non_immediate_oops, barriers);
149 }
150
151 void ShenandoahNMethod::heal_nmethod(nmethod* nm) {
152 ShenandoahNMethod* data = gc_data(nm);
153 assert(data != nullptr, "Sanity");
154 assert(data->lock()->owned_by_self(), "Must hold the lock");
155
156 ShenandoahHeap* const heap = ShenandoahHeap::heap();
157 if (heap->is_concurrent_weak_root_in_progress() ||
158 heap->is_concurrent_strong_root_in_progress()) {
159 ShenandoahEvacOOMScope evac_scope;
160 heal_nmethod_metadata(data);
161 } else if (heap->is_concurrent_mark_in_progress()) {
162 ShenandoahKeepAliveClosure cl;
163 data->oops_do(&cl);
164 } else {
165 // There is possibility that GC is cancelled when it arrives final mark.
166 // In this case, concurrent root phase is skipped and degenerated GC should be
167 // followed, where nmethods are disarmed.
168 }
169 }
170
171 void ShenandoahNMethod::update_barriers() {
172 #ifdef COMPILER2
173 if (!ShenandoahGCStateCheckHotpatch) {
174 return;
175 }
176
177 ShenandoahHeap* heap = ShenandoahHeap::heap();
178
179 for (int c = 0; c < _barriers_count; c++) {
180 address pc = _barriers[c]._pc;
181 address stub_addr = _barriers[c]._stub_addr;
182 if (heap->is_idle()) {
183 ShenandoahBarrierSetAssembler::patch_branch_to_nop(pc);
184 } else {
185 ShenandoahBarrierSetAssembler::patch_nop_to_branch(pc, stub_addr);
186 }
187 }
188 #endif
189 }
190
191 #ifdef ASSERT
192 void ShenandoahNMethod::assert_correct() {
193 ShenandoahHeap* heap = ShenandoahHeap::heap();
194 for (int c = 0; c < _oops_count; c++) {
195 oop *loc = _oops[c];
196 assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
197 oop o = RawAccess<>::oop_load(loc);
198 shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
199 }
200
201 oop* const begin = _nm->oops_begin();
202 oop* const end = _nm->oops_end();
203 for (oop* p = begin; p < end; p++) {
204 if (*p != Universe::non_oop_word()) {
205 oop o = RawAccess<>::oop_load(p);
206 shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
207 }
208 }
209 }
210
239 assert(oops->contains(_oops[index]), "Must contain this oop");
240 }
241
242 for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) {
243 if (*p == Universe::non_oop_word()) continue;
244 count++;
245 assert(oops->contains(p), "Must contain this oop");
246 }
247
248 if (oops->length() < count) {
249 stringStream debug_stream;
250 debug_stream.print_cr("detected locs: %d", oops->length());
251 for (int i = 0; i < oops->length(); i++) {
252 debug_stream.print_cr("-> " PTR_FORMAT, p2i(oops->at(i)));
253 }
254 debug_stream.print_cr("recorded oops: %d", _oops_count);
255 for (int i = 0; i < _oops_count; i++) {
256 debug_stream.print_cr("-> " PTR_FORMAT, p2i(_oops[i]));
257 }
258 GrowableArray<oop*> check;
259 GrowableArray<ShenandoahNMethodBarrier> barriers;
260 bool non_immed;
261 parse(nm(), check, non_immed, barriers);
262 debug_stream.print_cr("check oops: %d", check.length());
263 for (int i = 0; i < check.length(); i++) {
264 debug_stream.print_cr("-> " PTR_FORMAT, p2i(check.at(i)));
265 }
266 fatal("Must match #detected: %d, #recorded: %d, #total: %d, begin: " PTR_FORMAT ", end: " PTR_FORMAT "\n%s",
267 oops->length(), _oops_count, count, p2i(nm()->oops_begin()), p2i(nm()->oops_end()), debug_stream.freeze());
268 }
269 }
270 #endif
271
272 ShenandoahNMethodTable::ShenandoahNMethodTable() :
273 _heap(ShenandoahHeap::heap()),
274 _index(0),
275 _itr_cnt(0) {
276 _list = new ShenandoahNMethodList(minSize);
277 }
278
279 ShenandoahNMethodTable::~ShenandoahNMethodTable() {
280 assert(_list != nullptr, "Sanity");
281 _list->release();
282 }
283
284 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) {
285 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
286 assert(_index >= 0 && _index <= _list->size(), "Sanity");
287
288 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
289
290 if (data != nullptr) {
291 assert(contain(nm), "Must have been registered");
292 assert(nm == data->nm(), "Must be same nmethod");
293 // Prevent updating a nmethod while concurrent iteration is in progress.
294 wait_until_concurrent_iteration_done();
295 ShenandoahNMethodLocker data_locker(data->lock());
296 data->update();
297 data->update_barriers();
298 } else {
299 // For a new nmethod, we can safely append it to the list, because
300 // concurrent iteration will not touch it.
301 data = ShenandoahNMethod::for_nmethod(nm);
302 assert(data != nullptr, "Sanity");
303 ShenandoahNMethod::attach_gc_data(nm, data);
304 ShenandoahLocker locker(&_lock);
305 log_register_nmethod(nm);
306 append(data);
307 data->update_barriers();
308 }
309 // Disarm new nmethod
310 ShenandoahNMethod::disarm_nmethod(nm);
311 }
312
313 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) {
314 assert_locked_or_safepoint(CodeCache_lock);
315
316 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
317 assert(data != nullptr, "Sanity");
318 log_unregister_nmethod(nm);
319 ShenandoahLocker locker(&_lock);
320 assert(contain(nm), "Must have been registered");
321
322 int idx = index_of(nm);
323 assert(idx >= 0 && idx < _index, "Invalid index");
324 ShenandoahNMethod::attach_gc_data(nm, nullptr);
325 remove(idx);
326 }
327
|