Path: blob/master/src/hotspot/share/gc/parallel/parMarkBitMap.cpp
41149 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/parallel/parMarkBitMap.inline.hpp"26#include "gc/parallel/psCompactionManager.inline.hpp"27#include "gc/parallel/psParallelCompact.inline.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/atomic.hpp"30#include "runtime/os.hpp"31#include "services/memTracker.hpp"32#include "utilities/align.hpp"33#include "utilities/bitMap.inline.hpp"3435bool36ParMarkBitMap::initialize(MemRegion covered_region)37{38const idx_t bits = bits_required(covered_region);39// The bits will be divided evenly between two bitmaps; each of them should be40// an integral number of words.41assert(is_aligned(bits, (BitsPerWord * 2)), "region size unaligned");4243const size_t words = bits / BitsPerWord;44const size_t raw_bytes = words * sizeof(idx_t);45const size_t page_sz = os::page_size_for_region_aligned(raw_bytes, 10);46const size_t granularity = os::vm_allocation_granularity();47_reserved_byte_size = align_up(raw_bytes, MAX2(page_sz, granularity));4849const size_t rs_align = page_sz == (size_t) os::vm_page_size() ? 0 :50MAX2(page_sz, granularity);51ReservedSpace rs(_reserved_byte_size, rs_align, page_sz);52const size_t used_page_sz = rs.page_size();53os::trace_page_sizes("Mark Bitmap", raw_bytes, raw_bytes, used_page_sz,54rs.base(), rs.size());5556MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);5758_virtual_space = new PSVirtualSpace(rs, page_sz);59if (_virtual_space != NULL && _virtual_space->expand_by(_reserved_byte_size)) {60_region_start = covered_region.start();61_region_size = covered_region.word_size();62BitMap::bm_word_t* map = (BitMap::bm_word_t*)_virtual_space->reserved_low_addr();63_beg_bits = BitMapView(map, bits / 2);64_end_bits = BitMapView(map + words / 2, bits / 2);65return true;66}6768_region_start = 0;69_region_size = 0;70if (_virtual_space != NULL) {71delete _virtual_space;72_virtual_space = NULL;73// Release memory reserved in the space.74rs.release();75}76return false;77}7879bool80ParMarkBitMap::mark_obj(HeapWord* addr, size_t size)81{82const idx_t beg_bit = addr_to_bit(addr);83if (_beg_bits.par_set_bit(beg_bit)) {84const idx_t end_bit = addr_to_bit(addr + size - 1);85bool end_bit_ok = _end_bits.par_set_bit(end_bit);86assert(end_bit_ok, "concurrency problem");87return true;88}89return false;90}9192inline bool93ParMarkBitMap::is_live_words_in_range_in_cache(ParCompactionManager* cm, HeapWord* beg_addr) const {94return cm->last_query_begin() == beg_addr;95}9697inline void98ParMarkBitMap::update_live_words_in_range_cache(ParCompactionManager* cm, HeapWord* beg_addr, oop end_obj, size_t result) const {99cm->set_last_query_begin(beg_addr);100cm->set_last_query_object(end_obj);101cm->set_last_query_return(result);102}103104size_t105ParMarkBitMap::live_words_in_range_helper(HeapWord* beg_addr, oop end_obj) const106{107assert(beg_addr <= cast_from_oop<HeapWord*>(end_obj), "bad range");108assert(is_marked(end_obj), "end_obj must be live");109110idx_t live_bits = 0;111112// The bitmap routines require the right boundary to be word-aligned.113const idx_t end_bit = addr_to_bit(cast_from_oop<HeapWord*>(end_obj));114const idx_t range_end = align_range_end(end_bit);115116idx_t beg_bit = find_obj_beg(addr_to_bit(beg_addr), range_end);117while (beg_bit < end_bit) {118idx_t tmp_end = find_obj_end(beg_bit, range_end);119assert(tmp_end < end_bit, "missing end bit");120live_bits += tmp_end - beg_bit + 1;121beg_bit = find_obj_beg(tmp_end + 1, range_end);122}123return bits_to_words(live_bits);124}125126size_t127ParMarkBitMap::live_words_in_range_use_cache(ParCompactionManager* cm, HeapWord* beg_addr, oop end_oop) const128{129HeapWord* last_beg = cm->last_query_begin();130HeapWord* last_obj = cast_from_oop<HeapWord*>(cm->last_query_object());131HeapWord* end_obj = cast_from_oop<HeapWord*>(end_oop);132133size_t last_ret = cm->last_query_return();134if (end_obj > last_obj) {135last_ret = last_ret + live_words_in_range_helper(last_obj, end_oop);136last_obj = end_obj;137} else if (end_obj < last_obj) {138// The cached value is for an object that is to the left (lower address) of the current139// end_obj. Calculate back from that cached value.140if (pointer_delta(end_obj, beg_addr) > pointer_delta(last_obj, end_obj)) {141last_ret = last_ret - live_words_in_range_helper(end_obj, cast_to_oop(last_obj));142} else {143last_ret = live_words_in_range_helper(beg_addr, end_oop);144}145last_obj = end_obj;146}147148update_live_words_in_range_cache(cm, last_beg, cast_to_oop(last_obj), last_ret);149return last_ret;150}151152size_t153ParMarkBitMap::live_words_in_range(ParCompactionManager* cm, HeapWord* beg_addr, oop end_obj) const154{155// Try to reuse result from ParCompactionManager cache first.156if (is_live_words_in_range_in_cache(cm, beg_addr)) {157return live_words_in_range_use_cache(cm, beg_addr, end_obj);158}159size_t ret = live_words_in_range_helper(beg_addr, end_obj);160update_live_words_in_range_cache(cm, beg_addr, end_obj, ret);161return ret;162}163164ParMarkBitMap::IterationStatus165ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,166idx_t range_beg, idx_t range_end) const167{168DEBUG_ONLY(verify_bit(range_beg);)169DEBUG_ONLY(verify_bit(range_end);)170assert(range_beg <= range_end, "live range invalid");171172// The bitmap routines require the right boundary to be word-aligned.173const idx_t search_end = align_range_end(range_end);174175idx_t cur_beg = find_obj_beg(range_beg, search_end);176while (cur_beg < range_end) {177const idx_t cur_end = find_obj_end(cur_beg, search_end);178if (cur_end >= range_end) {179// The obj ends outside the range.180live_closure->set_source(bit_to_addr(cur_beg));181return incomplete;182}183184const size_t size = obj_size(cur_beg, cur_end);185IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);186if (status != incomplete) {187assert(status == would_overflow || status == full, "sanity");188return status;189}190191// Successfully processed the object; look for the next object.192cur_beg = find_obj_beg(cur_end + 1, search_end);193}194195live_closure->set_source(bit_to_addr(range_end));196return complete;197}198199ParMarkBitMap::IterationStatus200ParMarkBitMap::iterate(ParMarkBitMapClosure* live_closure,201ParMarkBitMapClosure* dead_closure,202idx_t range_beg, idx_t range_end,203idx_t dead_range_end) const204{205DEBUG_ONLY(verify_bit(range_beg);)206DEBUG_ONLY(verify_bit(range_end);)207DEBUG_ONLY(verify_bit(dead_range_end);)208assert(range_beg <= range_end, "live range invalid");209assert(range_end <= dead_range_end, "dead range invalid");210211// The bitmap routines require the right boundary to be word-aligned.212const idx_t live_search_end = align_range_end(range_end);213const idx_t dead_search_end = align_range_end(dead_range_end);214215idx_t cur_beg = range_beg;216if (range_beg < range_end && is_unmarked(range_beg)) {217// The range starts with dead space. Look for the next object, then fill.218cur_beg = find_obj_beg(range_beg + 1, dead_search_end);219const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);220const size_t size = obj_size(range_beg, dead_space_end);221dead_closure->do_addr(bit_to_addr(range_beg), size);222}223224while (cur_beg < range_end) {225const idx_t cur_end = find_obj_end(cur_beg, live_search_end);226if (cur_end >= range_end) {227// The obj ends outside the range.228live_closure->set_source(bit_to_addr(cur_beg));229return incomplete;230}231232const size_t size = obj_size(cur_beg, cur_end);233IterationStatus status = live_closure->do_addr(bit_to_addr(cur_beg), size);234if (status != incomplete) {235assert(status == would_overflow || status == full, "sanity");236return status;237}238239// Look for the start of the next object.240const idx_t dead_space_beg = cur_end + 1;241cur_beg = find_obj_beg(dead_space_beg, dead_search_end);242if (cur_beg > dead_space_beg) {243// Found dead space; compute the size and invoke the dead closure.244const idx_t dead_space_end = MIN2(cur_beg - 1, dead_range_end - 1);245const size_t size = obj_size(dead_space_beg, dead_space_end);246dead_closure->do_addr(bit_to_addr(dead_space_beg), size);247}248}249250live_closure->set_source(bit_to_addr(range_end));251return complete;252}253254#ifdef ASSERT255void ParMarkBitMap::verify_clear() const256{257const idx_t* const beg = (const idx_t*)_virtual_space->committed_low_addr();258const idx_t* const end = (const idx_t*)_virtual_space->committed_high_addr();259for (const idx_t* p = beg; p < end; ++p) {260assert(*p == 0, "bitmap not clear");261}262}263#endif // #ifdef ASSERT264265266