1 /*
   2  * Copyright (c) 2001, 2019, 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/g1Arguments.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/g1ConcurrentRefine.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 #include "gc/g1/heapRegionManager.inline.hpp"
  31 #include "gc/g1/heapRegionSet.inline.hpp"
  32 #include "gc/g1/heterogeneousHeapRegionManager.hpp"
  33 #include "logging/logStream.hpp"
  34 #include "memory/allocation.hpp"
  35 #include "utilities/bitMap.inline.hpp"
  36 
  37 class MasterFreeRegionListChecker : public HeapRegionSetChecker {
  38 public:
  39   void check_mt_safety() {
  40     // Master Free List MT safety protocol:
  41     // (a) If we're at a safepoint, operations on the master free list
  42     // should be invoked by either the VM thread (which will serialize
  43     // them) or by the GC workers while holding the
  44     // FreeList_lock.
  45     // (b) If we're not at a safepoint, operations on the master free
  46     // list should be invoked while holding the Heap_lock.
  47 
  48     if (SafepointSynchronize::is_at_safepoint()) {
  49       guarantee(Thread::current()->is_VM_thread() ||
  50                 FreeList_lock->owned_by_self(), "master free list MT safety protocol at a safepoint");
  51     } else {
  52       guarantee(Heap_lock->owned_by_self(), "master free list MT safety protocol outside a safepoint");
  53     }
  54   }
  55   bool is_correct_type(HeapRegion* hr) { return hr->is_free(); }
  56   const char* get_description() { return "Free Regions"; }
  57 };
  58 
  59 HeapRegionManager::HeapRegionManager() :
  60   _bot_mapper(NULL),
  61   _cardtable_mapper(NULL),
  62   _card_counts_mapper(NULL),
  63   _available_map(mtGC),
  64   _num_committed(0),
  65   _allocated_heapregions_length(0),
  66   _regions(), _heap_mapper(NULL),
  67   _prev_bitmap_mapper(NULL),
  68   _next_bitmap_mapper(NULL),
  69   _free_list("Free list", new MasterFreeRegionListChecker())
  70 { }
  71 
  72 HeapRegionManager* HeapRegionManager::create_manager(G1CollectedHeap* heap) {
  73   if (G1Arguments::is_heterogeneous_heap()) {
  74     return new HeterogeneousHeapRegionManager((uint)(G1Arguments::heap_max_size_bytes() / HeapRegion::GrainBytes) /*heap size as num of regions*/);
  75   }
  76   return new HeapRegionManager();
  77 }
  78 
  79 void HeapRegionManager::initialize(G1RegionToSpaceMapper* heap_storage,
  80                                G1RegionToSpaceMapper* prev_bitmap,
  81                                G1RegionToSpaceMapper* next_bitmap,
  82                                G1RegionToSpaceMapper* bot,
  83                                G1RegionToSpaceMapper* cardtable,
  84                                G1RegionToSpaceMapper* card_counts) {
  85   _allocated_heapregions_length = 0;
  86 
  87   _heap_mapper = heap_storage;
  88 
  89   _prev_bitmap_mapper = prev_bitmap;
  90   _next_bitmap_mapper = next_bitmap;
  91 
  92   _bot_mapper = bot;
  93   _cardtable_mapper = cardtable;
  94 
  95   _card_counts_mapper = card_counts;
  96 
  97   MemRegion reserved = heap_storage->reserved();
  98   _regions.initialize(reserved.start(), reserved.end(), HeapRegion::GrainBytes);
  99 
 100   _available_map.initialize(_regions.length());
 101 }
 102 
 103 bool HeapRegionManager::is_available(uint region) const {
 104   return _available_map.at(region);
 105 }
 106 
 107 // If log is enabled, compare actual node index and the node index. If those are different
 108 // return the actual node index.
 109 // If log is disabled, just return the node index.
 110 static uint verify_actual_node_index(HeapWord* addr, uint node_index) {
 111   LogTarget(Debug, gc, heap, numa, verification) lt;
 112 
 113   if (lt.is_enabled()) {
 114     LogStream ls(lt);
 115 
 116     uint actual_node_index = G1MemoryNodeManager::mgr()->index_of_address(addr);
 117     if (node_index != actual_node_index) {
 118       ls.print_cr("Heap Region (%u) has different node index. actual index=%u, index=%u",
 119                   G1CollectedHeap::heap()->addr_to_region(addr), actual_node_index, node_index);
 120     }
 121     return actual_node_index;
 122   }
 123   return node_index;
 124 }
 125 
 126 HeapRegion* HeapRegionManager::allocate_free_region(HeapRegionType type, uint requested_node_index) {
 127   G1MemoryNodeManager* mgr = G1MemoryNodeManager::mgr();
 128   HeapRegion* hr = NULL;
 129   bool from_head = !type.is_young();
 130 
 131   if (mgr->num_active_nodes() > 1 && mgr->is_valid_node_index(requested_node_index)) {
 132     // Try to allocate with requested node index.
 133     hr = _free_list.remove_region_with_node_index(from_head, requested_node_index, NULL);
 134   }
 135 
 136   if (hr == NULL) {
 137     // If there's a single active node or we did not get a region from our requested node,
 138     // try without requested node index.
 139     hr = _free_list.remove_region(from_head);
 140   }
 141 
 142   if (hr != NULL) {
 143     assert(hr->next() == NULL, "Single region should not have next");
 144     assert(is_available(hr->hrm_index()), "Must be committed");
 145 
 146     verify_actual_node_index(hr->bottom(), hr->node_index());
 147   }
 148 
 149   return hr;
 150 }
 151 
 152 #ifdef ASSERT
 153 bool HeapRegionManager::is_free(HeapRegion* hr) const {
 154   return _free_list.contains(hr);
 155 }
 156 #endif
 157 
 158 HeapRegion* HeapRegionManager::new_heap_region(uint hrm_index) {
 159   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 160   HeapWord* bottom = g1h->bottom_addr_for_region(hrm_index);
 161   MemRegion mr(bottom, bottom + HeapRegion::GrainWords);
 162   assert(reserved().contains(mr), "invariant");
 163   return g1h->new_heap_region(hrm_index, mr);
 164 }
 165 
 166 void HeapRegionManager::commit_regions(uint index, size_t num_regions, WorkGang* pretouch_gang) {
 167   guarantee(num_regions > 0, "Must commit more than zero regions");
 168   guarantee(_num_committed + num_regions <= max_length(), "Cannot commit more than the maximum amount of regions");
 169 
 170   _num_committed += (uint)num_regions;
 171 
 172   _heap_mapper->commit_regions(index, num_regions, pretouch_gang);
 173 
 174   // Also commit auxiliary data
 175   _prev_bitmap_mapper->commit_regions(index, num_regions, pretouch_gang);
 176   _next_bitmap_mapper->commit_regions(index, num_regions, pretouch_gang);
 177 
 178   _bot_mapper->commit_regions(index, num_regions, pretouch_gang);
 179   _cardtable_mapper->commit_regions(index, num_regions, pretouch_gang);
 180 
 181   _card_counts_mapper->commit_regions(index, num_regions, pretouch_gang);
 182 }
 183 
 184 void HeapRegionManager::uncommit_regions(uint start, size_t num_regions) {
 185   guarantee(num_regions >= 1, "Need to specify at least one region to uncommit, tried to uncommit zero regions at %u", start);
 186   guarantee(_num_committed >= num_regions, "pre-condition");
 187 
 188   // Reset node index to distinguish with committed regions.
 189   for (uint i = start; i < start + num_regions; i++) {
 190     at(i)->set_node_index(G1MemoryNodeManager::InvalidNodeIndex);
 191   }
 192 
 193   // Print before uncommitting.
 194   if (G1CollectedHeap::heap()->hr_printer()->is_active()) {
 195     for (uint i = start; i < start + num_regions; i++) {
 196       HeapRegion* hr = at(i);
 197       G1CollectedHeap::heap()->hr_printer()->uncommit(hr);
 198     }
 199   }
 200 
 201   _num_committed -= (uint)num_regions;
 202 
 203   _available_map.par_clear_range(start, start + num_regions, BitMap::unknown_range);
 204   _heap_mapper->uncommit_regions(start, num_regions);
 205 
 206   // Also uncommit auxiliary data
 207   _prev_bitmap_mapper->uncommit_regions(start, num_regions);
 208   _next_bitmap_mapper->uncommit_regions(start, num_regions);
 209 
 210   _bot_mapper->uncommit_regions(start, num_regions);
 211   _cardtable_mapper->uncommit_regions(start, num_regions);
 212 
 213   _card_counts_mapper->uncommit_regions(start, num_regions);
 214 }
 215 
 216 static void print_node_id_of_regions(uint start, uint num_regions) {
 217   LogTarget(Trace, gc, heap, numa) lt;
 218 
 219   if (lt.is_enabled()) {
 220     LogStream ls(lt);
 221 
 222     // Below logs are checked by TestG1NUMATouchRegions.java.
 223     ls.print_cr("Numa id of heap regions from %u to %u", start, start + num_regions - 1);
 224     ls.print_cr("Heap Region# : numa id of pages");
 225 
 226     for (uint i = start; i < start + num_regions; i++) {
 227       ls.print_cr("%6u : %02u", i, G1CollectedHeap::heap()->region_at(i)->node_index());
 228     }
 229   }
 230 }
 231 
 232 // Set node index of the given HeapRegion.
 233 // If AlwaysPreTouch is enabled, set with actual node index.
 234 // If it is disabled, set with preferred node index which is already decided.
 235 static void set_heapregion_node_index(HeapRegion* hr) {
 236   uint node_index = G1MemoryNodeManager::mgr()->preferred_index_for_address(hr->bottom());
 237   if(AlwaysPreTouch) {
 238     // If we already pretouched, we can check actual node index here.
 239     node_index = verify_actual_node_index(hr->bottom(), node_index);
 240   }
 241   hr->set_node_index(node_index);
 242 }
 243 
 244 void HeapRegionManager::make_regions_available(uint start, uint num_regions, WorkGang* pretouch_gang) {
 245   guarantee(num_regions > 0, "No point in calling this for zero regions");
 246   commit_regions(start, num_regions, pretouch_gang);
 247   for (uint i = start; i < start + num_regions; i++) {
 248     if (_regions.get_by_index(i) == NULL) {
 249       HeapRegion* new_hr = new_heap_region(i);
 250       OrderAccess::storestore();
 251       _regions.set_by_index(i, new_hr);
 252       _allocated_heapregions_length = MAX2(_allocated_heapregions_length, i + 1);
 253     }
 254   }
 255 
 256   _available_map.par_set_range(start, start + num_regions, BitMap::unknown_range);
 257 
 258   for (uint i = start; i < start + num_regions; i++) {
 259     assert(is_available(i), "Just made region %u available but is apparently not.", i);
 260     HeapRegion* hr = at(i);
 261     if (G1CollectedHeap::heap()->hr_printer()->is_active()) {
 262       G1CollectedHeap::heap()->hr_printer()->commit(hr);
 263     }
 264     HeapWord* bottom = G1CollectedHeap::heap()->bottom_addr_for_region(i);
 265     MemRegion mr(bottom, bottom + HeapRegion::GrainWords);
 266 
 267     hr->initialize(mr);
 268     // Set node index of the heap region after initialization but before inserting
 269     // to free list.
 270     set_heapregion_node_index(hr);
 271     insert_into_free_list(at(i));
 272   }
 273 
 274   print_node_id_of_regions(start, num_regions);
 275 }
 276 
 277 MemoryUsage HeapRegionManager::get_auxiliary_data_memory_usage() const {
 278   size_t used_sz =
 279     _prev_bitmap_mapper->committed_size() +
 280     _next_bitmap_mapper->committed_size() +
 281     _bot_mapper->committed_size() +
 282     _cardtable_mapper->committed_size() +
 283     _card_counts_mapper->committed_size();
 284 
 285   size_t committed_sz =
 286     _prev_bitmap_mapper->reserved_size() +
 287     _next_bitmap_mapper->reserved_size() +
 288     _bot_mapper->reserved_size() +
 289     _cardtable_mapper->reserved_size() +
 290     _card_counts_mapper->reserved_size();
 291 
 292   return MemoryUsage(0, used_sz, committed_sz, committed_sz);
 293 }
 294 
 295 uint HeapRegionManager::expand_by(uint num_regions, WorkGang* pretouch_workers) {
 296   return expand_at(0, num_regions, pretouch_workers);
 297 }
 298 
 299 uint HeapRegionManager::expand_at(uint start, uint num_regions, WorkGang* pretouch_workers) {
 300   if (num_regions == 0) {
 301     return 0;
 302   }
 303 
 304   uint cur = start;
 305   uint idx_last_found = 0;
 306   uint num_last_found = 0;
 307 
 308   uint expanded = 0;
 309 
 310   while (expanded < num_regions &&
 311          (num_last_found = find_unavailable_from_idx(cur, &idx_last_found)) > 0) {
 312     uint to_expand = MIN2(num_regions - expanded, num_last_found);
 313     make_regions_available(idx_last_found, to_expand, pretouch_workers);
 314     expanded += to_expand;
 315     cur = idx_last_found + num_last_found + 1;
 316   }
 317 
 318   verify_optional();
 319   return expanded;
 320 }
 321 
 322  uint HeapRegionManager::expand_on_preferred_node(uint preferred_index) {
 323   uint expand_candidate = UINT_MAX;
 324   for (uint i = 0; i < max_length(); i++) {
 325     if (is_available(i)) {
 326       // Already in use continue
 327       continue;
 328     }
 329     // Always save the candidate so we can expand later on.
 330     expand_candidate = i;
 331     if (is_on_preferred_index(expand_candidate, preferred_index)) {
 332       // We have found a candidate on the preffered node, break.
 333       break;
 334     }
 335   }
 336 
 337   if (expand_candidate == UINT_MAX) {
 338      // No regions left, expand failed.
 339     return 0;
 340   }
 341 
 342   make_regions_available(expand_candidate, 1, NULL);
 343   return 1;
 344  }
 345 
 346  bool HeapRegionManager::is_on_preferred_index(uint region_index, uint preferred_node_index) {
 347    uint region_node_index = G1MemoryNodeManager::mgr()->preferred_index_for_address(
 348                              G1CollectedHeap::heap()->bottom_addr_for_region(region_index));
 349   return region_node_index == preferred_node_index ||
 350          preferred_node_index == G1MemoryNodeManager::AnyNodeIndex;
 351  }
 352 
 353 uint HeapRegionManager::find_contiguous(size_t num, bool empty_only) {
 354   uint found = 0;
 355   size_t length_found = 0;
 356   uint cur = 0;
 357 
 358   while (length_found < num && cur < max_length()) {
 359     HeapRegion* hr = _regions.get_by_index(cur);
 360     if ((!empty_only && !is_available(cur)) || (is_available(cur) && hr != NULL && hr->is_empty())) {
 361       // This region is a potential candidate for allocation into.
 362       length_found++;
 363     } else {
 364       // This region is not a candidate. The next region is the next possible one.
 365       found = cur + 1;
 366       length_found = 0;
 367     }
 368     cur++;
 369   }
 370 
 371   if (length_found == num) {
 372     for (uint i = found; i < (found + num); i++) {
 373       HeapRegion* hr = _regions.get_by_index(i);
 374       // sanity check
 375       guarantee((!empty_only && !is_available(i)) || (is_available(i) && hr != NULL && hr->is_empty()),
 376                 "Found region sequence starting at " UINT32_FORMAT ", length " SIZE_FORMAT
 377                 " that is not empty at " UINT32_FORMAT ". Hr is " PTR_FORMAT, found, num, i, p2i(hr));
 378     }
 379     return found;
 380   } else {
 381     return G1_NO_HRM_INDEX;
 382   }
 383 }
 384 
 385 HeapRegion* HeapRegionManager::next_region_in_heap(const HeapRegion* r) const {
 386   guarantee(r != NULL, "Start region must be a valid region");
 387   guarantee(is_available(r->hrm_index()), "Trying to iterate starting from region %u which is not in the heap", r->hrm_index());
 388   for (uint i = r->hrm_index() + 1; i < _allocated_heapregions_length; i++) {
 389     HeapRegion* hr = _regions.get_by_index(i);
 390     if (is_available(i)) {
 391       return hr;
 392     }
 393   }
 394   return NULL;
 395 }
 396 
 397 void HeapRegionManager::iterate(HeapRegionClosure* blk) const {
 398   uint len = max_length();
 399 
 400   for (uint i = 0; i < len; i++) {
 401     if (!is_available(i)) {
 402       continue;
 403     }
 404     guarantee(at(i) != NULL, "Tried to access region %u that has a NULL HeapRegion*", i);
 405     bool res = blk->do_heap_region(at(i));
 406     if (res) {
 407       blk->set_incomplete();
 408       return;
 409     }
 410   }
 411 }
 412 
 413 uint HeapRegionManager::find_unavailable_from_idx(uint start_idx, uint* res_idx) const {
 414   guarantee(res_idx != NULL, "checking");
 415   guarantee(start_idx <= (max_length() + 1), "checking");
 416 
 417   uint num_regions = 0;
 418 
 419   uint cur = start_idx;
 420   while (cur < max_length() && is_available(cur)) {
 421     cur++;
 422   }
 423   if (cur == max_length()) {
 424     return num_regions;
 425   }
 426   *res_idx = cur;
 427   while (cur < max_length() && !is_available(cur)) {
 428     cur++;
 429   }
 430   num_regions = cur - *res_idx;
 431 #ifdef ASSERT
 432   for (uint i = *res_idx; i < (*res_idx + num_regions); i++) {
 433     assert(!is_available(i), "just checking");
 434   }
 435   assert(cur == max_length() || num_regions == 0 || is_available(cur),
 436          "The region at the current position %u must be available or at the end of the heap.", cur);
 437 #endif
 438   return num_regions;
 439 }
 440 
 441 uint HeapRegionManager::find_highest_free(bool* expanded) {
 442   // Loop downwards from the highest region index, looking for an
 443   // entry which is either free or not yet committed.  If not yet
 444   // committed, expand_at that index.
 445   uint curr = max_length() - 1;
 446   while (true) {
 447     HeapRegion *hr = _regions.get_by_index(curr);
 448     if (hr == NULL || !is_available(curr)) {
 449       uint res = expand_at(curr, 1, NULL);
 450       if (res == 1) {
 451         *expanded = true;
 452         return curr;
 453       }
 454     } else {
 455       if (hr->is_free()) {
 456         *expanded = false;
 457         return curr;
 458       }
 459     }
 460     if (curr == 0) {
 461       return G1_NO_HRM_INDEX;
 462     }
 463     curr--;
 464   }
 465 }
 466 
 467 bool HeapRegionManager::allocate_containing_regions(MemRegion range, size_t* commit_count, WorkGang* pretouch_workers) {
 468   size_t commits = 0;
 469   uint start_index = (uint)_regions.get_index_by_address(range.start());
 470   uint last_index = (uint)_regions.get_index_by_address(range.last());
 471 
 472   // Ensure that each G1 region in the range is free, returning false if not.
 473   // Commit those that are not yet available, and keep count.
 474   for (uint curr_index = start_index; curr_index <= last_index; curr_index++) {
 475     if (!is_available(curr_index)) {
 476       commits++;
 477       expand_at(curr_index, 1, pretouch_workers);
 478     }
 479     HeapRegion* curr_region  = _regions.get_by_index(curr_index);
 480     if (!curr_region->is_free()) {
 481       return false;
 482     }
 483   }
 484 
 485   allocate_free_regions_starting_at(start_index, (last_index - start_index) + 1);
 486   *commit_count = commits;
 487   return true;
 488 }
 489 
 490 void HeapRegionManager::par_iterate(HeapRegionClosure* blk, HeapRegionClaimer* hrclaimer, const uint start_index) const {
 491   // Every worker will actually look at all regions, skipping over regions that
 492   // are currently not committed.
 493   // This also (potentially) iterates over regions newly allocated during GC. This
 494   // is no problem except for some extra work.
 495   const uint n_regions = hrclaimer->n_regions();
 496   for (uint count = 0; count < n_regions; count++) {
 497     const uint index = (start_index + count) % n_regions;
 498     assert(index < n_regions, "sanity");
 499     // Skip over unavailable regions
 500     if (!is_available(index)) {
 501       continue;
 502     }
 503     HeapRegion* r = _regions.get_by_index(index);
 504     // We'll ignore regions already claimed.
 505     // However, if the iteration is specified as concurrent, the values for
 506     // is_starts_humongous and is_continues_humongous can not be trusted,
 507     // and we should just blindly iterate over regions regardless of their
 508     // humongous status.
 509     if (hrclaimer->is_region_claimed(index)) {
 510       continue;
 511     }
 512     // OK, try to claim it
 513     if (!hrclaimer->claim_region(index)) {
 514       continue;
 515     }
 516     bool res = blk->do_heap_region(r);
 517     if (res) {
 518       return;
 519     }
 520   }
 521 }
 522 
 523 uint HeapRegionManager::shrink_by(uint num_regions_to_remove) {
 524   assert(length() > 0, "the region sequence should not be empty");
 525   assert(length() <= _allocated_heapregions_length, "invariant");
 526   assert(_allocated_heapregions_length > 0, "we should have at least one region committed");
 527   assert(num_regions_to_remove < length(), "We should never remove all regions");
 528 
 529   if (num_regions_to_remove == 0) {
 530     return 0;
 531   }
 532 
 533   uint removed = 0;
 534   uint cur = _allocated_heapregions_length - 1;
 535   uint idx_last_found = 0;
 536   uint num_last_found = 0;
 537 
 538   while ((removed < num_regions_to_remove) &&
 539       (num_last_found = find_empty_from_idx_reverse(cur, &idx_last_found)) > 0) {
 540     uint to_remove = MIN2(num_regions_to_remove - removed, num_last_found);
 541 
 542     shrink_at(idx_last_found + num_last_found - to_remove, to_remove);
 543 
 544     cur = idx_last_found;
 545     removed += to_remove;
 546   }
 547 
 548   verify_optional();
 549 
 550   return removed;
 551 }
 552 
 553 void HeapRegionManager::shrink_at(uint index, size_t num_regions) {
 554 #ifdef ASSERT
 555   for (uint i = index; i < (index + num_regions); i++) {
 556     assert(is_available(i), "Expected available region at index %u", i);
 557     assert(at(i)->is_empty(), "Expected empty region at index %u", i);
 558     assert(at(i)->is_free(), "Expected free region at index %u", i);
 559   }
 560 #endif
 561   uncommit_regions(index, num_regions);
 562 }
 563 
 564 uint HeapRegionManager::find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const {
 565   guarantee(start_idx < _allocated_heapregions_length, "checking");
 566   guarantee(res_idx != NULL, "checking");
 567 
 568   uint num_regions_found = 0;
 569 
 570   jlong cur = start_idx;
 571   while (cur != -1 && !(is_available(cur) && at(cur)->is_empty())) {
 572     cur--;
 573   }
 574   if (cur == -1) {
 575     return num_regions_found;
 576   }
 577   jlong old_cur = cur;
 578   // cur indexes the first empty region
 579   while (cur != -1 && is_available(cur) && at(cur)->is_empty()) {
 580     cur--;
 581   }
 582   *res_idx = cur + 1;
 583   num_regions_found = old_cur - cur;
 584 
 585 #ifdef ASSERT
 586   for (uint i = *res_idx; i < (*res_idx + num_regions_found); i++) {
 587     assert(at(i)->is_empty(), "just checking");
 588   }
 589 #endif
 590   return num_regions_found;
 591 }
 592 
 593 void HeapRegionManager::verify() {
 594   guarantee(length() <= _allocated_heapregions_length,
 595             "invariant: _length: %u _allocated_length: %u",
 596             length(), _allocated_heapregions_length);
 597   guarantee(_allocated_heapregions_length <= max_length(),
 598             "invariant: _allocated_length: %u _max_length: %u",
 599             _allocated_heapregions_length, max_length());
 600 
 601   bool prev_committed = true;
 602   uint num_committed = 0;
 603   HeapWord* prev_end = heap_bottom();
 604   for (uint i = 0; i < _allocated_heapregions_length; i++) {
 605     if (!is_available(i)) {
 606       prev_committed = false;
 607       continue;
 608     }
 609     num_committed++;
 610     HeapRegion* hr = _regions.get_by_index(i);
 611     guarantee(hr != NULL, "invariant: i: %u", i);
 612     guarantee(!prev_committed || hr->bottom() == prev_end,
 613               "invariant i: %u " HR_FORMAT " prev_end: " PTR_FORMAT,
 614               i, HR_FORMAT_PARAMS(hr), p2i(prev_end));
 615     guarantee(hr->hrm_index() == i,
 616               "invariant: i: %u hrm_index(): %u", i, hr->hrm_index());
 617     // Asserts will fire if i is >= _length
 618     HeapWord* addr = hr->bottom();
 619     guarantee(addr_to_region(addr) == hr, "sanity");
 620     // We cannot check whether the region is part of a particular set: at the time
 621     // this method may be called, we have only completed allocation of the regions,
 622     // but not put into a region set.
 623     prev_committed = true;
 624     prev_end = hr->end();
 625   }
 626   for (uint i = _allocated_heapregions_length; i < max_length(); i++) {
 627     guarantee(_regions.get_by_index(i) == NULL, "invariant i: %u", i);
 628   }
 629 
 630   guarantee(num_committed == _num_committed, "Found %u committed regions, but should be %u", num_committed, _num_committed);
 631   _free_list.verify();
 632 }
 633 
 634 #ifndef PRODUCT
 635 void HeapRegionManager::verify_optional() {
 636   verify();
 637 }
 638 #endif // PRODUCT
 639 
 640 HeapRegionClaimer::HeapRegionClaimer(uint n_workers) :
 641     _n_workers(n_workers), _n_regions(G1CollectedHeap::heap()->_hrm->_allocated_heapregions_length), _claims(NULL) {
 642   assert(n_workers > 0, "Need at least one worker.");
 643   uint* new_claims = NEW_C_HEAP_ARRAY(uint, _n_regions, mtGC);
 644   memset(new_claims, Unclaimed, sizeof(*_claims) * _n_regions);
 645   _claims = new_claims;
 646 }
 647 
 648 HeapRegionClaimer::~HeapRegionClaimer() {
 649   FREE_C_HEAP_ARRAY(uint, _claims);
 650 }
 651 
 652 uint HeapRegionClaimer::offset_for_worker(uint worker_id) const {
 653   assert(worker_id < _n_workers, "Invalid worker_id.");
 654   return _n_regions * worker_id / _n_workers;
 655 }
 656 
 657 bool HeapRegionClaimer::is_region_claimed(uint region_index) const {
 658   assert(region_index < _n_regions, "Invalid index.");
 659   return _claims[region_index] == Claimed;
 660 }
 661 
 662 bool HeapRegionClaimer::claim_region(uint region_index) {
 663   assert(region_index < _n_regions, "Invalid index.");
 664   uint old_val = Atomic::cmpxchg(Claimed, &_claims[region_index], Unclaimed);
 665   return old_val == Unclaimed;
 666 }