75 76 void HeapRegionManager::commit_regions(uint index, size_t num_regions) { 77 guarantee(num_regions > 0, "Must commit more than zero regions"); 78 guarantee(_num_committed + num_regions <= max_length(), "Cannot commit more than the maximum amount of regions"); 79 80 _num_committed += (uint)num_regions; 81 82 _heap_mapper->commit_regions(index, num_regions); 83 84 // Also commit auxiliary data 85 _prev_bitmap_mapper->commit_regions(index, num_regions); 86 _next_bitmap_mapper->commit_regions(index, num_regions); 87 88 _bot_mapper->commit_regions(index, num_regions); 89 _cardtable_mapper->commit_regions(index, num_regions); 90 91 _card_counts_mapper->commit_regions(index, num_regions); 92 } 93 94 void HeapRegionManager::uncommit_regions(uint start, size_t num_regions) { 95 guarantee(num_regions >= 1, err_msg("Need to specify at least one region to uncommit, tried to uncommit zero regions at %u", start)); 96 guarantee(_num_committed >= num_regions, "pre-condition"); 97 98 // Print before uncommitting. 99 if (G1CollectedHeap::heap()->hr_printer()->is_active()) { 100 for (uint i = start; i < start + num_regions; i++) { 101 HeapRegion* hr = at(i); 102 G1CollectedHeap::heap()->hr_printer()->uncommit(hr->bottom(), hr->end()); 103 } 104 } 105 106 _num_committed -= (uint)num_regions; 107 108 _available_map.par_clear_range(start, start + num_regions, BitMap::unknown_range); 109 _heap_mapper->uncommit_regions(start, num_regions); 110 111 // Also uncommit auxiliary data 112 _prev_bitmap_mapper->uncommit_regions(start, num_regions); 113 _next_bitmap_mapper->uncommit_regions(start, num_regions); 114 115 _bot_mapper->uncommit_regions(start, num_regions); 116 _cardtable_mapper->uncommit_regions(start, num_regions); 117 118 _card_counts_mapper->uncommit_regions(start, num_regions); 119 } 120 121 void HeapRegionManager::make_regions_available(uint start, uint num_regions) { 122 guarantee(num_regions > 0, "No point in calling this for zero regions"); 123 commit_regions(start, num_regions); 124 for (uint i = start; i < start + num_regions; i++) { 125 if (_regions.get_by_index(i) == NULL) { 126 HeapRegion* new_hr = new_heap_region(i); 127 _regions.set_by_index(i, new_hr); 128 _allocated_heapregions_length = MAX2(_allocated_heapregions_length, i + 1); 129 } 130 } 131 132 _available_map.par_set_range(start, start + num_regions, BitMap::unknown_range); 133 134 for (uint i = start; i < start + num_regions; i++) { 135 assert(is_available(i), err_msg("Just made region %u available but is apparently not.", i)); 136 HeapRegion* hr = at(i); 137 if (G1CollectedHeap::heap()->hr_printer()->is_active()) { 138 G1CollectedHeap::heap()->hr_printer()->commit(hr->bottom(), hr->end()); 139 } 140 HeapWord* bottom = G1CollectedHeap::heap()->bottom_addr_for_region(i); 141 MemRegion mr(bottom, bottom + HeapRegion::GrainWords); 142 143 hr->initialize(mr); 144 insert_into_free_list(at(i)); 145 } 146 } 147 148 MemoryUsage HeapRegionManager::get_auxiliary_data_memory_usage() const { 149 size_t used_sz = 150 _prev_bitmap_mapper->committed_size() + 151 _next_bitmap_mapper->committed_size() + 152 _bot_mapper->committed_size() + 153 _cardtable_mapper->committed_size() + 154 _card_counts_mapper->committed_size(); 155 196 uint cur = 0; 197 198 while (length_found < num && cur < max_length()) { 199 HeapRegion* hr = _regions.get_by_index(cur); 200 if ((!empty_only && !is_available(cur)) || (is_available(cur) && hr != NULL && hr->is_empty())) { 201 // This region is a potential candidate for allocation into. 202 length_found++; 203 } else { 204 // This region is not a candidate. The next region is the next possible one. 205 found = cur + 1; 206 length_found = 0; 207 } 208 cur++; 209 } 210 211 if (length_found == num) { 212 for (uint i = found; i < (found + num); i++) { 213 HeapRegion* hr = _regions.get_by_index(i); 214 // sanity check 215 guarantee((!empty_only && !is_available(i)) || (is_available(i) && hr != NULL && hr->is_empty()), 216 err_msg("Found region sequence starting at " UINT32_FORMAT ", length " SIZE_FORMAT 217 " that is not empty at " UINT32_FORMAT ". Hr is " PTR_FORMAT, found, num, i, p2i(hr))); 218 } 219 return found; 220 } else { 221 return G1_NO_HRM_INDEX; 222 } 223 } 224 225 HeapRegion* HeapRegionManager::next_region_in_heap(const HeapRegion* r) const { 226 guarantee(r != NULL, "Start region must be a valid region"); 227 guarantee(is_available(r->hrm_index()), err_msg("Trying to iterate starting from region %u which is not in the heap", r->hrm_index())); 228 for (uint i = r->hrm_index() + 1; i < _allocated_heapregions_length; i++) { 229 HeapRegion* hr = _regions.get_by_index(i); 230 if (is_available(i)) { 231 return hr; 232 } 233 } 234 return NULL; 235 } 236 237 void HeapRegionManager::iterate(HeapRegionClosure* blk) const { 238 uint len = max_length(); 239 240 for (uint i = 0; i < len; i++) { 241 if (!is_available(i)) { 242 continue; 243 } 244 guarantee(at(i) != NULL, err_msg("Tried to access region %u that has a NULL HeapRegion*", i)); 245 bool res = blk->doHeapRegion(at(i)); 246 if (res) { 247 blk->incomplete(); 248 return; 249 } 250 } 251 } 252 253 uint HeapRegionManager::find_unavailable_from_idx(uint start_idx, uint* res_idx) const { 254 guarantee(res_idx != NULL, "checking"); 255 guarantee(start_idx <= (max_length() + 1), "checking"); 256 257 uint num_regions = 0; 258 259 uint cur = start_idx; 260 while (cur < max_length() && is_available(cur)) { 261 cur++; 262 } 263 if (cur == max_length()) { 264 return num_regions; 265 } 266 *res_idx = cur; 267 while (cur < max_length() && !is_available(cur)) { 268 cur++; 269 } 270 num_regions = cur - *res_idx; 271 #ifdef ASSERT 272 for (uint i = *res_idx; i < (*res_idx + num_regions); i++) { 273 assert(!is_available(i), "just checking"); 274 } 275 assert(cur == max_length() || num_regions == 0 || is_available(cur), 276 err_msg("The region at the current position %u must be available or at the end of the heap.", cur)); 277 #endif 278 return num_regions; 279 } 280 281 uint HeapRegionManager::find_highest_free(bool* expanded) { 282 // Loop downwards from the highest region index, looking for an 283 // entry which is either free or not yet committed. If not yet 284 // committed, expand_at that index. 285 uint curr = max_length() - 1; 286 while (true) { 287 HeapRegion *hr = _regions.get_by_index(curr); 288 if (hr == NULL) { 289 uint res = expand_at(curr, 1); 290 if (res == 1) { 291 *expanded = true; 292 return curr; 293 } 294 } else { 295 if (hr->is_free()) { 296 *expanded = false; 357 if (!hrclaimer->claim_region(index)) { 358 continue; 359 } 360 // Success! 361 // As mentioned above, special treatment of humongous regions can only be 362 // done if we are iterating non-concurrently. 363 if (!concurrent && r->is_starts_humongous()) { 364 // If the region is "starts humongous" we'll iterate over its 365 // "continues humongous" first; in fact we'll do them 366 // first. The order is important. In one case, calling the 367 // closure on the "starts humongous" region might de-allocate 368 // and clear all its "continues humongous" regions and, as a 369 // result, we might end up processing them twice. So, we'll do 370 // them first (note: most closures will ignore them anyway) and 371 // then we'll do the "starts humongous" region. 372 for (uint ch_index = index + 1; ch_index < index + r->region_num(); ch_index++) { 373 HeapRegion* chr = _regions.get_by_index(ch_index); 374 375 assert(chr->is_continues_humongous(), "Must be humongous region"); 376 assert(chr->humongous_start_region() == r, 377 err_msg("Must work on humongous continuation of the original start region " 378 PTR_FORMAT ", but is " PTR_FORMAT, p2i(r), p2i(chr))); 379 assert(!hrclaimer->is_region_claimed(ch_index), 380 "Must not have been claimed yet because claiming of humongous continuation first claims the start region"); 381 382 // Claim the region so no other worker tries to process the region. When a worker processes a 383 // starts_humongous region it may also process the associated continues_humongous regions. 384 // The continues_humongous regions can be changed to free regions. Unless this worker claims 385 // all of these regions, other workers might try claim and process these newly free regions. 386 bool claim_result = hrclaimer->claim_region(ch_index); 387 guarantee(claim_result, "We should always be able to claim the continuesHumongous part of the humongous object"); 388 389 bool res2 = blk->doHeapRegion(chr); 390 if (res2) { 391 return; 392 } 393 394 // Right now, this holds (i.e., no closure that actually 395 // does something with "continues humongous" regions 396 // clears them). We might have to weaken it in the future, 397 // but let's leave these two asserts here for extra safety. 398 assert(chr->is_continues_humongous(), "should still be the case"); 423 uint num_last_found = 0; 424 425 while ((removed < num_regions_to_remove) && 426 (num_last_found = find_empty_from_idx_reverse(cur, &idx_last_found)) > 0) { 427 uint to_remove = MIN2(num_regions_to_remove - removed, num_last_found); 428 429 shrink_at(idx_last_found + num_last_found - to_remove, to_remove); 430 431 cur = idx_last_found; 432 removed += to_remove; 433 } 434 435 verify_optional(); 436 437 return removed; 438 } 439 440 void HeapRegionManager::shrink_at(uint index, size_t num_regions) { 441 #ifdef ASSERT 442 for (uint i = index; i < (index + num_regions); i++) { 443 assert(is_available(i), err_msg("Expected available region at index %u", i)); 444 assert(at(i)->is_empty(), err_msg("Expected empty region at index %u", i)); 445 assert(at(i)->is_free(), err_msg("Expected free region at index %u", i)); 446 } 447 #endif 448 uncommit_regions(index, num_regions); 449 } 450 451 uint HeapRegionManager::find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const { 452 guarantee(start_idx < _allocated_heapregions_length, "checking"); 453 guarantee(res_idx != NULL, "checking"); 454 455 uint num_regions_found = 0; 456 457 jlong cur = start_idx; 458 while (cur != -1 && !(is_available(cur) && at(cur)->is_empty())) { 459 cur--; 460 } 461 if (cur == -1) { 462 return num_regions_found; 463 } 464 jlong old_cur = cur; 465 // cur indexes the first empty region 466 while (cur != -1 && is_available(cur) && at(cur)->is_empty()) { 467 cur--; 468 } 469 *res_idx = cur + 1; 470 num_regions_found = old_cur - cur; 471 472 #ifdef ASSERT 473 for (uint i = *res_idx; i < (*res_idx + num_regions_found); i++) { 474 assert(at(i)->is_empty(), "just checking"); 475 } 476 #endif 477 return num_regions_found; 478 } 479 480 void HeapRegionManager::verify() { 481 guarantee(length() <= _allocated_heapregions_length, 482 err_msg("invariant: _length: %u _allocated_length: %u", 483 length(), _allocated_heapregions_length)); 484 guarantee(_allocated_heapregions_length <= max_length(), 485 err_msg("invariant: _allocated_length: %u _max_length: %u", 486 _allocated_heapregions_length, max_length())); 487 488 bool prev_committed = true; 489 uint num_committed = 0; 490 HeapWord* prev_end = heap_bottom(); 491 for (uint i = 0; i < _allocated_heapregions_length; i++) { 492 if (!is_available(i)) { 493 prev_committed = false; 494 continue; 495 } 496 num_committed++; 497 HeapRegion* hr = _regions.get_by_index(i); 498 guarantee(hr != NULL, err_msg("invariant: i: %u", i)); 499 guarantee(!prev_committed || hr->bottom() == prev_end, 500 err_msg("invariant i: %u " HR_FORMAT " prev_end: " PTR_FORMAT, 501 i, HR_FORMAT_PARAMS(hr), p2i(prev_end))); 502 guarantee(hr->hrm_index() == i, 503 err_msg("invariant: i: %u hrm_index(): %u", i, hr->hrm_index())); 504 // Asserts will fire if i is >= _length 505 HeapWord* addr = hr->bottom(); 506 guarantee(addr_to_region(addr) == hr, "sanity"); 507 // We cannot check whether the region is part of a particular set: at the time 508 // this method may be called, we have only completed allocation of the regions, 509 // but not put into a region set. 510 prev_committed = true; 511 if (hr->is_starts_humongous()) { 512 prev_end = hr->orig_end(); 513 } else { 514 prev_end = hr->end(); 515 } 516 } 517 for (uint i = _allocated_heapregions_length; i < max_length(); i++) { 518 guarantee(_regions.get_by_index(i) == NULL, err_msg("invariant i: %u", i)); 519 } 520 521 guarantee(num_committed == _num_committed, err_msg("Found %u committed regions, but should be %u", num_committed, _num_committed)); 522 _free_list.verify(); 523 } 524 525 #ifndef PRODUCT 526 void HeapRegionManager::verify_optional() { 527 verify(); 528 } 529 #endif // PRODUCT 530 531 HeapRegionClaimer::HeapRegionClaimer(uint n_workers) : 532 _n_workers(n_workers), _n_regions(G1CollectedHeap::heap()->_hrm._allocated_heapregions_length), _claims(NULL) { 533 assert(n_workers > 0, "Need at least one worker."); 534 _claims = NEW_C_HEAP_ARRAY(uint, _n_regions, mtGC); 535 memset(_claims, Unclaimed, sizeof(*_claims) * _n_regions); 536 } 537 538 HeapRegionClaimer::~HeapRegionClaimer() { 539 if (_claims != NULL) { 540 FREE_C_HEAP_ARRAY(uint, _claims); 541 } | 75 76 void HeapRegionManager::commit_regions(uint index, size_t num_regions) { 77 guarantee(num_regions > 0, "Must commit more than zero regions"); 78 guarantee(_num_committed + num_regions <= max_length(), "Cannot commit more than the maximum amount of regions"); 79 80 _num_committed += (uint)num_regions; 81 82 _heap_mapper->commit_regions(index, num_regions); 83 84 // Also commit auxiliary data 85 _prev_bitmap_mapper->commit_regions(index, num_regions); 86 _next_bitmap_mapper->commit_regions(index, num_regions); 87 88 _bot_mapper->commit_regions(index, num_regions); 89 _cardtable_mapper->commit_regions(index, num_regions); 90 91 _card_counts_mapper->commit_regions(index, num_regions); 92 } 93 94 void HeapRegionManager::uncommit_regions(uint start, size_t num_regions) { 95 guarantee(num_regions >= 1, "Need to specify at least one region to uncommit, tried to uncommit zero regions at %u", start); 96 guarantee(_num_committed >= num_regions, "pre-condition"); 97 98 // Print before uncommitting. 99 if (G1CollectedHeap::heap()->hr_printer()->is_active()) { 100 for (uint i = start; i < start + num_regions; i++) { 101 HeapRegion* hr = at(i); 102 G1CollectedHeap::heap()->hr_printer()->uncommit(hr->bottom(), hr->end()); 103 } 104 } 105 106 _num_committed -= (uint)num_regions; 107 108 _available_map.par_clear_range(start, start + num_regions, BitMap::unknown_range); 109 _heap_mapper->uncommit_regions(start, num_regions); 110 111 // Also uncommit auxiliary data 112 _prev_bitmap_mapper->uncommit_regions(start, num_regions); 113 _next_bitmap_mapper->uncommit_regions(start, num_regions); 114 115 _bot_mapper->uncommit_regions(start, num_regions); 116 _cardtable_mapper->uncommit_regions(start, num_regions); 117 118 _card_counts_mapper->uncommit_regions(start, num_regions); 119 } 120 121 void HeapRegionManager::make_regions_available(uint start, uint num_regions) { 122 guarantee(num_regions > 0, "No point in calling this for zero regions"); 123 commit_regions(start, num_regions); 124 for (uint i = start; i < start + num_regions; i++) { 125 if (_regions.get_by_index(i) == NULL) { 126 HeapRegion* new_hr = new_heap_region(i); 127 _regions.set_by_index(i, new_hr); 128 _allocated_heapregions_length = MAX2(_allocated_heapregions_length, i + 1); 129 } 130 } 131 132 _available_map.par_set_range(start, start + num_regions, BitMap::unknown_range); 133 134 for (uint i = start; i < start + num_regions; i++) { 135 assert(is_available(i), "Just made region %u available but is apparently not.", i); 136 HeapRegion* hr = at(i); 137 if (G1CollectedHeap::heap()->hr_printer()->is_active()) { 138 G1CollectedHeap::heap()->hr_printer()->commit(hr->bottom(), hr->end()); 139 } 140 HeapWord* bottom = G1CollectedHeap::heap()->bottom_addr_for_region(i); 141 MemRegion mr(bottom, bottom + HeapRegion::GrainWords); 142 143 hr->initialize(mr); 144 insert_into_free_list(at(i)); 145 } 146 } 147 148 MemoryUsage HeapRegionManager::get_auxiliary_data_memory_usage() const { 149 size_t used_sz = 150 _prev_bitmap_mapper->committed_size() + 151 _next_bitmap_mapper->committed_size() + 152 _bot_mapper->committed_size() + 153 _cardtable_mapper->committed_size() + 154 _card_counts_mapper->committed_size(); 155 196 uint cur = 0; 197 198 while (length_found < num && cur < max_length()) { 199 HeapRegion* hr = _regions.get_by_index(cur); 200 if ((!empty_only && !is_available(cur)) || (is_available(cur) && hr != NULL && hr->is_empty())) { 201 // This region is a potential candidate for allocation into. 202 length_found++; 203 } else { 204 // This region is not a candidate. The next region is the next possible one. 205 found = cur + 1; 206 length_found = 0; 207 } 208 cur++; 209 } 210 211 if (length_found == num) { 212 for (uint i = found; i < (found + num); i++) { 213 HeapRegion* hr = _regions.get_by_index(i); 214 // sanity check 215 guarantee((!empty_only && !is_available(i)) || (is_available(i) && hr != NULL && hr->is_empty()), 216 "Found region sequence starting at " UINT32_FORMAT ", length " SIZE_FORMAT 217 " that is not empty at " UINT32_FORMAT ". Hr is " PTR_FORMAT, found, num, i, p2i(hr)); 218 } 219 return found; 220 } else { 221 return G1_NO_HRM_INDEX; 222 } 223 } 224 225 HeapRegion* HeapRegionManager::next_region_in_heap(const HeapRegion* r) const { 226 guarantee(r != NULL, "Start region must be a valid region"); 227 guarantee(is_available(r->hrm_index()), "Trying to iterate starting from region %u which is not in the heap", r->hrm_index()); 228 for (uint i = r->hrm_index() + 1; i < _allocated_heapregions_length; i++) { 229 HeapRegion* hr = _regions.get_by_index(i); 230 if (is_available(i)) { 231 return hr; 232 } 233 } 234 return NULL; 235 } 236 237 void HeapRegionManager::iterate(HeapRegionClosure* blk) const { 238 uint len = max_length(); 239 240 for (uint i = 0; i < len; i++) { 241 if (!is_available(i)) { 242 continue; 243 } 244 guarantee(at(i) != NULL, "Tried to access region %u that has a NULL HeapRegion*", i); 245 bool res = blk->doHeapRegion(at(i)); 246 if (res) { 247 blk->incomplete(); 248 return; 249 } 250 } 251 } 252 253 uint HeapRegionManager::find_unavailable_from_idx(uint start_idx, uint* res_idx) const { 254 guarantee(res_idx != NULL, "checking"); 255 guarantee(start_idx <= (max_length() + 1), "checking"); 256 257 uint num_regions = 0; 258 259 uint cur = start_idx; 260 while (cur < max_length() && is_available(cur)) { 261 cur++; 262 } 263 if (cur == max_length()) { 264 return num_regions; 265 } 266 *res_idx = cur; 267 while (cur < max_length() && !is_available(cur)) { 268 cur++; 269 } 270 num_regions = cur - *res_idx; 271 #ifdef ASSERT 272 for (uint i = *res_idx; i < (*res_idx + num_regions); i++) { 273 assert(!is_available(i), "just checking"); 274 } 275 assert(cur == max_length() || num_regions == 0 || is_available(cur), 276 "The region at the current position %u must be available or at the end of the heap.", cur); 277 #endif 278 return num_regions; 279 } 280 281 uint HeapRegionManager::find_highest_free(bool* expanded) { 282 // Loop downwards from the highest region index, looking for an 283 // entry which is either free or not yet committed. If not yet 284 // committed, expand_at that index. 285 uint curr = max_length() - 1; 286 while (true) { 287 HeapRegion *hr = _regions.get_by_index(curr); 288 if (hr == NULL) { 289 uint res = expand_at(curr, 1); 290 if (res == 1) { 291 *expanded = true; 292 return curr; 293 } 294 } else { 295 if (hr->is_free()) { 296 *expanded = false; 357 if (!hrclaimer->claim_region(index)) { 358 continue; 359 } 360 // Success! 361 // As mentioned above, special treatment of humongous regions can only be 362 // done if we are iterating non-concurrently. 363 if (!concurrent && r->is_starts_humongous()) { 364 // If the region is "starts humongous" we'll iterate over its 365 // "continues humongous" first; in fact we'll do them 366 // first. The order is important. In one case, calling the 367 // closure on the "starts humongous" region might de-allocate 368 // and clear all its "continues humongous" regions and, as a 369 // result, we might end up processing them twice. So, we'll do 370 // them first (note: most closures will ignore them anyway) and 371 // then we'll do the "starts humongous" region. 372 for (uint ch_index = index + 1; ch_index < index + r->region_num(); ch_index++) { 373 HeapRegion* chr = _regions.get_by_index(ch_index); 374 375 assert(chr->is_continues_humongous(), "Must be humongous region"); 376 assert(chr->humongous_start_region() == r, 377 "Must work on humongous continuation of the original start region " 378 PTR_FORMAT ", but is " PTR_FORMAT, p2i(r), p2i(chr)); 379 assert(!hrclaimer->is_region_claimed(ch_index), 380 "Must not have been claimed yet because claiming of humongous continuation first claims the start region"); 381 382 // Claim the region so no other worker tries to process the region. When a worker processes a 383 // starts_humongous region it may also process the associated continues_humongous regions. 384 // The continues_humongous regions can be changed to free regions. Unless this worker claims 385 // all of these regions, other workers might try claim and process these newly free regions. 386 bool claim_result = hrclaimer->claim_region(ch_index); 387 guarantee(claim_result, "We should always be able to claim the continuesHumongous part of the humongous object"); 388 389 bool res2 = blk->doHeapRegion(chr); 390 if (res2) { 391 return; 392 } 393 394 // Right now, this holds (i.e., no closure that actually 395 // does something with "continues humongous" regions 396 // clears them). We might have to weaken it in the future, 397 // but let's leave these two asserts here for extra safety. 398 assert(chr->is_continues_humongous(), "should still be the case"); 423 uint num_last_found = 0; 424 425 while ((removed < num_regions_to_remove) && 426 (num_last_found = find_empty_from_idx_reverse(cur, &idx_last_found)) > 0) { 427 uint to_remove = MIN2(num_regions_to_remove - removed, num_last_found); 428 429 shrink_at(idx_last_found + num_last_found - to_remove, to_remove); 430 431 cur = idx_last_found; 432 removed += to_remove; 433 } 434 435 verify_optional(); 436 437 return removed; 438 } 439 440 void HeapRegionManager::shrink_at(uint index, size_t num_regions) { 441 #ifdef ASSERT 442 for (uint i = index; i < (index + num_regions); i++) { 443 assert(is_available(i), "Expected available region at index %u", i); 444 assert(at(i)->is_empty(), "Expected empty region at index %u", i); 445 assert(at(i)->is_free(), "Expected free region at index %u", i); 446 } 447 #endif 448 uncommit_regions(index, num_regions); 449 } 450 451 uint HeapRegionManager::find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const { 452 guarantee(start_idx < _allocated_heapregions_length, "checking"); 453 guarantee(res_idx != NULL, "checking"); 454 455 uint num_regions_found = 0; 456 457 jlong cur = start_idx; 458 while (cur != -1 && !(is_available(cur) && at(cur)->is_empty())) { 459 cur--; 460 } 461 if (cur == -1) { 462 return num_regions_found; 463 } 464 jlong old_cur = cur; 465 // cur indexes the first empty region 466 while (cur != -1 && is_available(cur) && at(cur)->is_empty()) { 467 cur--; 468 } 469 *res_idx = cur + 1; 470 num_regions_found = old_cur - cur; 471 472 #ifdef ASSERT 473 for (uint i = *res_idx; i < (*res_idx + num_regions_found); i++) { 474 assert(at(i)->is_empty(), "just checking"); 475 } 476 #endif 477 return num_regions_found; 478 } 479 480 void HeapRegionManager::verify() { 481 guarantee(length() <= _allocated_heapregions_length, 482 "invariant: _length: %u _allocated_length: %u", 483 length(), _allocated_heapregions_length); 484 guarantee(_allocated_heapregions_length <= max_length(), 485 "invariant: _allocated_length: %u _max_length: %u", 486 _allocated_heapregions_length, max_length()); 487 488 bool prev_committed = true; 489 uint num_committed = 0; 490 HeapWord* prev_end = heap_bottom(); 491 for (uint i = 0; i < _allocated_heapregions_length; i++) { 492 if (!is_available(i)) { 493 prev_committed = false; 494 continue; 495 } 496 num_committed++; 497 HeapRegion* hr = _regions.get_by_index(i); 498 guarantee(hr != NULL, "invariant: i: %u", i); 499 guarantee(!prev_committed || hr->bottom() == prev_end, 500 "invariant i: %u " HR_FORMAT " prev_end: " PTR_FORMAT, 501 i, HR_FORMAT_PARAMS(hr), p2i(prev_end)); 502 guarantee(hr->hrm_index() == i, 503 "invariant: i: %u hrm_index(): %u", i, hr->hrm_index()); 504 // Asserts will fire if i is >= _length 505 HeapWord* addr = hr->bottom(); 506 guarantee(addr_to_region(addr) == hr, "sanity"); 507 // We cannot check whether the region is part of a particular set: at the time 508 // this method may be called, we have only completed allocation of the regions, 509 // but not put into a region set. 510 prev_committed = true; 511 if (hr->is_starts_humongous()) { 512 prev_end = hr->orig_end(); 513 } else { 514 prev_end = hr->end(); 515 } 516 } 517 for (uint i = _allocated_heapregions_length; i < max_length(); i++) { 518 guarantee(_regions.get_by_index(i) == NULL, "invariant i: %u", i); 519 } 520 521 guarantee(num_committed == _num_committed, "Found %u committed regions, but should be %u", num_committed, _num_committed); 522 _free_list.verify(); 523 } 524 525 #ifndef PRODUCT 526 void HeapRegionManager::verify_optional() { 527 verify(); 528 } 529 #endif // PRODUCT 530 531 HeapRegionClaimer::HeapRegionClaimer(uint n_workers) : 532 _n_workers(n_workers), _n_regions(G1CollectedHeap::heap()->_hrm._allocated_heapregions_length), _claims(NULL) { 533 assert(n_workers > 0, "Need at least one worker."); 534 _claims = NEW_C_HEAP_ARRAY(uint, _n_regions, mtGC); 535 memset(_claims, Unclaimed, sizeof(*_claims) * _n_regions); 536 } 537 538 HeapRegionClaimer::~HeapRegionClaimer() { 539 if (_claims != NULL) { 540 FREE_C_HEAP_ARRAY(uint, _claims); 541 } |