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