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 #include "precompiled.hpp" 26 27 #include "gc/shenandoah/shenandoahClosures.inline.hpp" 28 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp" 29 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 30 #include "gc/shenandoah/shenandoahNMethod.inline.hpp" 31 #include "memory/resourceArea.hpp" 32 33 ShenandoahNMethod::ShenandoahNMethod(nmethod* nm, GrowableArray<oop*>& oops, bool non_immediate_oops) : 34 _nm(nm), _oops(NULL), _oops_count(0), _unregistered(false) { 35 36 if (!oops.is_empty()) { 37 _oops_count = oops.length(); 38 _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC); 39 for (int c = 0; c < _oops_count; c++) { 40 _oops[c] = oops.at(c); 41 } 42 } 43 _has_non_immed_oops = non_immediate_oops; 44 45 assert_same_oops(); 46 } 47 48 ShenandoahNMethod::~ShenandoahNMethod() { 49 if (_oops != NULL) { 50 FREE_C_HEAP_ARRAY(oop*, _oops); 51 } 52 } 53 54 class ShenandoahHasCSetOopClosure : public OopClosure { 55 private: 56 ShenandoahHeap* const _heap; 57 bool _has_cset_oops; 58 59 public: 60 ShenandoahHasCSetOopClosure() : 61 _heap(ShenandoahHeap::heap()), 62 _has_cset_oops(false) { 63 } 64 65 bool has_cset_oops() const { 66 return _has_cset_oops; 67 } 68 69 void do_oop(oop* p) { 70 oop value = RawAccess<>::oop_load(p); 71 if (!_has_cset_oops && _heap->in_collection_set(value)) { 72 _has_cset_oops = true; 73 } 74 } 75 76 void do_oop(narrowOop* p) { 77 ShouldNotReachHere(); 78 } 79 }; 80 81 bool ShenandoahNMethod::has_cset_oops(ShenandoahHeap *heap) { 82 ShenandoahHasCSetOopClosure cl; 83 oops_do(&cl); 84 return cl.has_cset_oops(); 85 } 86 87 void ShenandoahNMethod::update() { 88 ResourceMark rm; 89 bool non_immediate_oops = false; 90 GrowableArray<oop*> oops; 91 92 detect_reloc_oops(nm(), oops, non_immediate_oops); 93 if (oops.length() != _oops_count) { 94 if (_oops != NULL) { 95 FREE_C_HEAP_ARRAY(oop*, _oops); 96 _oops = NULL; 97 } 98 99 _oops_count = oops.length(); 100 if (_oops_count > 0) { 101 _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC); 102 } 103 } 104 105 for (int index = 0; index < _oops_count; index ++) { 106 _oops[index] = oops.at(index); 107 } 108 _has_non_immed_oops = non_immediate_oops; 109 110 assert_same_oops(); 111 } 112 113 void ShenandoahNMethod::oops_do(OopClosure* oops, bool fix_relocations) { 114 for (int c = 0; c < _oops_count; c ++) { 115 oops->do_oop(_oops[c]); 116 } 117 118 oop* const begin = _nm->oops_begin(); 119 oop* const end = _nm->oops_end(); 120 for (oop* p = begin; p < end; p++) { 121 if (*p != Universe::non_oop_word()) { 122 oops->do_oop(p); 123 } 124 } 125 126 if (fix_relocations && _has_non_immed_oops) { 127 _nm->fix_oop_relocations(); 128 } 129 } 130 131 void ShenandoahNMethod::detect_reloc_oops(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops) { 132 has_non_immed_oops = false; 133 // Find all oops relocations 134 RelocIterator iter(nm); 135 while (iter.next()) { 136 if (iter.type() != relocInfo::oop_type) { 137 // Not an oop 138 continue; 139 } 140 141 oop_Relocation* r = iter.oop_reloc(); 142 if (!r->oop_is_immediate()) { 143 // Non-immediate oop found 144 has_non_immed_oops = true; 145 continue; 146 } 147 148 oop value = r->oop_value(); 149 if (value != NULL) { 150 oop* addr = r->oop_addr(); 151 shenandoah_assert_correct(addr, value); 152 shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc()); 153 shenandoah_assert_not_forwarded(addr, value); 154 // Non-NULL immediate oop found. NULL oops can safely be 155 // ignored since the method will be re-registered if they 156 // are later patched to be non-NULL. 157 oops.push(addr); 158 } 159 } 160 } 161 162 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) { 163 ResourceMark rm; 164 bool non_immediate_oops = false; 165 GrowableArray<oop*> oops; 166 167 detect_reloc_oops(nm, oops, non_immediate_oops); 168 169 // No embedded oops 170 if(!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() && 171 oops.is_empty() && nm->oops_begin() >= nm->oops_end()) { 172 return NULL; 173 } 174 175 return new ShenandoahNMethod(nm, oops, non_immediate_oops); 176 } 177 178 void ShenandoahNMethod::heal_nmethod(nmethod* nm) { 179 assert(ShenandoahHeap::heap()->is_concurrent_root_in_progress(), "Only this phase"); 180 ShenandoahNMethod* data = gc_data(nm); 181 assert(data != NULL, "Sanity"); 182 assert(data->lock()->owned_by_self(), "Must hold the lock"); 183 184 ShenandoahEvacOOMScope evac_scope; 185 ShenandoahEvacuateUpdateRootsClosure<> cl; 186 data->oops_do(&cl, true /*fix relocation*/); 187 } 188 189 #ifdef ASSERT 190 void ShenandoahNMethod::assert_alive_and_correct() { 191 assert(_nm->is_alive(), "only alive nmethods here"); 192 ShenandoahHeap* heap = ShenandoahHeap::heap(); 193 for (int c = 0; c < _oops_count; c++) { 194 oop *loc = _oops[c]; 195 assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*"); 196 oop o = RawAccess<>::oop_load(loc); 197 shenandoah_assert_correct_except(loc, o, o == NULL || heap->is_full_gc_move_in_progress()); 198 } 199 200 oop* const begin = _nm->oops_begin(); 201 oop* const end = _nm->oops_end(); 202 for (oop* p = begin; p < end; p++) { 203 if (*p != Universe::non_oop_word()) { 204 oop o = RawAccess<>::oop_load(p); 205 shenandoah_assert_correct_except(p, o, o == NULL || heap->is_full_gc_move_in_progress()); 206 } 207 } 208 } 209 210 class ShenandoahNMethodOopDetector : public OopClosure { 211 private: 212 ResourceMark rm; // For growable array allocation below. 213 GrowableArray<oop*> _oops; 214 215 public: 216 ShenandoahNMethodOopDetector() : _oops(10) {}; 217 218 void do_oop(oop* o) { 219 _oops.append(o); 220 } 221 void do_oop(narrowOop* o) { 222 fatal("NMethods should not have compressed oops embedded."); 223 } 224 225 GrowableArray<oop*>* oops() { 226 return &_oops; 227 } 228 229 bool has_oops() { 230 return !_oops.is_empty(); 231 } 232 }; 233 234 void ShenandoahNMethod::assert_same_oops(bool allow_dead) { 235 ShenandoahNMethodOopDetector detector; 236 nm()->oops_do(&detector, allow_dead); 237 238 GrowableArray<oop*>* oops = detector.oops(); 239 240 int count = _oops_count; 241 for (int index = 0; index < _oops_count; index ++) { 242 assert(oops->contains(_oops[index]), "Must contain this oop"); 243 } 244 245 for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) { 246 if (*p == Universe::non_oop_word()) continue; 247 count++; 248 assert(oops->contains(p), "Must contain this oop"); 249 } 250 251 #ifdef ASSERT 252 if (oops->length() < count) { 253 stringStream debug_stream; 254 debug_stream.print_cr("detected locs: %d", oops->length()); 255 for (int i = 0; i < oops->length(); i++) { 256 debug_stream.print_cr("-> " PTR_FORMAT, p2i(oops->at(i))); 257 } 258 debug_stream.print_cr("recorded oops: %d", _oops_count); 259 for (int i = 0; i < _oops_count; i++) { 260 debug_stream.print_cr("-> " PTR_FORMAT, p2i(_oops[i])); 261 } 262 GrowableArray<oop*> check; 263 bool non_immed; 264 detect_reloc_oops(nm(), check, non_immed); 265 debug_stream.print_cr("check oops: %d", check.length()); 266 for (int i = 0; i < check.length(); i++) { 267 debug_stream.print_cr("-> " PTR_FORMAT, p2i(check.at(i))); 268 } 269 fatal("Must match #detected: %d, #recorded: %d, #total: %d, begin: " PTR_FORMAT ", end: " PTR_FORMAT "\n%s", 270 oops->length(), _oops_count, count, p2i(nm()->oops_begin()), p2i(nm()->oops_end()), debug_stream.as_string()); 271 } 272 #endif 273 } 274 275 void ShenandoahNMethod::assert_no_oops(nmethod* nm, bool allow_dead) { 276 ShenandoahNMethodOopDetector detector; 277 nm->oops_do(&detector, allow_dead); 278 assert(detector.oops()->length() == 0, "Should not have oops"); 279 } 280 #endif 281 282 ShenandoahNMethodTable::ShenandoahNMethodTable() : 283 _heap(ShenandoahHeap::heap()), 284 _size(minSize), 285 _index(0), 286 _iteration_in_progress(false) { 287 _array = NEW_C_HEAP_ARRAY(ShenandoahNMethod*, _size, mtGC); 288 } 289 290 ShenandoahNMethodTable::~ShenandoahNMethodTable() { 291 assert(_array != NULL, "Sanity"); 292 FREE_C_HEAP_ARRAY(ShenandoahNMethod*, _array); 293 } 294 295 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) { 296 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held"); 297 assert(_index >= 0 && _index <= _size, "Sanity"); 298 299 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm); 300 ShenandoahReentrantLocker data_locker(data != NULL ? data->lock() : NULL); 301 302 if (data != NULL) { 303 assert(contain(nm), "Must have been registered"); 304 assert(nm == data->nm(), "Must be same nmethod"); 305 data->update(); 306 } else { 307 data = ShenandoahNMethod::for_nmethod(nm); 308 if (data == NULL) { 309 assert(!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 310 "Only possible when concurrent class unloading is off"); 311 return; 312 } 313 ShenandoahNMethod::attach_gc_data(nm, data); 314 ShenandoahLocker locker(&_lock); 315 log_register_nmethod(nm); 316 append(data); 317 } 318 // Disarm new nmethod 319 ShenandoahNMethod::disarm_nmethod(nm); 320 } 321 322 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) { 323 assert_locked_or_safepoint(CodeCache_lock); 324 325 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm); 326 if (data == NULL) { 327 assert(!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 328 "Only possible when concurrent class unloading is off"); 329 ShenandoahNMethod::assert_no_oops(nm, true /*allow_dead*/); 330 return; 331 } 332 333 if (Thread::current()->is_Code_cache_sweeper_thread()) { 334 wait_until_concurrent_iteration_done(); 335 } 336 log_unregister_nmethod(nm); 337 ShenandoahLocker locker(&_lock); 338 assert(contain(nm), "Must have been registered"); 339 340 ShenandoahReentrantLocker data_locker(data->lock()); 341 data->mark_unregistered(); 342 } 343 344 void ShenandoahNMethodTable::flush_nmethod(nmethod* nm) { 345 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held"); 346 assert(Thread::current()->is_Code_cache_sweeper_thread(), "Must from Sweep thread"); 347 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm); 348 assert(data != NULL || !ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 349 "Only possible when concurrent class unloading is off"); 350 if (data == NULL) { 351 ShenandoahNMethod::assert_no_oops(nm, true /*allow_dead*/); 352 return; 353 } 354 355 // Can not alter the array when iteration is in progress 356 wait_until_concurrent_iteration_done(); 357 log_flush_nmethod(nm); 358 359 ShenandoahLocker locker(&_lock); 360 int idx = index_of(nm); 361 assert(idx >= 0 && idx < _index, "Invalid index"); 362 ShenandoahNMethod::attach_gc_data(nm, NULL); 363 remove(idx); 364 } 365 366 bool ShenandoahNMethodTable::contain(nmethod* nm) const { 367 return index_of(nm) != -1; 368 } 369 370 ShenandoahNMethod* ShenandoahNMethodTable::at(int index) const { 371 assert(index >= 0 && index < _index, "Out of bound"); 372 return _array[index]; 373 } 374 375 int ShenandoahNMethodTable::index_of(nmethod* nm) const { 376 for (int index = 0; index < length(); index ++) { 377 if (_array[index]->nm() == nm) { 378 return index; 379 } 380 } 381 return -1; 382 } 383 384 void ShenandoahNMethodTable::remove(int idx) { 385 shenandoah_assert_locked_or_safepoint(CodeCache_lock); 386 assert(!_iteration_in_progress, "Can not happen"); 387 assert(_index >= 0 && _index <= _size, "Sanity"); 388 389 assert(idx >= 0 && idx < _index, "Out of bound"); 390 ShenandoahNMethod* snm = _array[idx]; 391 392 _index --; 393 _array[idx] = _array[_index]; 394 395 delete snm; 396 } 397 398 void ShenandoahNMethodTable::wait_until_concurrent_iteration_done() { 399 assert(CodeCache_lock->owned_by_self(), "Lock must be held"); 400 while (iteration_in_progress()) { 401 CodeCache_lock->wait_without_safepoint_check(); 402 } 403 } 404 405 void ShenandoahNMethodTable::append(ShenandoahNMethod* snm) { 406 if (is_full()) { 407 int new_size = 2 * _size; 408 ShenandoahNMethod** old_table = _array; 409 410 // Rebuild table and replace current one 411 rebuild(new_size); 412 413 // An iteration is in progress over early snapshot, 414 // can not release the array until iteration is completed 415 if (!iteration_in_progress()) { 416 FREE_C_HEAP_ARRAY(ShenandoahNMethod*, old_table); 417 } 418 } 419 420 _array[_index ++] = snm; 421 assert(_index >= 0 && _index <= _size, "Sanity"); 422 } 423 424 void ShenandoahNMethodTable::rebuild(int size) { 425 ShenandoahNMethod** arr = NEW_C_HEAP_ARRAY(ShenandoahNMethod*, size, mtGC); 426 for (int index = 0; index < _index; index ++) { 427 arr[index] = _array[index]; 428 } 429 _array = arr; 430 _size = size; 431 } 432 433 ShenandoahNMethodTableSnapshot* ShenandoahNMethodTable::snapshot_for_iteration() { 434 assert(!iteration_in_progress(), "Already in progress"); 435 _iteration_in_progress = true; 436 437 return new ShenandoahNMethodTableSnapshot(this); 438 } 439 440 void ShenandoahNMethodTable::finish_iteration(ShenandoahNMethodTableSnapshot* snapshot) { 441 assert(iteration_in_progress(), "Why we here?"); 442 assert(snapshot != NULL, "No snapshot"); 443 _iteration_in_progress = false; 444 445 // Table has been rebuilt during iteration, free old table 446 if (snapshot->_array != _array) { 447 FREE_C_HEAP_ARRAY(ShenandoahNMethod*, snapshot->_array); 448 } 449 delete snapshot; 450 } 451 452 void ShenandoahNMethodTable::log_register_nmethod(nmethod* nm) { 453 LogTarget(Debug, gc, nmethod) log; 454 if (!log.is_enabled()) { 455 return; 456 } 457 458 ResourceMark rm; 459 log.print("Register NMethod: %s.%s [" PTR_FORMAT "] (%s)", 460 nm->method()->method_holder()->external_name(), 461 nm->method()->name()->as_C_string(), 462 p2i(nm), 463 nm->compiler_name()); 464 } 465 466 void ShenandoahNMethodTable::log_unregister_nmethod(nmethod* nm) { 467 LogTarget(Debug, gc, nmethod) log; 468 if (!log.is_enabled()) { 469 return; 470 } 471 472 ResourceMark rm; 473 log.print("Unregister NMethod: %s.%s [" PTR_FORMAT "]", 474 nm->method()->method_holder()->external_name(), 475 nm->method()->name()->as_C_string(), 476 p2i(nm)); 477 } 478 479 void ShenandoahNMethodTable::log_flush_nmethod(nmethod* nm) { 480 LogTarget(Debug, gc, nmethod) log; 481 if (!log.is_enabled()) { 482 return; 483 } 484 485 ResourceMark rm; 486 log.print("Flush NMethod: (" PTR_FORMAT ")", p2i(nm)); 487 } 488 489 #ifdef ASSERT 490 void ShenandoahNMethodTable::assert_nmethods_alive_and_correct() { 491 assert_locked_or_safepoint(CodeCache_lock); 492 493 for (int index = 0; index < length(); index ++) { 494 ShenandoahNMethod* m = _array[index]; 495 // Concurrent unloading may have dead nmethods to be cleaned by sweeper 496 if (m->is_unregistered()) continue; 497 m->assert_alive_and_correct(); 498 } 499 } 500 #endif 501 502 ShenandoahNMethodTableSnapshot::ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table) : 503 _heap(ShenandoahHeap::heap()), _table(table), _array(table->_array), _length(table->_index), _claimed(0) { 504 } 505 506 void ShenandoahNMethodTableSnapshot::concurrent_nmethods_do(NMethodClosure* cl) { 507 size_t stride = 256; // educated guess 508 509 ShenandoahNMethod** list = _array; 510 size_t max = (size_t)_length; 511 while (_claimed < max) { 512 size_t cur = Atomic::fetch_and_add(&_claimed, stride); 513 size_t start = cur; 514 size_t end = MIN2(cur + stride, max); 515 if (start >= max) break; 516 517 for (size_t idx = start; idx < end; idx++) { 518 ShenandoahNMethod* data = list[idx]; 519 assert(data != NULL, "Should not be NULL"); 520 if (!data->is_unregistered()) { 521 cl->do_nmethod(data->nm()); 522 } 523 } 524 } 525 } 526 527 ShenandoahConcurrentNMethodIterator::ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table) : 528 _table(table), _table_snapshot(NULL) { 529 } 530 531 void ShenandoahConcurrentNMethodIterator::nmethods_do_begin() { 532 assert(CodeCache_lock->owned_by_self(), "Lock must be held"); 533 assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 534 "Only for concurrent class unloading"); 535 _table_snapshot = _table->snapshot_for_iteration(); 536 } 537 538 void ShenandoahConcurrentNMethodIterator::nmethods_do(NMethodClosure* cl) { 539 assert(_table_snapshot != NULL, "Must first call nmethod_do_begin()"); 540 _table_snapshot->concurrent_nmethods_do(cl); 541 } 542 543 void ShenandoahConcurrentNMethodIterator::nmethods_do_end() { 544 assert(CodeCache_lock->owned_by_self(), "Lock must be held"); 545 assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 546 "Only for concurrent class unloading"); 547 _table->finish_iteration(_table_snapshot); 548 CodeCache_lock->notify_all(); 549 }