1 /* 2 * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "memory/allocation.hpp" 25 26 #include "gc/shared/gcTimer.hpp" 27 #include "gc/shared/gcTraceTime.inline.hpp" 28 #include "gc/shared/parallelCleaning.hpp" 29 30 #include "gc/shenandoah/brooksPointer.hpp" 31 #include "gc/shenandoah/shenandoahBarrierSet.hpp" 32 #include "gc/shenandoah/shenandoahCollectionSet.hpp" 33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 34 #include "gc/shenandoah/shenandoahConcurrentMark.hpp" 35 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" 36 #include "gc/shenandoah/shenandoahConcurrentThread.hpp" 37 #include "gc/shenandoah/shenandoahFreeSet.hpp" 38 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 39 #include "gc/shenandoah/shenandoahHeapRegion.hpp" 40 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp" 41 #include "gc/shenandoah/shenandoahHumongous.hpp" 42 #include "gc/shenandoah/shenandoahMarkCompact.hpp" 43 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" 44 #include "gc/shenandoah/shenandoahPartialGC.hpp" 45 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 46 #include "gc/shenandoah/vm_operations_shenandoah.hpp" 47 48 #include "runtime/vmThread.hpp" 49 #include "services/mallocTracker.hpp" 50 51 SCMUpdateRefsClosure::SCMUpdateRefsClosure() : _heap(ShenandoahHeap::heap()) {} 52 53 #ifdef ASSERT 54 template <class T> 55 void AssertToSpaceClosure::do_oop_nv(T* p) { 56 T o = oopDesc::load_heap_oop(p); 57 if (! oopDesc::is_null(o)) { 58 oop obj = oopDesc::decode_heap_oop_not_null(o); 59 assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), 60 "need to-space object here obj: "PTR_FORMAT" , rb(obj): "PTR_FORMAT", p: "PTR_FORMAT, 61 p2i(obj), p2i(ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), p2i(p)); 62 } 63 } 64 65 void AssertToSpaceClosure::do_oop(narrowOop* p) { do_oop_nv(p); } 66 void AssertToSpaceClosure::do_oop(oop* p) { do_oop_nv(p); } 67 #endif 68 69 const char* ShenandoahHeap::name() const { 70 return "Shenandoah"; 71 } 72 73 void ShenandoahHeap::print_heap_locations(HeapWord* start, HeapWord* end) { 74 HeapWord* cur = NULL; 75 for (cur = start; cur < end; cur++) { 76 tty->print_cr(PTR_FORMAT" : "PTR_FORMAT, p2i(cur), p2i(*((HeapWord**) cur))); 77 } 78 } 79 80 class PrintHeapRegionsClosure : public 81 ShenandoahHeapRegionClosure { 82 private: 83 outputStream* _st; 84 public: 85 PrintHeapRegionsClosure() : _st(tty) {} 86 PrintHeapRegionsClosure(outputStream* st) : _st(st) {} 87 88 bool doHeapRegion(ShenandoahHeapRegion* r) { 89 r->print_on(_st); 90 return false; 91 } 92 }; 93 94 class ShenandoahPretouchTask : public AbstractGangTask { 95 private: 96 ShenandoahHeapRegionSet* _regions; 97 const size_t _bitmap_size; 98 const size_t _page_size; 99 char* _bitmap0_base; 100 char* _bitmap1_base; 101 public: 102 ShenandoahPretouchTask(ShenandoahHeapRegionSet* regions, 103 char* bitmap0_base, char* bitmap1_base, size_t bitmap_size, 104 size_t page_size) : 105 AbstractGangTask("Shenandoah PreTouch", 106 Universe::is_fully_initialized() ? GCId::current_raw() : 107 // During VM initialization there is 108 // no GC cycle that this task can be 109 // associated with. 110 GCId::undefined()), 111 _bitmap0_base(bitmap0_base), 112 _bitmap1_base(bitmap1_base), 113 _regions(regions), 114 _bitmap_size(bitmap_size), 115 _page_size(page_size) { 116 _regions->clear_current_index(); 117 }; 118 119 virtual void work(uint worker_id) { 120 ShenandoahHeapRegion* r = _regions->claim_next(); 121 while (r != NULL) { 122 log_trace(gc, heap)("Pretouch region " SIZE_FORMAT ": " PTR_FORMAT " -> " PTR_FORMAT, 123 r->region_number(), p2i(r->bottom()), p2i(r->end())); 124 os::pretouch_memory(r->bottom(), r->end(), _page_size); 125 126 size_t start = r->region_number() * ShenandoahHeapRegion::RegionSizeBytes / CMBitMap::heap_map_factor(); 127 size_t end = (r->region_number() + 1) * ShenandoahHeapRegion::RegionSizeBytes / CMBitMap::heap_map_factor(); 128 assert (end <= _bitmap_size, "end is sane: " SIZE_FORMAT " < " SIZE_FORMAT, end, _bitmap_size); 129 130 log_trace(gc, heap)("Pretouch bitmap under region " SIZE_FORMAT ": " PTR_FORMAT " -> " PTR_FORMAT, 131 r->region_number(), p2i(_bitmap0_base + start), p2i(_bitmap0_base + end)); 132 os::pretouch_memory(_bitmap0_base + start, _bitmap0_base + end, _page_size); 133 134 log_trace(gc, heap)("Pretouch bitmap under region " SIZE_FORMAT ": " PTR_FORMAT " -> " PTR_FORMAT, 135 r->region_number(), p2i(_bitmap1_base + start), p2i(_bitmap1_base + end)); 136 os::pretouch_memory(_bitmap1_base + start, _bitmap1_base + end, _page_size); 137 138 r = _regions->claim_next(); 139 } 140 } 141 }; 142 143 jint ShenandoahHeap::initialize() { 144 CollectedHeap::pre_initialize(); 145 146 size_t init_byte_size = collector_policy()->initial_heap_byte_size(); 147 size_t max_byte_size = collector_policy()->max_heap_byte_size(); 148 149 Universe::check_alignment(max_byte_size, 150 ShenandoahHeapRegion::RegionSizeBytes, 151 "shenandoah heap"); 152 Universe::check_alignment(init_byte_size, 153 ShenandoahHeapRegion::RegionSizeBytes, 154 "shenandoah heap"); 155 156 ReservedSpace heap_rs = Universe::reserve_heap(max_byte_size, 157 Arguments::conservative_max_heap_alignment()); 158 initialize_reserved_region((HeapWord*)heap_rs.base(), (HeapWord*) (heap_rs.base() + heap_rs.size())); 159 160 set_barrier_set(new ShenandoahBarrierSet(this)); 161 ReservedSpace pgc_rs = heap_rs.first_part(max_byte_size); 162 _storage.initialize(pgc_rs, init_byte_size); 163 164 _num_regions = init_byte_size / ShenandoahHeapRegion::RegionSizeBytes; 165 _max_regions = max_byte_size / ShenandoahHeapRegion::RegionSizeBytes; 166 _initialSize = _num_regions * ShenandoahHeapRegion::RegionSizeBytes; 167 size_t regionSizeWords = ShenandoahHeapRegion::RegionSizeBytes / HeapWordSize; 168 assert(init_byte_size == _initialSize, "tautology"); 169 _ordered_regions = new ShenandoahHeapRegionSet(_max_regions); 170 _collection_set = new ShenandoahCollectionSet(_max_regions); 171 _free_regions = new ShenandoahFreeSet(_max_regions); 172 173 // Initialize fast collection set test structure. 174 _in_cset_fast_test_length = _max_regions; 175 _in_cset_fast_test_base = 176 NEW_C_HEAP_ARRAY(bool, _in_cset_fast_test_length, mtGC); 177 _in_cset_fast_test = _in_cset_fast_test_base - 178 ((uintx) pgc_rs.base() >> ShenandoahHeapRegion::RegionSizeShift); 179 180 _next_top_at_mark_starts_base = 181 NEW_C_HEAP_ARRAY(HeapWord*, _max_regions, mtGC); 182 _next_top_at_mark_starts = _next_top_at_mark_starts_base - 183 ((uintx) pgc_rs.base() >> ShenandoahHeapRegion::RegionSizeShift); 184 185 _complete_top_at_mark_starts_base = 186 NEW_C_HEAP_ARRAY(HeapWord*, _max_regions, mtGC); 187 _complete_top_at_mark_starts = _complete_top_at_mark_starts_base - 188 ((uintx) pgc_rs.base() >> ShenandoahHeapRegion::RegionSizeShift); 189 190 size_t i = 0; 191 for (i = 0; i < _num_regions; i++) { 192 _in_cset_fast_test_base[i] = false; // Not in cset 193 HeapWord* bottom = (HeapWord*) pgc_rs.base() + regionSizeWords * i; 194 _complete_top_at_mark_starts_base[i] = bottom; 195 _next_top_at_mark_starts_base[i] = bottom; 196 } 197 198 { 199 ShenandoahHeapLock lock(this); 200 for (i = 0; i < _num_regions; i++) { 201 ShenandoahHeapRegion* current = new ShenandoahHeapRegion(this, (HeapWord*) pgc_rs.base() + 202 regionSizeWords * i, regionSizeWords, i); 203 _free_regions->add_region(current); 204 _ordered_regions->add_region(current); 205 } 206 } 207 assert(((size_t) _ordered_regions->active_regions()) == _num_regions, ""); 208 _first_region = _ordered_regions->get(0); 209 _first_region_bottom = _first_region->bottom(); 210 assert((((size_t) _first_region_bottom) & 211 (ShenandoahHeapRegion::RegionSizeBytes - 1)) == 0, 212 "misaligned heap: "PTR_FORMAT, p2i(_first_region_bottom)); 213 214 _numAllocs = 0; 215 216 if (log_is_enabled(Trace, gc, region)) { 217 ResourceMark rm; 218 outputStream* out = Log(gc, region)::trace_stream(); 219 log_trace(gc, region)("All Regions"); 220 _ordered_regions->print(out); 221 log_trace(gc, region)("Free Regions"); 222 _free_regions->print(out); 223 } 224 225 // The call below uses stuff (the SATB* things) that are in G1, but probably 226 // belong into a shared location. 227 JavaThread::satb_mark_queue_set().initialize(SATB_Q_CBL_mon, 228 SATB_Q_FL_lock, 229 20 /*G1SATBProcessCompletedThreshold */, 230 Shared_SATB_Q_lock); 231 232 // Reserve space for prev and next bitmap. 233 _bitmap_size = CMBitMap::compute_size(heap_rs.size()); 234 _heap_region = MemRegion((HeapWord*) heap_rs.base(), heap_rs.size() / HeapWordSize); 235 236 size_t page_size = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size(); 237 238 ReservedSpace bitmap0(_bitmap_size, page_size); 239 os::commit_memory_or_exit(bitmap0.base(), bitmap0.size(), false, "couldn't allocate mark bitmap"); 240 MemTracker::record_virtual_memory_type(bitmap0.base(), mtGC); 241 MemRegion bitmap_region0 = MemRegion((HeapWord*) bitmap0.base(), bitmap0.size() / HeapWordSize); 242 243 ReservedSpace bitmap1(_bitmap_size, page_size); 244 os::commit_memory_or_exit(bitmap1.base(), bitmap1.size(), false, "couldn't allocate mark bitmap"); 245 MemTracker::record_virtual_memory_type(bitmap1.base(), mtGC); 246 MemRegion bitmap_region1 = MemRegion((HeapWord*) bitmap1.base(), bitmap1.size() / HeapWordSize); 247 248 if (ShenandoahAlwaysPreTouch) { 249 assert (!AlwaysPreTouch, "Should have been overridden"); 250 251 // For NUMA, it is important to pre-touch the storage under bitmaps with worker threads, 252 // before initialize() below zeroes it with initializing thread. For any given region, 253 // we touch the region and the corresponding bitmaps from the same thread. 254 255 log_info(gc, heap)("Parallel pretouch " SIZE_FORMAT " regions with " SIZE_FORMAT " byte pages", 256 _ordered_regions->count(), page_size); 257 ShenandoahPretouchTask cl(_ordered_regions, bitmap0.base(), bitmap1.base(), _bitmap_size, page_size); 258 _workers->run_task(&cl); 259 } 260 261 _mark_bit_map0.initialize(_heap_region, bitmap_region0); 262 _complete_mark_bit_map = &_mark_bit_map0; 263 264 _mark_bit_map1.initialize(_heap_region, bitmap_region1); 265 _next_mark_bit_map = &_mark_bit_map1; 266 267 _connection_matrix = new ShenandoahConnectionMatrix(_max_regions); 268 _partial_gc = new ShenandoahPartialGC(this, _max_regions); 269 270 _monitoring_support = new ShenandoahMonitoringSupport(this); 271 272 _concurrent_gc_thread = new ShenandoahConcurrentThread(); 273 274 ShenandoahMarkCompact::initialize(); 275 276 return JNI_OK; 277 } 278 279 ShenandoahHeap::ShenandoahHeap(ShenandoahCollectorPolicy* policy) : 280 CollectedHeap(), 281 _shenandoah_policy(policy), 282 _concurrent_mark_in_progress(0), 283 _evacuation_in_progress(0), 284 _full_gc_in_progress(false), 285 _free_regions(NULL), 286 _collection_set(NULL), 287 _bytes_allocated_since_cm(0), 288 _bytes_allocated_during_cm(0), 289 _max_allocated_gc(0), 290 _allocated_last_gc(0), 291 _used_start_gc(0), 292 _max_workers(MAX2(ConcGCThreads, ParallelGCThreads)), 293 _ref_processor(NULL), 294 _in_cset_fast_test(NULL), 295 _in_cset_fast_test_base(NULL), 296 _next_top_at_mark_starts(NULL), 297 _next_top_at_mark_starts_base(NULL), 298 _complete_top_at_mark_starts(NULL), 299 _complete_top_at_mark_starts_base(NULL), 300 _mark_bit_map0(), 301 _mark_bit_map1(), 302 _connection_matrix(NULL), 303 _cancelled_concgc(false), 304 _need_update_refs(false), 305 _need_reset_bitmaps(false), 306 _heap_lock(0), 307 #ifdef ASSERT 308 _heap_lock_owner(NULL), 309 #endif 310 _gc_timer(new (ResourceObj::C_HEAP, mtGC) ConcurrentGCTimer()) 311 312 { 313 log_info(gc, init)("Parallel GC threads: "UINT32_FORMAT, ParallelGCThreads); 314 log_info(gc, init)("Concurrent GC threads: "UINT32_FORMAT, ConcGCThreads); 315 log_info(gc, init)("Parallel reference processing enabled: %s", BOOL_TO_STR(ParallelRefProcEnabled)); 316 317 _scm = new ShenandoahConcurrentMark(); 318 _used = 0; 319 320 _max_workers = MAX2(_max_workers, 1U); 321 _workers = new ShenandoahWorkGang("Shenandoah GC Threads", _max_workers, 322 /* are_GC_task_threads */true, 323 /* are_ConcurrentGC_threads */false); 324 if (_workers == NULL) { 325 vm_exit_during_initialization("Failed necessary allocation."); 326 } else { 327 _workers->initialize_workers(); 328 } 329 } 330 331 class ResetNextBitmapTask : public AbstractGangTask { 332 private: 333 ShenandoahHeapRegionSet* _regions; 334 335 public: 336 ResetNextBitmapTask(ShenandoahHeapRegionSet* regions) : 337 AbstractGangTask("Parallel Reset Bitmap Task"), 338 _regions(regions) { 339 _regions->clear_current_index(); 340 } 341 342 void work(uint worker_id) { 343 ShenandoahHeapRegion* region = _regions->claim_next(); 344 ShenandoahHeap* heap = ShenandoahHeap::heap(); 345 while (region != NULL) { 346 HeapWord* bottom = region->bottom(); 347 HeapWord* top = heap->next_top_at_mark_start(region->bottom()); 348 if (top > bottom) { 349 heap->next_mark_bit_map()->clear_range_large(MemRegion(bottom, top)); 350 } 351 region = _regions->claim_next(); 352 } 353 } 354 }; 355 356 void ShenandoahHeap::reset_next_mark_bitmap(WorkGang* workers) { 357 ResetNextBitmapTask task = ResetNextBitmapTask(_ordered_regions); 358 workers->run_task(&task); 359 } 360 361 class ResetCompleteBitmapTask : public AbstractGangTask { 362 private: 363 ShenandoahHeapRegionSet* _regions; 364 365 public: 366 ResetCompleteBitmapTask(ShenandoahHeapRegionSet* regions) : 367 AbstractGangTask("Parallel Reset Bitmap Task"), 368 _regions(regions) { 369 _regions->clear_current_index(); 370 } 371 372 void work(uint worker_id) { 373 ShenandoahHeapRegion* region = _regions->claim_next(); 374 ShenandoahHeap* heap = ShenandoahHeap::heap(); 375 while (region != NULL) { 376 HeapWord* bottom = region->bottom(); 377 HeapWord* top = heap->complete_top_at_mark_start(region->bottom()); 378 if (top > bottom) { 379 heap->complete_mark_bit_map()->clear_range_large(MemRegion(bottom, top)); 380 } 381 region = _regions->claim_next(); 382 } 383 } 384 }; 385 386 void ShenandoahHeap::reset_complete_mark_bitmap(WorkGang* workers) { 387 ResetCompleteBitmapTask task = ResetCompleteBitmapTask(_ordered_regions); 388 workers->run_task(&task); 389 } 390 391 bool ShenandoahHeap::is_next_bitmap_clear() { 392 HeapWord* start = _ordered_regions->bottom(); 393 HeapWord* end = _ordered_regions->end(); 394 return _next_mark_bit_map->getNextMarkedWordAddress(start, end) == end; 395 } 396 397 bool ShenandoahHeap::is_complete_bitmap_clear_range(HeapWord* start, HeapWord* end) { 398 return _complete_mark_bit_map->getNextMarkedWordAddress(start, end) == end; 399 } 400 401 void ShenandoahHeap::print_on(outputStream* st) const { 402 st->print("Shenandoah Heap"); 403 st->print(" total = " SIZE_FORMAT " K, used " SIZE_FORMAT " K ", capacity()/ K, used() /K); 404 st->print(" [" PTR_FORMAT ", " PTR_FORMAT ") ", 405 p2i(reserved_region().start()), 406 p2i(reserved_region().end())); 407 st->print("Region size = " SIZE_FORMAT "K ", ShenandoahHeapRegion::RegionSizeBytes / K); 408 if (_concurrent_mark_in_progress) { 409 st->print("marking "); 410 } 411 if (_evacuation_in_progress) { 412 st->print("evacuating "); 413 } 414 if (cancelled_concgc()) { 415 st->print("cancelled "); 416 } 417 st->print("\n"); 418 419 // Adapted from VirtualSpace::print_on(), which is non-PRODUCT only 420 st->print ("Virtual space:"); 421 if (_storage.special()) st->print(" (pinned in memory)"); 422 st->cr(); 423 st->print_cr(" - committed: " SIZE_FORMAT, _storage.committed_size()); 424 st->print_cr(" - reserved: " SIZE_FORMAT, _storage.reserved_size()); 425 st->print_cr(" - [low, high]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", p2i(_storage.low()), p2i(_storage.high())); 426 st->print_cr(" - [low_b, high_b]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", p2i(_storage.low_boundary()), p2i(_storage.high_boundary())); 427 428 if (Verbose) { 429 print_heap_regions(st); 430 } 431 } 432 433 class InitGCLABClosure : public ThreadClosure { 434 public: 435 void do_thread(Thread* thread) { 436 thread->gclab().initialize(true); 437 } 438 }; 439 440 void ShenandoahHeap::post_initialize() { 441 if (UseTLAB) { 442 // This is a very tricky point in VM lifetime. We cannot easily call Threads::threads_do 443 // here, because some system threads (VMThread, WatcherThread, etc) are not yet available. 444 // Their initialization should be handled separately. Is we miss some threads here, 445 // then any other TLAB-related activity would fail with asserts. 446 447 InitGCLABClosure init_gclabs; 448 { 449 MutexLocker ml(Threads_lock); 450 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { 451 init_gclabs.do_thread(thread); 452 } 453 } 454 gc_threads_do(&init_gclabs); 455 456 // gclab can not be initialized early during VM startup, as it can not determinate its max_size. 457 // Now, we will let WorkGang to initialize gclab when new worker is created. 458 _workers->set_initialize_gclab(); 459 } 460 461 _scm->initialize(_max_workers); 462 463 ref_processing_init(); 464 } 465 466 class CalculateUsedRegionClosure : public ShenandoahHeapRegionClosure { 467 size_t sum; 468 public: 469 470 CalculateUsedRegionClosure() { 471 sum = 0; 472 } 473 474 bool doHeapRegion(ShenandoahHeapRegion* r) { 475 sum = sum + r->used(); 476 return false; 477 } 478 479 size_t getResult() { return sum;} 480 }; 481 482 size_t ShenandoahHeap::calculateUsed() { 483 CalculateUsedRegionClosure cl; 484 heap_region_iterate(&cl); 485 return cl.getResult(); 486 } 487 488 void ShenandoahHeap::verify_heap_size_consistency() { 489 490 assert(calculateUsed() == used(), 491 "heap used size must be consistent heap-used: "SIZE_FORMAT" regions-used: "SIZE_FORMAT, used(), calculateUsed()); 492 } 493 494 size_t ShenandoahHeap::used() const { 495 OrderAccess::acquire(); 496 return _used; 497 } 498 499 void ShenandoahHeap::increase_used(size_t bytes) { 500 assert_heaplock_or_safepoint(); 501 _used += bytes; 502 } 503 504 void ShenandoahHeap::set_used(size_t bytes) { 505 assert_heaplock_or_safepoint(); 506 _used = bytes; 507 } 508 509 void ShenandoahHeap::decrease_used(size_t bytes) { 510 assert_heaplock_or_safepoint(); 511 assert(_used >= bytes, "never decrease heap size by more than we've left"); 512 _used -= bytes; 513 } 514 515 size_t ShenandoahHeap::capacity() const { 516 return _num_regions * ShenandoahHeapRegion::RegionSizeBytes; 517 } 518 519 bool ShenandoahHeap::is_maximal_no_gc() const { 520 Unimplemented(); 521 return true; 522 } 523 524 size_t ShenandoahHeap::max_capacity() const { 525 return _max_regions * ShenandoahHeapRegion::RegionSizeBytes; 526 } 527 528 size_t ShenandoahHeap::min_capacity() const { 529 return _initialSize; 530 } 531 532 VirtualSpace* ShenandoahHeap::storage() const { 533 return (VirtualSpace*) &_storage; 534 } 535 536 bool ShenandoahHeap::is_in(const void* p) const { 537 HeapWord* first_region_bottom = _first_region->bottom(); 538 HeapWord* last_region_end = first_region_bottom + (ShenandoahHeapRegion::RegionSizeBytes / HeapWordSize) * _num_regions; 539 return p >= _first_region_bottom && p < last_region_end; 540 } 541 542 bool ShenandoahHeap::is_scavengable(const void* p) { 543 return true; 544 } 545 546 HeapWord* ShenandoahHeap::allocate_from_gclab_slow(Thread* thread, size_t size) { 547 // Retain tlab and allocate object in shared space if 548 // the amount free in the tlab is too large to discard. 549 if (thread->gclab().free() > thread->gclab().refill_waste_limit()) { 550 thread->gclab().record_slow_allocation(size); 551 return NULL; 552 } 553 554 // Discard gclab and allocate a new one. 555 // To minimize fragmentation, the last GCLAB may be smaller than the rest. 556 size_t new_gclab_size = thread->gclab().compute_size(size); 557 558 thread->gclab().clear_before_allocation(); 559 560 if (new_gclab_size == 0) { 561 return NULL; 562 } 563 564 // Allocate a new GCLAB... 565 HeapWord* obj = allocate_new_gclab(new_gclab_size); 566 if (obj == NULL) { 567 return NULL; 568 } 569 570 if (ZeroTLAB) { 571 // ..and clear it. 572 Copy::zero_to_words(obj, new_gclab_size); 573 } else { 574 // ...and zap just allocated object. 575 #ifdef ASSERT 576 // Skip mangling the space corresponding to the object header to 577 // ensure that the returned space is not considered parsable by 578 // any concurrent GC thread. 579 size_t hdr_size = oopDesc::header_size(); 580 Copy::fill_to_words(obj + hdr_size, new_gclab_size - hdr_size, badHeapWordVal); 581 #endif // ASSERT 582 } 583 thread->gclab().fill(obj, obj + size, new_gclab_size); 584 return obj; 585 } 586 587 HeapWord* ShenandoahHeap::allocate_new_tlab(size_t word_size) { 588 return allocate_new_tlab(word_size, false); 589 } 590 591 HeapWord* ShenandoahHeap::allocate_new_gclab(size_t word_size) { 592 return allocate_new_tlab(word_size, true); 593 } 594 595 HeapWord* ShenandoahHeap::allocate_new_tlab(size_t word_size, bool evacuating) { 596 HeapWord* result = allocate_memory(word_size, evacuating); 597 598 if (result != NULL) { 599 assert(! in_collection_set(result), "Never allocate in dirty region"); 600 _bytes_allocated_since_cm += word_size * HeapWordSize; 601 602 log_develop_trace(gc, tlab)("allocating new tlab of size "SIZE_FORMAT" at addr "PTR_FORMAT, word_size, p2i(result)); 603 604 } 605 return result; 606 } 607 608 ShenandoahHeap* ShenandoahHeap::heap() { 609 CollectedHeap* heap = Universe::heap(); 610 assert(heap != NULL, "Unitialized access to ShenandoahHeap::heap()"); 611 assert(heap->kind() == CollectedHeap::ShenandoahHeap, "not a shenandoah heap"); 612 return (ShenandoahHeap*) heap; 613 } 614 615 ShenandoahHeap* ShenandoahHeap::heap_no_check() { 616 CollectedHeap* heap = Universe::heap(); 617 return (ShenandoahHeap*) heap; 618 } 619 620 HeapWord* ShenandoahHeap::allocate_memory_work(size_t word_size) { 621 622 ShenandoahHeapLock heap_lock(this); 623 624 HeapWord* result = allocate_memory_under_lock(word_size); 625 int grow_by = (word_size * HeapWordSize + ShenandoahHeapRegion::RegionSizeBytes - 1) / ShenandoahHeapRegion::RegionSizeBytes; 626 627 while (result == NULL && _num_regions + grow_by <= _max_regions) { 628 grow_heap_by(grow_by); 629 result = allocate_memory_under_lock(word_size); 630 } 631 632 return result; 633 } 634 635 HeapWord* ShenandoahHeap::allocate_memory(size_t word_size, bool evacuating) { 636 HeapWord* result = NULL; 637 result = allocate_memory_work(word_size); 638 639 if (!evacuating) { 640 // Allocation failed, try full-GC, then retry allocation. 641 // 642 // It might happen that one of the threads requesting allocation would unblock 643 // way later after full-GC happened, only to fail the second allocation, because 644 // other threads have already depleted the free storage. In this case, a better 645 // strategy would be to try full-GC again. 646 // 647 // Lacking the way to detect progress from "collect" call, we are left with blindly 648 // retrying for some bounded number of times. 649 // TODO: Poll if Full GC made enough progress to warrant retry. 650 int tries = 0; 651 while ((result == NULL) && (tries++ < ShenandoahFullGCTries)) { 652 log_debug(gc)("[" PTR_FORMAT " Failed to allocate " SIZE_FORMAT " bytes, doing full GC, try %d", 653 p2i(Thread::current()), word_size * HeapWordSize, tries); 654 collect(GCCause::_allocation_failure); 655 result = allocate_memory_work(word_size); 656 } 657 } 658 659 // Only update monitoring counters when not calling from a write-barrier. 660 // Otherwise we might attempt to grab the Service_lock, which we must 661 // not do when coming from a write-barrier (because the thread might 662 // already hold the Compile_lock). 663 if (! evacuating) { 664 monitoring_support()->update_counters(); 665 } 666 667 log_develop_trace(gc, alloc)("allocate memory chunk of size "SIZE_FORMAT" at addr "PTR_FORMAT " by thread %d ", 668 word_size, p2i(result), Thread::current()->osthread()->thread_id()); 669 670 return result; 671 } 672 673 bool ShenandoahHeap::call_from_write_barrier(bool evacuating) { 674 return evacuating && Thread::current()->is_Java_thread(); 675 } 676 677 HeapWord* ShenandoahHeap::allocate_memory_under_lock(size_t word_size) { 678 assert_heaplock_owned_by_current_thread(); 679 680 if (word_size * HeapWordSize > ShenandoahHeapRegion::RegionSizeBytes) { 681 return allocate_large_memory(word_size); 682 } 683 684 // Not enough memory in free region set. 685 // Coming out of full GC, it is possible that there is not 686 // free region available, so current_index may not be valid. 687 if (word_size * HeapWordSize > _free_regions->capacity()) return NULL; 688 689 ShenandoahHeapRegion* my_current_region = _free_regions->current_no_humongous(); 690 691 if (my_current_region == NULL) { 692 return NULL; // No more room to make a new region. OOM. 693 } 694 assert(my_current_region != NULL, "should have a region at this point"); 695 696 #ifdef ASSERT 697 if (in_collection_set(my_current_region)) { 698 print_heap_regions(); 699 } 700 #endif 701 assert(! in_collection_set(my_current_region), "never get targetted regions in free-lists"); 702 assert(! my_current_region->is_humongous(), "never attempt to allocate from humongous object regions"); 703 704 HeapWord* result = my_current_region->allocate(word_size); 705 706 while (result == NULL) { 707 // 2nd attempt. Try next region. 708 _free_regions->increase_used(my_current_region->free()); 709 ShenandoahHeapRegion* next_region = _free_regions->next_no_humongous(); 710 assert(next_region != my_current_region, "must not get current again"); 711 my_current_region = next_region; 712 713 if (my_current_region == NULL) { 714 return NULL; // No more room to make a new region. OOM. 715 } 716 assert(my_current_region != NULL, "should have a region at this point"); 717 assert(! in_collection_set(my_current_region), "never get targetted regions in free-lists"); 718 assert(! my_current_region->is_humongous(), "never attempt to allocate from humongous object regions"); 719 result = my_current_region->allocate(word_size); 720 } 721 722 my_current_region->increase_live_data_words(word_size); 723 increase_used(word_size * HeapWordSize); 724 _free_regions->increase_used(word_size * HeapWordSize); 725 return result; 726 } 727 728 HeapWord* ShenandoahHeap::allocate_large_memory(size_t words) { 729 assert_heaplock_owned_by_current_thread(); 730 731 uint required_regions = ShenandoahHumongous::required_regions(words * HeapWordSize); 732 if (required_regions > _max_regions) return NULL; 733 734 ShenandoahHeapRegion* r = _free_regions->allocate_contiguous(required_regions); 735 736 HeapWord* result = NULL; 737 738 if (r != NULL) { 739 result = r->bottom(); 740 741 log_debug(gc, humongous)("allocating humongous object of size: "SIZE_FORMAT" KB at location "PTR_FORMAT" in start region "SIZE_FORMAT, 742 (words * HeapWordSize) / K, p2i(result), r->region_number()); 743 } else { 744 log_debug(gc, humongous)("allocating humongous object of size: "SIZE_FORMAT" KB at location "PTR_FORMAT" failed", 745 (words * HeapWordSize) / K, p2i(result)); 746 } 747 748 749 return result; 750 751 } 752 753 HeapWord* ShenandoahHeap::mem_allocate(size_t size, 754 bool* gc_overhead_limit_was_exceeded) { 755 756 #ifdef ASSERT 757 if (ShenandoahVerify && _numAllocs > 1000000) { 758 _numAllocs = 0; 759 } 760 _numAllocs++; 761 #endif 762 HeapWord* filler = allocate_memory(BrooksPointer::word_size() + size, false); 763 HeapWord* result = filler + BrooksPointer::word_size(); 764 if (filler != NULL) { 765 BrooksPointer::initialize(oop(result)); 766 _bytes_allocated_since_cm += size * HeapWordSize; 767 768 assert(! in_collection_set(result), "never allocate in targetted region"); 769 return result; 770 } else { 771 /* 772 tty->print_cr("Out of memory. Requested number of words: "SIZE_FORMAT" used heap: "INT64_FORMAT", bytes allocated since last CM: "INT64_FORMAT, 773 size, used(), _bytes_allocated_since_cm); 774 { 775 print_heap_regions(); 776 tty->print("Printing "SIZE_FORMAT" free regions:\n", _free_regions->count()); 777 _free_regions->print(); 778 } 779 */ 780 return NULL; 781 } 782 } 783 784 class ParallelEvacuateRegionObjectClosure : public ObjectClosure { 785 private: 786 ShenandoahHeap* _heap; 787 Thread* _thread; 788 public: 789 ParallelEvacuateRegionObjectClosure(ShenandoahHeap* heap) : 790 _heap(heap), _thread(Thread::current()) { 791 } 792 793 void do_object(oop p) { 794 795 log_develop_trace(gc, compaction)("Calling ParallelEvacuateRegionObjectClosure on "PTR_FORMAT" of size %d\n", p2i((HeapWord*) p), p->size()); 796 797 assert(_heap->is_marked_complete(p), "expect only marked objects"); 798 if (oopDesc::unsafe_equals(p, ShenandoahBarrierSet::resolve_oop_static_not_null(p))) { 799 bool evac; 800 _heap->evacuate_object(p, _thread, evac); 801 } 802 } 803 }; 804 805 #ifdef ASSERT 806 class VerifyEvacuatedObjectClosure : public ObjectClosure { 807 808 public: 809 810 void do_object(oop p) { 811 if (ShenandoahHeap::heap()->is_marked_complete(p)) { 812 oop p_prime = oopDesc::bs()->read_barrier(p); 813 assert(! oopDesc::unsafe_equals(p, p_prime), "Should point to evacuated copy"); 814 if (p->klass() != p_prime->klass()) { 815 tty->print_cr("copy has different class than original:"); 816 p->klass()->print_on(tty); 817 p_prime->klass()->print_on(tty); 818 } 819 assert(p->klass() == p_prime->klass(), "Should have the same class p: "PTR_FORMAT", p_prime: "PTR_FORMAT, p2i(p), p2i(p_prime)); 820 // assert(p->mark() == p_prime->mark(), "Should have the same mark"); 821 assert(p->size() == p_prime->size(), "Should be the same size"); 822 assert(oopDesc::unsafe_equals(p_prime, oopDesc::bs()->read_barrier(p_prime)), "One forward once"); 823 } 824 } 825 }; 826 827 void ShenandoahHeap::verify_evacuated_region(ShenandoahHeapRegion* from_region) { 828 VerifyEvacuatedObjectClosure verify_evacuation; 829 marked_object_iterate(from_region, &verify_evacuation); 830 } 831 #endif 832 833 void ShenandoahHeap::parallel_evacuate_region(ShenandoahHeapRegion* from_region) { 834 835 assert(from_region->has_live(), "all-garbage regions are reclaimed earlier"); 836 837 ParallelEvacuateRegionObjectClosure evacuate_region(this); 838 839 marked_object_iterate(from_region, &evacuate_region); 840 841 #ifdef ASSERT 842 if (ShenandoahVerify && ! cancelled_concgc()) { 843 verify_evacuated_region(from_region); 844 } 845 #endif 846 } 847 848 class ParallelEvacuationTask : public AbstractGangTask { 849 private: 850 ShenandoahHeap* _sh; 851 ShenandoahCollectionSet* _cs; 852 853 public: 854 ParallelEvacuationTask(ShenandoahHeap* sh, 855 ShenandoahCollectionSet* cs) : 856 AbstractGangTask("Parallel Evacuation Task"), 857 _cs(cs), 858 _sh(sh) {} 859 860 void work(uint worker_id) { 861 862 ShenandoahHeapRegion* from_hr = _cs->claim_next(); 863 864 while (from_hr != NULL) { 865 log_develop_trace(gc, region)("Thread "INT32_FORMAT" claimed Heap Region "SIZE_FORMAT, 866 worker_id, 867 from_hr->region_number()); 868 869 assert(from_hr->has_live(), "all-garbage regions are reclaimed early"); 870 _sh->parallel_evacuate_region(from_hr); 871 872 if (_sh->cancelled_concgc()) { 873 log_develop_trace(gc, region)("Cancelled concgc while evacuating region " SIZE_FORMAT "\n", from_hr->region_number()); 874 break; 875 } 876 from_hr = _cs->claim_next(); 877 } 878 } 879 }; 880 881 class RecycleDirtyRegionsClosure: public ShenandoahHeapRegionClosure { 882 private: 883 ShenandoahHeap* _heap; 884 size_t _bytes_reclaimed; 885 public: 886 RecycleDirtyRegionsClosure() : _heap(ShenandoahHeap::heap()) {} 887 888 bool doHeapRegion(ShenandoahHeapRegion* r) { 889 890 assert (! _heap->cancelled_concgc(), "no recycling after cancelled marking"); 891 892 if (_heap->in_collection_set(r)) { 893 log_develop_trace(gc, region)("Recycling region " SIZE_FORMAT ":", r->region_number()); 894 _heap->decrease_used(r->used()); 895 _bytes_reclaimed += r->used(); 896 r->recycle(); 897 } 898 899 return false; 900 } 901 size_t bytes_reclaimed() { return _bytes_reclaimed;} 902 void clear_bytes_reclaimed() {_bytes_reclaimed = 0;} 903 }; 904 905 void ShenandoahHeap::recycle_dirty_regions() { 906 RecycleDirtyRegionsClosure cl; 907 cl.clear_bytes_reclaimed(); 908 909 heap_region_iterate(&cl); 910 911 _shenandoah_policy->record_bytes_reclaimed(cl.bytes_reclaimed()); 912 if (! cancelled_concgc()) { 913 clear_cset_fast_test(); 914 } 915 } 916 917 ShenandoahFreeSet* ShenandoahHeap::free_regions() { 918 return _free_regions; 919 } 920 921 void ShenandoahHeap::print_heap_regions(outputStream* st) const { 922 _ordered_regions->print(st); 923 } 924 925 class PrintAllRefsOopClosure: public ExtendedOopClosure { 926 private: 927 int _index; 928 const char* _prefix; 929 930 public: 931 PrintAllRefsOopClosure(const char* prefix) : _index(0), _prefix(prefix) {} 932 933 private: 934 template <class T> 935 inline void do_oop_work(T* p) { 936 oop o = oopDesc::load_decode_heap_oop(p); 937 if (o != NULL) { 938 if (ShenandoahHeap::heap()->is_in(o) && o->is_oop()) { 939 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT")-> "PTR_FORMAT" (marked: %s) (%s "PTR_FORMAT")", 940 _prefix, _index, 941 p2i(p), p2i(o), 942 BOOL_TO_STR(ShenandoahHeap::heap()->is_marked_complete(o)), 943 o->klass()->internal_name(), p2i(o->klass())); 944 } else { 945 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT" dirty -> "PTR_FORMAT" (not in heap, possibly corrupted or dirty)", 946 _prefix, _index, 947 p2i(p), p2i(o)); 948 } 949 } else { 950 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT") -> "PTR_FORMAT, _prefix, _index, p2i(p), p2i((HeapWord*) o)); 951 } 952 _index++; 953 } 954 955 public: 956 void do_oop(oop* p) { 957 do_oop_work(p); 958 } 959 960 void do_oop(narrowOop* p) { 961 do_oop_work(p); 962 } 963 964 }; 965 966 class PrintAllRefsObjectClosure : public ObjectClosure { 967 const char* _prefix; 968 969 public: 970 PrintAllRefsObjectClosure(const char* prefix) : _prefix(prefix) {} 971 972 void do_object(oop p) { 973 if (ShenandoahHeap::heap()->is_in(p)) { 974 tty->print_cr("%s object "PTR_FORMAT" (marked: %s) (%s "PTR_FORMAT") refers to:", 975 _prefix, p2i(p), 976 BOOL_TO_STR(ShenandoahHeap::heap()->is_marked_complete(p)), 977 p->klass()->internal_name(), p2i(p->klass())); 978 PrintAllRefsOopClosure cl(_prefix); 979 p->oop_iterate(&cl); 980 } 981 } 982 }; 983 984 void ShenandoahHeap::print_all_refs(const char* prefix) { 985 tty->print_cr("printing all references in the heap"); 986 tty->print_cr("root references:"); 987 988 ensure_parsability(false); 989 990 PrintAllRefsOopClosure cl(prefix); 991 roots_iterate(&cl); 992 993 tty->print_cr("heap references:"); 994 PrintAllRefsObjectClosure cl2(prefix); 995 object_iterate(&cl2); 996 } 997 998 class VerifyAfterMarkingOopClosure: public ExtendedOopClosure { 999 private: 1000 ShenandoahHeap* _heap; 1001 1002 public: 1003 VerifyAfterMarkingOopClosure() : 1004 _heap(ShenandoahHeap::heap()) { } 1005 1006 private: 1007 template <class T> 1008 inline void do_oop_work(T* p) { 1009 oop o = oopDesc::load_decode_heap_oop(p); 1010 if (o != NULL) { 1011 if (! _heap->is_marked_complete(o)) { 1012 _heap->print_heap_regions(); 1013 _heap->print_all_refs("post-mark"); 1014 tty->print_cr("oop not marked, although referrer is marked: "PTR_FORMAT": in_heap: %s, is_marked: %s", 1015 p2i((HeapWord*) o), BOOL_TO_STR(_heap->is_in(o)), BOOL_TO_STR(_heap->is_marked_complete(o))); 1016 _heap->print_heap_locations((HeapWord*) o, (HeapWord*) o + o->size()); 1017 1018 tty->print_cr("oop class: %s", o->klass()->internal_name()); 1019 if (_heap->is_in(p)) { 1020 oop referrer = oop(_heap->heap_region_containing(p)->block_start_const(p)); 1021 tty->print_cr("Referrer starts at addr "PTR_FORMAT, p2i((HeapWord*) referrer)); 1022 referrer->print(); 1023 _heap->print_heap_locations((HeapWord*) referrer, (HeapWord*) referrer + referrer->size()); 1024 } 1025 tty->print_cr("heap region containing object:"); 1026 _heap->heap_region_containing(o)->print(); 1027 tty->print_cr("heap region containing referrer:"); 1028 _heap->heap_region_containing(p)->print(); 1029 tty->print_cr("heap region containing forwardee:"); 1030 _heap->heap_region_containing(oopDesc::bs()->read_barrier(o))->print(); 1031 } 1032 assert(o->is_oop(), "oop must be an oop"); 1033 assert(Metaspace::contains(o->klass()), "klass pointer must go to metaspace"); 1034 if (! oopDesc::unsafe_equals(o, oopDesc::bs()->read_barrier(o))) { 1035 tty->print_cr("oops has forwardee: p: "PTR_FORMAT" (%s), o = "PTR_FORMAT" (%s), new-o: "PTR_FORMAT" (%s)", 1036 p2i(p), 1037 BOOL_TO_STR(_heap->in_collection_set(p)), 1038 p2i(o), 1039 BOOL_TO_STR(_heap->in_collection_set(o)), 1040 p2i((HeapWord*) oopDesc::bs()->read_barrier(o)), 1041 BOOL_TO_STR(_heap->in_collection_set(oopDesc::bs()->read_barrier(o)))); 1042 tty->print_cr("oop class: %s", o->klass()->internal_name()); 1043 } 1044 assert(oopDesc::unsafe_equals(o, oopDesc::bs()->read_barrier(o)), "oops must not be forwarded"); 1045 assert(! _heap->in_collection_set(o), "references must not point to dirty heap regions"); 1046 assert(_heap->is_marked_complete(o), "live oops must be marked current"); 1047 } 1048 } 1049 1050 public: 1051 void do_oop(oop* p) { 1052 do_oop_work(p); 1053 } 1054 1055 void do_oop(narrowOop* p) { 1056 do_oop_work(p); 1057 } 1058 1059 }; 1060 1061 void ShenandoahHeap::verify_heap_after_marking() { 1062 1063 verify_heap_size_consistency(); 1064 1065 log_trace(gc)("verifying heap after marking"); 1066 1067 VerifyAfterMarkingOopClosure cl; 1068 roots_iterate(&cl); 1069 ObjectToOopClosure objs(&cl); 1070 object_iterate(&objs); 1071 } 1072 1073 1074 void ShenandoahHeap::reclaim_humongous_region_at(ShenandoahHeapRegion* r) { 1075 assert(r->is_humongous_start(), "reclaim regions starting with the first one"); 1076 1077 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 1078 size_t size = humongous_obj->size() + BrooksPointer::word_size(); 1079 uint required_regions = ShenandoahHumongous::required_regions(size * HeapWordSize); 1080 uint index = r->region_number(); 1081 1082 1083 assert(!r->has_live(), "liveness must be zero"); 1084 1085 for(size_t i = 0; i < required_regions; i++) { 1086 1087 ShenandoahHeapRegion* region = _ordered_regions->get(index++); 1088 1089 assert((region->is_humongous_start() || region->is_humongous_continuation()), 1090 "expect correct humongous start or continuation"); 1091 1092 if (log_is_enabled(Debug, gc, humongous)) { 1093 log_debug(gc, humongous)("reclaiming "UINT32_FORMAT" humongous regions for object of size: "SIZE_FORMAT" words", required_regions, size); 1094 ResourceMark rm; 1095 outputStream* out = Log(gc, humongous)::debug_stream(); 1096 region->print_on(out); 1097 } 1098 1099 region->recycle(); 1100 ShenandoahHeap::heap()->decrease_used(ShenandoahHeapRegion::RegionSizeBytes); 1101 } 1102 } 1103 1104 class ShenandoahReclaimHumongousRegionsClosure : public ShenandoahHeapRegionClosure { 1105 1106 bool doHeapRegion(ShenandoahHeapRegion* r) { 1107 ShenandoahHeap* heap = ShenandoahHeap::heap(); 1108 1109 if (r->is_humongous_start()) { 1110 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 1111 if (! heap->is_marked_complete(humongous_obj)) { 1112 1113 heap->reclaim_humongous_region_at(r); 1114 } 1115 } 1116 return false; 1117 } 1118 }; 1119 1120 #ifdef ASSERT 1121 class CheckCollectionSetClosure: public ShenandoahHeapRegionClosure { 1122 bool doHeapRegion(ShenandoahHeapRegion* r) { 1123 assert(! ShenandoahHeap::heap()->in_collection_set(r), "Should have been cleared by now"); 1124 return false; 1125 } 1126 }; 1127 #endif 1128 1129 void ShenandoahHeap::prepare_for_concurrent_evacuation() { 1130 assert(_ordered_regions->get(0)->region_number() == 0, "FIXME CHF. FIXME CHF!"); 1131 1132 log_develop_trace(gc)("Thread %d started prepare_for_concurrent_evacuation", Thread::current()->osthread()->thread_id()); 1133 1134 if (!cancelled_concgc()) { 1135 1136 recycle_dirty_regions(); 1137 1138 ensure_parsability(true); 1139 1140 if (UseShenandoahMatrix) { 1141 if (PrintShenandoahMatrix) { 1142 outputStream* log = Log(gc)::info_stream(); 1143 connection_matrix()->print_on(log); 1144 } 1145 } 1146 1147 if (ShenandoahVerify || (UseShenandoahMatrix && VerifyShenandoahMatrix)) { 1148 verify_heap_reachable_at_safepoint(); 1149 } 1150 1151 #ifdef ASSERT 1152 if (ShenandoahVerify) { 1153 verify_heap_after_marking(); 1154 } 1155 #endif 1156 1157 // NOTE: This needs to be done during a stop the world pause, because 1158 // putting regions into the collection set concurrently with Java threads 1159 // will create a race. In particular, acmp could fail because when we 1160 // resolve the first operand, the containing region might not yet be in 1161 // the collection set, and thus return the original oop. When the 2nd 1162 // operand gets resolved, the region could be in the collection set 1163 // and the oop gets evacuated. If both operands have originally been 1164 // the same, we get false negatives. 1165 1166 { 1167 ShenandoahHeapLock lock(this); 1168 _collection_set->clear(); 1169 _free_regions->clear(); 1170 1171 ShenandoahReclaimHumongousRegionsClosure reclaim; 1172 heap_region_iterate(&reclaim); 1173 1174 #ifdef ASSERT 1175 CheckCollectionSetClosure ccsc; 1176 _ordered_regions->heap_region_iterate(&ccsc); 1177 #endif 1178 1179 _shenandoah_policy->choose_collection_set(_collection_set); 1180 1181 _shenandoah_policy->choose_free_set(_free_regions); 1182 } 1183 1184 _bytes_allocated_since_cm = 0; 1185 1186 Universe::update_heap_info_at_gc(); 1187 } 1188 } 1189 1190 1191 class RetireTLABClosure : public ThreadClosure { 1192 private: 1193 bool _retire; 1194 1195 public: 1196 RetireTLABClosure(bool retire) : _retire(retire) { 1197 } 1198 1199 void do_thread(Thread* thread) { 1200 thread->gclab().make_parsable(_retire); 1201 } 1202 }; 1203 1204 void ShenandoahHeap::ensure_parsability(bool retire_tlabs) { 1205 if (UseTLAB) { 1206 CollectedHeap::ensure_parsability(retire_tlabs); 1207 RetireTLABClosure cl(retire_tlabs); 1208 Threads::threads_do(&cl); 1209 } 1210 } 1211 1212 class ShenandoahEvacuateUpdateRootsClosure: public ExtendedOopClosure { 1213 private: 1214 ShenandoahHeap* _heap; 1215 Thread* _thread; 1216 public: 1217 ShenandoahEvacuateUpdateRootsClosure() : 1218 _heap(ShenandoahHeap::heap()), _thread(Thread::current()) { 1219 } 1220 1221 private: 1222 template <class T> 1223 void do_oop_work(T* p) { 1224 assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress"); 1225 1226 T o = oopDesc::load_heap_oop(p); 1227 if (! oopDesc::is_null(o)) { 1228 oop obj = oopDesc::decode_heap_oop_not_null(o); 1229 if (_heap->in_collection_set(obj)) { 1230 assert(_heap->is_marked_complete(obj), "only evacuate marked objects %d %d", 1231 _heap->is_marked_complete(obj), _heap->is_marked_complete(ShenandoahBarrierSet::resolve_oop_static_not_null(obj))); 1232 oop resolved = ShenandoahBarrierSet::resolve_oop_static_not_null(obj); 1233 if (oopDesc::unsafe_equals(resolved, obj)) { 1234 bool evac; 1235 resolved = _heap->evacuate_object(obj, _thread, evac); 1236 } 1237 oopDesc::encode_store_heap_oop(p, resolved); 1238 } 1239 } 1240 #ifdef ASSERT 1241 else { 1242 // tty->print_cr("not updating root at: "PTR_FORMAT" with object: "PTR_FORMAT", is_in_heap: %s, is_in_cset: %s, is_marked: %s", 1243 // p2i(p), 1244 // p2i((HeapWord*) obj), 1245 // BOOL_TO_STR(_heap->is_in(obj)), 1246 // BOOL_TO_STR(_heap->in_cset_fast_test(obj)), 1247 // BOOL_TO_STR(_heap->is_marked_complete(obj))); 1248 } 1249 #endif 1250 } 1251 1252 public: 1253 void do_oop(oop* p) { 1254 do_oop_work(p); 1255 } 1256 void do_oop(narrowOop* p) { 1257 do_oop_work(p); 1258 } 1259 }; 1260 1261 class ShenandoahEvacuateUpdateRootsTask : public AbstractGangTask { 1262 ShenandoahRootEvacuator* _rp; 1263 public: 1264 1265 ShenandoahEvacuateUpdateRootsTask(ShenandoahRootEvacuator* rp) : 1266 AbstractGangTask("Shenandoah evacuate and update roots"), 1267 _rp(rp) 1268 { 1269 // Nothing else to do. 1270 } 1271 1272 void work(uint worker_id) { 1273 ShenandoahEvacuateUpdateRootsClosure cl; 1274 MarkingCodeBlobClosure blobsCl(&cl, CodeBlobToOopClosure::FixRelocations); 1275 1276 _rp->process_evacuate_roots(&cl, &blobsCl, worker_id); 1277 } 1278 }; 1279 1280 class ShenandoahFixRootsTask : public AbstractGangTask { 1281 ShenandoahRootEvacuator* _rp; 1282 public: 1283 1284 ShenandoahFixRootsTask(ShenandoahRootEvacuator* rp) : 1285 AbstractGangTask("Shenandoah update roots"), 1286 _rp(rp) 1287 { 1288 // Nothing else to do. 1289 } 1290 1291 void work(uint worker_id) { 1292 SCMUpdateRefsClosure cl; 1293 MarkingCodeBlobClosure blobsCl(&cl, CodeBlobToOopClosure::FixRelocations); 1294 1295 _rp->process_evacuate_roots(&cl, &blobsCl, worker_id); 1296 } 1297 }; 1298 void ShenandoahHeap::evacuate_and_update_roots() { 1299 1300 COMPILER2_PRESENT(DerivedPointerTable::clear()); 1301 1302 #ifdef ASSERT 1303 if (ShenandoahVerifyReadsToFromSpace) { 1304 set_from_region_protection(false); 1305 } 1306 #endif 1307 1308 assert(SafepointSynchronize::is_at_safepoint(), "Only iterate roots while world is stopped"); 1309 ClassLoaderDataGraph::clear_claimed_marks(); 1310 1311 { 1312 ShenandoahRootEvacuator rp(this, workers()->active_workers(), ShenandoahCollectorPolicy::evac_thread_roots); 1313 ShenandoahEvacuateUpdateRootsTask roots_task(&rp); 1314 workers()->run_task(&roots_task); 1315 } 1316 1317 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 1318 1319 if (cancelled_concgc()) { 1320 // If initial evacuation has been cancelled, we need to update all references 1321 // after all workers have finished. Otherwise we might run into the following problem: 1322 // GC thread 1 cannot allocate anymore, thus evacuation fails, leaves from-space ptr of object X. 1323 // GC thread 2 evacuates the same object X to to-space 1324 // which leaves a truly dangling from-space reference in the first root oop*. This must not happen. 1325 // clear() and update_pointers() must always be called in pairs, 1326 // cannot nest with above clear()/update_pointers(). 1327 COMPILER2_PRESENT(DerivedPointerTable::clear()); 1328 ShenandoahRootEvacuator rp(this, workers()->active_workers(), ShenandoahCollectorPolicy::evac_thread_roots); 1329 ShenandoahFixRootsTask update_roots_task(&rp); 1330 workers()->run_task(&update_roots_task); 1331 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 1332 } 1333 1334 #ifdef ASSERT 1335 if (ShenandoahVerifyReadsToFromSpace) { 1336 set_from_region_protection(true); 1337 } 1338 #endif 1339 1340 #ifdef ASSERT 1341 { 1342 AssertToSpaceClosure cl; 1343 CodeBlobToOopClosure code_cl(&cl, !CodeBlobToOopClosure::FixRelocations); 1344 ShenandoahRootEvacuator rp(this, 1); 1345 rp.process_evacuate_roots(&cl, &code_cl, 0); 1346 } 1347 #endif 1348 } 1349 1350 1351 void ShenandoahHeap::do_evacuation() { 1352 1353 parallel_evacuate(); 1354 1355 if (ShenandoahVerify && ! cancelled_concgc()) { 1356 VM_ShenandoahVerifyHeapAfterEvacuation verify_after_evacuation; 1357 if (Thread::current()->is_VM_thread()) { 1358 verify_after_evacuation.doit(); 1359 } else { 1360 VMThread::execute(&verify_after_evacuation); 1361 } 1362 } 1363 1364 } 1365 1366 void ShenandoahHeap::parallel_evacuate() { 1367 log_develop_trace(gc)("starting parallel_evacuate"); 1368 1369 _shenandoah_policy->record_phase_start(ShenandoahCollectorPolicy::conc_evac); 1370 1371 if (log_is_enabled(Trace, gc, region)) { 1372 ResourceMark rm; 1373 outputStream *out = Log(gc, region)::trace_stream(); 1374 out->print("Printing all available regions"); 1375 print_heap_regions(out); 1376 } 1377 1378 if (log_is_enabled(Trace, gc, cset)) { 1379 ResourceMark rm; 1380 outputStream *out = Log(gc, cset)::trace_stream(); 1381 out->print("Printing collection set which contains "SIZE_FORMAT" regions:\n", _collection_set->count()); 1382 _collection_set->print(out); 1383 1384 out->print("Printing free set which contains "SIZE_FORMAT" regions:\n", _free_regions->count()); 1385 _free_regions->print(out); 1386 } 1387 1388 ParallelEvacuationTask evacuationTask = ParallelEvacuationTask(this, _collection_set); 1389 1390 1391 workers()->run_task(&evacuationTask); 1392 1393 if (log_is_enabled(Trace, gc, cset)) { 1394 ResourceMark rm; 1395 outputStream *out = Log(gc, cset)::trace_stream(); 1396 out->print("Printing postgc collection set which contains "SIZE_FORMAT" regions:\n", 1397 _collection_set->count()); 1398 1399 _collection_set->print(out); 1400 1401 out->print("Printing postgc free regions which contain "SIZE_FORMAT" free regions:\n", 1402 _free_regions->count()); 1403 _free_regions->print(out); 1404 1405 } 1406 1407 if (log_is_enabled(Trace, gc, region)) { 1408 ResourceMark rm; 1409 outputStream *out = Log(gc, region)::trace_stream(); 1410 out->print_cr("all regions after evacuation:"); 1411 print_heap_regions(out); 1412 } 1413 1414 _shenandoah_policy->record_phase_end(ShenandoahCollectorPolicy::conc_evac); 1415 } 1416 1417 class VerifyEvacuationClosure: public ExtendedOopClosure { 1418 private: 1419 ShenandoahHeap* _heap; 1420 ShenandoahHeapRegion* _from_region; 1421 1422 public: 1423 VerifyEvacuationClosure(ShenandoahHeapRegion* from_region) : 1424 _heap(ShenandoahHeap::heap()), _from_region(from_region) { } 1425 private: 1426 template <class T> 1427 inline void do_oop_work(T* p) { 1428 oop heap_oop = oopDesc::load_decode_heap_oop(p); 1429 if (! oopDesc::is_null(heap_oop)) { 1430 guarantee(! _from_region->is_in(heap_oop), "no references to from-region allowed after evacuation: "PTR_FORMAT, p2i((HeapWord*) heap_oop)); 1431 } 1432 } 1433 1434 public: 1435 void do_oop(oop* p) { 1436 do_oop_work(p); 1437 } 1438 1439 void do_oop(narrowOop* p) { 1440 do_oop_work(p); 1441 } 1442 1443 }; 1444 1445 void ShenandoahHeap::roots_iterate(OopClosure* cl) { 1446 1447 assert(SafepointSynchronize::is_at_safepoint(), "Only iterate roots while world is stopped"); 1448 1449 CodeBlobToOopClosure blobsCl(cl, false); 1450 CLDToOopClosure cldCl(cl); 1451 1452 ClassLoaderDataGraph::clear_claimed_marks(); 1453 1454 ShenandoahRootProcessor rp(this, 1); 1455 rp.process_all_roots(cl, NULL, &cldCl, &blobsCl, 0); 1456 } 1457 1458 void ShenandoahHeap::verify_evacuation(ShenandoahHeapRegion* from_region) { 1459 1460 VerifyEvacuationClosure rootsCl(from_region); 1461 roots_iterate(&rootsCl); 1462 1463 } 1464 1465 bool ShenandoahHeap::supports_tlab_allocation() const { 1466 return true; 1467 } 1468 1469 1470 size_t ShenandoahHeap::unsafe_max_tlab_alloc(Thread *thread) const { 1471 size_t idx = _free_regions->current_index(); 1472 ShenandoahHeapRegion* current = _free_regions->get(idx); 1473 if (current == NULL) { 1474 return 0; 1475 } else if (current->free() > MinTLABSize) { 1476 // Current region has enough space left, can use it. 1477 return current->free(); 1478 } else { 1479 // No more space in current region, we will take next free region 1480 // on the next TLAB allocation. 1481 return ShenandoahHeapRegion::RegionSizeBytes; 1482 } 1483 } 1484 1485 size_t ShenandoahHeap::max_tlab_size() const { 1486 return ShenandoahHeapRegion::RegionSizeBytes; 1487 } 1488 1489 class ResizeGCLABClosure : public ThreadClosure { 1490 public: 1491 void do_thread(Thread* thread) { 1492 thread->gclab().resize(); 1493 } 1494 }; 1495 1496 void ShenandoahHeap::resize_all_tlabs() { 1497 CollectedHeap::resize_all_tlabs(); 1498 1499 ResizeGCLABClosure cl; 1500 Threads::threads_do(&cl); 1501 } 1502 1503 class AccumulateStatisticsGCLABClosure : public ThreadClosure { 1504 public: 1505 void do_thread(Thread* thread) { 1506 thread->gclab().accumulate_statistics(); 1507 thread->gclab().initialize_statistics(); 1508 } 1509 }; 1510 1511 void ShenandoahHeap::accumulate_statistics_all_gclabs() { 1512 AccumulateStatisticsGCLABClosure cl; 1513 Threads::threads_do(&cl); 1514 } 1515 1516 bool ShenandoahHeap::can_elide_tlab_store_barriers() const { 1517 return true; 1518 } 1519 1520 oop ShenandoahHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) { 1521 // Overridden to do nothing. 1522 return new_obj; 1523 } 1524 1525 bool ShenandoahHeap::can_elide_initializing_store_barrier(oop new_obj) { 1526 return true; 1527 } 1528 1529 bool ShenandoahHeap::card_mark_must_follow_store() const { 1530 return false; 1531 } 1532 1533 void ShenandoahHeap::collect(GCCause::Cause cause) { 1534 assert(cause != GCCause::_gc_locker, "no JNI critical callback"); 1535 if (GCCause::is_user_requested_gc(cause)) { 1536 if (! DisableExplicitGC) { 1537 _concurrent_gc_thread->do_full_gc(cause); 1538 } 1539 } else if (cause == GCCause::_allocation_failure) { 1540 collector_policy()->set_should_clear_all_soft_refs(true); 1541 _concurrent_gc_thread->do_full_gc(cause); 1542 } 1543 } 1544 1545 void ShenandoahHeap::do_full_collection(bool clear_all_soft_refs) { 1546 //assert(false, "Shouldn't need to do full collections"); 1547 } 1548 1549 AdaptiveSizePolicy* ShenandoahHeap::size_policy() { 1550 Unimplemented(); 1551 return NULL; 1552 1553 } 1554 1555 CollectorPolicy* ShenandoahHeap::collector_policy() const { 1556 return _shenandoah_policy; 1557 } 1558 1559 1560 HeapWord* ShenandoahHeap::block_start(const void* addr) const { 1561 Space* sp = heap_region_containing(addr); 1562 if (sp != NULL) { 1563 return sp->block_start(addr); 1564 } 1565 return NULL; 1566 } 1567 1568 size_t ShenandoahHeap::block_size(const HeapWord* addr) const { 1569 Space* sp = heap_region_containing(addr); 1570 assert(sp != NULL, "block_size of address outside of heap"); 1571 return sp->block_size(addr); 1572 } 1573 1574 bool ShenandoahHeap::block_is_obj(const HeapWord* addr) const { 1575 Space* sp = heap_region_containing(addr); 1576 return sp->block_is_obj(addr); 1577 } 1578 1579 jlong ShenandoahHeap::millis_since_last_gc() { 1580 return 0; 1581 } 1582 1583 void ShenandoahHeap::prepare_for_verify() { 1584 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 1585 ensure_parsability(false); 1586 } 1587 } 1588 1589 void ShenandoahHeap::print_gc_threads_on(outputStream* st) const { 1590 workers()->print_worker_threads_on(st); 1591 } 1592 1593 void ShenandoahHeap::gc_threads_do(ThreadClosure* tcl) const { 1594 workers()->threads_do(tcl); 1595 } 1596 1597 void ShenandoahHeap::print_tracing_info() const { 1598 if (log_is_enabled(Info, gc, stats)) { 1599 ResourceMark rm; 1600 outputStream* out = Log(gc, stats)::info_stream(); 1601 _shenandoah_policy->print_tracing_info(out); 1602 } 1603 } 1604 1605 class ShenandoahVerifyRootsClosure: public ExtendedOopClosure { 1606 private: 1607 ShenandoahHeap* _heap; 1608 VerifyOption _vo; 1609 bool _failures; 1610 public: 1611 // _vo == UsePrevMarking -> use "prev" marking information, 1612 // _vo == UseNextMarking -> use "next" marking information, 1613 // _vo == UseMarkWord -> use mark word from object header. 1614 ShenandoahVerifyRootsClosure(VerifyOption vo) : 1615 _heap(ShenandoahHeap::heap()), 1616 _vo(vo), 1617 _failures(false) { } 1618 1619 bool failures() { return _failures; } 1620 1621 private: 1622 template <class T> 1623 inline void do_oop_work(T* p) { 1624 oop obj = oopDesc::load_decode_heap_oop(p); 1625 if (! oopDesc::is_null(obj) && ! obj->is_oop()) { 1626 { // Just for debugging. 1627 tty->print_cr("Root location "PTR_FORMAT 1628 "verified "PTR_FORMAT, p2i(p), p2i((void*) obj)); 1629 // obj->print_on(tty); 1630 } 1631 } 1632 guarantee(obj->is_oop_or_null(), "is oop or null"); 1633 } 1634 1635 public: 1636 void do_oop(oop* p) { 1637 do_oop_work(p); 1638 } 1639 1640 void do_oop(narrowOop* p) { 1641 do_oop_work(p); 1642 } 1643 1644 }; 1645 1646 class ShenandoahVerifyHeapClosure: public ObjectClosure { 1647 private: 1648 ShenandoahVerifyRootsClosure _rootsCl; 1649 public: 1650 ShenandoahVerifyHeapClosure(ShenandoahVerifyRootsClosure rc) : 1651 _rootsCl(rc) {}; 1652 1653 void do_object(oop p) { 1654 _rootsCl.do_oop(&p); 1655 } 1656 }; 1657 1658 class ShenandoahVerifyKlassClosure: public KlassClosure { 1659 OopClosure *_oop_closure; 1660 public: 1661 ShenandoahVerifyKlassClosure(OopClosure* cl) : _oop_closure(cl) {} 1662 void do_klass(Klass* k) { 1663 k->oops_do(_oop_closure); 1664 } 1665 }; 1666 1667 void ShenandoahHeap::verify(VerifyOption vo) { 1668 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 1669 1670 ShenandoahVerifyRootsClosure rootsCl(vo); 1671 1672 assert(Thread::current()->is_VM_thread(), 1673 "Expected to be executed serially by the VM thread at this point"); 1674 1675 roots_iterate(&rootsCl); 1676 1677 bool failures = rootsCl.failures(); 1678 log_trace(gc)("verify failures: %s", BOOL_TO_STR(failures)); 1679 1680 ShenandoahVerifyHeapClosure heapCl(rootsCl); 1681 1682 object_iterate(&heapCl); 1683 // TODO: Implement rest of it. 1684 } else { 1685 tty->print("(SKIPPING roots, heapRegions, remset) "); 1686 } 1687 } 1688 size_t ShenandoahHeap::tlab_capacity(Thread *thr) const { 1689 return _free_regions->capacity(); 1690 } 1691 1692 class ShenandoahIterateObjectClosureRegionClosure: public ShenandoahHeapRegionClosure { 1693 ObjectClosure* _cl; 1694 public: 1695 ShenandoahIterateObjectClosureRegionClosure(ObjectClosure* cl) : _cl(cl) {} 1696 bool doHeapRegion(ShenandoahHeapRegion* r) { 1697 ShenandoahHeap::heap()->marked_object_iterate(r, _cl); 1698 return false; 1699 } 1700 }; 1701 1702 void ShenandoahHeap::object_iterate(ObjectClosure* cl) { 1703 ShenandoahIterateObjectClosureRegionClosure blk(cl); 1704 heap_region_iterate(&blk, false, true); 1705 } 1706 1707 class ShenandoahSafeObjectIterateAdjustPtrsClosure : public MetadataAwareOopClosure { 1708 private: 1709 ShenandoahHeap* _heap; 1710 1711 public: 1712 ShenandoahSafeObjectIterateAdjustPtrsClosure() : _heap(ShenandoahHeap::heap()) {} 1713 1714 private: 1715 template <class T> 1716 inline void do_oop_work(T* p) { 1717 T o = oopDesc::load_heap_oop(p); 1718 if (!oopDesc::is_null(o)) { 1719 oop obj = oopDesc::decode_heap_oop_not_null(o); 1720 oopDesc::encode_store_heap_oop(p, BrooksPointer::forwardee(obj)); 1721 } 1722 } 1723 public: 1724 void do_oop(oop* p) { 1725 do_oop_work(p); 1726 } 1727 void do_oop(narrowOop* p) { 1728 do_oop_work(p); 1729 } 1730 }; 1731 1732 class ShenandoahSafeObjectIterateAndUpdate : public ObjectClosure { 1733 private: 1734 ObjectClosure* _cl; 1735 public: 1736 ShenandoahSafeObjectIterateAndUpdate(ObjectClosure *cl) : _cl(cl) {} 1737 1738 virtual void do_object(oop obj) { 1739 assert (oopDesc::unsafe_equals(obj, BrooksPointer::forwardee(obj)), 1740 "avoid double-counting: only non-forwarded objects here"); 1741 1742 // Fix up the ptrs. 1743 ShenandoahSafeObjectIterateAdjustPtrsClosure adjust_ptrs; 1744 obj->oop_iterate(&adjust_ptrs); 1745 1746 // Can reply the object now: 1747 _cl->do_object(obj); 1748 } 1749 }; 1750 1751 void ShenandoahHeap::safe_object_iterate(ObjectClosure* cl) { 1752 assert(SafepointSynchronize::is_at_safepoint(), "safe iteration is only available during safepoints"); 1753 1754 // Safe iteration does objects only with correct references. 1755 // This is why we skip dirty regions that have stale copies of objects, 1756 // and fix up the pointers in the returned objects. 1757 1758 ShenandoahSafeObjectIterateAndUpdate safe_cl(cl); 1759 ShenandoahIterateObjectClosureRegionClosure blk(&safe_cl); 1760 heap_region_iterate(&blk, 1761 /* skip_dirty_regions = */ true, 1762 /* skip_humongous_continuations = */ true); 1763 1764 _need_update_refs = false; // already updated the references 1765 } 1766 1767 // Apply blk->doHeapRegion() on all committed regions in address order, 1768 // terminating the iteration early if doHeapRegion() returns true. 1769 void ShenandoahHeap::heap_region_iterate(ShenandoahHeapRegionClosure* blk, bool skip_dirty_regions, bool skip_humongous_continuation) const { 1770 for (size_t i = 0; i < _num_regions; i++) { 1771 ShenandoahHeapRegion* current = _ordered_regions->get(i); 1772 if (skip_humongous_continuation && current->is_humongous_continuation()) { 1773 continue; 1774 } 1775 if (skip_dirty_regions && in_collection_set(current)) { 1776 continue; 1777 } 1778 if (blk->doHeapRegion(current)) { 1779 return; 1780 } 1781 } 1782 } 1783 1784 class ClearLivenessClosure : public ShenandoahHeapRegionClosure { 1785 ShenandoahHeap* sh; 1786 public: 1787 ClearLivenessClosure(ShenandoahHeap* heap) : sh(heap) { } 1788 1789 bool doHeapRegion(ShenandoahHeapRegion* r) { 1790 r->clear_live_data(); 1791 sh->set_next_top_at_mark_start(r->bottom(), r->top()); 1792 return false; 1793 } 1794 }; 1795 1796 void ShenandoahHeap::start_concurrent_marking() { 1797 1798 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::accumulate_stats); 1799 accumulate_statistics_all_tlabs(); 1800 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::accumulate_stats); 1801 1802 set_concurrent_mark_in_progress(true); 1803 // We need to reset all TLABs because we'd lose marks on all objects allocated in them. 1804 if (UseTLAB) { 1805 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::make_parsable); 1806 ensure_parsability(true); 1807 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::make_parsable); 1808 } 1809 1810 _shenandoah_policy->record_bytes_allocated(_bytes_allocated_since_cm); 1811 _used_start_gc = used(); 1812 1813 #ifdef ASSERT 1814 if (ShenandoahDumpHeapBeforeConcurrentMark) { 1815 ensure_parsability(false); 1816 print_all_refs("pre-mark"); 1817 } 1818 #endif 1819 1820 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::clear_liveness); 1821 ClearLivenessClosure clc(this); 1822 heap_region_iterate(&clc); 1823 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::clear_liveness); 1824 1825 if (UseShenandoahMatrix) { 1826 connection_matrix()->clear_all(); 1827 } 1828 // print_all_refs("pre -mark"); 1829 1830 // oopDesc::_debug = true; 1831 1832 // Make above changes visible to worker threads 1833 OrderAccess::fence(); 1834 1835 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::scan_roots); 1836 concurrentMark()->init_mark_roots(); 1837 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::scan_roots); 1838 1839 // print_all_refs("pre-mark2"); 1840 } 1841 1842 class VerifyAfterEvacuationClosure : public ExtendedOopClosure { 1843 1844 ShenandoahHeap* _sh; 1845 1846 public: 1847 VerifyAfterEvacuationClosure() : _sh ( ShenandoahHeap::heap() ) {} 1848 1849 template<class T> void do_oop_nv(T* p) { 1850 T heap_oop = oopDesc::load_heap_oop(p); 1851 if (!oopDesc::is_null(heap_oop)) { 1852 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 1853 guarantee(_sh->in_collection_set(obj) == (! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))), 1854 "forwarded objects can only exist in dirty (from-space) regions is_dirty: %s, is_forwarded: %s obj-klass: %s, marked: %s", 1855 BOOL_TO_STR(_sh->in_collection_set(obj)), 1856 BOOL_TO_STR(! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))), 1857 obj->klass()->external_name(), 1858 BOOL_TO_STR(_sh->is_marked_complete(obj)) 1859 ); 1860 obj = oopDesc::bs()->read_barrier(obj); 1861 guarantee(! _sh->in_collection_set(obj), "forwarded oops must not point to dirty regions"); 1862 guarantee(obj->is_oop(), "is_oop"); 1863 guarantee(Metaspace::contains(obj->klass()), "klass pointer must go to metaspace"); 1864 } 1865 } 1866 1867 void do_oop(oop* p) { do_oop_nv(p); } 1868 void do_oop(narrowOop* p) { do_oop_nv(p); } 1869 1870 }; 1871 1872 void ShenandoahHeap::verify_heap_after_evacuation() { 1873 1874 verify_heap_size_consistency(); 1875 1876 ensure_parsability(false); 1877 1878 VerifyAfterEvacuationClosure cl; 1879 roots_iterate(&cl); 1880 1881 ObjectToOopClosure objs(&cl); 1882 object_iterate(&objs); 1883 1884 } 1885 1886 class VerifyRegionsAfterUpdateRefsClosure : public ShenandoahHeapRegionClosure { 1887 public: 1888 bool doHeapRegion(ShenandoahHeapRegion* r) { 1889 assert(! ShenandoahHeap::heap()->in_collection_set(r), "no region must be in collection set"); 1890 return false; 1891 } 1892 }; 1893 1894 void ShenandoahHeap::swap_mark_bitmaps() { 1895 // Swap bitmaps. 1896 CMBitMap* tmp1 = _complete_mark_bit_map; 1897 _complete_mark_bit_map = _next_mark_bit_map; 1898 _next_mark_bit_map = tmp1; 1899 1900 // Swap top-at-mark-start pointers 1901 HeapWord** tmp2 = _complete_top_at_mark_starts; 1902 _complete_top_at_mark_starts = _next_top_at_mark_starts; 1903 _next_top_at_mark_starts = tmp2; 1904 1905 HeapWord** tmp3 = _complete_top_at_mark_starts_base; 1906 _complete_top_at_mark_starts_base = _next_top_at_mark_starts_base; 1907 _next_top_at_mark_starts_base = tmp3; 1908 } 1909 1910 class VerifyReachableHeapClosure : public ExtendedOopClosure { 1911 private: 1912 SCMObjToScanQueue* _queue; 1913 ShenandoahHeap* _heap; 1914 CMBitMap* _map; 1915 bool _check_matrix; 1916 oop _obj; 1917 public: 1918 VerifyReachableHeapClosure(SCMObjToScanQueue* queue, CMBitMap* map, bool check_matrix) : 1919 _queue(queue), _heap(ShenandoahHeap::heap()), _map(map), _check_matrix(check_matrix) {}; 1920 template <class T> 1921 void do_oop_work(T* p) { 1922 T o = oopDesc::load_heap_oop(p); 1923 if (!oopDesc::is_null(o)) { 1924 oop obj = oopDesc::decode_heap_oop_not_null(o); 1925 guarantee(check_obj_alignment(obj), "sanity"); 1926 1927 guarantee(!oopDesc::is_null(obj), "sanity"); 1928 guarantee(_heap->is_in(obj), "sanity"); 1929 1930 oop forw = BrooksPointer::forwardee(obj); 1931 guarantee(!oopDesc::is_null(forw), "sanity"); 1932 guarantee(_heap->is_in(forw), "sanity"); 1933 1934 guarantee(oopDesc::unsafe_equals(obj, forw), "should not be forwarded"); 1935 1936 if (_check_matrix) { 1937 uint from_idx = _heap->heap_region_index_containing(p); 1938 uint to_idx = _heap->heap_region_index_containing(obj); 1939 if (!_heap->connection_matrix()->is_connected(from_idx, to_idx)) { 1940 tty->print_cr("from-obj: "); 1941 _obj->print_on(tty); 1942 tty->print_cr("to-obj:"); 1943 obj->print_on(tty); 1944 tty->print_cr("from-obj allocated after mark: %s", BOOL_TO_STR(_heap->allocated_after_complete_mark_start((HeapWord*) _obj))); 1945 tty->print_cr("to-obj allocated after mark: %s", BOOL_TO_STR(_heap->allocated_after_complete_mark_start((HeapWord*) obj))); 1946 tty->print_cr("from-obj marked: %s", BOOL_TO_STR(_heap->is_marked_complete(_obj))); 1947 tty->print_cr("to-obj marked: %s", BOOL_TO_STR(_heap->is_marked_complete(obj))); 1948 tty->print_cr("from-idx: %u, to-idx: %u", from_idx, to_idx); 1949 1950 oop fwd_from = BrooksPointer::forwardee(_obj); 1951 oop fwd_to = BrooksPointer::forwardee(obj); 1952 tty->print_cr("from-obj forwardee: " PTR_FORMAT, p2i(fwd_from)); 1953 tty->print_cr("to-obj forwardee: " PTR_FORMAT, p2i(fwd_to)); 1954 tty->print_cr("forward(from-obj) marked: %s", BOOL_TO_STR(_heap->is_marked_complete(fwd_from))); 1955 tty->print_cr("forward(to-obj) marked: %s", BOOL_TO_STR(_heap->is_marked_complete(fwd_to))); 1956 uint fwd_from_idx = _heap->heap_region_index_containing(fwd_from); 1957 uint fwd_to_idx = _heap->heap_region_index_containing(fwd_to); 1958 tty->print_cr("forward(from-idx): %u, forward(to-idx): %u", fwd_from_idx, fwd_to_idx); 1959 tty->print_cr("forward(from) connected with forward(to)? %s", BOOL_TO_STR(_heap->connection_matrix()->is_connected(fwd_from_idx, fwd_to_idx))); 1960 } 1961 guarantee(oopDesc::unsafe_equals(ShenandoahBarrierSet::resolve_oop_static_not_null(obj), obj), "polizeilich verboten"); 1962 guarantee(_heap->connection_matrix()->is_connected(from_idx, to_idx), "must be connected"); 1963 } 1964 1965 if (_map->parMark((HeapWord*) obj)) { 1966 _queue->push(SCMTask(obj)); 1967 } 1968 } 1969 } 1970 1971 void do_oop(oop* p) { do_oop_work(p); } 1972 void do_oop(narrowOop* p) { do_oop_work(p); } 1973 void set_obj(oop o) { _obj = o; } 1974 }; 1975 1976 void ShenandoahHeap::verify_heap_reachable_at_safepoint() { 1977 guarantee(SafepointSynchronize::is_at_safepoint(), "only when nothing else happens"); 1978 1979 OrderAccess::fence(); 1980 ensure_parsability(false); 1981 1982 // Allocate temporary bitmap for storing marking wavefront: 1983 ReservedSpace bm(_bitmap_size, os::vm_page_size()); 1984 os::commit_memory_or_exit(bm.base(), bm.size(), false, "couldn't allocate verification bitmap"); 1985 MemTracker::record_virtual_memory_type(bm.base(), mtGC); 1986 MemRegion verify_bitmap_region = MemRegion((HeapWord*) bm.base(), bm.size() / HeapWordSize); 1987 1988 CMBitMap _verification_bit_map; 1989 _verification_bit_map.initialize(_heap_region, verify_bitmap_region); 1990 MemRegion mr = MemRegion(_verification_bit_map.startWord(), _verification_bit_map.endWord()); 1991 _verification_bit_map.clear_range_large(mr); 1992 1993 // Initialize a single queue 1994 SCMObjToScanQueue* q = new SCMObjToScanQueue(); 1995 q->initialize(); 1996 1997 // Scan root set 1998 ClassLoaderDataGraph::clear_claimed_marks(); 1999 ShenandoahRootProcessor rp(this, 1); 2000 2001 { 2002 VerifyReachableHeapClosure cl(q, &_verification_bit_map, false); 2003 CLDToOopClosure cld_cl(&cl); 2004 CodeBlobToOopClosure code_cl(&cl, ! CodeBlobToOopClosure::FixRelocations); 2005 rp.process_all_roots(&cl, &cl, &cld_cl, &code_cl, 0); 2006 } 2007 2008 // Finish the scan 2009 { 2010 VerifyReachableHeapClosure cl(q, &_verification_bit_map, UseShenandoahMatrix && VerifyShenandoahMatrix); 2011 SCMTask task; 2012 while ((q->pop_buffer(task) || 2013 q->pop_local(task) || 2014 q->pop_overflow(task))) { 2015 oop obj = task.obj(); 2016 assert(!oopDesc::is_null(obj), "must not be null"); 2017 cl.set_obj(obj); 2018 obj->oop_iterate(&cl); 2019 } 2020 } 2021 2022 // Clean up! 2023 os::uncommit_memory(bm.base(), bm.size()); 2024 delete(q); 2025 } 2026 2027 void ShenandoahHeap::stop_concurrent_marking() { 2028 assert(concurrent_mark_in_progress(), "How else could we get here?"); 2029 if (! cancelled_concgc()) { 2030 // If we needed to update refs, and concurrent marking has been cancelled, 2031 // we need to finish updating references. 2032 set_need_update_refs(false); 2033 swap_mark_bitmaps(); 2034 } 2035 set_concurrent_mark_in_progress(false); 2036 2037 if (log_is_enabled(Trace, gc, region)) { 2038 ResourceMark rm; 2039 outputStream* out = Log(gc, region)::trace_stream(); 2040 print_heap_regions(out); 2041 } 2042 2043 } 2044 2045 void ShenandoahHeap::set_concurrent_mark_in_progress(bool in_progress) { 2046 _concurrent_mark_in_progress = in_progress ? 1 : 0; 2047 JavaThread::satb_mark_queue_set().set_active_all_threads(in_progress, !in_progress); 2048 } 2049 2050 void ShenandoahHeap::set_evacuation_in_progress_concurrently(bool in_progress) { 2051 // Note: it is important to first release the _evacuation_in_progress flag here, 2052 // so that Java threads can get out of oom_during_evacuation() and reach a safepoint, 2053 // in case a VM task is pending. 2054 set_evacuation_in_progress(in_progress); 2055 MutexLocker mu(Threads_lock); 2056 JavaThread::set_evacuation_in_progress_all_threads(in_progress); 2057 } 2058 2059 void ShenandoahHeap::set_evacuation_in_progress_at_safepoint(bool in_progress) { 2060 assert(SafepointSynchronize::is_at_safepoint(), "Only call this at safepoint"); 2061 set_evacuation_in_progress(in_progress); 2062 JavaThread::set_evacuation_in_progress_all_threads(in_progress); 2063 } 2064 2065 void ShenandoahHeap::set_evacuation_in_progress(bool in_progress) { 2066 _evacuation_in_progress = in_progress ? 1 : 0; 2067 OrderAccess::fence(); 2068 } 2069 2070 void ShenandoahHeap::verify_copy(oop p,oop c){ 2071 assert(! oopDesc::unsafe_equals(p, oopDesc::bs()->read_barrier(p)), "forwarded correctly"); 2072 assert(oopDesc::unsafe_equals(oopDesc::bs()->read_barrier(p), c), "verify pointer is correct"); 2073 if (p->klass() != c->klass()) { 2074 print_heap_regions(); 2075 } 2076 assert(p->klass() == c->klass(), "verify class p-size: "INT32_FORMAT" c-size: "INT32_FORMAT, p->size(), c->size()); 2077 assert(p->size() == c->size(), "verify size"); 2078 // Object may have been locked between copy and verification 2079 // assert(p->mark() == c->mark(), "verify mark"); 2080 assert(oopDesc::unsafe_equals(c, oopDesc::bs()->read_barrier(c)), "verify only forwarded once"); 2081 } 2082 2083 void ShenandoahHeap::oom_during_evacuation() { 2084 log_develop_trace(gc)("Out of memory during evacuation, cancel evacuation, schedule full GC by thread %d", 2085 Thread::current()->osthread()->thread_id()); 2086 2087 // We ran out of memory during evacuation. Cancel evacuation, and schedule a full-GC. 2088 collector_policy()->set_should_clear_all_soft_refs(true); 2089 concurrent_thread()->try_set_full_gc(); 2090 cancel_concgc(_oom_evacuation); 2091 2092 if ((! Thread::current()->is_GC_task_thread()) && (! Thread::current()->is_ConcurrentGC_thread())) { 2093 assert(! Threads_lock->owned_by_self() 2094 || SafepointSynchronize::is_at_safepoint(), "must not hold Threads_lock here"); 2095 log_warning(gc)("OOM during evacuation. Let Java thread wait until evacuation finishes."); 2096 while (_evacuation_in_progress) { // wait. 2097 Thread::current()->_ParkEvent->park(1); 2098 } 2099 } 2100 2101 } 2102 2103 HeapWord* ShenandoahHeap::tlab_post_allocation_setup(HeapWord* obj) { 2104 // Initialize Brooks pointer for the next object 2105 HeapWord* result = obj + BrooksPointer::word_size(); 2106 BrooksPointer::initialize(oop(result)); 2107 return result; 2108 } 2109 2110 uint ShenandoahHeap::oop_extra_words() { 2111 return BrooksPointer::word_size(); 2112 } 2113 2114 void ShenandoahHeap::grow_heap_by(size_t num_regions) { 2115 size_t base = _num_regions; 2116 ensure_new_regions(num_regions); 2117 for (size_t i = 0; i < num_regions; i++) { 2118 size_t new_region_index = i + base; 2119 HeapWord* start = _first_region_bottom + (ShenandoahHeapRegion::RegionSizeBytes / HeapWordSize) * new_region_index; 2120 ShenandoahHeapRegion* new_region = new ShenandoahHeapRegion(this, start, ShenandoahHeapRegion::RegionSizeBytes / HeapWordSize, new_region_index); 2121 2122 if (log_is_enabled(Trace, gc, region)) { 2123 ResourceMark rm; 2124 outputStream* out = Log(gc, region)::trace_stream(); 2125 out->print_cr("allocating new region at index: "SIZE_FORMAT, new_region_index); 2126 new_region->print_on(out); 2127 } 2128 2129 assert(_ordered_regions->active_regions() == new_region->region_number(), "must match"); 2130 _ordered_regions->add_region(new_region); 2131 _in_cset_fast_test_base[new_region_index] = false; // Not in cset 2132 _next_top_at_mark_starts_base[new_region_index] = new_region->bottom(); 2133 _complete_top_at_mark_starts_base[new_region_index] = new_region->bottom(); 2134 2135 _free_regions->add_region(new_region); 2136 } 2137 } 2138 2139 void ShenandoahHeap::ensure_new_regions(size_t new_regions) { 2140 2141 size_t num_regions = _num_regions; 2142 size_t new_num_regions = num_regions + new_regions; 2143 assert(new_num_regions <= _max_regions, "we checked this earlier"); 2144 2145 size_t expand_size = new_regions * ShenandoahHeapRegion::RegionSizeBytes; 2146 log_trace(gc, region)("expanding storage by "SIZE_FORMAT_HEX" bytes, for "SIZE_FORMAT" new regions", expand_size, new_regions); 2147 bool success = _storage.expand_by(expand_size, ShenandoahAlwaysPreTouch); 2148 assert(success, "should always be able to expand by requested size"); 2149 2150 _num_regions = new_num_regions; 2151 2152 } 2153 2154 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() : 2155 _heap(ShenandoahHeap::heap_no_check()) { 2156 } 2157 2158 void ShenandoahForwardedIsAliveClosure::init(ShenandoahHeap* heap) { 2159 _heap = heap; 2160 } 2161 2162 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) { 2163 2164 assert(_heap != NULL, "sanity"); 2165 obj = ShenandoahBarrierSet::resolve_oop_static_not_null(obj); 2166 #ifdef ASSERT 2167 if (_heap->concurrent_mark_in_progress()) { 2168 assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), "only query to-space"); 2169 } 2170 #endif 2171 assert(!oopDesc::is_null(obj), "null"); 2172 return _heap->is_marked_next(obj); 2173 } 2174 2175 void ShenandoahHeap::ref_processing_init() { 2176 MemRegion mr = reserved_region(); 2177 2178 isAlive.init(ShenandoahHeap::heap()); 2179 assert(_max_workers > 0, "Sanity"); 2180 2181 _ref_processor = 2182 new ReferenceProcessor(mr, // span 2183 ParallelRefProcEnabled, 2184 // mt processing 2185 _max_workers, 2186 // degree of mt processing 2187 true, 2188 // mt discovery 2189 _max_workers, 2190 // degree of mt discovery 2191 false, 2192 // Reference discovery is not atomic 2193 &isAlive); 2194 } 2195 2196 #ifdef ASSERT 2197 void ShenandoahHeap::set_from_region_protection(bool protect) { 2198 for (uint i = 0; i < _num_regions; i++) { 2199 ShenandoahHeapRegion* region = _ordered_regions->get(i); 2200 if (region != NULL && in_collection_set(region)) { 2201 if (protect) { 2202 region->memProtectionOn(); 2203 } else { 2204 region->memProtectionOff(); 2205 } 2206 } 2207 } 2208 } 2209 #endif 2210 2211 size_t ShenandoahHeap::num_regions() { 2212 return _num_regions; 2213 } 2214 2215 size_t ShenandoahHeap::max_regions() { 2216 return _max_regions; 2217 } 2218 2219 GCTracer* ShenandoahHeap::tracer() { 2220 return shenandoahPolicy()->tracer(); 2221 } 2222 2223 size_t ShenandoahHeap::tlab_used(Thread* thread) const { 2224 return _free_regions->used(); 2225 } 2226 2227 void ShenandoahHeap::cancel_concgc(GCCause::Cause cause) { 2228 if (try_cancel_concgc()) { 2229 log_info(gc)("Cancelling concurrent GC: %s", GCCause::to_string(cause)); 2230 _shenandoah_policy->report_concgc_cancelled(); 2231 } 2232 } 2233 2234 void ShenandoahHeap::cancel_concgc(ShenandoahCancelCause cause) { 2235 if (try_cancel_concgc()) { 2236 log_info(gc)("Cancelling concurrent GC: %s", cancel_cause_to_string(cause)); 2237 _shenandoah_policy->report_concgc_cancelled(); 2238 } 2239 } 2240 2241 const char* ShenandoahHeap::cancel_cause_to_string(ShenandoahCancelCause cause) { 2242 switch (cause) { 2243 case _oom_evacuation: 2244 return "Out of memory for evacuation"; 2245 case _vm_stop: 2246 return "Stopping VM"; 2247 default: 2248 return "Unknown"; 2249 } 2250 } 2251 2252 void ShenandoahHeap::clear_cancelled_concgc() { 2253 set_cancelled_concgc(false); 2254 } 2255 2256 uint ShenandoahHeap::max_workers() { 2257 return _max_workers; 2258 } 2259 2260 void ShenandoahHeap::stop() { 2261 // The shutdown sequence should be able to terminate when GC is running. 2262 2263 // Step 1. Notify control thread that we are in shutdown. 2264 // Note that we cannot do that with stop(), because stop() is blocking and waits for the actual shutdown. 2265 // Doing stop() here would wait for the normal GC cycle to complete, never falling through to cancel below. 2266 _concurrent_gc_thread->prepare_for_graceful_shutdown(); 2267 2268 // Step 2. Notify GC workers that we are cancelling GC. 2269 cancel_concgc(_vm_stop); 2270 2271 // Step 3. Wait until GC worker exits normally. 2272 _concurrent_gc_thread->stop(); 2273 } 2274 2275 void ShenandoahHeap::unlink_string_and_symbol_table(BoolObjectClosure* is_alive, bool process_strings, bool process_symbols) { 2276 2277 StringSymbolTableUnlinkTask shenandoah_unlink_task(is_alive, process_strings, process_symbols); 2278 workers()->run_task(&shenandoah_unlink_task); 2279 2280 // if (G1StringDedup::is_enabled()) { 2281 // G1StringDedup::unlink(is_alive); 2282 // } 2283 } 2284 2285 void ShenandoahHeap::set_need_update_refs(bool need_update_refs) { 2286 _need_update_refs = need_update_refs; 2287 } 2288 2289 //fixme this should be in heapregionset 2290 ShenandoahHeapRegion* ShenandoahHeap::next_compaction_region(const ShenandoahHeapRegion* r) { 2291 size_t region_idx = r->region_number() + 1; 2292 ShenandoahHeapRegion* next = _ordered_regions->get(region_idx); 2293 guarantee(next->region_number() == region_idx, "region number must match"); 2294 while (next->is_humongous()) { 2295 region_idx = next->region_number() + 1; 2296 next = _ordered_regions->get(region_idx); 2297 guarantee(next->region_number() == region_idx, "region number must match"); 2298 } 2299 return next; 2300 } 2301 2302 void ShenandoahHeap::set_region_in_collection_set(size_t region_index, bool b) { 2303 _in_cset_fast_test_base[region_index] = b; 2304 } 2305 2306 ShenandoahMonitoringSupport* ShenandoahHeap::monitoring_support() { 2307 return _monitoring_support; 2308 } 2309 2310 CMBitMap* ShenandoahHeap::complete_mark_bit_map() { 2311 return _complete_mark_bit_map; 2312 } 2313 2314 CMBitMap* ShenandoahHeap::next_mark_bit_map() { 2315 return _next_mark_bit_map; 2316 } 2317 2318 void ShenandoahHeap::add_free_region(ShenandoahHeapRegion* r) { 2319 _free_regions->add_region(r); 2320 } 2321 2322 void ShenandoahHeap::clear_free_regions() { 2323 _free_regions->clear(); 2324 } 2325 2326 address ShenandoahHeap::in_cset_fast_test_addr() { 2327 return (address) (ShenandoahHeap::heap()->_in_cset_fast_test); 2328 } 2329 2330 address ShenandoahHeap::cancelled_concgc_addr() { 2331 return (address) &(ShenandoahHeap::heap()->_cancelled_concgc); 2332 } 2333 2334 void ShenandoahHeap::clear_cset_fast_test() { 2335 assert(_in_cset_fast_test_base != NULL, "sanity"); 2336 memset(_in_cset_fast_test_base, false, 2337 _in_cset_fast_test_length * sizeof(bool)); 2338 } 2339 2340 size_t ShenandoahHeap::conservative_max_heap_alignment() { 2341 return ShenandoahMaxRegionSize; 2342 } 2343 2344 size_t ShenandoahHeap::bytes_allocated_since_cm() { 2345 return _bytes_allocated_since_cm; 2346 } 2347 2348 void ShenandoahHeap::set_bytes_allocated_since_cm(size_t bytes) { 2349 _bytes_allocated_since_cm = bytes; 2350 } 2351 2352 size_t ShenandoahHeap::max_allocated_gc() { 2353 return _max_allocated_gc; 2354 } 2355 2356 void ShenandoahHeap::set_next_top_at_mark_start(HeapWord* region_base, HeapWord* addr) { 2357 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2358 _next_top_at_mark_starts[index] = addr; 2359 } 2360 2361 HeapWord* ShenandoahHeap::next_top_at_mark_start(HeapWord* region_base) { 2362 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2363 return _next_top_at_mark_starts[index]; 2364 } 2365 2366 void ShenandoahHeap::set_complete_top_at_mark_start(HeapWord* region_base, HeapWord* addr) { 2367 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2368 _complete_top_at_mark_starts[index] = addr; 2369 } 2370 2371 HeapWord* ShenandoahHeap::complete_top_at_mark_start(HeapWord* region_base) { 2372 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2373 return _complete_top_at_mark_starts[index]; 2374 } 2375 2376 void ShenandoahHeap::set_full_gc_in_progress(bool in_progress) { 2377 _full_gc_in_progress = in_progress; 2378 } 2379 2380 bool ShenandoahHeap::is_full_gc_in_progress() const { 2381 return _full_gc_in_progress; 2382 } 2383 2384 class NMethodOopInitializer : public OopClosure { 2385 private: 2386 ShenandoahHeap* _heap; 2387 public: 2388 NMethodOopInitializer() : _heap(ShenandoahHeap::heap()) { 2389 } 2390 2391 private: 2392 template <class T> 2393 inline void do_oop_work(T* p) { 2394 T o = oopDesc::load_heap_oop(p); 2395 if (! oopDesc::is_null(o)) { 2396 oop obj1 = oopDesc::decode_heap_oop_not_null(o); 2397 oop obj2 = oopDesc::bs()->write_barrier(obj1); 2398 if (! oopDesc::unsafe_equals(obj1, obj2)) { 2399 oopDesc::encode_store_heap_oop(p, obj2); 2400 } 2401 } 2402 } 2403 2404 public: 2405 void do_oop(oop* o) { 2406 do_oop_work(o); 2407 } 2408 void do_oop(narrowOop* o) { 2409 do_oop_work(o); 2410 } 2411 }; 2412 2413 void ShenandoahHeap::register_nmethod(nmethod* nm) { 2414 NMethodOopInitializer init; 2415 nm->oops_do(&init); 2416 nm->fix_oop_relocations(); 2417 } 2418 2419 void ShenandoahHeap::unregister_nmethod(nmethod* nm) { 2420 } 2421 2422 void ShenandoahHeap::pin_object(oop o) { 2423 heap_region_containing(o)->pin(); 2424 } 2425 2426 void ShenandoahHeap::unpin_object(oop o) { 2427 heap_region_containing(o)->unpin(); 2428 } 2429 2430 2431 GCTimer* ShenandoahHeap::gc_timer() const { 2432 return _gc_timer; 2433 } 2434 2435 class RecordAllRefsOopClosure: public ExtendedOopClosure { 2436 private: 2437 int _x; 2438 int *_matrix; 2439 int _num_regions; 2440 oop _p; 2441 2442 public: 2443 RecordAllRefsOopClosure(int *matrix, int x, size_t num_regions, oop p) : 2444 _matrix(matrix), _x(x), _num_regions(num_regions), _p(p) {} 2445 2446 template <class T> 2447 void do_oop_work(T* p) { 2448 oop o = oopDesc::load_decode_heap_oop(p); 2449 if (o != NULL) { 2450 if (ShenandoahHeap::heap()->is_in(o) && o->is_oop() ) { 2451 int y = ShenandoahHeap::heap()->heap_region_containing(o)->region_number(); 2452 _matrix[_x * _num_regions + y]++; 2453 } 2454 } 2455 } 2456 void do_oop(oop* p) { 2457 do_oop_work(p); 2458 } 2459 2460 void do_oop(narrowOop* p) { 2461 do_oop_work(p); 2462 } 2463 2464 }; 2465 2466 class RecordAllRefsObjectClosure : public ObjectClosure { 2467 int *_matrix; 2468 size_t _num_regions; 2469 2470 public: 2471 RecordAllRefsObjectClosure(int *matrix, size_t num_regions) : 2472 _matrix(matrix), _num_regions(num_regions) {} 2473 2474 void do_object(oop p) { 2475 if (ShenandoahHeap::heap()->is_in(p) && ShenandoahHeap::heap()->is_marked_next(p) && p->is_oop()) { 2476 int x = ShenandoahHeap::heap()->heap_region_containing(p)->region_number(); 2477 RecordAllRefsOopClosure cl(_matrix, x, _num_regions, p); 2478 p->oop_iterate(&cl); 2479 } 2480 } 2481 }; 2482 void ShenandoahHeap::calculate_matrix(int* connections) { 2483 log_develop_trace(gc)("calculating matrix"); 2484 ensure_parsability(false); 2485 int num = num_regions(); 2486 2487 for (int i = 0; i < num; i++) { 2488 for (int j = 0; j < num; j++) { 2489 connections[i * num + j] = 0; 2490 } 2491 } 2492 2493 RecordAllRefsOopClosure cl(connections, 0, num, NULL); 2494 roots_iterate(&cl); 2495 2496 RecordAllRefsObjectClosure cl2(connections, num); 2497 object_iterate(&cl2); 2498 2499 } 2500 2501 void ShenandoahHeap::print_matrix(int* connections) { 2502 int num = num_regions(); 2503 int cs_regions = 0; 2504 int referenced = 0; 2505 2506 for (int i = 0; i < num; i++) { 2507 size_t liveData = ShenandoahHeap::heap()->regions()->get(i)->get_live_data_bytes(); 2508 2509 int numReferencedRegions = 0; 2510 int numReferencedByRegions = 0; 2511 2512 for (int j = 0; j < num; j++) { 2513 if (connections[i * num + j] > 0) 2514 numReferencedRegions++; 2515 2516 if (connections [j * num + i] > 0) 2517 numReferencedByRegions++; 2518 2519 cs_regions++; 2520 referenced += numReferencedByRegions; 2521 } 2522 2523 if (ShenandoahHeap::heap()->regions()->get(i)->has_live()) { 2524 tty->print("Region %d is referenced by %d regions {", 2525 i, numReferencedByRegions); 2526 int col_count = 0; 2527 for (int j = 0; j < num; j++) { 2528 int foo = connections[j * num + i]; 2529 if (foo > 0) { 2530 col_count++; 2531 if ((col_count % 10) == 0) 2532 tty->print("\n"); 2533 tty->print("%d(%d), ", j,foo); 2534 } 2535 } 2536 tty->print("} \n"); 2537 } 2538 } 2539 2540 double avg = (double)referenced / (double) cs_regions; 2541 tty->print("Average Number of regions scanned / region = %lf\n", avg); 2542 } 2543 2544 class ShenandoahCountGarbageClosure : public ShenandoahHeapRegionClosure { 2545 private: 2546 size_t _garbage; 2547 public: 2548 ShenandoahCountGarbageClosure() : _garbage(0) { 2549 } 2550 2551 bool doHeapRegion(ShenandoahHeapRegion* r) { 2552 if (! r->is_humongous() && ! r->is_pinned() && ! r->in_collection_set()) { 2553 _garbage += r->garbage(); 2554 } 2555 return false; 2556 } 2557 2558 size_t garbage() { 2559 return _garbage; 2560 } 2561 }; 2562 2563 size_t ShenandoahHeap::garbage() { 2564 ShenandoahCountGarbageClosure cl; 2565 heap_region_iterate(&cl); 2566 return cl.garbage(); 2567 } 2568 2569 ShenandoahConnectionMatrix* ShenandoahHeap::connection_matrix() { 2570 return _connection_matrix; 2571 } 2572 2573 ShenandoahPartialGC* ShenandoahHeap::partial_gc() { 2574 return _partial_gc; 2575 } 2576 2577 void ShenandoahHeap::do_partial_collection() { 2578 { 2579 ShenandoahHeapLock lock(this); 2580 partial_gc()->prepare(); 2581 } 2582 partial_gc()->do_partial_collection(); 2583 } 2584 2585 #ifdef ASSERT 2586 void ShenandoahHeap::assert_heaplock_owned_by_current_thread() { 2587 assert(_heap_lock == locked, "must be locked"); 2588 assert(_heap_lock_owner == Thread::current(), "must be owned by current thread"); 2589 } 2590 2591 void ShenandoahHeap::assert_heaplock_or_safepoint() { 2592 Thread* thr = Thread::current(); 2593 assert((_heap_lock == locked && _heap_lock_owner == thr) || 2594 (SafepointSynchronize::is_at_safepoint() && thr->is_VM_thread()), 2595 "must own heap lock or by VM thread at safepoint"); 2596 } 2597 2598 #endif