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 assert(oops->length() == oop_count(), "Must match"); 241 242 for (int index = 0; index < _oops_count; index ++) { 243 assert(oops->contains(_oops[index]), "Must contain this oop"); 244 } 245 246 for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) { 247 assert(oops->contains(p), "Must contain this oop"); 248 } 249 } 250 251 void ShenandoahNMethod::assert_no_oops(nmethod* nm, bool allow_dead) { 252 ShenandoahNMethodOopDetector detector; 253 nm->oops_do(&detector, allow_dead); 254 assert(detector.oops()->length() == 0, "Should not have oops"); 255 } 256 #endif 257 258 ShenandoahNMethodTable::ShenandoahNMethodTable() : 259 _heap(ShenandoahHeap::heap()), 260 _size(minSize), 261 _index(0), 262 _iteration_in_progress(false) { 263 _array = NEW_C_HEAP_ARRAY(ShenandoahNMethod*, _size, mtGC); 264 } 265 266 ShenandoahNMethodTable::~ShenandoahNMethodTable() { 267 assert(_array != NULL, "Sanity"); 268 FREE_C_HEAP_ARRAY(ShenandoahNMethod*, _array); 269 } 270 271 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) { 272 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held"); 273 assert(_index >= 0 && _index <= _size, "Sanity"); 274 275 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm); 276 ShenandoahReentrantLocker data_locker(data != NULL ? data->lock() : NULL); 277 278 if (data != NULL) { 279 assert(contain(nm), "Must have been registered"); 280 data->update(); 281 } else { 282 data = ShenandoahNMethod::for_nmethod(nm); 283 if (data == NULL) { 284 assert(!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 285 "Only possible when concurrent class unloading is off"); 286 return; 287 } 288 ShenandoahNMethod::attach_gc_data(nm, data); 289 ShenandoahLocker locker(&_lock); 290 log_register_nmethod(nm); 291 append(data); 292 } 293 // Disarm new nmethod 294 ShenandoahNMethod::disarm_nmethod(nm); 295 } 296 297 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) { 298 assert_locked_or_safepoint(CodeCache_lock); 299 300 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm); 301 if (data == NULL) { 302 assert(!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 303 "Only possible when concurrent class unloading is off"); 304 ShenandoahNMethod::assert_no_oops(nm, true /*allow_dead*/); 305 return; 306 } 307 308 if (Thread::current()->is_Code_cache_sweeper_thread()) { 309 wait_until_concurrent_iteration_done(); 310 } 311 log_unregister_nmethod(nm); 312 ShenandoahLocker locker(&_lock); 313 assert(contain(nm), "Must have been registered"); 314 315 ShenandoahReentrantLocker data_locker(data->lock()); 316 data->mark_unregistered(); 317 } 318 319 void ShenandoahNMethodTable::flush_nmethod(nmethod* nm) { 320 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held"); 321 assert(Thread::current()->is_Code_cache_sweeper_thread(), "Must from Sweep thread"); 322 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm); 323 assert(data != NULL || !ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 324 "Only possible when concurrent class unloading is off"); 325 if (data == NULL) { 326 ShenandoahNMethod::assert_no_oops(nm, true /*allow_dead*/); 327 return; 328 } 329 330 // Can not alter the array when iteration is in progress 331 wait_until_concurrent_iteration_done(); 332 log_flush_nmethod(nm); 333 334 ShenandoahLocker locker(&_lock); 335 int idx = index_of(nm); 336 assert(idx >= 0 && idx < _index, "Invalid index"); 337 ShenandoahNMethod::attach_gc_data(nm, NULL); 338 remove(idx); 339 } 340 341 bool ShenandoahNMethodTable::contain(nmethod* nm) const { 342 return index_of(nm) != -1; 343 } 344 345 ShenandoahNMethod* ShenandoahNMethodTable::at(int index) const { 346 assert(index >= 0 && index < _index, "Out of bound"); 347 return _array[index]; 348 } 349 350 int ShenandoahNMethodTable::index_of(nmethod* nm) const { 351 for (int index = 0; index < length(); index ++) { 352 if (_array[index]->nm() == nm) { 353 return index; 354 } 355 } 356 return -1; 357 } 358 359 void ShenandoahNMethodTable::remove(int idx) { 360 shenandoah_assert_locked_or_safepoint(CodeCache_lock); 361 assert(!_iteration_in_progress, "Can not happen"); 362 assert(_index >= 0 && _index <= _size, "Sanity"); 363 364 assert(idx >= 0 && idx < _index, "Out of bound"); 365 ShenandoahNMethod* snm = _array[idx]; 366 367 _index --; 368 _array[idx] = _array[_index]; 369 370 delete snm; 371 } 372 373 void ShenandoahNMethodTable::wait_until_concurrent_iteration_done() { 374 assert(CodeCache_lock->owned_by_self(), "Lock must be held"); 375 while (iteration_in_progress()) { 376 CodeCache_lock->wait_without_safepoint_check(); 377 } 378 } 379 380 void ShenandoahNMethodTable::append(ShenandoahNMethod* snm) { 381 if (is_full()) { 382 int new_size = 2 * _size; 383 ShenandoahNMethod** old_table = _array; 384 385 // Rebuild table and replace current one 386 rebuild(new_size); 387 388 // An iteration is in progress over early snapshot, 389 // can not release the array until iteration is completed 390 if (!iteration_in_progress()) { 391 FREE_C_HEAP_ARRAY(ShenandoahNMethod*, old_table); 392 } 393 } 394 395 _array[_index ++] = snm; 396 assert(_index >= 0 && _index <= _size, "Sanity"); 397 } 398 399 void ShenandoahNMethodTable::rebuild(int size) { 400 ShenandoahNMethod** arr = NEW_C_HEAP_ARRAY(ShenandoahNMethod*, size, mtGC); 401 for (int index = 0; index < _index; index ++) { 402 arr[index] = _array[index]; 403 } 404 _array = arr; 405 _size = size; 406 } 407 408 ShenandoahNMethodTableSnapshot* ShenandoahNMethodTable::snapshot_for_iteration() { 409 assert(!iteration_in_progress(), "Already in progress"); 410 _iteration_in_progress = true; 411 412 return new ShenandoahNMethodTableSnapshot(this); 413 } 414 415 void ShenandoahNMethodTable::finish_iteration(ShenandoahNMethodTableSnapshot* snapshot) { 416 assert(iteration_in_progress(), "Why we here?"); 417 assert(snapshot != NULL, "No snapshot"); 418 _iteration_in_progress = false; 419 420 // Table has been rebuilt during iteration, free old table 421 if (snapshot->_array != _array) { 422 FREE_C_HEAP_ARRAY(ShenandoahNMethod*, snapshot->_array); 423 } 424 delete snapshot; 425 } 426 427 void ShenandoahNMethodTable::log_register_nmethod(nmethod* nm) { 428 LogTarget(Debug, gc, nmethod) log; 429 if (!log.is_enabled()) { 430 return; 431 } 432 433 ResourceMark rm; 434 log.print("Register NMethod: %s.%s [" PTR_FORMAT "] (%s)", 435 nm->method()->method_holder()->external_name(), 436 nm->method()->name()->as_C_string(), 437 p2i(nm), 438 nm->compiler_name()); 439 } 440 441 void ShenandoahNMethodTable::log_unregister_nmethod(nmethod* nm) { 442 LogTarget(Debug, gc, nmethod) log; 443 if (!log.is_enabled()) { 444 return; 445 } 446 447 ResourceMark rm; 448 log.print("Unregister NMethod: %s.%s [" PTR_FORMAT "]", 449 nm->method()->method_holder()->external_name(), 450 nm->method()->name()->as_C_string(), 451 p2i(nm)); 452 } 453 454 void ShenandoahNMethodTable::log_flush_nmethod(nmethod* nm) { 455 LogTarget(Debug, gc, nmethod) log; 456 if (!log.is_enabled()) { 457 return; 458 } 459 460 ResourceMark rm; 461 log.print("Flush NMethod: (" PTR_FORMAT ")", p2i(nm)); 462 } 463 464 #ifdef ASSERT 465 void ShenandoahNMethodTable::assert_nmethods_alive_and_correct() { 466 assert_locked_or_safepoint(CodeCache_lock); 467 468 for (int index = 0; index < length(); index ++) { 469 ShenandoahNMethod* m = _array[index]; 470 // Concurrent unloading may have dead nmethods to be cleaned by sweeper 471 if (m->is_unregistered()) continue; 472 m->assert_alive_and_correct(); 473 } 474 } 475 #endif 476 477 ShenandoahNMethodTableSnapshot::ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table) : 478 _heap(ShenandoahHeap::heap()), _table(table), _array(table->_array), _length(table->_index), _claimed(0) { 479 } 480 481 void ShenandoahNMethodTableSnapshot::concurrent_nmethods_do(NMethodClosure* cl) { 482 size_t stride = 256; // educated guess 483 484 ShenandoahNMethod** list = _array; 485 size_t max = (size_t)_length; 486 while (_claimed < max) { 487 size_t cur = Atomic::fetch_and_add(&_claimed, stride); 488 size_t start = cur; 489 size_t end = MIN2(cur + stride, max); 490 if (start >= max) break; 491 492 for (size_t idx = start; idx < end; idx++) { 493 ShenandoahNMethod* data = list[idx]; 494 assert(data != NULL, "Should not be NULL"); 495 if (!data->is_unregistered()) { 496 cl->do_nmethod(data->nm()); 497 } 498 } 499 } 500 } 501 502 ShenandoahConcurrentNMethodIterator::ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table) : 503 _table(table), _table_snapshot(NULL) { 504 } 505 506 void ShenandoahConcurrentNMethodIterator::nmethods_do_begin() { 507 assert(CodeCache_lock->owned_by_self(), "Lock must be held"); 508 assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 509 "Only for concurrent class unloading"); 510 _table_snapshot = _table->snapshot_for_iteration(); 511 } 512 513 void ShenandoahConcurrentNMethodIterator::nmethods_do(NMethodClosure* cl) { 514 assert(_table_snapshot != NULL, "Must first call nmethod_do_begin()"); 515 _table_snapshot->concurrent_nmethods_do(cl); 516 } 517 518 void ShenandoahConcurrentNMethodIterator::nmethods_do_end() { 519 assert(CodeCache_lock->owned_by_self(), "Lock must be held"); 520 assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), 521 "Only for concurrent class unloading"); 522 _table->finish_iteration(_table_snapshot); 523 CodeCache_lock->notify_all(); 524 }