parMarkBitMap.cpp 8.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
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.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "precompiled.hpp"
#include "gc_implementation/parallelScavenge/parMarkBitMap.hpp"
#include "gc_implementation/parallelScavenge/parMarkBitMap.inline.hpp"
#include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/os.hpp"
#include "utilities/bitMap.inline.hpp"
#ifdef TARGET_OS_FAMILY_linux
# include "os_linux.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_solaris
# include "os_solaris.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_windows
# include "os_windows.inline.hpp"
#endif
N
never 已提交
41 42 43
#ifdef TARGET_OS_FAMILY_bsd
# include "os_bsd.inline.hpp"
#endif
D
duke 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

bool
ParMarkBitMap::initialize(MemRegion covered_region)
{
  const idx_t bits = bits_required(covered_region);
  // The bits will be divided evenly between two bitmaps; each of them should be
  // an integral number of words.
  assert(bits % (BitsPerWord * 2) == 0, "region size unaligned");

  const size_t words = bits / BitsPerWord;
  const size_t raw_bytes = words * sizeof(idx_t);
  const size_t page_sz = os::page_size_for_region(raw_bytes, raw_bytes, 10);
  const size_t granularity = os::vm_allocation_granularity();
  const size_t bytes = align_size_up(raw_bytes, MAX2(page_sz, granularity));

  const size_t rs_align = page_sz == (size_t) os::vm_page_size() ? 0 :
    MAX2(page_sz, granularity);
61
  ReservedSpace rs(bytes, rs_align, rs_align > 0);
D
duke 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  os::trace_page_sizes("par bitmap", raw_bytes, raw_bytes, page_sz,
                       rs.base(), rs.size());
  _virtual_space = new PSVirtualSpace(rs, page_sz);
  if (_virtual_space != NULL && _virtual_space->expand_by(bytes)) {
    _region_start = covered_region.start();
    _region_size = covered_region.word_size();
    idx_t* map = (idx_t*)_virtual_space->reserved_low_addr();
    _beg_bits.set_map(map);
    _beg_bits.set_size(bits / 2);
    _end_bits.set_map(map + words / 2);
    _end_bits.set_size(bits / 2);
    return true;
  }

  _region_start = 0;
  _region_size = 0;
  if (_virtual_space != NULL) {
    delete _virtual_space;
    _virtual_space = NULL;
81 82
    // Release memory reserved in the space.
    rs.release();
D
duke 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  }
  return false;
}

#ifdef ASSERT
extern size_t mark_bitmap_count;
extern size_t mark_bitmap_size;
#endif  // #ifdef ASSERT

bool
ParMarkBitMap::mark_obj(HeapWord* addr, size_t size)
{
  const idx_t beg_bit = addr_to_bit(addr);
  if (_beg_bits.par_set_bit(beg_bit)) {
    const idx_t end_bit = addr_to_bit(addr + size - 1);
    bool end_bit_ok = _end_bits.par_set_bit(end_bit);
    assert(end_bit_ok, "concurrency problem");
    DEBUG_ONLY(Atomic::inc_ptr(&mark_bitmap_count));
    DEBUG_ONLY(Atomic::add_ptr(size, &mark_bitmap_size));
    return true;
  }
  return false;
}

size_t
ParMarkBitMap::live_words_in_range(HeapWord* beg_addr, HeapWord* end_addr) const
{
  assert(beg_addr <= end_addr, "bad range");

  idx_t live_bits = 0;

  // The bitmap routines require the right boundary to be word-aligned.
  const idx_t end_bit = addr_to_bit(end_addr);
  const idx_t range_end = BitMap::word_align_up(end_bit);

  idx_t beg_bit = find_obj_beg(addr_to_bit(beg_addr), range_end);
  while (beg_bit < end_bit) {
    idx_t tmp_end = find_obj_end(beg_bit, range_end);
    if (tmp_end < end_bit) {
      live_bits += tmp_end - beg_bit + 1;
      beg_bit = find_obj_beg(tmp_end + 1, range_end);
    } else {
      live_bits += end_bit - beg_bit;  // No + 1 here; end_bit is not counted.
      return bits_to_words(live_bits);
    }
  }
  return bits_to_words(live_bits);
}

size_t ParMarkBitMap::live_words_in_range(HeapWord* beg_addr, oop end_obj) const
{
  assert(beg_addr <= (HeapWord*)end_obj, "bad range");
  assert(is_marked(end_obj), "end_obj must be live");

  idx_t live_bits = 0;

  // The bitmap routines require the right boundary to be word-aligned.
  const idx_t end_bit = addr_to_bit((HeapWord*)end_obj);
  const idx_t range_end = BitMap::word_align_up(end_bit);

  idx_t beg_bit = find_obj_beg(addr_to_bit(beg_addr), range_end);
  while (beg_bit < end_bit) {
    idx_t tmp_end = find_obj_end(beg_bit, range_end);
    assert(tmp_end < end_bit, "missing end bit");
    live_bits += tmp_end - beg_bit + 1;
    beg_bit = find_obj_beg(tmp_end + 1, range_end);
  }
  return bits_to_words(live_bits);
}

ParMarkBitMap::IterationStatus
ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,
                       idx_t range_beg, idx_t range_end) const
{
  DEBUG_ONLY(verify_bit(range_beg);)
  DEBUG_ONLY(verify_bit(range_end);)
  assert(range_beg <= range_end, "live range invalid");

  // The bitmap routines require the right boundary to be word-aligned.
  const idx_t search_end = BitMap::word_align_up(range_end);

  idx_t cur_beg = find_obj_beg(range_beg, search_end);
  while (cur_beg < range_end) {
    const idx_t cur_end = find_obj_end(cur_beg, search_end);
    if (cur_end >= range_end) {
      // The obj ends outside the range.
      live_closure->set_source(bit_to_addr(cur_beg));
      return incomplete;
    }

    const size_t size = obj_size(cur_beg, cur_end);
    IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);
    if (status != incomplete) {
      assert(status == would_overflow || status == full, "sanity");
      return status;
    }

    // Successfully processed the object; look for the next object.
    cur_beg = find_obj_beg(cur_end + 1, search_end);
  }

  live_closure->set_source(bit_to_addr(range_end));
  return complete;
}

ParMarkBitMap::IterationStatus
ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,
                       ParMarkBitMapClosure* dead_closure,
                       idx_t range_beg, idx_t range_end,
                       idx_t dead_range_end) const
{
  DEBUG_ONLY(verify_bit(range_beg);)
  DEBUG_ONLY(verify_bit(range_end);)
  DEBUG_ONLY(verify_bit(dead_range_end);)
  assert(range_beg <= range_end, "live range invalid");
  assert(range_end <= dead_range_end, "dead range invalid");

  // The bitmap routines require the right boundary to be word-aligned.
  const idx_t live_search_end = BitMap::word_align_up(range_end);
  const idx_t dead_search_end = BitMap::word_align_up(dead_range_end);

  idx_t cur_beg = range_beg;
  if (range_beg < range_end && is_unmarked(range_beg)) {
    // The range starts with dead space.  Look for the next object, then fill.
    cur_beg = find_obj_beg(range_beg + 1, dead_search_end);
    const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);
    const size_t size = obj_size(range_beg, dead_space_end);
    dead_closure->do_addr(bit_to_addr(range_beg), size);
  }

  while (cur_beg < range_end) {
    const idx_t cur_end = find_obj_end(cur_beg, live_search_end);
    if (cur_end >= range_end) {
      // The obj ends outside the range.
      live_closure->set_source(bit_to_addr(cur_beg));
      return incomplete;
    }

    const size_t size = obj_size(cur_beg, cur_end);
    IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);
    if (status != incomplete) {
      assert(status == would_overflow || status == full, "sanity");
      return status;
    }

    // Look for the start of the next object.
    const idx_t dead_space_beg = cur_end + 1;
    cur_beg = find_obj_beg(dead_space_beg, dead_search_end);
    if (cur_beg > dead_space_beg) {
      // Found dead space; compute the size and invoke the dead closure.
      const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);
      const size_t size = obj_size(dead_space_beg, dead_space_end);
      dead_closure->do_addr(bit_to_addr(dead_space_beg), size);
    }
  }

  live_closure->set_source(bit_to_addr(range_end));
  return complete;
}

#ifndef PRODUCT
void ParMarkBitMap::reset_counters()
{
  _cas_tries = _cas_retries = _cas_by_another = 0;
}
#endif  // #ifndef PRODUCT

#ifdef ASSERT
void ParMarkBitMap::verify_clear() const
{
  const idx_t* const beg = (const idx_t*)_virtual_space->committed_low_addr();
  const idx_t* const end = (const idx_t*)_virtual_space->committed_high_addr();
  for (const idx_t* p = beg; p < end; ++p) {
    assert(*p == 0, "bitmap not clear");
  }
}
#endif  // #ifdef ASSERT