1 /*
   2  * Copyright (c) 2005, 2008, 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 "incls/_precompiled.incl"
  26 # include "incls/_parMarkBitMap.cpp.incl"
  27 
  28 bool
  29 ParMarkBitMap::initialize(MemRegion covered_region)
  30 {
  31   const idx_t bits = bits_required(covered_region);
  32   // The bits will be divided evenly between two bitmaps; each of them should be
  33   // an integral number of words.
  34   assert(bits % (BitsPerWord * 2) == 0, "region size unaligned");
  35 
  36   const size_t words = bits / BitsPerWord;
  37   const size_t raw_bytes = words * sizeof(idx_t);
  38   const size_t page_sz = os::page_size_for_region(raw_bytes, raw_bytes, 10);
  39   const size_t granularity = os::vm_allocation_granularity();
  40   const size_t bytes = align_size_up(raw_bytes, MAX2(page_sz, granularity));
  41 
  42   const size_t rs_align = page_sz == (size_t) os::vm_page_size() ? 0 :
  43     MAX2(page_sz, granularity);
  44   ReservedSpace rs(bytes, rs_align, rs_align > 0);
  45   os::trace_page_sizes("par bitmap", raw_bytes, raw_bytes, page_sz,
  46                        rs.base(), rs.size());
  47   _virtual_space = new PSVirtualSpace(rs, page_sz);
  48   if (_virtual_space != NULL && _virtual_space->expand_by(bytes)) {
  49     _region_start = covered_region.start();
  50     _region_size = covered_region.word_size();
  51     idx_t* map = (idx_t*)_virtual_space->reserved_low_addr();
  52     _beg_bits.set_map(map);
  53     _beg_bits.set_size(bits / 2);
  54     _end_bits.set_map(map + words / 2);
  55     _end_bits.set_size(bits / 2);
  56     return true;
  57   }
  58 
  59   _region_start = 0;
  60   _region_size = 0;
  61   if (_virtual_space != NULL) {
  62     delete _virtual_space;
  63     _virtual_space = NULL;
  64     // Release memory reserved in the space.
  65     rs.release();
  66   }
  67   return false;
  68 }
  69 
  70 #ifdef ASSERT
  71 extern size_t mark_bitmap_count;
  72 extern size_t mark_bitmap_size;
  73 #endif  // #ifdef ASSERT
  74 
  75 bool
  76 ParMarkBitMap::mark_obj(HeapWord* addr, size_t size)
  77 {
  78   const idx_t beg_bit = addr_to_bit(addr);
  79   if (_beg_bits.par_set_bit(beg_bit)) {
  80     const idx_t end_bit = addr_to_bit(addr + size - 1);
  81     bool end_bit_ok = _end_bits.par_set_bit(end_bit);
  82     assert(end_bit_ok, "concurrency problem");
  83     DEBUG_ONLY(Atomic::inc_ptr(&mark_bitmap_count));
  84     DEBUG_ONLY(Atomic::add_ptr(size, &mark_bitmap_size));
  85     return true;
  86   }
  87   return false;
  88 }
  89 
  90 size_t
  91 ParMarkBitMap::live_words_in_range(HeapWord* beg_addr, HeapWord* end_addr) const
  92 {
  93   assert(beg_addr <= end_addr, "bad range");
  94 
  95   idx_t live_bits = 0;
  96 
  97   // The bitmap routines require the right boundary to be word-aligned.
  98   const idx_t end_bit = addr_to_bit(end_addr);
  99   const idx_t range_end = BitMap::word_align_up(end_bit);
 100 
 101   idx_t beg_bit = find_obj_beg(addr_to_bit(beg_addr), range_end);
 102   while (beg_bit < end_bit) {
 103     idx_t tmp_end = find_obj_end(beg_bit, range_end);
 104     if (tmp_end < end_bit) {
 105       live_bits += tmp_end - beg_bit + 1;
 106       beg_bit = find_obj_beg(tmp_end + 1, range_end);
 107     } else {
 108       live_bits += end_bit - beg_bit;  // No + 1 here; end_bit is not counted.
 109       return bits_to_words(live_bits);
 110     }
 111   }
 112   return bits_to_words(live_bits);
 113 }
 114 
 115 size_t ParMarkBitMap::live_words_in_range(HeapWord* beg_addr, oop end_obj) const
 116 {
 117   assert(beg_addr <= (HeapWord*)end_obj, "bad range");
 118   assert(is_marked(end_obj), "end_obj must be live");
 119 
 120   idx_t live_bits = 0;
 121 
 122   // The bitmap routines require the right boundary to be word-aligned.
 123   const idx_t end_bit = addr_to_bit((HeapWord*)end_obj);
 124   const idx_t range_end = BitMap::word_align_up(end_bit);
 125 
 126   idx_t beg_bit = find_obj_beg(addr_to_bit(beg_addr), range_end);
 127   while (beg_bit < end_bit) {
 128     idx_t tmp_end = find_obj_end(beg_bit, range_end);
 129     assert(tmp_end < end_bit, "missing end bit");
 130     live_bits += tmp_end - beg_bit + 1;
 131     beg_bit = find_obj_beg(tmp_end + 1, range_end);
 132   }
 133   return bits_to_words(live_bits);
 134 }
 135 
 136 ParMarkBitMap::IterationStatus
 137 ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,
 138                        idx_t range_beg, idx_t range_end) const
 139 {
 140   DEBUG_ONLY(verify_bit(range_beg);)
 141   DEBUG_ONLY(verify_bit(range_end);)
 142   assert(range_beg <= range_end, "live range invalid");
 143 
 144   // The bitmap routines require the right boundary to be word-aligned.
 145   const idx_t search_end = BitMap::word_align_up(range_end);
 146 
 147   idx_t cur_beg = find_obj_beg(range_beg, search_end);
 148   while (cur_beg < range_end) {
 149     const idx_t cur_end = find_obj_end(cur_beg, search_end);
 150     if (cur_end >= range_end) {
 151       // The obj ends outside the range.
 152       live_closure->set_source(bit_to_addr(cur_beg));
 153       return incomplete;
 154     }
 155 
 156     const size_t size = obj_size(cur_beg, cur_end);
 157     IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);
 158     if (status != incomplete) {
 159       assert(status == would_overflow || status == full, "sanity");
 160       return status;
 161     }
 162 
 163     // Successfully processed the object; look for the next object.
 164     cur_beg = find_obj_beg(cur_end + 1, search_end);
 165   }
 166 
 167   live_closure->set_source(bit_to_addr(range_end));
 168   return complete;
 169 }
 170 
 171 ParMarkBitMap::IterationStatus
 172 ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,
 173                        ParMarkBitMapClosure* dead_closure,
 174                        idx_t range_beg, idx_t range_end,
 175                        idx_t dead_range_end) const
 176 {
 177   DEBUG_ONLY(verify_bit(range_beg);)
 178   DEBUG_ONLY(verify_bit(range_end);)
 179   DEBUG_ONLY(verify_bit(dead_range_end);)
 180   assert(range_beg <= range_end, "live range invalid");
 181   assert(range_end <= dead_range_end, "dead range invalid");
 182 
 183   // The bitmap routines require the right boundary to be word-aligned.
 184   const idx_t live_search_end = BitMap::word_align_up(range_end);
 185   const idx_t dead_search_end = BitMap::word_align_up(dead_range_end);
 186 
 187   idx_t cur_beg = range_beg;
 188   if (range_beg < range_end && is_unmarked(range_beg)) {
 189     // The range starts with dead space.  Look for the next object, then fill.
 190     cur_beg = find_obj_beg(range_beg + 1, dead_search_end);
 191     const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);
 192     const size_t size = obj_size(range_beg, dead_space_end);
 193     dead_closure->do_addr(bit_to_addr(range_beg), size);
 194   }
 195 
 196   while (cur_beg < range_end) {
 197     const idx_t cur_end = find_obj_end(cur_beg, live_search_end);
 198     if (cur_end >= range_end) {
 199       // The obj ends outside the range.
 200       live_closure->set_source(bit_to_addr(cur_beg));
 201       return incomplete;
 202     }
 203 
 204     const size_t size = obj_size(cur_beg, cur_end);
 205     IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);
 206     if (status != incomplete) {
 207       assert(status == would_overflow || status == full, "sanity");
 208       return status;
 209     }
 210 
 211     // Look for the start of the next object.
 212     const idx_t dead_space_beg = cur_end + 1;
 213     cur_beg = find_obj_beg(dead_space_beg, dead_search_end);
 214     if (cur_beg > dead_space_beg) {
 215       // Found dead space; compute the size and invoke the dead closure.
 216       const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);
 217       const size_t size = obj_size(dead_space_beg, dead_space_end);
 218       dead_closure->do_addr(bit_to_addr(dead_space_beg), size);
 219     }
 220   }
 221 
 222   live_closure->set_source(bit_to_addr(range_end));
 223   return complete;
 224 }
 225 
 226 #ifndef PRODUCT
 227 void ParMarkBitMap::reset_counters()
 228 {
 229   _cas_tries = _cas_retries = _cas_by_another = 0;
 230 }
 231 #endif  // #ifndef PRODUCT
 232 
 233 #ifdef ASSERT
 234 void ParMarkBitMap::verify_clear() const
 235 {
 236   const idx_t* const beg = (const idx_t*)_virtual_space->committed_low_addr();
 237   const idx_t* const end = (const idx_t*)_virtual_space->committed_high_addr();
 238   for (const idx_t* p = beg; p < end; ++p) {
 239     assert(*p == 0, "bitmap not clear");
 240   }
 241 }
 242 #endif  // #ifdef ASSERT