1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)parMarkBitMap.cpp    1.31 07/10/04 10:49:33 JVM"
   3 #endif
   4 /*
   5  * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_parMarkBitMap.cpp.incl"
  30 
  31 bool
  32 ParMarkBitMap::initialize(MemRegion covered_region)
  33 {
  34   const idx_t bits = bits_required(covered_region);
  35   // The bits will be divided evenly between two bitmaps; each of them should be
  36   // an integral number of words.
  37   assert(bits % (BitsPerWord * 2) == 0, "region size unaligned");
  38 
  39   const size_t words = bits / BitsPerWord;
  40   const size_t raw_bytes = words * sizeof(idx_t);
  41   const size_t page_sz = os::page_size_for_region(raw_bytes, raw_bytes, 10);
  42   const size_t granularity = os::vm_allocation_granularity();
  43   const size_t bytes = align_size_up(raw_bytes, MAX2(page_sz, granularity));
  44 
  45   const size_t rs_align = page_sz == (size_t) os::vm_page_size() ? 0 :
  46     MAX2(page_sz, granularity);
  47   ReservedSpace rs(bytes, rs_align, false);
  48   os::trace_page_sizes("par bitmap", raw_bytes, raw_bytes, page_sz,
  49                        rs.base(), rs.size());
  50   _virtual_space = new PSVirtualSpace(rs, page_sz);
  51   if (_virtual_space != NULL && _virtual_space->expand_by(bytes)) {
  52     _region_start = covered_region.start();
  53     _region_size = covered_region.word_size();
  54     idx_t* map = (idx_t*)_virtual_space->reserved_low_addr();
  55     _beg_bits.set_map(map);
  56     _beg_bits.set_size(bits / 2);
  57     _end_bits.set_map(map + words / 2);
  58     _end_bits.set_size(bits / 2);
  59     return true;
  60   }
  61 
  62   _region_start = 0;
  63   _region_size = 0;
  64   if (_virtual_space != NULL) {
  65     delete _virtual_space;
  66     _virtual_space = NULL;
  67   }
  68   return false;
  69 }
  70 
  71 #ifdef ASSERT
  72 extern size_t mark_bitmap_count;
  73 extern size_t mark_bitmap_size;
  74 #endif  // #ifdef ASSERT
  75 
  76 bool
  77 ParMarkBitMap::mark_obj(HeapWord* addr, size_t size)
  78 {
  79   const idx_t beg_bit = addr_to_bit(addr);
  80   if (_beg_bits.par_set_bit(beg_bit)) {
  81     const idx_t end_bit = addr_to_bit(addr + size - 1);
  82     bool end_bit_ok = _end_bits.par_set_bit(end_bit);
  83     assert(end_bit_ok, "concurrency problem");
  84     DEBUG_ONLY(Atomic::inc_ptr(&mark_bitmap_count));
  85     DEBUG_ONLY(Atomic::add_ptr(size, &mark_bitmap_size));
  86     return true;
  87   }
  88   return false;
  89 }
  90 
  91 size_t
  92 ParMarkBitMap::live_words_in_range(HeapWord* beg_addr, HeapWord* end_addr) const
  93 {
  94   assert(beg_addr <= end_addr, "bad range");
  95 
  96   idx_t live_bits = 0;
  97 
  98   // The bitmap routines require the right boundary to be word-aligned.
  99   const idx_t end_bit = addr_to_bit(end_addr);
 100   const idx_t range_end = BitMap::word_align_up(end_bit);
 101 
 102   idx_t beg_bit = find_obj_beg(addr_to_bit(beg_addr), range_end);
 103   while (beg_bit < end_bit) {
 104     idx_t tmp_end = find_obj_end(beg_bit, range_end);
 105     if (tmp_end < end_bit) {
 106       live_bits += tmp_end - beg_bit + 1;
 107       beg_bit = find_obj_beg(tmp_end + 1, range_end);
 108     } else {
 109       live_bits += end_bit - beg_bit;  // No + 1 here; end_bit is not counted. 
 110       return bits_to_words(live_bits);
 111     }
 112   }
 113   return bits_to_words(live_bits);
 114 }
 115 
 116 size_t ParMarkBitMap::live_words_in_range(HeapWord* beg_addr, oop end_obj) const
 117 {
 118   assert(beg_addr <= (HeapWord*)end_obj, "bad range");
 119   assert(is_marked(end_obj), "end_obj must be live");
 120 
 121   idx_t live_bits = 0;
 122 
 123   // The bitmap routines require the right boundary to be word-aligned.
 124   const idx_t end_bit = addr_to_bit((HeapWord*)end_obj);
 125   const idx_t range_end = BitMap::word_align_up(end_bit);
 126 
 127   idx_t beg_bit = find_obj_beg(addr_to_bit(beg_addr), range_end);
 128   while (beg_bit < end_bit) {
 129     idx_t tmp_end = find_obj_end(beg_bit, range_end);
 130     assert(tmp_end < end_bit, "missing end bit");
 131     live_bits += tmp_end - beg_bit + 1;
 132     beg_bit = find_obj_beg(tmp_end + 1, range_end);
 133   }
 134   return bits_to_words(live_bits);
 135 }
 136 
 137 ParMarkBitMap::IterationStatus
 138 ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,
 139                        idx_t range_beg, idx_t range_end) const
 140 {
 141   DEBUG_ONLY(verify_bit(range_beg);)
 142   DEBUG_ONLY(verify_bit(range_end);)
 143   assert(range_beg <= range_end, "live range invalid");
 144 
 145   // The bitmap routines require the right boundary to be word-aligned.
 146   const idx_t search_end = BitMap::word_align_up(range_end);
 147 
 148   idx_t cur_beg = find_obj_beg(range_beg, search_end);
 149   while (cur_beg < range_end) {
 150     const idx_t cur_end = find_obj_end(cur_beg, search_end);
 151     if (cur_end >= range_end) {
 152       // The obj ends outside the range.
 153       live_closure->set_source(bit_to_addr(cur_beg));
 154       return incomplete;
 155     }
 156 
 157     const size_t size = obj_size(cur_beg, cur_end);
 158     IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);
 159     if (status != incomplete) {
 160       assert(status == would_overflow || status == full, "sanity");
 161       return status;
 162     }
 163 
 164     // Successfully processed the object; look for the next object.
 165     cur_beg = find_obj_beg(cur_end + 1, search_end);
 166   }
 167 
 168   live_closure->set_source(bit_to_addr(range_end));
 169   return complete;
 170 }
 171 
 172 ParMarkBitMap::IterationStatus
 173 ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,
 174                        ParMarkBitMapClosure* dead_closure,
 175                        idx_t range_beg, idx_t range_end,
 176                        idx_t dead_range_end) const
 177 {
 178   DEBUG_ONLY(verify_bit(range_beg);)
 179   DEBUG_ONLY(verify_bit(range_end);)
 180   DEBUG_ONLY(verify_bit(dead_range_end);)
 181   assert(range_beg <= range_end, "live range invalid");
 182   assert(range_end <= dead_range_end, "dead range invalid");
 183 
 184   // The bitmap routines require the right boundary to be word-aligned.
 185   const idx_t live_search_end = BitMap::word_align_up(range_end);
 186   const idx_t dead_search_end = BitMap::word_align_up(dead_range_end);
 187 
 188   idx_t cur_beg = range_beg;
 189   if (range_beg < range_end && is_unmarked(range_beg)) {
 190     // The range starts with dead space.  Look for the next object, then fill.
 191     cur_beg = find_obj_beg(range_beg + 1, dead_search_end);
 192     const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);
 193     const size_t size = obj_size(range_beg, dead_space_end);
 194     dead_closure->do_addr(bit_to_addr(range_beg), size);
 195   }
 196     
 197   while (cur_beg < range_end) {
 198     const idx_t cur_end = find_obj_end(cur_beg, live_search_end);
 199     if (cur_end >= range_end) {
 200       // The obj ends outside the range.
 201       live_closure->set_source(bit_to_addr(cur_beg));
 202       return incomplete;
 203     }
 204 
 205     const size_t size = obj_size(cur_beg, cur_end);
 206     IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);
 207     if (status != incomplete) {
 208       assert(status == would_overflow || status == full, "sanity");
 209       return status;
 210     }
 211 
 212     // Look for the start of the next object.
 213     const idx_t dead_space_beg = cur_end + 1;
 214     cur_beg = find_obj_beg(dead_space_beg, dead_search_end);
 215     if (cur_beg > dead_space_beg) {
 216       // Found dead space; compute the size and invoke the dead closure.
 217       const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);
 218       const size_t size = obj_size(dead_space_beg, dead_space_end);
 219       dead_closure->do_addr(bit_to_addr(dead_space_beg), size);
 220     }
 221   }
 222 
 223   live_closure->set_source(bit_to_addr(range_end));
 224   return complete;
 225 }
 226 
 227 #ifndef PRODUCT
 228 void ParMarkBitMap::reset_counters()
 229 {
 230   _cas_tries = _cas_retries = _cas_by_another = 0;
 231 }
 232 #endif  // #ifndef PRODUCT
 233 
 234 #ifdef ASSERT
 235 void ParMarkBitMap::verify_clear() const
 236 {
 237   const idx_t* const beg = (const idx_t*)_virtual_space->committed_low_addr();
 238   const idx_t* const end = (const idx_t*)_virtual_space->committed_high_addr();
 239   for (const idx_t* p = beg; p < end; ++p) {
 240     assert(*p == 0, "bitmap not clear");
 241   }
 242 }
 243 #endif  // #ifdef ASSERT