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 }
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);
|
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, GrowableArray<ShenandoahNMethodBarrier>& barriers) :
35 _nm(nm), _oops(nullptr), _oops_count(0), _barriers(nullptr), _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 if (!barriers.is_empty()) {
49 _barriers_count = barriers.length();
50 _barriers = NEW_C_HEAP_ARRAY(ShenandoahNMethodBarrier, _barriers_count, mtGC);
51 for (int c = 0; c < _barriers_count; c++) {
52 _barriers[c] = barriers.at(c);
53 }
54 }
55 }
56
57 ShenandoahNMethod::~ShenandoahNMethod() {
58 if (_oops != nullptr) {
59 FREE_C_HEAP_ARRAY(oop*, _oops);
60 }
61 if (_barriers != nullptr) {
62 FREE_C_HEAP_ARRAY(ShenandoahNMethodBarrier, _barriers);
63 }
64 }
65
66 void ShenandoahNMethod::update() {
67 ResourceMark rm;
68 bool non_immediate_oops = false;
69 GrowableArray<oop*> oops;
70 GrowableArray<ShenandoahNMethodBarrier> barriers;
71
72 parse(nm(), oops, non_immediate_oops, barriers);
73 if (oops.length() != _oops_count) {
74 if (_oops != nullptr) {
75 FREE_C_HEAP_ARRAY(oop*, _oops);
76 _oops = nullptr;
77 }
78
79 _oops_count = oops.length();
80 if (_oops_count > 0) {
81 _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
82 }
83 }
84
85 for (int index = 0; index < _oops_count; index ++) {
86 _oops[index] = oops.at(index);
87 }
88 _has_non_immed_oops = non_immediate_oops;
89
90 assert_same_oops();
91 }
92
93 void ShenandoahNMethod::parse(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers) {
94 has_non_immed_oops = false;
95 RelocIterator iter(nm);
96 while (iter.next()) {
97 switch (iter.type()) {
98 case relocInfo::oop_type: {
99 oop_Relocation* r = iter.oop_reloc();
100 if (!r->oop_is_immediate()) {
101 // Non-immediate oop found
102 has_non_immed_oops = true;
103 break;
104 }
105
106 oop value = r->oop_value();
107 if (value != nullptr) {
108 oop* addr = r->oop_addr();
109 shenandoah_assert_correct(addr, value);
110 shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc());
111 shenandoah_assert_not_forwarded(addr, value);
112 // Non-null immediate oop found. null oops can safely be
113 // ignored since the method will be re-registered if they
114 // are later patched to be non-null.
115 oops.push(addr);
116 }
117 break;
118 }
119 case relocInfo::barrier_type: {
120 assert(ShenandoahGCStateCheckHotpatch, "Who emits these?");
121 barrier_Relocation* r = iter.barrier_reloc();
122
123 // TODO: Move to assembler?
124 #ifdef AMD64
125 NativeInstruction* ni = nativeInstruction_at(r->addr());
126 assert(ni->is_jump(), "Initial code version: GC barrier fastpath must be a jump");
127 NativeJump* jmp = nativeJump_at(r->addr());
128
129 ShenandoahNMethodBarrier b;
130 b._barrier_pc = r->addr();
131 b._stub_addr = jmp->jump_destination();
132 // TODO: Can technically figure out which GC state we care about in this reloc.
133 // b._gc_state_fast_bit = r->format();
134 barriers.push(b);
135 #else
136 Unimplemented();
137 #endif
138
139 break;
140 }
141 default:
142 // We do not care about other relocations.
143 break;
144 }
145 }
146 }
147
148 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
149 ResourceMark rm;
150 bool non_immediate_oops = false;
151 GrowableArray<oop*> oops;
152 GrowableArray<ShenandoahNMethodBarrier> barriers;
153
154 parse(nm, oops, non_immediate_oops, barriers);
155 return new ShenandoahNMethod(nm, oops, non_immediate_oops, barriers);
156 }
157
158 void ShenandoahNMethod::heal_nmethod(nmethod* nm) {
159 ShenandoahNMethod* data = gc_data(nm);
160 assert(data != nullptr, "Sanity");
161 assert(data->lock()->owned_by_self(), "Must hold the lock");
162
163 ShenandoahHeap* const heap = ShenandoahHeap::heap();
164 if (heap->is_concurrent_weak_root_in_progress() ||
165 heap->is_concurrent_strong_root_in_progress()) {
166 ShenandoahEvacOOMScope evac_scope;
167 heal_nmethod_metadata(data);
168 } else if (heap->is_concurrent_mark_in_progress()) {
169 ShenandoahKeepAliveClosure cl;
170 data->oops_do(&cl);
171 } else {
172 // There is possibility that GC is cancelled when it arrives final mark.
173 // In this case, concurrent root phase is skipped and degenerated GC should be
174 // followed, where nmethods are disarmed.
175 }
176
177 // Update all barriers
178 data->update_barriers();
179 }
180
181 #ifdef AMD64
182 void insert_5_byte_nop(address pc) {
183 *(pc + 0) = 0x0F;
184 *(pc + 1) = 0x1F;
185 *(pc + 2) = 0x44;
186 *(pc + 3) = 0x00;
187 *(pc + 4) = 0x00;
188 ICache::invalidate_range(pc, 5);
189 }
190
191 bool is_5_byte_nop(address pc) {
192 if (*(pc + 0) != 0x0F) return false;
193 if (*(pc + 1) != 0x1F) return false;
194 if (*(pc + 2) != 0x44) return false;
195 if (*(pc + 3) != 0x00) return false;
196 if (*(pc + 4) != 0x00) return false;
197 return true;
198 }
199 #endif
200
201 void ShenandoahNMethod::update_barriers() {
202 if (!ShenandoahGCStateCheckHotpatch) return;
203
204 ShenandoahHeap* heap = ShenandoahHeap::heap();
205
206 for (int c = 0; c < _barriers_count; c++) {
207 ShenandoahNMethodBarrier& barrier = _barriers[c];
208
209 address pc = barrier._barrier_pc;
210 NativeInstruction* ni = nativeInstruction_at(pc);
211
212 // TODO: This should really be in assembler?
213 #ifdef AMD64
214 if (heap->is_idle()) {
215 // Heap is idle: insert nops
216 if (ni->is_jump()) {
217 insert_5_byte_nop(pc);
218 } else {
219 assert(is_5_byte_nop(pc), "Sanity: should already be nop at PC " PTR_FORMAT ": %02x%02x%02x%02x%02x",
220 p2i(pc), *(pc + 0), *(pc + 1), *(pc + 2), *(pc + 3), *(pc + 4));
221 }
222 } else {
223 // Heap is active: insert jumps to barrier stubs
224 if (is_5_byte_nop(pc)) {
225 NativeJump::insert(pc, barrier._stub_addr);
226 } else {
227 assert(ni->is_jump(), "Sanity: should already be jump at PC " PTR_FORMAT ": %02x%02x%02x%02x%02x",
228 p2i(pc), *(pc + 0), *(pc + 1), *(pc + 2), *(pc + 3), *(pc + 4));
229 }
230 }
231 #else
232 Unimplemented();
233 #endif
234 }
235 }
236
237 #ifdef ASSERT
238 void ShenandoahNMethod::assert_correct() {
239 ShenandoahHeap* heap = ShenandoahHeap::heap();
240 for (int c = 0; c < _oops_count; c++) {
241 oop *loc = _oops[c];
242 assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
243 oop o = RawAccess<>::oop_load(loc);
244 shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
245 }
246
247 oop* const begin = _nm->oops_begin();
248 oop* const end = _nm->oops_end();
249 for (oop* p = begin; p < end; p++) {
250 if (*p != Universe::non_oop_word()) {
251 oop o = RawAccess<>::oop_load(p);
252 shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
253 }
254 }
285 assert(oops->contains(_oops[index]), "Must contain this oop");
286 }
287
288 for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) {
289 if (*p == Universe::non_oop_word()) continue;
290 count++;
291 assert(oops->contains(p), "Must contain this oop");
292 }
293
294 if (oops->length() < count) {
295 stringStream debug_stream;
296 debug_stream.print_cr("detected locs: %d", oops->length());
297 for (int i = 0; i < oops->length(); i++) {
298 debug_stream.print_cr("-> " PTR_FORMAT, p2i(oops->at(i)));
299 }
300 debug_stream.print_cr("recorded oops: %d", _oops_count);
301 for (int i = 0; i < _oops_count; i++) {
302 debug_stream.print_cr("-> " PTR_FORMAT, p2i(_oops[i]));
303 }
304 GrowableArray<oop*> check;
305 GrowableArray<ShenandoahNMethodBarrier> barriers;
306 bool non_immed;
307 parse(nm(), check, non_immed, barriers);
308 debug_stream.print_cr("check oops: %d", check.length());
309 for (int i = 0; i < check.length(); i++) {
310 debug_stream.print_cr("-> " PTR_FORMAT, p2i(check.at(i)));
311 }
312 fatal("Must match #detected: %d, #recorded: %d, #total: %d, begin: " PTR_FORMAT ", end: " PTR_FORMAT "\n%s",
313 oops->length(), _oops_count, count, p2i(nm()->oops_begin()), p2i(nm()->oops_end()), debug_stream.freeze());
314 }
315 }
316 #endif
317
318 ShenandoahNMethodTable::ShenandoahNMethodTable() :
319 _heap(ShenandoahHeap::heap()),
320 _index(0),
321 _itr_cnt(0) {
322 _list = new ShenandoahNMethodList(minSize);
323 }
324
325 ShenandoahNMethodTable::~ShenandoahNMethodTable() {
326 assert(_list != nullptr, "Sanity");
327 _list->release();
328 }
329
330 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) {
331 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
332 assert(_index >= 0 && _index <= _list->size(), "Sanity");
333
334 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
335
336 if (data != nullptr) {
337 assert(contain(nm), "Must have been registered");
338 assert(nm == data->nm(), "Must be same nmethod");
339 // Prevent updating a nmethod while concurrent iteration is in progress.
340 wait_until_concurrent_iteration_done();
341 ShenandoahNMethodLocker data_locker(data->lock());
342 data->update();
343 data->update_barriers();
344 } else {
345 // For a new nmethod, we can safely append it to the list, because
346 // concurrent iteration will not touch it.
347 data = ShenandoahNMethod::for_nmethod(nm);
348 data->update_barriers();
349 assert(data != nullptr, "Sanity");
350 ShenandoahNMethod::attach_gc_data(nm, data);
351 ShenandoahLocker locker(&_lock);
352 log_register_nmethod(nm);
353 append(data);
354 }
355 // Disarm new nmethod
356 ShenandoahNMethod::disarm_nmethod(nm);
357 }
358
359 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) {
360 assert_locked_or_safepoint(CodeCache_lock);
361
362 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
363 assert(data != nullptr, "Sanity");
364 log_unregister_nmethod(nm);
365 ShenandoahLocker locker(&_lock);
366 assert(contain(nm), "Must have been registered");
367
368 int idx = index_of(nm);
|