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 size_t bitmap_size = CMBitMap::compute_size(heap_rs.size()); 234 MemRegion 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 _heap->evacuate_object(p, _thread); 800 } 801 } 802 }; 803 804 #ifdef ASSERT 805 class VerifyEvacuatedObjectClosure : public ObjectClosure { 806 807 public: 808 809 void do_object(oop p) { 810 if (ShenandoahHeap::heap()->is_marked_complete(p)) { 811 oop p_prime = oopDesc::bs()->read_barrier(p); 812 assert(! oopDesc::unsafe_equals(p, p_prime), "Should point to evacuated copy"); 813 if (p->klass() != p_prime->klass()) { 814 tty->print_cr("copy has different class than original:"); 815 p->klass()->print_on(tty); 816 p_prime->klass()->print_on(tty); 817 } 818 assert(p->klass() == p_prime->klass(), "Should have the same class p: "PTR_FORMAT", p_prime: "PTR_FORMAT, p2i(p), p2i(p_prime)); 819 // assert(p->mark() == p_prime->mark(), "Should have the same mark"); 820 assert(p->size() == p_prime->size(), "Should be the same size"); 821 assert(oopDesc::unsafe_equals(p_prime, oopDesc::bs()->read_barrier(p_prime)), "One forward once"); 822 } 823 } 824 }; 825 826 void ShenandoahHeap::verify_evacuated_region(ShenandoahHeapRegion* from_region) { 827 VerifyEvacuatedObjectClosure verify_evacuation; 828 marked_object_iterate(from_region, &verify_evacuation); 829 } 830 #endif 831 832 void ShenandoahHeap::parallel_evacuate_region(ShenandoahHeapRegion* from_region) { 833 834 assert(from_region->has_live(), "all-garbage regions are reclaimed earlier"); 835 836 ParallelEvacuateRegionObjectClosure evacuate_region(this); 837 838 marked_object_iterate(from_region, &evacuate_region); 839 840 #ifdef ASSERT 841 if (ShenandoahVerify && ! cancelled_concgc()) { 842 verify_evacuated_region(from_region); 843 } 844 #endif 845 } 846 847 class ParallelEvacuationTask : public AbstractGangTask { 848 private: 849 ShenandoahHeap* _sh; 850 ShenandoahCollectionSet* _cs; 851 852 public: 853 ParallelEvacuationTask(ShenandoahHeap* sh, 854 ShenandoahCollectionSet* cs) : 855 AbstractGangTask("Parallel Evacuation Task"), 856 _cs(cs), 857 _sh(sh) {} 858 859 void work(uint worker_id) { 860 861 ShenandoahHeapRegion* from_hr = _cs->claim_next(); 862 863 while (from_hr != NULL) { 864 log_develop_trace(gc, region)("Thread "INT32_FORMAT" claimed Heap Region "SIZE_FORMAT, 865 worker_id, 866 from_hr->region_number()); 867 868 assert(from_hr->has_live(), "all-garbage regions are reclaimed early"); 869 _sh->parallel_evacuate_region(from_hr); 870 871 if (_sh->cancelled_concgc()) { 872 log_develop_trace(gc, region)("Cancelled concgc while evacuating region " SIZE_FORMAT "\n", from_hr->region_number()); 873 break; 874 } 875 from_hr = _cs->claim_next(); 876 } 877 } 878 }; 879 880 class RecycleDirtyRegionsClosure: public ShenandoahHeapRegionClosure { 881 private: 882 ShenandoahHeap* _heap; 883 size_t _bytes_reclaimed; 884 public: 885 RecycleDirtyRegionsClosure() : _heap(ShenandoahHeap::heap()) {} 886 887 bool doHeapRegion(ShenandoahHeapRegion* r) { 888 889 assert (! _heap->cancelled_concgc(), "no recycling after cancelled marking"); 890 891 if (_heap->in_collection_set(r)) { 892 log_develop_trace(gc, region)("Recycling region " SIZE_FORMAT ":", r->region_number()); 893 _heap->decrease_used(r->used()); 894 _bytes_reclaimed += r->used(); 895 r->recycle(); 896 } 897 898 return false; 899 } 900 size_t bytes_reclaimed() { return _bytes_reclaimed;} 901 void clear_bytes_reclaimed() {_bytes_reclaimed = 0;} 902 }; 903 904 void ShenandoahHeap::recycle_dirty_regions() { 905 RecycleDirtyRegionsClosure cl; 906 cl.clear_bytes_reclaimed(); 907 908 heap_region_iterate(&cl); 909 910 _shenandoah_policy->record_bytes_reclaimed(cl.bytes_reclaimed()); 911 if (! cancelled_concgc()) { 912 clear_cset_fast_test(); 913 } 914 } 915 916 ShenandoahFreeSet* ShenandoahHeap::free_regions() { 917 return _free_regions; 918 } 919 920 void ShenandoahHeap::print_heap_regions(outputStream* st) const { 921 _ordered_regions->print(st); 922 } 923 924 class PrintAllRefsOopClosure: public ExtendedOopClosure { 925 private: 926 int _index; 927 const char* _prefix; 928 929 public: 930 PrintAllRefsOopClosure(const char* prefix) : _index(0), _prefix(prefix) {} 931 932 private: 933 template <class T> 934 inline void do_oop_work(T* p) { 935 oop o = oopDesc::load_decode_heap_oop(p); 936 if (o != NULL) { 937 if (ShenandoahHeap::heap()->is_in(o) && o->is_oop()) { 938 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT")-> "PTR_FORMAT" (marked: %s) (%s "PTR_FORMAT")", 939 _prefix, _index, 940 p2i(p), p2i(o), 941 BOOL_TO_STR(ShenandoahHeap::heap()->is_marked_complete(o)), 942 o->klass()->internal_name(), p2i(o->klass())); 943 } else { 944 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT" dirty -> "PTR_FORMAT" (not in heap, possibly corrupted or dirty)", 945 _prefix, _index, 946 p2i(p), p2i(o)); 947 } 948 } else { 949 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT") -> "PTR_FORMAT, _prefix, _index, p2i(p), p2i((HeapWord*) o)); 950 } 951 _index++; 952 } 953 954 public: 955 void do_oop(oop* p) { 956 do_oop_work(p); 957 } 958 959 void do_oop(narrowOop* p) { 960 do_oop_work(p); 961 } 962 963 }; 964 965 class PrintAllRefsObjectClosure : public ObjectClosure { 966 const char* _prefix; 967 968 public: 969 PrintAllRefsObjectClosure(const char* prefix) : _prefix(prefix) {} 970 971 void do_object(oop p) { 972 if (ShenandoahHeap::heap()->is_in(p)) { 973 tty->print_cr("%s object "PTR_FORMAT" (marked: %s) (%s "PTR_FORMAT") refers to:", 974 _prefix, p2i(p), 975 BOOL_TO_STR(ShenandoahHeap::heap()->is_marked_complete(p)), 976 p->klass()->internal_name(), p2i(p->klass())); 977 PrintAllRefsOopClosure cl(_prefix); 978 p->oop_iterate(&cl); 979 } 980 } 981 }; 982 983 void ShenandoahHeap::print_all_refs(const char* prefix) { 984 tty->print_cr("printing all references in the heap"); 985 tty->print_cr("root references:"); 986 987 ensure_parsability(false); 988 989 PrintAllRefsOopClosure cl(prefix); 990 roots_iterate(&cl); 991 992 tty->print_cr("heap references:"); 993 PrintAllRefsObjectClosure cl2(prefix); 994 object_iterate(&cl2); 995 } 996 997 class VerifyAfterMarkingOopClosure: public ExtendedOopClosure { 998 private: 999 ShenandoahHeap* _heap; 1000 1001 public: 1002 VerifyAfterMarkingOopClosure() : 1003 _heap(ShenandoahHeap::heap()) { } 1004 1005 private: 1006 template <class T> 1007 inline void do_oop_work(T* p) { 1008 oop o = oopDesc::load_decode_heap_oop(p); 1009 if (o != NULL) { 1010 if (! _heap->is_marked_complete(o)) { 1011 _heap->print_heap_regions(); 1012 _heap->print_all_refs("post-mark"); 1013 tty->print_cr("oop not marked, although referrer is marked: "PTR_FORMAT": in_heap: %s, is_marked: %s", 1014 p2i((HeapWord*) o), BOOL_TO_STR(_heap->is_in(o)), BOOL_TO_STR(_heap->is_marked_complete(o))); 1015 _heap->print_heap_locations((HeapWord*) o, (HeapWord*) o + o->size()); 1016 1017 tty->print_cr("oop class: %s", o->klass()->internal_name()); 1018 if (_heap->is_in(p)) { 1019 oop referrer = oop(_heap->heap_region_containing(p)->block_start_const(p)); 1020 tty->print_cr("Referrer starts at addr "PTR_FORMAT, p2i((HeapWord*) referrer)); 1021 referrer->print(); 1022 _heap->print_heap_locations((HeapWord*) referrer, (HeapWord*) referrer + referrer->size()); 1023 } 1024 tty->print_cr("heap region containing object:"); 1025 _heap->heap_region_containing(o)->print(); 1026 tty->print_cr("heap region containing referrer:"); 1027 _heap->heap_region_containing(p)->print(); 1028 tty->print_cr("heap region containing forwardee:"); 1029 _heap->heap_region_containing(oopDesc::bs()->read_barrier(o))->print(); 1030 } 1031 assert(o->is_oop(), "oop must be an oop"); 1032 assert(Metaspace::contains(o->klass()), "klass pointer must go to metaspace"); 1033 if (! oopDesc::unsafe_equals(o, oopDesc::bs()->read_barrier(o))) { 1034 tty->print_cr("oops has forwardee: p: "PTR_FORMAT" (%s), o = "PTR_FORMAT" (%s), new-o: "PTR_FORMAT" (%s)", 1035 p2i(p), 1036 BOOL_TO_STR(_heap->in_collection_set(p)), 1037 p2i(o), 1038 BOOL_TO_STR(_heap->in_collection_set(o)), 1039 p2i((HeapWord*) oopDesc::bs()->read_barrier(o)), 1040 BOOL_TO_STR(_heap->in_collection_set(oopDesc::bs()->read_barrier(o)))); 1041 tty->print_cr("oop class: %s", o->klass()->internal_name()); 1042 } 1043 assert(oopDesc::unsafe_equals(o, oopDesc::bs()->read_barrier(o)), "oops must not be forwarded"); 1044 assert(! _heap->in_collection_set(o), "references must not point to dirty heap regions"); 1045 assert(_heap->is_marked_complete(o), "live oops must be marked current"); 1046 } 1047 } 1048 1049 public: 1050 void do_oop(oop* p) { 1051 do_oop_work(p); 1052 } 1053 1054 void do_oop(narrowOop* p) { 1055 do_oop_work(p); 1056 } 1057 1058 }; 1059 1060 void ShenandoahHeap::verify_heap_after_marking() { 1061 1062 verify_heap_size_consistency(); 1063 1064 log_trace(gc)("verifying heap after marking"); 1065 1066 VerifyAfterMarkingOopClosure cl; 1067 roots_iterate(&cl); 1068 ObjectToOopClosure objs(&cl); 1069 object_iterate(&objs); 1070 } 1071 1072 1073 void ShenandoahHeap::reclaim_humongous_region_at(ShenandoahHeapRegion* r) { 1074 assert(r->is_humongous_start(), "reclaim regions starting with the first one"); 1075 1076 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 1077 size_t size = humongous_obj->size() + BrooksPointer::word_size(); 1078 uint required_regions = ShenandoahHumongous::required_regions(size * HeapWordSize); 1079 uint index = r->region_number(); 1080 1081 1082 assert(!r->has_live(), "liveness must be zero"); 1083 1084 for(size_t i = 0; i < required_regions; i++) { 1085 1086 ShenandoahHeapRegion* region = _ordered_regions->get(index++); 1087 1088 assert((region->is_humongous_start() || region->is_humongous_continuation()), 1089 "expect correct humongous start or continuation"); 1090 1091 if (log_is_enabled(Debug, gc, humongous)) { 1092 log_debug(gc, humongous)("reclaiming "UINT32_FORMAT" humongous regions for object of size: "SIZE_FORMAT" words", required_regions, size); 1093 ResourceMark rm; 1094 outputStream* out = Log(gc, humongous)::debug_stream(); 1095 region->print_on(out); 1096 } 1097 1098 region->recycle(); 1099 ShenandoahHeap::heap()->decrease_used(ShenandoahHeapRegion::RegionSizeBytes); 1100 } 1101 } 1102 1103 class ShenandoahReclaimHumongousRegionsClosure : public ShenandoahHeapRegionClosure { 1104 1105 bool doHeapRegion(ShenandoahHeapRegion* r) { 1106 ShenandoahHeap* heap = ShenandoahHeap::heap(); 1107 1108 if (r->is_humongous_start()) { 1109 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 1110 if (! heap->is_marked_complete(humongous_obj)) { 1111 1112 heap->reclaim_humongous_region_at(r); 1113 } 1114 } 1115 return false; 1116 } 1117 }; 1118 1119 #ifdef ASSERT 1120 class CheckCollectionSetClosure: public ShenandoahHeapRegionClosure { 1121 bool doHeapRegion(ShenandoahHeapRegion* r) { 1122 assert(! ShenandoahHeap::heap()->in_collection_set(r), "Should have been cleared by now"); 1123 return false; 1124 } 1125 }; 1126 #endif 1127 1128 void ShenandoahHeap::prepare_for_concurrent_evacuation() { 1129 assert(_ordered_regions->get(0)->region_number() == 0, "FIXME CHF. FIXME CHF!"); 1130 1131 log_develop_trace(gc)("Thread %d started prepare_for_concurrent_evacuation", Thread::current()->osthread()->thread_id()); 1132 1133 if (!cancelled_concgc()) { 1134 1135 recycle_dirty_regions(); 1136 1137 ensure_parsability(true); 1138 1139 if (UseShenandoahMatrix) { 1140 if (PrintShenandoahMatrix) { 1141 outputStream* log = Log(gc)::info_stream(); 1142 connection_matrix()->print_on(log); 1143 } 1144 if (VerifyShenandoahMatrix) { 1145 verify_matrix(); 1146 } 1147 } 1148 1149 #ifdef ASSERT 1150 if (ShenandoahVerify) { 1151 verify_heap_after_marking(); 1152 } 1153 #endif 1154 1155 // NOTE: This needs to be done during a stop the world pause, because 1156 // putting regions into the collection set concurrently with Java threads 1157 // will create a race. In particular, acmp could fail because when we 1158 // resolve the first operand, the containing region might not yet be in 1159 // the collection set, and thus return the original oop. When the 2nd 1160 // operand gets resolved, the region could be in the collection set 1161 // and the oop gets evacuated. If both operands have originally been 1162 // the same, we get false negatives. 1163 1164 { 1165 ShenandoahHeapLock lock(this); 1166 _collection_set->clear(); 1167 _free_regions->clear(); 1168 1169 ShenandoahReclaimHumongousRegionsClosure reclaim; 1170 heap_region_iterate(&reclaim); 1171 1172 #ifdef ASSERT 1173 CheckCollectionSetClosure ccsc; 1174 _ordered_regions->heap_region_iterate(&ccsc); 1175 #endif 1176 1177 _shenandoah_policy->choose_collection_set(_collection_set); 1178 1179 _shenandoah_policy->choose_free_set(_free_regions); 1180 } 1181 1182 _bytes_allocated_since_cm = 0; 1183 1184 Universe::update_heap_info_at_gc(); 1185 } 1186 } 1187 1188 1189 class RetireTLABClosure : public ThreadClosure { 1190 private: 1191 bool _retire; 1192 1193 public: 1194 RetireTLABClosure(bool retire) : _retire(retire) { 1195 } 1196 1197 void do_thread(Thread* thread) { 1198 thread->gclab().make_parsable(_retire); 1199 } 1200 }; 1201 1202 void ShenandoahHeap::ensure_parsability(bool retire_tlabs) { 1203 if (UseTLAB) { 1204 CollectedHeap::ensure_parsability(retire_tlabs); 1205 RetireTLABClosure cl(retire_tlabs); 1206 Threads::threads_do(&cl); 1207 } 1208 } 1209 1210 class ShenandoahEvacuateUpdateRootsClosure: public ExtendedOopClosure { 1211 private: 1212 ShenandoahHeap* _heap; 1213 Thread* _thread; 1214 public: 1215 ShenandoahEvacuateUpdateRootsClosure() : 1216 _heap(ShenandoahHeap::heap()), _thread(Thread::current()) { 1217 } 1218 1219 private: 1220 template <class T> 1221 void do_oop_work(T* p) { 1222 assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress"); 1223 1224 T o = oopDesc::load_heap_oop(p); 1225 if (! oopDesc::is_null(o)) { 1226 oop obj = oopDesc::decode_heap_oop_not_null(o); 1227 if (_heap->in_collection_set(obj)) { 1228 assert(_heap->is_marked_complete(obj), "only evacuate marked objects %d %d", 1229 _heap->is_marked_complete(obj), _heap->is_marked_complete(ShenandoahBarrierSet::resolve_oop_static_not_null(obj))); 1230 oop resolved = ShenandoahBarrierSet::resolve_oop_static_not_null(obj); 1231 if (oopDesc::unsafe_equals(resolved, obj)) { 1232 resolved = _heap->evacuate_object(obj, _thread); 1233 } 1234 oopDesc::encode_store_heap_oop(p, resolved); 1235 } 1236 } 1237 #ifdef ASSERT 1238 else { 1239 // tty->print_cr("not updating root at: "PTR_FORMAT" with object: "PTR_FORMAT", is_in_heap: %s, is_in_cset: %s, is_marked: %s", 1240 // p2i(p), 1241 // p2i((HeapWord*) obj), 1242 // BOOL_TO_STR(_heap->is_in(obj)), 1243 // BOOL_TO_STR(_heap->in_cset_fast_test(obj)), 1244 // BOOL_TO_STR(_heap->is_marked_complete(obj))); 1245 } 1246 #endif 1247 } 1248 1249 public: 1250 void do_oop(oop* p) { 1251 do_oop_work(p); 1252 } 1253 void do_oop(narrowOop* p) { 1254 do_oop_work(p); 1255 } 1256 }; 1257 1258 class ShenandoahEvacuateUpdateRootsTask : public AbstractGangTask { 1259 ShenandoahRootEvacuator* _rp; 1260 public: 1261 1262 ShenandoahEvacuateUpdateRootsTask(ShenandoahRootEvacuator* rp) : 1263 AbstractGangTask("Shenandoah evacuate and update roots"), 1264 _rp(rp) 1265 { 1266 // Nothing else to do. 1267 } 1268 1269 void work(uint worker_id) { 1270 ShenandoahEvacuateUpdateRootsClosure cl; 1271 MarkingCodeBlobClosure blobsCl(&cl, CodeBlobToOopClosure::FixRelocations); 1272 1273 _rp->process_evacuate_roots(&cl, &blobsCl, worker_id); 1274 } 1275 }; 1276 1277 class ShenandoahFixRootsTask : public AbstractGangTask { 1278 ShenandoahRootEvacuator* _rp; 1279 public: 1280 1281 ShenandoahFixRootsTask(ShenandoahRootEvacuator* rp) : 1282 AbstractGangTask("Shenandoah update roots"), 1283 _rp(rp) 1284 { 1285 // Nothing else to do. 1286 } 1287 1288 void work(uint worker_id) { 1289 SCMUpdateRefsClosure cl; 1290 MarkingCodeBlobClosure blobsCl(&cl, CodeBlobToOopClosure::FixRelocations); 1291 1292 _rp->process_evacuate_roots(&cl, &blobsCl, worker_id); 1293 } 1294 }; 1295 void ShenandoahHeap::evacuate_and_update_roots() { 1296 1297 COMPILER2_PRESENT(DerivedPointerTable::clear()); 1298 1299 #ifdef ASSERT 1300 if (ShenandoahVerifyReadsToFromSpace) { 1301 set_from_region_protection(false); 1302 } 1303 #endif 1304 1305 assert(SafepointSynchronize::is_at_safepoint(), "Only iterate roots while world is stopped"); 1306 ClassLoaderDataGraph::clear_claimed_marks(); 1307 1308 { 1309 ShenandoahRootEvacuator rp(this, workers()->active_workers(), ShenandoahCollectorPolicy::evac_thread_roots); 1310 ShenandoahEvacuateUpdateRootsTask roots_task(&rp); 1311 workers()->run_task(&roots_task); 1312 } 1313 1314 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 1315 1316 if (cancelled_concgc()) { 1317 // If initial evacuation has been cancelled, we need to update all references 1318 // after all workers have finished. Otherwise we might run into the following problem: 1319 // GC thread 1 cannot allocate anymore, thus evacuation fails, leaves from-space ptr of object X. 1320 // GC thread 2 evacuates the same object X to to-space 1321 // which leaves a truly dangling from-space reference in the first root oop*. This must not happen. 1322 // clear() and update_pointers() must always be called in pairs, 1323 // cannot nest with above clear()/update_pointers(). 1324 COMPILER2_PRESENT(DerivedPointerTable::clear()); 1325 ShenandoahRootEvacuator rp(this, workers()->active_workers(), ShenandoahCollectorPolicy::evac_thread_roots); 1326 ShenandoahFixRootsTask update_roots_task(&rp); 1327 workers()->run_task(&update_roots_task); 1328 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 1329 } 1330 1331 #ifdef ASSERT 1332 if (ShenandoahVerifyReadsToFromSpace) { 1333 set_from_region_protection(true); 1334 } 1335 #endif 1336 1337 #ifdef ASSERT 1338 { 1339 AssertToSpaceClosure cl; 1340 CodeBlobToOopClosure code_cl(&cl, !CodeBlobToOopClosure::FixRelocations); 1341 ShenandoahRootEvacuator rp(this, 1); 1342 rp.process_evacuate_roots(&cl, &code_cl, 0); 1343 } 1344 #endif 1345 } 1346 1347 1348 void ShenandoahHeap::do_evacuation() { 1349 1350 parallel_evacuate(); 1351 1352 if (ShenandoahVerify && ! cancelled_concgc()) { 1353 VM_ShenandoahVerifyHeapAfterEvacuation verify_after_evacuation; 1354 if (Thread::current()->is_VM_thread()) { 1355 verify_after_evacuation.doit(); 1356 } else { 1357 VMThread::execute(&verify_after_evacuation); 1358 } 1359 } 1360 1361 } 1362 1363 void ShenandoahHeap::parallel_evacuate() { 1364 log_develop_trace(gc)("starting parallel_evacuate"); 1365 1366 _shenandoah_policy->record_phase_start(ShenandoahCollectorPolicy::conc_evac); 1367 1368 if (log_is_enabled(Trace, gc, region)) { 1369 ResourceMark rm; 1370 outputStream *out = Log(gc, region)::trace_stream(); 1371 out->print("Printing all available regions"); 1372 print_heap_regions(out); 1373 } 1374 1375 if (log_is_enabled(Trace, gc, cset)) { 1376 ResourceMark rm; 1377 outputStream *out = Log(gc, cset)::trace_stream(); 1378 out->print("Printing collection set which contains "SIZE_FORMAT" regions:\n", _collection_set->count()); 1379 _collection_set->print(out); 1380 1381 out->print("Printing free set which contains "SIZE_FORMAT" regions:\n", _free_regions->count()); 1382 _free_regions->print(out); 1383 } 1384 1385 ParallelEvacuationTask evacuationTask = ParallelEvacuationTask(this, _collection_set); 1386 1387 1388 workers()->run_task(&evacuationTask); 1389 1390 if (log_is_enabled(Trace, gc, cset)) { 1391 ResourceMark rm; 1392 outputStream *out = Log(gc, cset)::trace_stream(); 1393 out->print("Printing postgc collection set which contains "SIZE_FORMAT" regions:\n", 1394 _collection_set->count()); 1395 1396 _collection_set->print(out); 1397 1398 out->print("Printing postgc free regions which contain "SIZE_FORMAT" free regions:\n", 1399 _free_regions->count()); 1400 _free_regions->print(out); 1401 1402 } 1403 1404 if (log_is_enabled(Trace, gc, region)) { 1405 ResourceMark rm; 1406 outputStream *out = Log(gc, region)::trace_stream(); 1407 out->print_cr("all regions after evacuation:"); 1408 print_heap_regions(out); 1409 } 1410 1411 _shenandoah_policy->record_phase_end(ShenandoahCollectorPolicy::conc_evac); 1412 } 1413 1414 class VerifyEvacuationClosure: public ExtendedOopClosure { 1415 private: 1416 ShenandoahHeap* _heap; 1417 ShenandoahHeapRegion* _from_region; 1418 1419 public: 1420 VerifyEvacuationClosure(ShenandoahHeapRegion* from_region) : 1421 _heap(ShenandoahHeap::heap()), _from_region(from_region) { } 1422 private: 1423 template <class T> 1424 inline void do_oop_work(T* p) { 1425 oop heap_oop = oopDesc::load_decode_heap_oop(p); 1426 if (! oopDesc::is_null(heap_oop)) { 1427 guarantee(! _from_region->is_in(heap_oop), "no references to from-region allowed after evacuation: "PTR_FORMAT, p2i((HeapWord*) heap_oop)); 1428 } 1429 } 1430 1431 public: 1432 void do_oop(oop* p) { 1433 do_oop_work(p); 1434 } 1435 1436 void do_oop(narrowOop* p) { 1437 do_oop_work(p); 1438 } 1439 1440 }; 1441 1442 void ShenandoahHeap::roots_iterate(OopClosure* cl) { 1443 1444 assert(SafepointSynchronize::is_at_safepoint(), "Only iterate roots while world is stopped"); 1445 1446 CodeBlobToOopClosure blobsCl(cl, false); 1447 CLDToOopClosure cldCl(cl); 1448 1449 ClassLoaderDataGraph::clear_claimed_marks(); 1450 1451 ShenandoahRootProcessor rp(this, 1); 1452 rp.process_all_roots(cl, NULL, &cldCl, &blobsCl, 0); 1453 } 1454 1455 void ShenandoahHeap::verify_evacuation(ShenandoahHeapRegion* from_region) { 1456 1457 VerifyEvacuationClosure rootsCl(from_region); 1458 roots_iterate(&rootsCl); 1459 1460 } 1461 1462 bool ShenandoahHeap::supports_tlab_allocation() const { 1463 return true; 1464 } 1465 1466 1467 size_t ShenandoahHeap::unsafe_max_tlab_alloc(Thread *thread) const { 1468 size_t idx = _free_regions->current_index(); 1469 ShenandoahHeapRegion* current = _free_regions->get(idx); 1470 if (current == NULL) { 1471 return 0; 1472 } else if (current->free() > MinTLABSize) { 1473 // Current region has enough space left, can use it. 1474 return current->free(); 1475 } else { 1476 // No more space in current region, we will take next free region 1477 // on the next TLAB allocation. 1478 return ShenandoahHeapRegion::RegionSizeBytes; 1479 } 1480 } 1481 1482 size_t ShenandoahHeap::max_tlab_size() const { 1483 return ShenandoahHeapRegion::RegionSizeBytes; 1484 } 1485 1486 class ResizeGCLABClosure : public ThreadClosure { 1487 public: 1488 void do_thread(Thread* thread) { 1489 thread->gclab().resize(); 1490 } 1491 }; 1492 1493 void ShenandoahHeap::resize_all_tlabs() { 1494 CollectedHeap::resize_all_tlabs(); 1495 1496 ResizeGCLABClosure cl; 1497 Threads::threads_do(&cl); 1498 } 1499 1500 class AccumulateStatisticsGCLABClosure : public ThreadClosure { 1501 public: 1502 void do_thread(Thread* thread) { 1503 thread->gclab().accumulate_statistics(); 1504 thread->gclab().initialize_statistics(); 1505 } 1506 }; 1507 1508 void ShenandoahHeap::accumulate_statistics_all_gclabs() { 1509 AccumulateStatisticsGCLABClosure cl; 1510 Threads::threads_do(&cl); 1511 } 1512 1513 bool ShenandoahHeap::can_elide_tlab_store_barriers() const { 1514 return true; 1515 } 1516 1517 oop ShenandoahHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) { 1518 // Overridden to do nothing. 1519 return new_obj; 1520 } 1521 1522 bool ShenandoahHeap::can_elide_initializing_store_barrier(oop new_obj) { 1523 return true; 1524 } 1525 1526 bool ShenandoahHeap::card_mark_must_follow_store() const { 1527 return false; 1528 } 1529 1530 void ShenandoahHeap::collect(GCCause::Cause cause) { 1531 assert(cause != GCCause::_gc_locker, "no JNI critical callback"); 1532 if (GCCause::is_user_requested_gc(cause)) { 1533 if (! DisableExplicitGC) { 1534 _concurrent_gc_thread->do_full_gc(cause); 1535 } 1536 } else if (cause == GCCause::_allocation_failure) { 1537 collector_policy()->set_should_clear_all_soft_refs(true); 1538 _concurrent_gc_thread->do_full_gc(cause); 1539 } 1540 } 1541 1542 void ShenandoahHeap::do_full_collection(bool clear_all_soft_refs) { 1543 //assert(false, "Shouldn't need to do full collections"); 1544 } 1545 1546 AdaptiveSizePolicy* ShenandoahHeap::size_policy() { 1547 Unimplemented(); 1548 return NULL; 1549 1550 } 1551 1552 CollectorPolicy* ShenandoahHeap::collector_policy() const { 1553 return _shenandoah_policy; 1554 } 1555 1556 1557 HeapWord* ShenandoahHeap::block_start(const void* addr) const { 1558 Space* sp = heap_region_containing(addr); 1559 if (sp != NULL) { 1560 return sp->block_start(addr); 1561 } 1562 return NULL; 1563 } 1564 1565 size_t ShenandoahHeap::block_size(const HeapWord* addr) const { 1566 Space* sp = heap_region_containing(addr); 1567 assert(sp != NULL, "block_size of address outside of heap"); 1568 return sp->block_size(addr); 1569 } 1570 1571 bool ShenandoahHeap::block_is_obj(const HeapWord* addr) const { 1572 Space* sp = heap_region_containing(addr); 1573 return sp->block_is_obj(addr); 1574 } 1575 1576 jlong ShenandoahHeap::millis_since_last_gc() { 1577 return 0; 1578 } 1579 1580 void ShenandoahHeap::prepare_for_verify() { 1581 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 1582 ensure_parsability(false); 1583 } 1584 } 1585 1586 void ShenandoahHeap::print_gc_threads_on(outputStream* st) const { 1587 workers()->print_worker_threads_on(st); 1588 } 1589 1590 void ShenandoahHeap::gc_threads_do(ThreadClosure* tcl) const { 1591 workers()->threads_do(tcl); 1592 } 1593 1594 void ShenandoahHeap::print_tracing_info() const { 1595 if (log_is_enabled(Info, gc, stats)) { 1596 ResourceMark rm; 1597 outputStream* out = Log(gc, stats)::info_stream(); 1598 _shenandoah_policy->print_tracing_info(out); 1599 } 1600 } 1601 1602 class ShenandoahVerifyRootsClosure: public ExtendedOopClosure { 1603 private: 1604 ShenandoahHeap* _heap; 1605 VerifyOption _vo; 1606 bool _failures; 1607 public: 1608 // _vo == UsePrevMarking -> use "prev" marking information, 1609 // _vo == UseNextMarking -> use "next" marking information, 1610 // _vo == UseMarkWord -> use mark word from object header. 1611 ShenandoahVerifyRootsClosure(VerifyOption vo) : 1612 _heap(ShenandoahHeap::heap()), 1613 _vo(vo), 1614 _failures(false) { } 1615 1616 bool failures() { return _failures; } 1617 1618 private: 1619 template <class T> 1620 inline void do_oop_work(T* p) { 1621 oop obj = oopDesc::load_decode_heap_oop(p); 1622 if (! oopDesc::is_null(obj) && ! obj->is_oop()) { 1623 { // Just for debugging. 1624 tty->print_cr("Root location "PTR_FORMAT 1625 "verified "PTR_FORMAT, p2i(p), p2i((void*) obj)); 1626 // obj->print_on(tty); 1627 } 1628 } 1629 guarantee(obj->is_oop_or_null(), "is oop or null"); 1630 } 1631 1632 public: 1633 void do_oop(oop* p) { 1634 do_oop_work(p); 1635 } 1636 1637 void do_oop(narrowOop* p) { 1638 do_oop_work(p); 1639 } 1640 1641 }; 1642 1643 class ShenandoahVerifyHeapClosure: public ObjectClosure { 1644 private: 1645 ShenandoahVerifyRootsClosure _rootsCl; 1646 public: 1647 ShenandoahVerifyHeapClosure(ShenandoahVerifyRootsClosure rc) : 1648 _rootsCl(rc) {}; 1649 1650 void do_object(oop p) { 1651 _rootsCl.do_oop(&p); 1652 } 1653 }; 1654 1655 class ShenandoahVerifyKlassClosure: public KlassClosure { 1656 OopClosure *_oop_closure; 1657 public: 1658 ShenandoahVerifyKlassClosure(OopClosure* cl) : _oop_closure(cl) {} 1659 void do_klass(Klass* k) { 1660 k->oops_do(_oop_closure); 1661 } 1662 }; 1663 1664 void ShenandoahHeap::verify(VerifyOption vo) { 1665 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 1666 1667 ShenandoahVerifyRootsClosure rootsCl(vo); 1668 1669 assert(Thread::current()->is_VM_thread(), 1670 "Expected to be executed serially by the VM thread at this point"); 1671 1672 roots_iterate(&rootsCl); 1673 1674 bool failures = rootsCl.failures(); 1675 log_trace(gc)("verify failures: %s", BOOL_TO_STR(failures)); 1676 1677 ShenandoahVerifyHeapClosure heapCl(rootsCl); 1678 1679 object_iterate(&heapCl); 1680 // TODO: Implement rest of it. 1681 } else { 1682 tty->print("(SKIPPING roots, heapRegions, remset) "); 1683 } 1684 } 1685 size_t ShenandoahHeap::tlab_capacity(Thread *thr) const { 1686 return _free_regions->capacity(); 1687 } 1688 1689 class ShenandoahIterateObjectClosureRegionClosure: public ShenandoahHeapRegionClosure { 1690 ObjectClosure* _cl; 1691 public: 1692 ShenandoahIterateObjectClosureRegionClosure(ObjectClosure* cl) : _cl(cl) {} 1693 bool doHeapRegion(ShenandoahHeapRegion* r) { 1694 ShenandoahHeap::heap()->marked_object_iterate(r, _cl); 1695 return false; 1696 } 1697 }; 1698 1699 void ShenandoahHeap::object_iterate(ObjectClosure* cl) { 1700 ShenandoahIterateObjectClosureRegionClosure blk(cl); 1701 heap_region_iterate(&blk, false, true); 1702 } 1703 1704 class ShenandoahSafeObjectIterateAdjustPtrsClosure : public MetadataAwareOopClosure { 1705 private: 1706 ShenandoahHeap* _heap; 1707 1708 public: 1709 ShenandoahSafeObjectIterateAdjustPtrsClosure() : _heap(ShenandoahHeap::heap()) {} 1710 1711 private: 1712 template <class T> 1713 inline void do_oop_work(T* p) { 1714 T o = oopDesc::load_heap_oop(p); 1715 if (!oopDesc::is_null(o)) { 1716 oop obj = oopDesc::decode_heap_oop_not_null(o); 1717 oopDesc::encode_store_heap_oop(p, BrooksPointer::forwardee(obj)); 1718 } 1719 } 1720 public: 1721 void do_oop(oop* p) { 1722 do_oop_work(p); 1723 } 1724 void do_oop(narrowOop* p) { 1725 do_oop_work(p); 1726 } 1727 }; 1728 1729 class ShenandoahSafeObjectIterateAndUpdate : public ObjectClosure { 1730 private: 1731 ObjectClosure* _cl; 1732 public: 1733 ShenandoahSafeObjectIterateAndUpdate(ObjectClosure *cl) : _cl(cl) {} 1734 1735 virtual void do_object(oop obj) { 1736 assert (oopDesc::unsafe_equals(obj, BrooksPointer::forwardee(obj)), 1737 "avoid double-counting: only non-forwarded objects here"); 1738 1739 // Fix up the ptrs. 1740 ShenandoahSafeObjectIterateAdjustPtrsClosure adjust_ptrs; 1741 obj->oop_iterate(&adjust_ptrs); 1742 1743 // Can reply the object now: 1744 _cl->do_object(obj); 1745 } 1746 }; 1747 1748 void ShenandoahHeap::safe_object_iterate(ObjectClosure* cl) { 1749 assert(SafepointSynchronize::is_at_safepoint(), "safe iteration is only available during safepoints"); 1750 1751 // Safe iteration does objects only with correct references. 1752 // This is why we skip dirty regions that have stale copies of objects, 1753 // and fix up the pointers in the returned objects. 1754 1755 ShenandoahSafeObjectIterateAndUpdate safe_cl(cl); 1756 ShenandoahIterateObjectClosureRegionClosure blk(&safe_cl); 1757 heap_region_iterate(&blk, 1758 /* skip_dirty_regions = */ true, 1759 /* skip_humongous_continuations = */ true); 1760 1761 _need_update_refs = false; // already updated the references 1762 } 1763 1764 // Apply blk->doHeapRegion() on all committed regions in address order, 1765 // terminating the iteration early if doHeapRegion() returns true. 1766 void ShenandoahHeap::heap_region_iterate(ShenandoahHeapRegionClosure* blk, bool skip_dirty_regions, bool skip_humongous_continuation) const { 1767 for (size_t i = 0; i < _num_regions; i++) { 1768 ShenandoahHeapRegion* current = _ordered_regions->get(i); 1769 if (skip_humongous_continuation && current->is_humongous_continuation()) { 1770 continue; 1771 } 1772 if (skip_dirty_regions && in_collection_set(current)) { 1773 continue; 1774 } 1775 if (blk->doHeapRegion(current)) { 1776 return; 1777 } 1778 } 1779 } 1780 1781 class ClearLivenessClosure : public ShenandoahHeapRegionClosure { 1782 ShenandoahHeap* sh; 1783 public: 1784 ClearLivenessClosure(ShenandoahHeap* heap) : sh(heap) { } 1785 1786 bool doHeapRegion(ShenandoahHeapRegion* r) { 1787 r->clear_live_data(); 1788 sh->set_next_top_at_mark_start(r->bottom(), r->top()); 1789 return false; 1790 } 1791 }; 1792 1793 void ShenandoahHeap::start_concurrent_marking() { 1794 1795 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::accumulate_stats); 1796 accumulate_statistics_all_tlabs(); 1797 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::accumulate_stats); 1798 1799 set_concurrent_mark_in_progress(true); 1800 // We need to reset all TLABs because we'd lose marks on all objects allocated in them. 1801 if (UseTLAB) { 1802 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::make_parsable); 1803 ensure_parsability(true); 1804 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::make_parsable); 1805 } 1806 1807 _shenandoah_policy->record_bytes_allocated(_bytes_allocated_since_cm); 1808 _used_start_gc = used(); 1809 1810 #ifdef ASSERT 1811 if (ShenandoahDumpHeapBeforeConcurrentMark) { 1812 ensure_parsability(false); 1813 print_all_refs("pre-mark"); 1814 } 1815 #endif 1816 1817 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::clear_liveness); 1818 ClearLivenessClosure clc(this); 1819 heap_region_iterate(&clc); 1820 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::clear_liveness); 1821 1822 if (UseShenandoahMatrix) { 1823 connection_matrix()->clear_all(); 1824 } 1825 // print_all_refs("pre -mark"); 1826 1827 // oopDesc::_debug = true; 1828 1829 // Make above changes visible to worker threads 1830 OrderAccess::fence(); 1831 1832 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::scan_roots); 1833 concurrentMark()->init_mark_roots(); 1834 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::scan_roots); 1835 1836 // print_all_refs("pre-mark2"); 1837 } 1838 1839 class VerifyAfterEvacuationClosure : public ExtendedOopClosure { 1840 1841 ShenandoahHeap* _sh; 1842 1843 public: 1844 VerifyAfterEvacuationClosure() : _sh ( ShenandoahHeap::heap() ) {} 1845 1846 template<class T> void do_oop_nv(T* p) { 1847 T heap_oop = oopDesc::load_heap_oop(p); 1848 if (!oopDesc::is_null(heap_oop)) { 1849 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 1850 guarantee(_sh->in_collection_set(obj) == (! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))), 1851 "forwarded objects can only exist in dirty (from-space) regions is_dirty: %s, is_forwarded: %s obj-klass: %s, marked: %s", 1852 BOOL_TO_STR(_sh->in_collection_set(obj)), 1853 BOOL_TO_STR(! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))), 1854 obj->klass()->external_name(), 1855 BOOL_TO_STR(_sh->is_marked_complete(obj)) 1856 ); 1857 obj = oopDesc::bs()->read_barrier(obj); 1858 guarantee(! _sh->in_collection_set(obj), "forwarded oops must not point to dirty regions"); 1859 guarantee(obj->is_oop(), "is_oop"); 1860 guarantee(Metaspace::contains(obj->klass()), "klass pointer must go to metaspace"); 1861 } 1862 } 1863 1864 void do_oop(oop* p) { do_oop_nv(p); } 1865 void do_oop(narrowOop* p) { do_oop_nv(p); } 1866 1867 }; 1868 1869 void ShenandoahHeap::verify_heap_after_evacuation() { 1870 1871 verify_heap_size_consistency(); 1872 1873 ensure_parsability(false); 1874 1875 VerifyAfterEvacuationClosure cl; 1876 roots_iterate(&cl); 1877 1878 ObjectToOopClosure objs(&cl); 1879 object_iterate(&objs); 1880 1881 } 1882 1883 class VerifyRegionsAfterUpdateRefsClosure : public ShenandoahHeapRegionClosure { 1884 public: 1885 bool doHeapRegion(ShenandoahHeapRegion* r) { 1886 assert(! ShenandoahHeap::heap()->in_collection_set(r), "no region must be in collection set"); 1887 return false; 1888 } 1889 }; 1890 1891 void ShenandoahHeap::swap_mark_bitmaps() { 1892 // Swap bitmaps. 1893 CMBitMap* tmp1 = _complete_mark_bit_map; 1894 _complete_mark_bit_map = _next_mark_bit_map; 1895 _next_mark_bit_map = tmp1; 1896 1897 // Swap top-at-mark-start pointers 1898 HeapWord** tmp2 = _complete_top_at_mark_starts; 1899 _complete_top_at_mark_starts = _next_top_at_mark_starts; 1900 _next_top_at_mark_starts = tmp2; 1901 1902 HeapWord** tmp3 = _complete_top_at_mark_starts_base; 1903 _complete_top_at_mark_starts_base = _next_top_at_mark_starts_base; 1904 _next_top_at_mark_starts_base = tmp3; 1905 } 1906 1907 class ShenandoahVerifyMatrixOopClosure : public ExtendedOopClosure { 1908 private: 1909 oop _obj; 1910 1911 template <class T> 1912 inline void do_oop_nv(T* p) { 1913 T o = oopDesc::load_heap_oop(p); 1914 if (! oopDesc::is_null(o)) { 1915 oop obj = oopDesc::decode_heap_oop_not_null(o); 1916 ShenandoahHeap* heap = ShenandoahHeap::heap(); 1917 guarantee(heap->is_marked_complete(obj), "must be marked"); 1918 1919 uint from_idx = heap->heap_region_index_containing(p); 1920 uint to_idx = heap->heap_region_index_containing(obj); 1921 if (!heap->connection_matrix()->is_connected(from_idx, to_idx)) { 1922 tty->print_cr("from-obj: "); 1923 _obj->print_on(tty); 1924 tty->print_cr("to-obj:"); 1925 obj->print_on(tty); 1926 tty->print_cr("from-obj allocated after mark: %s", BOOL_TO_STR(heap->allocated_after_complete_mark_start((HeapWord*) _obj))); 1927 tty->print_cr("to-obj allocated after mark: %s", BOOL_TO_STR(heap->allocated_after_complete_mark_start((HeapWord*) obj))); 1928 tty->print_cr("from-obj marked: %s", BOOL_TO_STR(heap->is_marked_complete(_obj))); 1929 tty->print_cr("to-obj marked: %s", BOOL_TO_STR(heap->is_marked_complete(obj))); 1930 tty->print_cr("from-idx: %u, to-idx: %u", from_idx, to_idx); 1931 1932 oop fwd_from = BrooksPointer::forwardee(_obj); 1933 oop fwd_to = BrooksPointer::forwardee(obj); 1934 tty->print_cr("from-obj forwardee: " PTR_FORMAT, p2i(fwd_from)); 1935 tty->print_cr("to-obj forwardee: " PTR_FORMAT, p2i(fwd_to)); 1936 tty->print_cr("forward(from-obj) marked: %s", BOOL_TO_STR(heap->is_marked_complete(fwd_from))); 1937 tty->print_cr("forward(to-obj) marked: %s", BOOL_TO_STR(heap->is_marked_complete(fwd_to))); 1938 uint fwd_from_idx = heap->heap_region_index_containing(fwd_from); 1939 uint fwd_to_idx = heap->heap_region_index_containing(fwd_to); 1940 tty->print_cr("forward(from-idx): %u, forward(to-idx): %u", fwd_from_idx, fwd_to_idx); 1941 tty->print_cr("forward(from) connected with forward(to)? %s", BOOL_TO_STR(heap->connection_matrix()->is_connected(fwd_from_idx, fwd_to_idx))); 1942 tty->print_cr("sizeof(bool): %lu", sizeof(bool)); 1943 } 1944 guarantee(oopDesc::unsafe_equals(ShenandoahBarrierSet::resolve_oop_static_not_null(obj), obj), "polizeilich verboten"); 1945 guarantee(heap->connection_matrix()->is_connected(from_idx, to_idx), "must be connected"); 1946 } 1947 } 1948 1949 public: 1950 ShenandoahVerifyMatrixOopClosure(oop obj) : _obj(obj) {} 1951 1952 void do_oop(oop* o) { 1953 do_oop_nv(o); 1954 } 1955 1956 void do_oop(narrowOop* o) { 1957 do_oop_nv(o); 1958 } 1959 }; 1960 1961 class ShenandoahVerifyMatrixObjectClosure : public ObjectClosure { 1962 public: 1963 void do_object(oop obj) { 1964 guarantee(ShenandoahHeap::heap()->is_marked_complete(obj), "must be marked"); 1965 ShenandoahVerifyMatrixOopClosure cl(obj); 1966 obj->oop_iterate(&cl); 1967 } 1968 1969 }; 1970 1971 class ShenandoahVerifyMatrixRegionClosure : public ShenandoahHeapRegionClosure { 1972 bool doHeapRegion(ShenandoahHeapRegion* r) { 1973 ShenandoahVerifyMatrixObjectClosure cl; 1974 ShenandoahHeap::heap()->marked_object_iterate(r, &cl); 1975 return false; 1976 } 1977 }; 1978 1979 void ShenandoahHeap::verify_matrix() { 1980 OrderAccess::fence(); 1981 ensure_parsability(false); 1982 ShenandoahVerifyMatrixRegionClosure cl; 1983 heap_region_iterate(&cl, true, true); 1984 } 1985 1986 void ShenandoahHeap::stop_concurrent_marking() { 1987 assert(concurrent_mark_in_progress(), "How else could we get here?"); 1988 if (! cancelled_concgc()) { 1989 // If we needed to update refs, and concurrent marking has been cancelled, 1990 // we need to finish updating references. 1991 set_need_update_refs(false); 1992 swap_mark_bitmaps(); 1993 } 1994 set_concurrent_mark_in_progress(false); 1995 1996 if (log_is_enabled(Trace, gc, region)) { 1997 ResourceMark rm; 1998 outputStream* out = Log(gc, region)::trace_stream(); 1999 print_heap_regions(out); 2000 } 2001 2002 } 2003 2004 void ShenandoahHeap::set_concurrent_mark_in_progress(bool in_progress) { 2005 _concurrent_mark_in_progress = in_progress ? 1 : 0; 2006 JavaThread::satb_mark_queue_set().set_active_all_threads(in_progress, !in_progress); 2007 } 2008 2009 void ShenandoahHeap::set_evacuation_in_progress_concurrently(bool in_progress) { 2010 // Note: it is important to first release the _evacuation_in_progress flag here, 2011 // so that Java threads can get out of oom_during_evacuation() and reach a safepoint, 2012 // in case a VM task is pending. 2013 set_evacuation_in_progress(in_progress); 2014 MutexLocker mu(Threads_lock); 2015 JavaThread::set_evacuation_in_progress_all_threads(in_progress); 2016 } 2017 2018 void ShenandoahHeap::set_evacuation_in_progress_at_safepoint(bool in_progress) { 2019 assert(SafepointSynchronize::is_at_safepoint(), "Only call this at safepoint"); 2020 set_evacuation_in_progress(in_progress); 2021 JavaThread::set_evacuation_in_progress_all_threads(in_progress); 2022 } 2023 2024 void ShenandoahHeap::set_evacuation_in_progress(bool in_progress) { 2025 _evacuation_in_progress = in_progress ? 1 : 0; 2026 OrderAccess::fence(); 2027 } 2028 2029 void ShenandoahHeap::verify_copy(oop p,oop c){ 2030 assert(! oopDesc::unsafe_equals(p, oopDesc::bs()->read_barrier(p)), "forwarded correctly"); 2031 assert(oopDesc::unsafe_equals(oopDesc::bs()->read_barrier(p), c), "verify pointer is correct"); 2032 if (p->klass() != c->klass()) { 2033 print_heap_regions(); 2034 } 2035 assert(p->klass() == c->klass(), "verify class p-size: "INT32_FORMAT" c-size: "INT32_FORMAT, p->size(), c->size()); 2036 assert(p->size() == c->size(), "verify size"); 2037 // Object may have been locked between copy and verification 2038 // assert(p->mark() == c->mark(), "verify mark"); 2039 assert(oopDesc::unsafe_equals(c, oopDesc::bs()->read_barrier(c)), "verify only forwarded once"); 2040 } 2041 2042 void ShenandoahHeap::oom_during_evacuation() { 2043 log_develop_trace(gc)("Out of memory during evacuation, cancel evacuation, schedule full GC by thread %d", 2044 Thread::current()->osthread()->thread_id()); 2045 2046 // We ran out of memory during evacuation. Cancel evacuation, and schedule a full-GC. 2047 collector_policy()->set_should_clear_all_soft_refs(true); 2048 concurrent_thread()->try_set_full_gc(); 2049 cancel_concgc(_oom_evacuation); 2050 2051 if ((! Thread::current()->is_GC_task_thread()) && (! Thread::current()->is_ConcurrentGC_thread())) { 2052 assert(! Threads_lock->owned_by_self() 2053 || SafepointSynchronize::is_at_safepoint(), "must not hold Threads_lock here"); 2054 log_warning(gc)("OOM during evacuation. Let Java thread wait until evacuation finishes."); 2055 while (_evacuation_in_progress) { // wait. 2056 Thread::current()->_ParkEvent->park(1); 2057 } 2058 } 2059 2060 } 2061 2062 HeapWord* ShenandoahHeap::tlab_post_allocation_setup(HeapWord* obj) { 2063 // Initialize Brooks pointer for the next object 2064 HeapWord* result = obj + BrooksPointer::word_size(); 2065 BrooksPointer::initialize(oop(result)); 2066 return result; 2067 } 2068 2069 uint ShenandoahHeap::oop_extra_words() { 2070 return BrooksPointer::word_size(); 2071 } 2072 2073 void ShenandoahHeap::grow_heap_by(size_t num_regions) { 2074 size_t base = _num_regions; 2075 ensure_new_regions(num_regions); 2076 for (size_t i = 0; i < num_regions; i++) { 2077 size_t new_region_index = i + base; 2078 HeapWord* start = _first_region_bottom + (ShenandoahHeapRegion::RegionSizeBytes / HeapWordSize) * new_region_index; 2079 ShenandoahHeapRegion* new_region = new ShenandoahHeapRegion(this, start, ShenandoahHeapRegion::RegionSizeBytes / HeapWordSize, new_region_index); 2080 2081 if (log_is_enabled(Trace, gc, region)) { 2082 ResourceMark rm; 2083 outputStream* out = Log(gc, region)::trace_stream(); 2084 out->print_cr("allocating new region at index: "SIZE_FORMAT, new_region_index); 2085 new_region->print_on(out); 2086 } 2087 2088 assert(_ordered_regions->active_regions() == new_region->region_number(), "must match"); 2089 _ordered_regions->add_region(new_region); 2090 _in_cset_fast_test_base[new_region_index] = false; // Not in cset 2091 _next_top_at_mark_starts_base[new_region_index] = new_region->bottom(); 2092 _complete_top_at_mark_starts_base[new_region_index] = new_region->bottom(); 2093 2094 _free_regions->add_region(new_region); 2095 } 2096 } 2097 2098 void ShenandoahHeap::ensure_new_regions(size_t new_regions) { 2099 2100 size_t num_regions = _num_regions; 2101 size_t new_num_regions = num_regions + new_regions; 2102 assert(new_num_regions <= _max_regions, "we checked this earlier"); 2103 2104 size_t expand_size = new_regions * ShenandoahHeapRegion::RegionSizeBytes; 2105 log_trace(gc, region)("expanding storage by "SIZE_FORMAT_HEX" bytes, for "SIZE_FORMAT" new regions", expand_size, new_regions); 2106 bool success = _storage.expand_by(expand_size, ShenandoahAlwaysPreTouch); 2107 assert(success, "should always be able to expand by requested size"); 2108 2109 _num_regions = new_num_regions; 2110 2111 } 2112 2113 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() : 2114 _heap(ShenandoahHeap::heap_no_check()) { 2115 } 2116 2117 void ShenandoahForwardedIsAliveClosure::init(ShenandoahHeap* heap) { 2118 _heap = heap; 2119 } 2120 2121 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) { 2122 2123 assert(_heap != NULL, "sanity"); 2124 obj = ShenandoahBarrierSet::resolve_oop_static_not_null(obj); 2125 #ifdef ASSERT 2126 if (_heap->concurrent_mark_in_progress()) { 2127 assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), "only query to-space"); 2128 } 2129 #endif 2130 assert(!oopDesc::is_null(obj), "null"); 2131 return _heap->is_marked_next(obj); 2132 } 2133 2134 void ShenandoahHeap::ref_processing_init() { 2135 MemRegion mr = reserved_region(); 2136 2137 isAlive.init(ShenandoahHeap::heap()); 2138 assert(_max_workers > 0, "Sanity"); 2139 2140 _ref_processor = 2141 new ReferenceProcessor(mr, // span 2142 ParallelRefProcEnabled, 2143 // mt processing 2144 _max_workers, 2145 // degree of mt processing 2146 true, 2147 // mt discovery 2148 _max_workers, 2149 // degree of mt discovery 2150 false, 2151 // Reference discovery is not atomic 2152 &isAlive); 2153 } 2154 2155 #ifdef ASSERT 2156 void ShenandoahHeap::set_from_region_protection(bool protect) { 2157 for (uint i = 0; i < _num_regions; i++) { 2158 ShenandoahHeapRegion* region = _ordered_regions->get(i); 2159 if (region != NULL && in_collection_set(region)) { 2160 if (protect) { 2161 region->memProtectionOn(); 2162 } else { 2163 region->memProtectionOff(); 2164 } 2165 } 2166 } 2167 } 2168 #endif 2169 2170 size_t ShenandoahHeap::num_regions() { 2171 return _num_regions; 2172 } 2173 2174 size_t ShenandoahHeap::max_regions() { 2175 return _max_regions; 2176 } 2177 2178 GCTracer* ShenandoahHeap::tracer() { 2179 return shenandoahPolicy()->tracer(); 2180 } 2181 2182 size_t ShenandoahHeap::tlab_used(Thread* thread) const { 2183 return _free_regions->used(); 2184 } 2185 2186 void ShenandoahHeap::cancel_concgc(GCCause::Cause cause) { 2187 if (try_cancel_concgc()) { 2188 log_info(gc)("Cancelling concurrent GC: %s", GCCause::to_string(cause)); 2189 _shenandoah_policy->report_concgc_cancelled(); 2190 } 2191 } 2192 2193 void ShenandoahHeap::cancel_concgc(ShenandoahCancelCause cause) { 2194 if (try_cancel_concgc()) { 2195 log_info(gc)("Cancelling concurrent GC: %s", cancel_cause_to_string(cause)); 2196 _shenandoah_policy->report_concgc_cancelled(); 2197 } 2198 } 2199 2200 const char* ShenandoahHeap::cancel_cause_to_string(ShenandoahCancelCause cause) { 2201 switch (cause) { 2202 case _oom_evacuation: 2203 return "Out of memory for evacuation"; 2204 case _vm_stop: 2205 return "Stopping VM"; 2206 default: 2207 return "Unknown"; 2208 } 2209 } 2210 2211 void ShenandoahHeap::clear_cancelled_concgc() { 2212 set_cancelled_concgc(false); 2213 } 2214 2215 uint ShenandoahHeap::max_workers() { 2216 return _max_workers; 2217 } 2218 2219 void ShenandoahHeap::stop() { 2220 // The shutdown sequence should be able to terminate when GC is running. 2221 2222 // Step 1. Notify control thread that we are in shutdown. 2223 // Note that we cannot do that with stop(), because stop() is blocking and waits for the actual shutdown. 2224 // Doing stop() here would wait for the normal GC cycle to complete, never falling through to cancel below. 2225 _concurrent_gc_thread->prepare_for_graceful_shutdown(); 2226 2227 // Step 2. Notify GC workers that we are cancelling GC. 2228 cancel_concgc(_vm_stop); 2229 2230 // Step 3. Wait until GC worker exits normally. 2231 _concurrent_gc_thread->stop(); 2232 } 2233 2234 void ShenandoahHeap::unlink_string_and_symbol_table(BoolObjectClosure* is_alive, bool process_strings, bool process_symbols) { 2235 2236 StringSymbolTableUnlinkTask shenandoah_unlink_task(is_alive, process_strings, process_symbols); 2237 workers()->run_task(&shenandoah_unlink_task); 2238 2239 // if (G1StringDedup::is_enabled()) { 2240 // G1StringDedup::unlink(is_alive); 2241 // } 2242 } 2243 2244 void ShenandoahHeap::set_need_update_refs(bool need_update_refs) { 2245 _need_update_refs = need_update_refs; 2246 } 2247 2248 //fixme this should be in heapregionset 2249 ShenandoahHeapRegion* ShenandoahHeap::next_compaction_region(const ShenandoahHeapRegion* r) { 2250 size_t region_idx = r->region_number() + 1; 2251 ShenandoahHeapRegion* next = _ordered_regions->get(region_idx); 2252 guarantee(next->region_number() == region_idx, "region number must match"); 2253 while (next->is_humongous()) { 2254 region_idx = next->region_number() + 1; 2255 next = _ordered_regions->get(region_idx); 2256 guarantee(next->region_number() == region_idx, "region number must match"); 2257 } 2258 return next; 2259 } 2260 2261 void ShenandoahHeap::set_region_in_collection_set(size_t region_index, bool b) { 2262 _in_cset_fast_test_base[region_index] = b; 2263 } 2264 2265 ShenandoahMonitoringSupport* ShenandoahHeap::monitoring_support() { 2266 return _monitoring_support; 2267 } 2268 2269 CMBitMap* ShenandoahHeap::complete_mark_bit_map() { 2270 return _complete_mark_bit_map; 2271 } 2272 2273 CMBitMap* ShenandoahHeap::next_mark_bit_map() { 2274 return _next_mark_bit_map; 2275 } 2276 2277 void ShenandoahHeap::add_free_region(ShenandoahHeapRegion* r) { 2278 _free_regions->add_region(r); 2279 } 2280 2281 void ShenandoahHeap::clear_free_regions() { 2282 _free_regions->clear(); 2283 } 2284 2285 address ShenandoahHeap::in_cset_fast_test_addr() { 2286 return (address) (ShenandoahHeap::heap()->_in_cset_fast_test); 2287 } 2288 2289 address ShenandoahHeap::cancelled_concgc_addr() { 2290 return (address) &(ShenandoahHeap::heap()->_cancelled_concgc); 2291 } 2292 2293 void ShenandoahHeap::clear_cset_fast_test() { 2294 assert(_in_cset_fast_test_base != NULL, "sanity"); 2295 memset(_in_cset_fast_test_base, false, 2296 _in_cset_fast_test_length * sizeof(bool)); 2297 } 2298 2299 size_t ShenandoahHeap::conservative_max_heap_alignment() { 2300 return ShenandoahMaxRegionSize; 2301 } 2302 2303 size_t ShenandoahHeap::bytes_allocated_since_cm() { 2304 return _bytes_allocated_since_cm; 2305 } 2306 2307 void ShenandoahHeap::set_bytes_allocated_since_cm(size_t bytes) { 2308 _bytes_allocated_since_cm = bytes; 2309 } 2310 2311 size_t ShenandoahHeap::max_allocated_gc() { 2312 return _max_allocated_gc; 2313 } 2314 2315 void ShenandoahHeap::set_next_top_at_mark_start(HeapWord* region_base, HeapWord* addr) { 2316 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2317 _next_top_at_mark_starts[index] = addr; 2318 } 2319 2320 HeapWord* ShenandoahHeap::next_top_at_mark_start(HeapWord* region_base) { 2321 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2322 return _next_top_at_mark_starts[index]; 2323 } 2324 2325 void ShenandoahHeap::set_complete_top_at_mark_start(HeapWord* region_base, HeapWord* addr) { 2326 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2327 _complete_top_at_mark_starts[index] = addr; 2328 } 2329 2330 HeapWord* ShenandoahHeap::complete_top_at_mark_start(HeapWord* region_base) { 2331 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::RegionSizeShift; 2332 return _complete_top_at_mark_starts[index]; 2333 } 2334 2335 void ShenandoahHeap::set_full_gc_in_progress(bool in_progress) { 2336 _full_gc_in_progress = in_progress; 2337 } 2338 2339 bool ShenandoahHeap::is_full_gc_in_progress() const { 2340 return _full_gc_in_progress; 2341 } 2342 2343 class NMethodOopInitializer : public OopClosure { 2344 private: 2345 ShenandoahHeap* _heap; 2346 public: 2347 NMethodOopInitializer() : _heap(ShenandoahHeap::heap()) { 2348 } 2349 2350 private: 2351 template <class T> 2352 inline void do_oop_work(T* p) { 2353 T o = oopDesc::load_heap_oop(p); 2354 if (! oopDesc::is_null(o)) { 2355 oop obj1 = oopDesc::decode_heap_oop_not_null(o); 2356 oop obj2 = oopDesc::bs()->write_barrier(obj1); 2357 if (! oopDesc::unsafe_equals(obj1, obj2)) { 2358 oopDesc::encode_store_heap_oop(p, obj2); 2359 } 2360 } 2361 } 2362 2363 public: 2364 void do_oop(oop* o) { 2365 do_oop_work(o); 2366 } 2367 void do_oop(narrowOop* o) { 2368 do_oop_work(o); 2369 } 2370 }; 2371 2372 void ShenandoahHeap::register_nmethod(nmethod* nm) { 2373 NMethodOopInitializer init; 2374 nm->oops_do(&init); 2375 nm->fix_oop_relocations(); 2376 } 2377 2378 void ShenandoahHeap::unregister_nmethod(nmethod* nm) { 2379 } 2380 2381 void ShenandoahHeap::pin_object(oop o) { 2382 heap_region_containing(o)->pin(); 2383 } 2384 2385 void ShenandoahHeap::unpin_object(oop o) { 2386 heap_region_containing(o)->unpin(); 2387 } 2388 2389 2390 GCTimer* ShenandoahHeap::gc_timer() const { 2391 return _gc_timer; 2392 } 2393 2394 class RecordAllRefsOopClosure: public ExtendedOopClosure { 2395 private: 2396 int _x; 2397 int *_matrix; 2398 int _num_regions; 2399 oop _p; 2400 2401 public: 2402 RecordAllRefsOopClosure(int *matrix, int x, size_t num_regions, oop p) : 2403 _matrix(matrix), _x(x), _num_regions(num_regions), _p(p) {} 2404 2405 template <class T> 2406 void do_oop_work(T* p) { 2407 oop o = oopDesc::load_decode_heap_oop(p); 2408 if (o != NULL) { 2409 if (ShenandoahHeap::heap()->is_in(o) && o->is_oop() ) { 2410 int y = ShenandoahHeap::heap()->heap_region_containing(o)->region_number(); 2411 _matrix[_x * _num_regions + y]++; 2412 } 2413 } 2414 } 2415 void do_oop(oop* p) { 2416 do_oop_work(p); 2417 } 2418 2419 void do_oop(narrowOop* p) { 2420 do_oop_work(p); 2421 } 2422 2423 }; 2424 2425 class RecordAllRefsObjectClosure : public ObjectClosure { 2426 int *_matrix; 2427 size_t _num_regions; 2428 2429 public: 2430 RecordAllRefsObjectClosure(int *matrix, size_t num_regions) : 2431 _matrix(matrix), _num_regions(num_regions) {} 2432 2433 void do_object(oop p) { 2434 if (ShenandoahHeap::heap()->is_in(p) && ShenandoahHeap::heap()->is_marked_next(p) && p->is_oop()) { 2435 int x = ShenandoahHeap::heap()->heap_region_containing(p)->region_number(); 2436 RecordAllRefsOopClosure cl(_matrix, x, _num_regions, p); 2437 p->oop_iterate(&cl); 2438 } 2439 } 2440 }; 2441 void ShenandoahHeap::calculate_matrix(int* connections) { 2442 log_develop_trace(gc)("calculating matrix"); 2443 ensure_parsability(false); 2444 int num = num_regions(); 2445 2446 for (int i = 0; i < num; i++) { 2447 for (int j = 0; j < num; j++) { 2448 connections[i * num + j] = 0; 2449 } 2450 } 2451 2452 RecordAllRefsOopClosure cl(connections, 0, num, NULL); 2453 roots_iterate(&cl); 2454 2455 RecordAllRefsObjectClosure cl2(connections, num); 2456 object_iterate(&cl2); 2457 2458 } 2459 2460 void ShenandoahHeap::print_matrix(int* connections) { 2461 int num = num_regions(); 2462 int cs_regions = 0; 2463 int referenced = 0; 2464 2465 for (int i = 0; i < num; i++) { 2466 size_t liveData = ShenandoahHeap::heap()->regions()->get(i)->get_live_data_bytes(); 2467 2468 int numReferencedRegions = 0; 2469 int numReferencedByRegions = 0; 2470 2471 for (int j = 0; j < num; j++) { 2472 if (connections[i * num + j] > 0) 2473 numReferencedRegions++; 2474 2475 if (connections [j * num + i] > 0) 2476 numReferencedByRegions++; 2477 2478 cs_regions++; 2479 referenced += numReferencedByRegions; 2480 } 2481 2482 if (ShenandoahHeap::heap()->regions()->get(i)->has_live()) { 2483 tty->print("Region %d is referenced by %d regions {", 2484 i, numReferencedByRegions); 2485 int col_count = 0; 2486 for (int j = 0; j < num; j++) { 2487 int foo = connections[j * num + i]; 2488 if (foo > 0) { 2489 col_count++; 2490 if ((col_count % 10) == 0) 2491 tty->print("\n"); 2492 tty->print("%d(%d), ", j,foo); 2493 } 2494 } 2495 tty->print("} \n"); 2496 } 2497 } 2498 2499 double avg = (double)referenced / (double) cs_regions; 2500 tty->print("Average Number of regions scanned / region = %lf\n", avg); 2501 } 2502 2503 class ShenandoahCountGarbageClosure : public ShenandoahHeapRegionClosure { 2504 private: 2505 size_t _garbage; 2506 public: 2507 ShenandoahCountGarbageClosure() : _garbage(0) { 2508 } 2509 2510 bool doHeapRegion(ShenandoahHeapRegion* r) { 2511 if (! r->is_humongous() && ! r->is_pinned() && ! r->in_collection_set()) { 2512 _garbage += r->garbage(); 2513 } 2514 return false; 2515 } 2516 2517 size_t garbage() { 2518 return _garbage; 2519 } 2520 }; 2521 2522 size_t ShenandoahHeap::garbage() { 2523 ShenandoahCountGarbageClosure cl; 2524 heap_region_iterate(&cl); 2525 return cl.garbage(); 2526 } 2527 2528 ShenandoahConnectionMatrix* ShenandoahHeap::connection_matrix() { 2529 return _connection_matrix; 2530 } 2531 2532 ShenandoahPartialGC* ShenandoahHeap::partial_gc() { 2533 return _partial_gc; 2534 } 2535 2536 void ShenandoahHeap::do_partial_collection() { 2537 { 2538 ShenandoahHeapLock lock(this); 2539 partial_gc()->prepare(); 2540 } 2541 partial_gc()->do_partial_collection(); 2542 } 2543 2544 #ifdef ASSERT 2545 void ShenandoahHeap::assert_heaplock_owned_by_current_thread() { 2546 assert(_heap_lock == locked, "must be locked"); 2547 assert(_heap_lock_owner == Thread::current(), "must be owned by current thread"); 2548 } 2549 2550 void ShenandoahHeap::assert_heaplock_or_safepoint() { 2551 Thread* thr = Thread::current(); 2552 assert((_heap_lock == locked && _heap_lock_owner == thr) || 2553 (SafepointSynchronize::is_at_safepoint() && thr->is_VM_thread()), 2554 "must own heap lock or by VM thread at safepoint"); 2555 } 2556 2557 #endif