1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc/g1/g1CollectedHeap.inline.hpp" 27 #include "gc/g1/g1ConcurrentMark.inline.hpp" 28 #include "gc/g1/g1CardLiveData.inline.hpp" 29 #include "gc/g1/suspendibleThreadSet.hpp" 30 #include "gc/shared/workgroup.hpp" 31 #include "logging/log.hpp" 32 #include "memory/universe.hpp" 33 #include "runtime/atomic.hpp" 34 #include "runtime/globals.hpp" 35 #include "runtime/os.hpp" 36 #include "utilities/bitMap.inline.hpp" 37 #include "utilities/debug.hpp" 38 39 G1CardLiveData::G1CardLiveData() : 40 _max_capacity(0), 41 _cards_per_region(0), 42 _gc_timestamp_at_create(0), 43 _live_regions(NULL), 44 _live_regions_size_in_bits(0), 45 _live_cards(NULL), 46 _live_cards_size_in_bits(0) { 47 } 48 49 G1CardLiveData::~G1CardLiveData() { 50 free_large_bitmap(_live_cards, _live_cards_size_in_bits); 51 free_large_bitmap(_live_regions, _live_regions_size_in_bits); 52 } 53 54 G1CardLiveData::bm_word_t* G1CardLiveData::allocate_large_bitmap(size_t size_in_bits) { 55 size_t size_in_words = BitMap::calc_size_in_words(size_in_bits); 56 57 bm_word_t* map = MmapArrayAllocator<bm_word_t, mtGC>::allocate(size_in_words); 58 59 return map; 60 } 61 62 void G1CardLiveData::free_large_bitmap(bm_word_t* bitmap, size_t size_in_bits) { 63 MmapArrayAllocator<bm_word_t, mtGC>::free(bitmap, BitMap::calc_size_in_words(size_in_bits)); 64 } 65 66 void G1CardLiveData::initialize(size_t max_capacity, uint num_max_regions) { 67 assert(max_capacity % num_max_regions == 0, 68 "Given capacity must be evenly divisible by region size."); 69 size_t region_size = max_capacity / num_max_regions; 70 assert(region_size % (G1SATBCardTableModRefBS::card_size * BitsPerWord) == 0, 71 "Region size must be evenly divisible by area covered by a single word."); 72 _max_capacity = max_capacity; 73 _cards_per_region = region_size / G1SATBCardTableModRefBS::card_size; 74 75 _live_regions_size_in_bits = live_region_bitmap_size_in_bits(); 76 _live_regions = allocate_large_bitmap(_live_regions_size_in_bits); 77 _live_cards_size_in_bits = live_card_bitmap_size_in_bits(); 78 _live_cards = allocate_large_bitmap(_live_cards_size_in_bits); 79 } 80 81 void G1CardLiveData::pretouch() { 82 live_cards_bm().pretouch(); 83 live_regions_bm().pretouch(); 84 } 85 86 size_t G1CardLiveData::live_region_bitmap_size_in_bits() const { 87 return _max_capacity / (_cards_per_region << G1SATBCardTableModRefBS::card_shift); 88 } 89 90 size_t G1CardLiveData::live_card_bitmap_size_in_bits() const { 91 return _max_capacity >> G1SATBCardTableModRefBS::card_shift; 92 } 93 94 // Helper class that provides functionality to generate the Live Data Count 95 // information. 96 class G1CardLiveDataHelper VALUE_OBJ_CLASS_SPEC { 97 private: 98 BitMapView _region_bm; 99 BitMapView _card_bm; 100 101 // The card number of the bottom of the G1 heap. 102 // Used in biasing indices into accounting card bitmaps. 103 BitMap::idx_t _heap_card_bias; 104 105 // Utility routine to set an exclusive range of bits on the given 106 // bitmap, optimized for very small ranges. 107 // There must be at least one bit to set. 108 void set_card_bitmap_range(BitMap::idx_t start_idx, 109 BitMap::idx_t end_idx) { 110 111 // Set the exclusive bit range [start_idx, end_idx). 112 assert((end_idx - start_idx) > 0, "at least one bit"); 113 114 // For small ranges use a simple loop; otherwise use set_range. 115 // The range is made up of the cards that are spanned by an object/mem 116 // region so 8 cards will allow up to object sizes up to 4K to be handled 117 // using the loop. 118 if ((end_idx - start_idx) <= 8) { 119 for (BitMap::idx_t i = start_idx; i < end_idx; i += 1) { 120 _card_bm.set_bit(i); 121 } 122 } else { 123 _card_bm.set_range(start_idx, end_idx); 124 } 125 } 126 127 // We cache the last mark set. This avoids setting the same bit multiple times. 128 // This is particularly interesting for dense bitmaps, as this avoids doing 129 // lots of work most of the time. 130 BitMap::idx_t _last_marked_bit_idx; 131 132 void clear_card_bitmap_range(HeapWord* start, HeapWord* end) { 133 BitMap::idx_t start_idx = card_live_bitmap_index_for(start); 134 BitMap::idx_t end_idx = card_live_bitmap_index_for((HeapWord*)align_ptr_up(end, CardTableModRefBS::card_size)); 135 136 _card_bm.clear_range(start_idx, end_idx); 137 } 138 139 // Mark the card liveness bitmap for the object spanning from start to end. 140 void mark_card_bitmap_range(HeapWord* start, HeapWord* end) { 141 BitMap::idx_t start_idx = card_live_bitmap_index_for(start); 142 BitMap::idx_t end_idx = card_live_bitmap_index_for((HeapWord*)align_ptr_up(end, CardTableModRefBS::card_size)); 143 144 assert((end_idx - start_idx) > 0, "Trying to mark zero sized range."); 145 146 if (start_idx == _last_marked_bit_idx) { 147 start_idx++; 148 } 149 if (start_idx == end_idx) { 150 return; 151 } 152 153 // Set the bits in the card bitmap for the cards spanned by this object. 154 set_card_bitmap_range(start_idx, end_idx); 155 _last_marked_bit_idx = end_idx - 1; 156 } 157 158 void reset_mark_cache() { 159 _last_marked_bit_idx = (BitMap::idx_t)-1; 160 } 161 162 public: 163 // Returns the index in the per-card liveness count bitmap 164 // for the given address 165 inline BitMap::idx_t card_live_bitmap_index_for(HeapWord* addr) { 166 // Below, the term "card num" means the result of shifting an address 167 // by the card shift -- address 0 corresponds to card number 0. One 168 // must subtract the card num of the bottom of the heap to obtain a 169 // card table index. 170 BitMap::idx_t card_num = uintptr_t(addr) >> CardTableModRefBS::card_shift; 171 return card_num - _heap_card_bias; 172 } 173 174 // Takes a region that's not empty (i.e., it has at least one 175 // live object in it and sets its corresponding bit on the region 176 // bitmap to 1. 177 void set_bit_for_region(HeapRegion* hr) { 178 _region_bm.par_set_bit(hr->hrm_index()); 179 } 180 181 void reset_live_data(HeapRegion* hr) { 182 clear_card_bitmap_range(hr->next_top_at_mark_start(), hr->end()); 183 } 184 185 // Mark the range of bits covered by allocations done since the last marking 186 // in the given heap region, i.e. from NTAMS to top of the given region. 187 // Returns if there has been some allocation in this region since the last marking. 188 bool mark_allocated_since_marking(HeapRegion* hr) { 189 reset_mark_cache(); 190 191 HeapWord* ntams = hr->next_top_at_mark_start(); 192 HeapWord* top = hr->top(); 193 194 assert(hr->bottom() <= ntams && ntams <= hr->end(), "Preconditions."); 195 196 // Mark the allocated-since-marking portion... 197 if (ntams < top) { 198 mark_card_bitmap_range(ntams, top); 199 return true; 200 } else { 201 return false; 202 } 203 } 204 205 // Mark the range of bits covered by live objects on the mark bitmap between 206 // bottom and NTAMS of the given region. 207 // Returns the number of live bytes marked within that area for the given 208 // heap region. 209 size_t mark_marked_during_marking(G1CMBitMap* mark_bitmap, HeapRegion* hr) { 210 reset_mark_cache(); 211 212 size_t marked_bytes = 0; 213 214 HeapWord* ntams = hr->next_top_at_mark_start(); 215 HeapWord* start = hr->bottom(); 216 217 if (ntams <= start) { 218 // Skip empty regions. 219 return 0; 220 } 221 if (hr->is_humongous()) { 222 HeapRegion* start_region = hr->humongous_start_region(); 223 if (mark_bitmap->isMarked(start_region->bottom())) { 224 mark_card_bitmap_range(start, hr->top()); 225 return pointer_delta(hr->top(), start, 1); 226 } else { 227 // Humongous start object was actually dead. 228 return 0; 229 } 230 } 231 232 assert(start <= hr->end() && start <= ntams && ntams <= hr->end(), 233 "Preconditions not met - " 234 "start: " PTR_FORMAT ", ntams: " PTR_FORMAT ", end: " PTR_FORMAT, 235 p2i(start), p2i(ntams), p2i(hr->end())); 236 237 // Find the first marked object at or after "start". 238 start = mark_bitmap->getNextMarkedWordAddress(start, ntams); 239 while (start < ntams) { 240 oop obj = oop(start); 241 size_t obj_size = obj->size(); 242 HeapWord* obj_end = start + obj_size; 243 244 assert(obj_end <= hr->end(), "Humongous objects must have been handled elsewhere."); 245 246 mark_card_bitmap_range(start, obj_end); 247 248 // Add the size of this object to the number of marked bytes. 249 marked_bytes += obj_size * HeapWordSize; 250 251 // Find the next marked object after this one. 252 start = mark_bitmap->getNextMarkedWordAddress(obj_end, ntams); 253 } 254 255 return marked_bytes; 256 } 257 258 G1CardLiveDataHelper(G1CardLiveData* live_data, HeapWord* base_address) : 259 _region_bm(live_data->live_regions_bm()), 260 _card_bm(live_data->live_cards_bm()) { 261 // Calculate the card number for the bottom of the heap. Used 262 // in biasing indexes into the accounting card bitmaps. 263 _heap_card_bias = 264 uintptr_t(base_address) >> CardTableModRefBS::card_shift; 265 } 266 }; 267 268 class G1CreateCardLiveDataTask: public AbstractGangTask { 269 // Aggregate the counting data that was constructed concurrently 270 // with marking. 271 class G1CreateLiveDataClosure : public HeapRegionClosure { 272 G1CardLiveDataHelper _helper; 273 274 G1CMBitMap* _mark_bitmap; 275 276 G1ConcurrentMark* _cm; 277 public: 278 G1CreateLiveDataClosure(G1CollectedHeap* g1h, 279 G1ConcurrentMark* cm, 280 G1CMBitMap* mark_bitmap, 281 G1CardLiveData* live_data) : 282 HeapRegionClosure(), 283 _helper(live_data, g1h->reserved_region().start()), 284 _mark_bitmap(mark_bitmap), 285 _cm(cm) { } 286 287 bool doHeapRegion(HeapRegion* hr) { 288 size_t marked_bytes = _helper.mark_marked_during_marking(_mark_bitmap, hr); 289 if (marked_bytes > 0) { 290 hr->add_to_marked_bytes(marked_bytes); 291 } 292 293 return (_cm->do_yield_check() && _cm->has_aborted()); 294 } 295 }; 296 297 G1ConcurrentMark* _cm; 298 G1CardLiveData* _live_data; 299 HeapRegionClaimer _hr_claimer; 300 301 public: 302 G1CreateCardLiveDataTask(G1CMBitMap* bitmap, 303 G1CardLiveData* live_data, 304 uint n_workers) : 305 AbstractGangTask("G1 Create Live Data"), 306 _live_data(live_data), 307 _hr_claimer(n_workers) { 308 } 309 310 void work(uint worker_id) { 311 SuspendibleThreadSetJoiner sts_join; 312 313 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 314 G1ConcurrentMark* cm = g1h->concurrent_mark(); 315 G1CreateLiveDataClosure cl(g1h, cm, cm->nextMarkBitMap(), _live_data); 316 g1h->heap_region_par_iterate(&cl, worker_id, &_hr_claimer); 317 } 318 }; 319 320 void G1CardLiveData::create(WorkGang* workers, G1CMBitMap* mark_bitmap) { 321 _gc_timestamp_at_create = G1CollectedHeap::heap()->get_gc_time_stamp(); 322 323 uint n_workers = workers->active_workers(); 324 325 G1CreateCardLiveDataTask cl(mark_bitmap, 326 this, 327 n_workers); 328 workers->run_task(&cl); 329 } 330 331 class G1FinalizeCardLiveDataTask: public AbstractGangTask { 332 // Finalizes the liveness counting data. 333 // Sets the bits corresponding to the interval [NTAMS, top] 334 // (which contains the implicitly live objects) in the 335 // card liveness bitmap. Also sets the bit for each region 336 // containing live data, in the region liveness bitmap. 337 class G1FinalizeCardLiveDataClosure: public HeapRegionClosure { 338 private: 339 G1CardLiveDataHelper _helper; 340 341 uint _gc_timestamp_at_create; 342 343 bool has_been_reclaimed(HeapRegion* hr) const { 344 return hr->get_gc_time_stamp() > _gc_timestamp_at_create; 345 } 346 public: 347 G1FinalizeCardLiveDataClosure(G1CollectedHeap* g1h, 348 G1CMBitMap* bitmap, 349 G1CardLiveData* live_data) : 350 HeapRegionClosure(), 351 _helper(live_data, g1h->reserved_region().start()), 352 _gc_timestamp_at_create(live_data->gc_timestamp_at_create()) { } 353 354 bool doHeapRegion(HeapRegion* hr) { 355 if (has_been_reclaimed(hr)) { 356 _helper.reset_live_data(hr); 357 } 358 bool allocated_since_marking = _helper.mark_allocated_since_marking(hr); 359 if (allocated_since_marking || hr->next_marked_bytes() > 0) { 360 _helper.set_bit_for_region(hr); 361 } 362 return false; 363 } 364 }; 365 366 G1CMBitMap* _bitmap; 367 368 G1CardLiveData* _live_data; 369 370 HeapRegionClaimer _hr_claimer; 371 372 public: 373 G1FinalizeCardLiveDataTask(G1CMBitMap* bitmap, G1CardLiveData* live_data, uint n_workers) : 374 AbstractGangTask("G1 Finalize Card Live Data"), 375 _bitmap(bitmap), 376 _live_data(live_data), 377 _hr_claimer(n_workers) { 378 } 379 380 void work(uint worker_id) { 381 G1FinalizeCardLiveDataClosure cl(G1CollectedHeap::heap(), _bitmap, _live_data); 382 383 G1CollectedHeap::heap()->heap_region_par_iterate(&cl, worker_id, &_hr_claimer); 384 } 385 }; 386 387 void G1CardLiveData::finalize(WorkGang* workers, G1CMBitMap* mark_bitmap) { 388 // Finalize the live data. 389 G1FinalizeCardLiveDataTask cl(mark_bitmap, 390 this, 391 workers->active_workers()); 392 workers->run_task(&cl); 393 } 394 395 class G1ClearCardLiveDataTask : public AbstractGangTask { 396 BitMapView _bitmap; 397 size_t _num_chunks; 398 size_t _cur_chunk; 399 public: 400 G1ClearCardLiveDataTask(const BitMapView& bitmap, size_t num_tasks) : 401 AbstractGangTask("G1 Clear Card Live Data"), 402 _bitmap(bitmap), 403 _num_chunks(num_tasks), 404 _cur_chunk(0) { 405 } 406 407 static size_t chunk_size() { return M; } 408 409 virtual void work(uint worker_id) { 410 while (true) { 411 size_t to_process = Atomic::add(1, &_cur_chunk) - 1; 412 if (to_process >= _num_chunks) { 413 break; 414 } 415 416 BitMap::idx_t start = M * BitsPerByte * to_process; 417 BitMap::idx_t end = MIN2(start + M * BitsPerByte, _bitmap.size()); 418 _bitmap.clear_range(start, end); 419 } 420 } 421 }; 422 423 void G1CardLiveData::clear(WorkGang* workers) { 424 guarantee(Universe::is_fully_initialized(), "Should not call this during initialization."); 425 426 size_t const num_chunks = align_size_up(live_cards_bm().size_in_bytes(), G1ClearCardLiveDataTask::chunk_size()) / G1ClearCardLiveDataTask::chunk_size(); 427 uint const num_workers = (uint)MIN2(num_chunks, (size_t)workers->active_workers()); 428 429 G1ClearCardLiveDataTask cl(live_cards_bm(), num_chunks); 430 431 log_debug(gc, ergo)("Running %s using %u workers for " SIZE_FORMAT " work units.", cl.name(), num_workers, num_chunks); 432 workers->run_task(&cl, num_workers); 433 434 // The region live bitmap is always very small, even for huge heaps. Clear 435 // directly. 436 live_regions_bm().clear(); 437 } 438 439 class G1VerifyCardLiveDataTask: public AbstractGangTask { 440 // Heap region closure used for verifying the live count data 441 // that was created concurrently and finalized during 442 // the remark pause. This closure is applied to the heap 443 // regions during the STW cleanup pause. 444 class G1VerifyCardLiveDataClosure: public HeapRegionClosure { 445 private: 446 G1CollectedHeap* _g1h; 447 G1CMBitMap* _mark_bitmap; 448 G1CardLiveDataHelper _helper; 449 450 G1CardLiveData* _act_live_data; 451 452 G1CardLiveData* _exp_live_data; 453 454 int _failures; 455 456 // Completely recreates the live data count for the given heap region and 457 // returns the number of bytes marked. 458 size_t create_live_data_count(HeapRegion* hr) { 459 size_t bytes_marked = _helper.mark_marked_during_marking(_mark_bitmap, hr); 460 bool allocated_since_marking = _helper.mark_allocated_since_marking(hr); 461 if (allocated_since_marking || bytes_marked > 0) { 462 _helper.set_bit_for_region(hr); 463 } 464 return bytes_marked; 465 } 466 public: 467 G1VerifyCardLiveDataClosure(G1CollectedHeap* g1h, 468 G1CMBitMap* mark_bitmap, 469 G1CardLiveData* act_live_data, 470 G1CardLiveData* exp_live_data) : 471 _g1h(g1h), 472 _mark_bitmap(mark_bitmap), 473 _helper(exp_live_data, g1h->reserved_region().start()), 474 _act_live_data(act_live_data), 475 _exp_live_data(exp_live_data), 476 _failures(0) { } 477 478 int failures() const { return _failures; } 479 480 bool doHeapRegion(HeapRegion* hr) { 481 int failures = 0; 482 483 // Walk the marking bitmap for this region and set the corresponding bits 484 // in the expected region and card bitmaps. 485 size_t exp_marked_bytes = create_live_data_count(hr); 486 size_t act_marked_bytes = hr->next_marked_bytes(); 487 // Verify the marked bytes for this region. 488 489 if (exp_marked_bytes != act_marked_bytes) { 490 log_error(gc)("Expected marked bytes " SIZE_FORMAT " != actual marked bytes " SIZE_FORMAT " in region %u", exp_marked_bytes, act_marked_bytes, hr->hrm_index()); 491 failures += 1; 492 } else if (exp_marked_bytes > HeapRegion::GrainBytes) { 493 log_error(gc)("Expected marked bytes " SIZE_FORMAT " larger than possible " SIZE_FORMAT " in region %u", exp_marked_bytes, HeapRegion::GrainBytes, hr->hrm_index()); 494 failures += 1; 495 } 496 497 // Verify the bit, for this region, in the actual and expected 498 // (which was just calculated) region bit maps. 499 uint index = hr->hrm_index(); 500 501 bool expected = _exp_live_data->is_region_live(index); 502 bool actual = _act_live_data->is_region_live(index); 503 if (expected != actual) { 504 log_error(gc)("Expected liveness %d not equal actual %d in region %u", expected, actual, hr->hrm_index()); 505 failures += 1; 506 } 507 508 // Verify that the card bit maps for the cards spanned by the current 509 // region match. 510 BitMap::idx_t start_idx = _helper.card_live_bitmap_index_for(hr->bottom()); 511 BitMap::idx_t end_idx = _helper.card_live_bitmap_index_for(hr->top()); 512 513 for (BitMap::idx_t i = start_idx; i < end_idx; i+=1) { 514 expected = _exp_live_data->is_card_live_at(i); 515 actual = _act_live_data->is_card_live_at(i); 516 517 if (expected != actual) { 518 log_error(gc)("Expected card liveness %d not equal actual card liveness %d at card " SIZE_FORMAT " in region %u", expected, actual, i, hr->hrm_index()); 519 failures += 1; 520 } 521 } 522 523 _failures += failures; 524 525 // We could stop iteration over the heap when we 526 // find the first violating region by returning true. 527 return false; 528 } 529 }; 530 protected: 531 G1CollectedHeap* _g1h; 532 G1CMBitMap* _mark_bitmap; 533 534 G1CardLiveData* _act_live_data; 535 536 G1CardLiveData _exp_live_data; 537 538 int _failures; 539 540 HeapRegionClaimer _hr_claimer; 541 542 public: 543 G1VerifyCardLiveDataTask(G1CMBitMap* bitmap, 544 G1CardLiveData* act_live_data, 545 uint n_workers) 546 : AbstractGangTask("G1 Verify Card Live Data"), 547 _g1h(G1CollectedHeap::heap()), 548 _mark_bitmap(bitmap), 549 _act_live_data(act_live_data), 550 _exp_live_data(), 551 _failures(0), 552 _hr_claimer(n_workers) { 553 assert(VerifyDuringGC, "don't call this otherwise"); 554 _exp_live_data.initialize(_g1h->max_capacity(), _g1h->max_regions()); 555 } 556 557 void work(uint worker_id) { 558 G1VerifyCardLiveDataClosure cl(_g1h, 559 _mark_bitmap, 560 _act_live_data, 561 &_exp_live_data); 562 _g1h->heap_region_par_iterate(&cl, worker_id, &_hr_claimer); 563 564 Atomic::add(cl.failures(), &_failures); 565 } 566 567 int failures() const { return _failures; } 568 }; 569 570 void G1CardLiveData::verify(WorkGang* workers, G1CMBitMap* actual_bitmap) { 571 ResourceMark rm; 572 573 G1VerifyCardLiveDataTask cl(actual_bitmap, 574 this, 575 workers->active_workers()); 576 workers->run_task(&cl); 577 578 guarantee(cl.failures() == 0, "Unexpected accounting failures"); 579 } 580 581 #ifndef PRODUCT 582 void G1CardLiveData::verify_is_clear() { 583 assert(live_cards_bm().count_one_bits() == 0, "Live cards bitmap must be clear."); 584 assert(live_regions_bm().count_one_bits() == 0, "Live regions bitmap must be clear."); 585 } 586 #endif