src/share/vm/gc_implementation/g1/heapRegionSeq.cpp

changeset 7091
a8ea2f110d87
parent 7073
4d3a43351904
child 7092
39189caa2894
     1.1 --- a/src/share/vm/gc_implementation/g1/heapRegionSeq.cpp	Wed Aug 27 09:36:55 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,450 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.
    1.11 - *
    1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 - * version 2 for more details (a copy is included in the LICENSE file that
    1.16 - * accompanied this code).
    1.17 - *
    1.18 - * You should have received a copy of the GNU General Public License version
    1.19 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 - *
    1.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 - * or visit www.oracle.com if you need additional information or have any
    1.24 - * questions.
    1.25 - *
    1.26 - */
    1.27 -
    1.28 -#include "precompiled.hpp"
    1.29 -#include "gc_implementation/g1/heapRegion.hpp"
    1.30 -#include "gc_implementation/g1/heapRegionSeq.inline.hpp"
    1.31 -#include "gc_implementation/g1/heapRegionSet.inline.hpp"
    1.32 -#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
    1.33 -#include "gc_implementation/g1/concurrentG1Refine.hpp"
    1.34 -#include "memory/allocation.hpp"
    1.35 -
    1.36 -void HeapRegionSeq::initialize(G1RegionToSpaceMapper* heap_storage,
    1.37 -                               G1RegionToSpaceMapper* prev_bitmap,
    1.38 -                               G1RegionToSpaceMapper* next_bitmap,
    1.39 -                               G1RegionToSpaceMapper* bot,
    1.40 -                               G1RegionToSpaceMapper* cardtable,
    1.41 -                               G1RegionToSpaceMapper* card_counts) {
    1.42 -  _allocated_heapregions_length = 0;
    1.43 -
    1.44 -  _heap_mapper = heap_storage;
    1.45 -
    1.46 -  _prev_bitmap_mapper = prev_bitmap;
    1.47 -  _next_bitmap_mapper = next_bitmap;
    1.48 -
    1.49 -  _bot_mapper = bot;
    1.50 -  _cardtable_mapper = cardtable;
    1.51 -
    1.52 -  _card_counts_mapper = card_counts;
    1.53 -
    1.54 -  MemRegion reserved = heap_storage->reserved();
    1.55 -  _regions.initialize(reserved.start(), reserved.end(), HeapRegion::GrainBytes);
    1.56 -
    1.57 -  _available_map.resize(_regions.length(), false);
    1.58 -  _available_map.clear();
    1.59 -}
    1.60 -
    1.61 -bool HeapRegionSeq::is_available(uint region) const {
    1.62 -  return _available_map.at(region);
    1.63 -}
    1.64 -
    1.65 -#ifdef ASSERT
    1.66 -bool HeapRegionSeq::is_free(HeapRegion* hr) const {
    1.67 -  return _free_list.contains(hr);
    1.68 -}
    1.69 -#endif
    1.70 -
    1.71 -HeapRegion* HeapRegionSeq::new_heap_region(uint hrs_index) {
    1.72 -  HeapWord* bottom = G1CollectedHeap::heap()->bottom_addr_for_region(hrs_index);
    1.73 -  MemRegion mr(bottom, bottom + HeapRegion::GrainWords);
    1.74 -  assert(reserved().contains(mr), "invariant");
    1.75 -  return new HeapRegion(hrs_index, G1CollectedHeap::heap()->bot_shared(), mr);
    1.76 -}
    1.77 -
    1.78 -void HeapRegionSeq::commit_regions(uint index, size_t num_regions) {
    1.79 -  guarantee(num_regions > 0, "Must commit more than zero regions");
    1.80 -  guarantee(_num_committed + num_regions <= max_length(), "Cannot commit more than the maximum amount of regions");
    1.81 -
    1.82 -  _num_committed += (uint)num_regions;
    1.83 -
    1.84 -  _heap_mapper->commit_regions(index, num_regions);
    1.85 -
    1.86 -  // Also commit auxiliary data
    1.87 -  _prev_bitmap_mapper->commit_regions(index, num_regions);
    1.88 -  _next_bitmap_mapper->commit_regions(index, num_regions);
    1.89 -
    1.90 -  _bot_mapper->commit_regions(index, num_regions);
    1.91 -  _cardtable_mapper->commit_regions(index, num_regions);
    1.92 -
    1.93 -  _card_counts_mapper->commit_regions(index, num_regions);
    1.94 -}
    1.95 -
    1.96 -void HeapRegionSeq::uncommit_regions(uint start, size_t num_regions) {
    1.97 -  guarantee(num_regions >= 1, err_msg("Need to specify at least one region to uncommit, tried to uncommit zero regions at %u", start));
    1.98 -  guarantee(_num_committed >= num_regions, "pre-condition");
    1.99 -
   1.100 -  // Print before uncommitting.
   1.101 -  if (G1CollectedHeap::heap()->hr_printer()->is_active()) {
   1.102 -    for (uint i = start; i < start + num_regions; i++) {
   1.103 -      HeapRegion* hr = at(i);
   1.104 -      G1CollectedHeap::heap()->hr_printer()->uncommit(hr->bottom(), hr->end());
   1.105 -    }
   1.106 -  }
   1.107 -
   1.108 -  _num_committed -= (uint)num_regions;
   1.109 -
   1.110 -  _available_map.par_clear_range(start, start + num_regions, BitMap::unknown_range);
   1.111 -  _heap_mapper->uncommit_regions(start, num_regions);
   1.112 -
   1.113 -  // Also uncommit auxiliary data
   1.114 -  _prev_bitmap_mapper->uncommit_regions(start, num_regions);
   1.115 -  _next_bitmap_mapper->uncommit_regions(start, num_regions);
   1.116 -
   1.117 -  _bot_mapper->uncommit_regions(start, num_regions);
   1.118 -  _cardtable_mapper->uncommit_regions(start, num_regions);
   1.119 -
   1.120 -  _card_counts_mapper->uncommit_regions(start, num_regions);
   1.121 -}
   1.122 -
   1.123 -void HeapRegionSeq::make_regions_available(uint start, uint num_regions) {
   1.124 -  guarantee(num_regions > 0, "No point in calling this for zero regions");
   1.125 -  commit_regions(start, num_regions);
   1.126 -  for (uint i = start; i < start + num_regions; i++) {
   1.127 -    if (_regions.get_by_index(i) == NULL) {
   1.128 -      HeapRegion* new_hr = new_heap_region(i);
   1.129 -      _regions.set_by_index(i, new_hr);
   1.130 -      _allocated_heapregions_length = MAX2(_allocated_heapregions_length, i + 1);
   1.131 -    }
   1.132 -  }
   1.133 -
   1.134 -  _available_map.par_set_range(start, start + num_regions, BitMap::unknown_range);
   1.135 -
   1.136 -  for (uint i = start; i < start + num_regions; i++) {
   1.137 -    assert(is_available(i), err_msg("Just made region %u available but is apparently not.", i));
   1.138 -    HeapRegion* hr = at(i);
   1.139 -    if (G1CollectedHeap::heap()->hr_printer()->is_active()) {
   1.140 -      G1CollectedHeap::heap()->hr_printer()->commit(hr->bottom(), hr->end());
   1.141 -    }
   1.142 -    HeapWord* bottom = G1CollectedHeap::heap()->bottom_addr_for_region(i);
   1.143 -    MemRegion mr(bottom, bottom + HeapRegion::GrainWords);
   1.144 -
   1.145 -    hr->initialize(mr);
   1.146 -    insert_into_free_list(at(i));
   1.147 -  }
   1.148 -}
   1.149 -
   1.150 -uint HeapRegionSeq::expand_by(uint num_regions) {
   1.151 -  return expand_at(0, num_regions);
   1.152 -}
   1.153 -
   1.154 -uint HeapRegionSeq::expand_at(uint start, uint num_regions) {
   1.155 -  if (num_regions == 0) {
   1.156 -    return 0;
   1.157 -  }
   1.158 -
   1.159 -  uint cur = start;
   1.160 -  uint idx_last_found = 0;
   1.161 -  uint num_last_found = 0;
   1.162 -
   1.163 -  uint expanded = 0;
   1.164 -
   1.165 -  while (expanded < num_regions &&
   1.166 -         (num_last_found = find_unavailable_from_idx(cur, &idx_last_found)) > 0) {
   1.167 -    uint to_expand = MIN2(num_regions - expanded, num_last_found);
   1.168 -    make_regions_available(idx_last_found, to_expand);
   1.169 -    expanded += to_expand;
   1.170 -    cur = idx_last_found + num_last_found + 1;
   1.171 -  }
   1.172 -
   1.173 -  verify_optional();
   1.174 -  return expanded;
   1.175 -}
   1.176 -
   1.177 -uint HeapRegionSeq::find_contiguous(size_t num, bool empty_only) {
   1.178 -  uint found = 0;
   1.179 -  size_t length_found = 0;
   1.180 -  uint cur = 0;
   1.181 -
   1.182 -  while (length_found < num && cur < max_length()) {
   1.183 -    HeapRegion* hr = _regions.get_by_index(cur);
   1.184 -    if ((!empty_only && !is_available(cur)) || (is_available(cur) && hr != NULL && hr->is_empty())) {
   1.185 -      // This region is a potential candidate for allocation into.
   1.186 -      length_found++;
   1.187 -    } else {
   1.188 -      // This region is not a candidate. The next region is the next possible one.
   1.189 -      found = cur + 1;
   1.190 -      length_found = 0;
   1.191 -    }
   1.192 -    cur++;
   1.193 -  }
   1.194 -
   1.195 -  if (length_found == num) {
   1.196 -    for (uint i = found; i < (found + num); i++) {
   1.197 -      HeapRegion* hr = _regions.get_by_index(i);
   1.198 -      // sanity check
   1.199 -      guarantee((!empty_only && !is_available(i)) || (is_available(i) && hr != NULL && hr->is_empty()),
   1.200 -                err_msg("Found region sequence starting at " UINT32_FORMAT ", length " SIZE_FORMAT
   1.201 -                        " that is not empty at " UINT32_FORMAT ". Hr is " PTR_FORMAT, found, num, i, p2i(hr)));
   1.202 -    }
   1.203 -    return found;
   1.204 -  } else {
   1.205 -    return G1_NO_HRS_INDEX;
   1.206 -  }
   1.207 -}
   1.208 -
   1.209 -HeapRegion* HeapRegionSeq::next_region_in_heap(const HeapRegion* r) const {
   1.210 -  guarantee(r != NULL, "Start region must be a valid region");
   1.211 -  guarantee(is_available(r->hrs_index()), err_msg("Trying to iterate starting from region %u which is not in the heap", r->hrs_index()));
   1.212 -  for (uint i = r->hrs_index() + 1; i < _allocated_heapregions_length; i++) {
   1.213 -    HeapRegion* hr = _regions.get_by_index(i);
   1.214 -    if (is_available(i)) {
   1.215 -      return hr;
   1.216 -    }
   1.217 -  }
   1.218 -  return NULL;
   1.219 -}
   1.220 -
   1.221 -void HeapRegionSeq::iterate(HeapRegionClosure* blk) const {
   1.222 -  uint len = max_length();
   1.223 -
   1.224 -  for (uint i = 0; i < len; i++) {
   1.225 -    if (!is_available(i)) {
   1.226 -      continue;
   1.227 -    }
   1.228 -    guarantee(at(i) != NULL, err_msg("Tried to access region %u that has a NULL HeapRegion*", i));
   1.229 -    bool res = blk->doHeapRegion(at(i));
   1.230 -    if (res) {
   1.231 -      blk->incomplete();
   1.232 -      return;
   1.233 -    }
   1.234 -  }
   1.235 -}
   1.236 -
   1.237 -uint HeapRegionSeq::find_unavailable_from_idx(uint start_idx, uint* res_idx) const {
   1.238 -  guarantee(res_idx != NULL, "checking");
   1.239 -  guarantee(start_idx <= (max_length() + 1), "checking");
   1.240 -
   1.241 -  uint num_regions = 0;
   1.242 -
   1.243 -  uint cur = start_idx;
   1.244 -  while (cur < max_length() && is_available(cur)) {
   1.245 -    cur++;
   1.246 -  }
   1.247 -  if (cur == max_length()) {
   1.248 -    return num_regions;
   1.249 -  }
   1.250 -  *res_idx = cur;
   1.251 -  while (cur < max_length() && !is_available(cur)) {
   1.252 -    cur++;
   1.253 -  }
   1.254 -  num_regions = cur - *res_idx;
   1.255 -#ifdef ASSERT
   1.256 -  for (uint i = *res_idx; i < (*res_idx + num_regions); i++) {
   1.257 -    assert(!is_available(i), "just checking");
   1.258 -  }
   1.259 -  assert(cur == max_length() || num_regions == 0 || is_available(cur),
   1.260 -         err_msg("The region at the current position %u must be available or at the end of the heap.", cur));
   1.261 -#endif
   1.262 -  return num_regions;
   1.263 -}
   1.264 -
   1.265 -uint HeapRegionSeq::start_region_for_worker(uint worker_i, uint num_workers, uint num_regions) const {
   1.266 -  return num_regions * worker_i / num_workers;
   1.267 -}
   1.268 -
   1.269 -void HeapRegionSeq::par_iterate(HeapRegionClosure* blk, uint worker_id, uint num_workers, jint claim_value) const {
   1.270 -  const uint start_index = start_region_for_worker(worker_id, num_workers, _allocated_heapregions_length);
   1.271 -
   1.272 -  // Every worker will actually look at all regions, skipping over regions that
   1.273 -  // are currently not committed.
   1.274 -  // This also (potentially) iterates over regions newly allocated during GC. This
   1.275 -  // is no problem except for some extra work.
   1.276 -  for (uint count = 0; count < _allocated_heapregions_length; count++) {
   1.277 -    const uint index = (start_index + count) % _allocated_heapregions_length;
   1.278 -    assert(0 <= index && index < _allocated_heapregions_length, "sanity");
   1.279 -    // Skip over unavailable regions
   1.280 -    if (!is_available(index)) {
   1.281 -      continue;
   1.282 -    }
   1.283 -    HeapRegion* r = _regions.get_by_index(index);
   1.284 -    // We'll ignore "continues humongous" regions (we'll process them
   1.285 -    // when we come across their corresponding "start humongous"
   1.286 -    // region) and regions already claimed.
   1.287 -    if (r->claim_value() == claim_value || r->continuesHumongous()) {
   1.288 -      continue;
   1.289 -    }
   1.290 -    // OK, try to claim it
   1.291 -    if (!r->claimHeapRegion(claim_value)) {
   1.292 -      continue;
   1.293 -    }
   1.294 -    // Success!
   1.295 -    if (r->startsHumongous()) {
   1.296 -      // If the region is "starts humongous" we'll iterate over its
   1.297 -      // "continues humongous" first; in fact we'll do them
   1.298 -      // first. The order is important. In one case, calling the
   1.299 -      // closure on the "starts humongous" region might de-allocate
   1.300 -      // and clear all its "continues humongous" regions and, as a
   1.301 -      // result, we might end up processing them twice. So, we'll do
   1.302 -      // them first (note: most closures will ignore them anyway) and
   1.303 -      // then we'll do the "starts humongous" region.
   1.304 -      for (uint ch_index = index + 1; ch_index < index + r->region_num(); ch_index++) {
   1.305 -        HeapRegion* chr = _regions.get_by_index(ch_index);
   1.306 -
   1.307 -        assert(chr->continuesHumongous(), "Must be humongous region");
   1.308 -        assert(chr->humongous_start_region() == r,
   1.309 -               err_msg("Must work on humongous continuation of the original start region "
   1.310 -                       PTR_FORMAT ", but is " PTR_FORMAT, p2i(r), p2i(chr)));
   1.311 -        assert(chr->claim_value() != claim_value,
   1.312 -               "Must not have been claimed yet because claiming of humongous continuation first claims the start region");
   1.313 -
   1.314 -        bool claim_result = chr->claimHeapRegion(claim_value);
   1.315 -        // We should always be able to claim it; no one else should
   1.316 -        // be trying to claim this region.
   1.317 -        guarantee(claim_result, "We should always be able to claim the continuesHumongous part of the humongous object");
   1.318 -
   1.319 -        bool res2 = blk->doHeapRegion(chr);
   1.320 -        if (res2) {
   1.321 -          return;
   1.322 -        }
   1.323 -
   1.324 -        // Right now, this holds (i.e., no closure that actually
   1.325 -        // does something with "continues humongous" regions
   1.326 -        // clears them). We might have to weaken it in the future,
   1.327 -        // but let's leave these two asserts here for extra safety.
   1.328 -        assert(chr->continuesHumongous(), "should still be the case");
   1.329 -        assert(chr->humongous_start_region() == r, "sanity");
   1.330 -      }
   1.331 -    }
   1.332 -
   1.333 -    bool res = blk->doHeapRegion(r);
   1.334 -    if (res) {
   1.335 -      return;
   1.336 -    }
   1.337 -  }
   1.338 -}
   1.339 -
   1.340 -uint HeapRegionSeq::shrink_by(uint num_regions_to_remove) {
   1.341 -  assert(length() > 0, "the region sequence should not be empty");
   1.342 -  assert(length() <= _allocated_heapregions_length, "invariant");
   1.343 -  assert(_allocated_heapregions_length > 0, "we should have at least one region committed");
   1.344 -  assert(num_regions_to_remove < length(), "We should never remove all regions");
   1.345 -
   1.346 -  if (num_regions_to_remove == 0) {
   1.347 -    return 0;
   1.348 -  }
   1.349 -
   1.350 -  uint removed = 0;
   1.351 -  uint cur = _allocated_heapregions_length - 1;
   1.352 -  uint idx_last_found = 0;
   1.353 -  uint num_last_found = 0;
   1.354 -
   1.355 -  while ((removed < num_regions_to_remove) &&
   1.356 -      (num_last_found = find_empty_from_idx_reverse(cur, &idx_last_found)) > 0) {
   1.357 -    // Only allow uncommit from the end of the heap.
   1.358 -    if ((idx_last_found + num_last_found) != _allocated_heapregions_length) {
   1.359 -      return 0;
   1.360 -    }
   1.361 -    uint to_remove = MIN2(num_regions_to_remove - removed, num_last_found);
   1.362 -
   1.363 -    uncommit_regions(idx_last_found + num_last_found - to_remove, to_remove);
   1.364 -
   1.365 -    cur -= num_last_found;
   1.366 -    removed += to_remove;
   1.367 -  }
   1.368 -
   1.369 -  verify_optional();
   1.370 -
   1.371 -  return removed;
   1.372 -}
   1.373 -
   1.374 -uint HeapRegionSeq::find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const {
   1.375 -  guarantee(start_idx < _allocated_heapregions_length, "checking");
   1.376 -  guarantee(res_idx != NULL, "checking");
   1.377 -
   1.378 -  uint num_regions_found = 0;
   1.379 -
   1.380 -  jlong cur = start_idx;
   1.381 -  while (cur != -1 && !(is_available(cur) && at(cur)->is_empty())) {
   1.382 -    cur--;
   1.383 -  }
   1.384 -  if (cur == -1) {
   1.385 -    return num_regions_found;
   1.386 -  }
   1.387 -  jlong old_cur = cur;
   1.388 -  // cur indexes the first empty region
   1.389 -  while (cur != -1 && is_available(cur) && at(cur)->is_empty()) {
   1.390 -    cur--;
   1.391 -  }
   1.392 -  *res_idx = cur + 1;
   1.393 -  num_regions_found = old_cur - cur;
   1.394 -
   1.395 -#ifdef ASSERT
   1.396 -  for (uint i = *res_idx; i < (*res_idx + num_regions_found); i++) {
   1.397 -    assert(at(i)->is_empty(), "just checking");
   1.398 -  }
   1.399 -#endif
   1.400 -  return num_regions_found;
   1.401 -}
   1.402 -
   1.403 -void HeapRegionSeq::verify() {
   1.404 -  guarantee(length() <= _allocated_heapregions_length,
   1.405 -            err_msg("invariant: _length: %u _allocated_length: %u",
   1.406 -                    length(), _allocated_heapregions_length));
   1.407 -  guarantee(_allocated_heapregions_length <= max_length(),
   1.408 -            err_msg("invariant: _allocated_length: %u _max_length: %u",
   1.409 -                    _allocated_heapregions_length, max_length()));
   1.410 -
   1.411 -  bool prev_committed = true;
   1.412 -  uint num_committed = 0;
   1.413 -  HeapWord* prev_end = heap_bottom();
   1.414 -  for (uint i = 0; i < _allocated_heapregions_length; i++) {
   1.415 -    if (!is_available(i)) {
   1.416 -      prev_committed = false;
   1.417 -      continue;
   1.418 -    }
   1.419 -    num_committed++;
   1.420 -    HeapRegion* hr = _regions.get_by_index(i);
   1.421 -    guarantee(hr != NULL, err_msg("invariant: i: %u", i));
   1.422 -    guarantee(!prev_committed || hr->bottom() == prev_end,
   1.423 -              err_msg("invariant i: %u "HR_FORMAT" prev_end: "PTR_FORMAT,
   1.424 -                      i, HR_FORMAT_PARAMS(hr), p2i(prev_end)));
   1.425 -    guarantee(hr->hrs_index() == i,
   1.426 -              err_msg("invariant: i: %u hrs_index(): %u", i, hr->hrs_index()));
   1.427 -    // Asserts will fire if i is >= _length
   1.428 -    HeapWord* addr = hr->bottom();
   1.429 -    guarantee(addr_to_region(addr) == hr, "sanity");
   1.430 -    // We cannot check whether the region is part of a particular set: at the time
   1.431 -    // this method may be called, we have only completed allocation of the regions,
   1.432 -    // but not put into a region set.
   1.433 -    prev_committed = true;
   1.434 -    if (hr->startsHumongous()) {
   1.435 -      prev_end = hr->orig_end();
   1.436 -    } else {
   1.437 -      prev_end = hr->end();
   1.438 -    }
   1.439 -  }
   1.440 -  for (uint i = _allocated_heapregions_length; i < max_length(); i++) {
   1.441 -    guarantee(_regions.get_by_index(i) == NULL, err_msg("invariant i: %u", i));
   1.442 -  }
   1.443 -
   1.444 -  guarantee(num_committed == _num_committed, err_msg("Found %u committed regions, but should be %u", num_committed, _num_committed));
   1.445 -  _free_list.verify();
   1.446 -}
   1.447 -
   1.448 -#ifndef PRODUCT
   1.449 -void HeapRegionSeq::verify_optional() {
   1.450 -  verify();
   1.451 -}
   1.452 -#endif // PRODUCT
   1.453 -

mercurial