heapRegionSeq.cpp 10.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25
#include "precompiled.hpp"
26 27 28
#include "gc_implementation/g1/heapRegion.hpp"
#include "gc_implementation/g1/heapRegionSeq.inline.hpp"
#include "gc_implementation/g1/heapRegionSets.hpp"
29 30
#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
#include "memory/allocation.hpp"
31

32
// Private
33

34 35 36 37 38 39
size_t HeapRegionSeq::find_contiguous_from(size_t from, size_t num) {
  size_t len = length();
  assert(num > 1, "use this only for sequences of length 2 or greater");
  assert(from <= len,
         err_msg("from: "SIZE_FORMAT" should be valid and <= than "SIZE_FORMAT,
                 from, len));
40

41 42
  size_t curr = from;
  size_t first = G1_NULL_HRS_INDEX;
43
  size_t num_so_far = 0;
44 45 46
  while (curr < len && num_so_far < num) {
    if (at(curr)->is_empty()) {
      if (first == G1_NULL_HRS_INDEX) {
47 48 49 50 51 52
        first = curr;
        num_so_far = 1;
      } else {
        num_so_far += 1;
      }
    } else {
53
      first = G1_NULL_HRS_INDEX;
54 55 56 57 58 59
      num_so_far = 0;
    }
    curr += 1;
  }
  assert(num_so_far <= num, "post-condition");
  if (num_so_far == num) {
T
tonyp 已提交
60
    // we found enough space for the humongous object
61 62 63 64
    assert(from <= first && first < len, "post-condition");
    assert(first < curr && (curr - first) == num, "post-condition");
    for (size_t i = first; i < first + num; ++i) {
      assert(at(i)->is_empty(), "post-condition");
65 66 67 68
    }
    return first;
  } else {
    // we failed to find enough space for the humongous object
69
    return G1_NULL_HRS_INDEX;
70 71 72
  }
}

73
// Public
74

75 76 77 78 79 80
void HeapRegionSeq::initialize(HeapWord* bottom, HeapWord* end,
                               size_t max_length) {
  assert((size_t) bottom % HeapRegion::GrainBytes == 0,
         "bottom should be heap region aligned");
  assert((size_t) end % HeapRegion::GrainBytes == 0,
         "end should be heap region aligned");
81

82 83 84 85 86 87 88 89 90 91 92
  _length = 0;
  _heap_bottom = bottom;
  _heap_end = end;
  _region_shift = HeapRegion::LogOfHRGrainBytes;
  _next_search_index = 0;
  _allocated_length = 0;
  _max_length = max_length;

  _regions = NEW_C_HEAP_ARRAY(HeapRegion*, max_length);
  memset(_regions, 0, max_length * sizeof(HeapRegion*));
  _regions_biased = _regions - ((size_t) bottom >> _region_shift);
93

94 95 96
  assert(&_regions[0] == &_regions_biased[addr_to_index_biased(bottom)],
         "bottom should be included in the region with index 0");
}
97

98 99 100 101 102
MemRegion HeapRegionSeq::expand_by(HeapWord* old_end,
                                   HeapWord* new_end,
                                   FreeRegionList* list) {
  assert(old_end < new_end, "don't call it otherwise");
  G1CollectedHeap* g1h = G1CollectedHeap::heap();
103

104 105 106 107 108
  HeapWord* next_bottom = old_end;
  assert(_heap_bottom <= next_bottom, "invariant");
  while (next_bottom < new_end) {
    assert(next_bottom < _heap_end, "invariant");
    size_t index = length();
109

110 111 112 113
    assert(index < _max_length, "otherwise we cannot expand further");
    if (index == 0) {
      // We have not allocated any regions so far
      assert(next_bottom == _heap_bottom, "invariant");
114
    } else {
115 116 117 118 119 120 121 122 123 124
      // next_bottom should match the end of the last/previous region
      assert(next_bottom == at(index - 1)->end(), "invariant");
    }

    if (index == _allocated_length) {
      // We have to allocate a new HeapRegion.
      HeapRegion* new_hr = g1h->new_heap_region(index, next_bottom);
      if (new_hr == NULL) {
        // allocation failed, we bail out and return what we have done so far
        return MemRegion(old_end, next_bottom);
125
      }
126 127 128
      assert(_regions[index] == NULL, "invariant");
      _regions[index] = new_hr;
      increment_length(&_allocated_length);
129
    }
130 131 132 133 134 135 136
    // Have to increment the length first, otherwise we will get an
    // assert failure at(index) below.
    increment_length(&_length);
    HeapRegion* hr = at(index);
    list->add_as_tail(hr);

    next_bottom = hr->end();
137
  }
138 139 140 141 142 143 144 145 146 147 148
  assert(next_bottom == new_end, "post-condition");
  return MemRegion(old_end, next_bottom);
}

size_t HeapRegionSeq::free_suffix() {
  size_t res = 0;
  size_t index = length();
  while (index > 0) {
    index -= 1;
    if (!at(index)->is_empty()) {
      break;
149
    }
150
    res += 1;
151
  }
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  return res;
}

size_t HeapRegionSeq::find_contiguous(size_t num) {
  assert(num > 1, "use this only for sequences of length 2 or greater");
  assert(_next_search_index <= length(),
         err_msg("_next_search_indeex: "SIZE_FORMAT" "
                 "should be valid and <= than "SIZE_FORMAT,
                 _next_search_index, length()));

  size_t start = _next_search_index;
  size_t res = find_contiguous_from(start, num);
  if (res == G1_NULL_HRS_INDEX && start > 0) {
    // Try starting from the beginning. If _next_search_index was 0,
    // no point in doing this again.
    res = find_contiguous_from(0, num);
  }
  if (res != G1_NULL_HRS_INDEX) {
    assert(res < length(),
           err_msg("res: "SIZE_FORMAT" should be valid", res));
    _next_search_index = res + num;
    assert(_next_search_index <= length(),
           err_msg("_next_search_indeex: "SIZE_FORMAT" "
                   "should be valid and <= than "SIZE_FORMAT,
                   _next_search_index, length()));
177
  }
178 179 180 181 182
  return res;
}

void HeapRegionSeq::iterate(HeapRegionClosure* blk) const {
  iterate_from((HeapRegion*) NULL, blk);
183 184
}

185 186 187 188 189 190 191 192 193 194
void HeapRegionSeq::iterate_from(HeapRegion* hr, HeapRegionClosure* blk) const {
  size_t hr_index = 0;
  if (hr != NULL) {
    hr_index = (size_t) hr->hrs_index();
  }

  size_t len = length();
  for (size_t i = hr_index; i < len; i += 1) {
    bool res = blk->doHeapRegion(at(i));
    if (res) {
195 196 197 198
      blk->incomplete();
      return;
    }
  }
199 200 201
  for (size_t i = 0; i < hr_index; i += 1) {
    bool res = blk->doHeapRegion(at(i));
    if (res) {
202 203 204 205 206 207 208
      blk->incomplete();
      return;
    }
  }
}

MemRegion HeapRegionSeq::shrink_by(size_t shrink_bytes,
209
                                   size_t* num_regions_deleted) {
210 211
  // Reset this in case it's currently pointing into the regions that
  // we just removed.
212
  _next_search_index = 0;
213

214 215
  assert(shrink_bytes % os::vm_page_size() == 0, "unaligned");
  assert(shrink_bytes % HeapRegion::GrainBytes == 0, "unaligned");
216 217 218
  assert(length() > 0, "the region sequence should not be empty");
  assert(length() <= _allocated_length, "invariant");
  assert(_allocated_length > 0, "we should have at least one region committed");
219

220 221 222 223 224
  // around the loop, i will be the next region to be removed
  size_t i = length() - 1;
  assert(i > 0, "we should never remove all regions");
  // [last_start, end) is the MemRegion that covers the regions we will remove.
  HeapWord* end = at(i)->end();
225
  HeapWord* last_start = end;
226 227 228 229 230 231
  *num_regions_deleted = 0;
  while (shrink_bytes > 0) {
    HeapRegion* cur = at(i);
    // We should leave the humongous regions where they are.
    if (cur->isHumongous()) break;
    // We should stop shrinking if we come across a non-empty region.
232
    if (!cur->is_empty()) break;
233 234 235

    i -= 1;
    *num_regions_deleted += 1;
236 237
    shrink_bytes -= cur->capacity();
    last_start = cur->bottom();
238 239 240 241 242 243 244
    decrement_length(&_length);
    // We will reclaim the HeapRegion. _allocated_length should be
    // covering this index. So, even though we removed the region from
    // the active set by decreasing _length, we still have it
    // available in the future if we need to re-use it.
    assert(i > 0, "we should never remove all regions");
    assert(length() > 0, "we should never remove all regions");
245 246 247 248
  }
  return MemRegion(last_start, end);
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262
#ifndef PRODUCT
void HeapRegionSeq::verify_optional() {
  guarantee(_length <= _allocated_length,
            err_msg("invariant: _length: "SIZE_FORMAT" "
                    "_allocated_length: "SIZE_FORMAT,
                    _length, _allocated_length));
  guarantee(_allocated_length <= _max_length,
            err_msg("invariant: _allocated_length: "SIZE_FORMAT" "
                    "_max_length: "SIZE_FORMAT,
                    _allocated_length, _max_length));
  guarantee(_next_search_index <= _length,
            err_msg("invariant: _next_search_index: "SIZE_FORMAT" "
                    "_length: "SIZE_FORMAT,
                    _next_search_index, _length));
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  HeapWord* prev_end = _heap_bottom;
  for (size_t i = 0; i < _allocated_length; i += 1) {
    HeapRegion* hr = _regions[i];
    guarantee(hr != NULL, err_msg("invariant: i: "SIZE_FORMAT, i));
    guarantee(hr->bottom() == prev_end,
              err_msg("invariant i: "SIZE_FORMAT" "HR_FORMAT" "
                      "prev_end: "PTR_FORMAT,
                      i, HR_FORMAT_PARAMS(hr), prev_end));
    guarantee(hr->hrs_index() == i,
              err_msg("invariant: i: "SIZE_FORMAT" hrs_index(): "SIZE_FORMAT,
                      i, hr->hrs_index()));
    if (i < _length) {
      // Asserts will fire if i is >= _length
      HeapWord* addr = hr->bottom();
      guarantee(addr_to_region(addr) == hr, "sanity");
      guarantee(addr_to_region_unsafe(addr) == hr, "sanity");
    } else {
      guarantee(hr->is_empty(), "sanity");
      guarantee(!hr->isHumongous(), "sanity");
      // using assert instead of guarantee here since containing_set()
      // is only available in non-product builds.
      assert(hr->containing_set() == NULL, "sanity");
    }
    if (hr->startsHumongous()) {
      prev_end = hr->orig_end();
    } else {
      prev_end = hr->end();
    }
  }
  for (size_t i = _allocated_length; i < _max_length; i += 1) {
    guarantee(_regions[i] == NULL, err_msg("invariant i: "SIZE_FORMAT, i));
  }
296
}
297
#endif // PRODUCT