2596N/A/*
3067N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
2596N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2596N/A *
2596N/A * This code is free software; you can redistribute it and/or modify it
2596N/A * under the terms of the GNU General Public License version 2 only, as
2596N/A * published by the Free Software Foundation.
2596N/A *
2596N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2596N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2596N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2596N/A * version 2 for more details (a copy is included in the LICENSE file that
2596N/A * accompanied this code).
2596N/A *
2596N/A * You should have received a copy of the GNU General Public License version
2596N/A * 2 along with this work; if not, write to the Free Software Foundation,
2596N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2596N/A *
2596N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2596N/A * or visit www.oracle.com if you need additional information or have any
2596N/A * questions.
2596N/A *
2596N/A */
2596N/A
2596N/A#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_INLINE_HPP
2596N/A#define SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_INLINE_HPP
2596N/A
2596N/A#include "gc_implementation/g1/concurrentMark.hpp"
2596N/A#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
2596N/A
4050N/A// Utility routine to set an exclusive range of cards on the given
4050N/A// card liveness bitmap
4050N/Ainline void ConcurrentMark::set_card_bitmap_range(BitMap* card_bm,
4050N/A BitMap::idx_t start_idx,
4050N/A BitMap::idx_t end_idx,
4050N/A bool is_par) {
4050N/A
4050N/A // Set the exclusive bit range [start_idx, end_idx).
4050N/A assert((end_idx - start_idx) > 0, "at least one card");
4050N/A assert(end_idx <= card_bm->size(), "sanity");
4050N/A
4050N/A // Silently clip the end index
4050N/A end_idx = MIN2(end_idx, card_bm->size());
4050N/A
4050N/A // For small ranges use a simple loop; otherwise use set_range or
4050N/A // use par_at_put_range (if parallel). The range is made up of the
4050N/A // cards that are spanned by an object/mem region so 8 cards will
4050N/A // allow up to object sizes up to 4K to be handled using the loop.
4050N/A if ((end_idx - start_idx) <= 8) {
4050N/A for (BitMap::idx_t i = start_idx; i < end_idx; i += 1) {
4050N/A if (is_par) {
4050N/A card_bm->par_set_bit(i);
4050N/A } else {
4050N/A card_bm->set_bit(i);
4050N/A }
4050N/A }
4050N/A } else {
4050N/A // Note BitMap::par_at_put_range() and BitMap::set_range() are exclusive.
4050N/A if (is_par) {
4050N/A card_bm->par_at_put_range(start_idx, end_idx, true);
4050N/A } else {
4050N/A card_bm->set_range(start_idx, end_idx);
4050N/A }
4050N/A }
4050N/A}
4050N/A
3119N/A// Returns the index in the liveness accounting card bitmap
3119N/A// for the given address
3119N/Ainline BitMap::idx_t ConcurrentMark::card_bitmap_index_for(HeapWord* addr) {
3119N/A // Below, the term "card num" means the result of shifting an address
3119N/A // by the card shift -- address 0 corresponds to card number 0. One
3119N/A // must subtract the card num of the bottom of the heap to obtain a
3119N/A // card table index.
3119N/A intptr_t card_num = intptr_t(uintptr_t(addr) >> CardTableModRefBS::card_shift);
3119N/A return card_num - heap_bottom_card_num();
3119N/A}
3119N/A
3119N/A// Counts the given memory region in the given task/worker
3119N/A// counting data structures.
3119N/Ainline void ConcurrentMark::count_region(MemRegion mr, HeapRegion* hr,
3119N/A size_t* marked_bytes_array,
3119N/A BitMap* task_card_bm) {
3119N/A G1CollectedHeap* g1h = _g1h;
4050N/A CardTableModRefBS* ct_bs = (CardTableModRefBS*) (g1h->barrier_set());
4050N/A
3119N/A HeapWord* start = mr.start();
4050N/A HeapWord* end = mr.end();
3119N/A size_t region_size_bytes = mr.byte_size();
3681N/A uint index = hr->hrs_index();
3119N/A
3119N/A assert(!hr->continuesHumongous(), "should not be HC region");
3119N/A assert(hr == g1h->heap_region_containing(start), "sanity");
3119N/A assert(hr == g1h->heap_region_containing(mr.last()), "sanity");
3119N/A assert(marked_bytes_array != NULL, "pre-condition");
3119N/A assert(task_card_bm != NULL, "pre-condition");
3119N/A
3119N/A // Add to the task local marked bytes for this region.
3119N/A marked_bytes_array[index] += region_size_bytes;
3119N/A
3119N/A BitMap::idx_t start_idx = card_bitmap_index_for(start);
4050N/A BitMap::idx_t end_idx = card_bitmap_index_for(end);
3119N/A
4050N/A // Note: if we're looking at the last region in heap - end
4050N/A // could be actually just beyond the end of the heap; end_idx
4050N/A // will then correspond to a (non-existent) card that is also
4050N/A // just beyond the heap.
4050N/A if (g1h->is_in_g1_reserved(end) && !ct_bs->is_card_aligned(end)) {
4050N/A // end of region is not card aligned - incremement to cover
4050N/A // all the cards spanned by the region.
4050N/A end_idx += 1;
3119N/A }
4050N/A // The card bitmap is task/worker specific => no need to use
4050N/A // the 'par' BitMap routines.
4050N/A // Set bits in the exclusive bit range [start_idx, end_idx).
4050N/A set_card_bitmap_range(task_card_bm, start_idx, end_idx, false /* is_par */);
3119N/A}
3119N/A
3120N/A// Counts the given memory region in the task/worker counting
3120N/A// data structures for the given worker id.
3120N/Ainline void ConcurrentMark::count_region(MemRegion mr,
3120N/A HeapRegion* hr,
3120N/A uint worker_id) {
3120N/A size_t* marked_bytes_array = count_marked_bytes_array_for(worker_id);
3120N/A BitMap* task_card_bm = count_card_bitmap_for(worker_id);
3120N/A count_region(mr, hr, marked_bytes_array, task_card_bm);
3120N/A}
3120N/A
3119N/A// Counts the given memory region, which may be a single object, in the
3119N/A// task/worker counting data structures for the given worker id.
3119N/Ainline void ConcurrentMark::count_region(MemRegion mr, uint worker_id) {
3119N/A HeapWord* addr = mr.start();
3119N/A HeapRegion* hr = _g1h->heap_region_containing_raw(addr);
3120N/A count_region(mr, hr, worker_id);
3119N/A}
3119N/A
3119N/A// Counts the given object in the given task/worker counting data structures.
3119N/Ainline void ConcurrentMark::count_object(oop obj,
3119N/A HeapRegion* hr,
3119N/A size_t* marked_bytes_array,
3119N/A BitMap* task_card_bm) {
3119N/A MemRegion mr((HeapWord*)obj, obj->size());
3119N/A count_region(mr, hr, marked_bytes_array, task_card_bm);
3119N/A}
3119N/A
3119N/A// Counts the given object in the task/worker counting data
3119N/A// structures for the given worker id.
3120N/Ainline void ConcurrentMark::count_object(oop obj,
3120N/A HeapRegion* hr,
3120N/A uint worker_id) {
3119N/A size_t* marked_bytes_array = count_marked_bytes_array_for(worker_id);
3119N/A BitMap* task_card_bm = count_card_bitmap_for(worker_id);
3119N/A HeapWord* addr = (HeapWord*) obj;
3119N/A count_object(obj, hr, marked_bytes_array, task_card_bm);
3119N/A}
3119N/A
3119N/A// Attempts to mark the given object and, if successful, counts
3119N/A// the object in the given task/worker counting structures.
3119N/Ainline bool ConcurrentMark::par_mark_and_count(oop obj,
3119N/A HeapRegion* hr,
3119N/A size_t* marked_bytes_array,
3119N/A BitMap* task_card_bm) {
3119N/A HeapWord* addr = (HeapWord*)obj;
3119N/A if (_nextMarkBitMap->parMark(addr)) {
3119N/A // Update the task specific count data for the object.
3119N/A count_object(obj, hr, marked_bytes_array, task_card_bm);
3119N/A return true;
3119N/A }
3119N/A return false;
3119N/A}
3119N/A
3119N/A// Attempts to mark the given object and, if successful, counts
3119N/A// the object in the task/worker counting structures for the
3119N/A// given worker id.
3119N/Ainline bool ConcurrentMark::par_mark_and_count(oop obj,
3120N/A size_t word_size,
3120N/A HeapRegion* hr,
3120N/A uint worker_id) {
3120N/A HeapWord* addr = (HeapWord*)obj;
3120N/A if (_nextMarkBitMap->parMark(addr)) {
3120N/A MemRegion mr(addr, word_size);
3120N/A count_region(mr, hr, worker_id);
3120N/A return true;
3120N/A }
3120N/A return false;
3120N/A}
3120N/A
3120N/A// Attempts to mark the given object and, if successful, counts
3120N/A// the object in the task/worker counting structures for the
3120N/A// given worker id.
3120N/Ainline bool ConcurrentMark::par_mark_and_count(oop obj,
3119N/A HeapRegion* hr,
3119N/A uint worker_id) {
3119N/A HeapWord* addr = (HeapWord*)obj;
3119N/A if (_nextMarkBitMap->parMark(addr)) {
3119N/A // Update the task specific count data for the object.
3119N/A count_object(obj, hr, worker_id);
3119N/A return true;
3119N/A }
3119N/A return false;
3119N/A}
3119N/A
3119N/A// As above - but we don't know the heap region containing the
3119N/A// object and so have to supply it.
3119N/Ainline bool ConcurrentMark::par_mark_and_count(oop obj, uint worker_id) {
3119N/A HeapWord* addr = (HeapWord*)obj;
3119N/A HeapRegion* hr = _g1h->heap_region_containing_raw(addr);
3119N/A return par_mark_and_count(obj, hr, worker_id);
3119N/A}
3119N/A
3119N/A// Similar to the above routine but we already know the size, in words, of
3119N/A// the object that we wish to mark/count
3119N/Ainline bool ConcurrentMark::par_mark_and_count(oop obj,
3119N/A size_t word_size,
3119N/A uint worker_id) {
3119N/A HeapWord* addr = (HeapWord*)obj;
3119N/A if (_nextMarkBitMap->parMark(addr)) {
3119N/A // Update the task specific count data for the object.
3119N/A MemRegion mr(addr, word_size);
3119N/A count_region(mr, worker_id);
3119N/A return true;
3119N/A }
3119N/A return false;
3119N/A}
3119N/A
3119N/A// Unconditionally mark the given object, and unconditinally count
3119N/A// the object in the counting structures for worker id 0.
3119N/A// Should *not* be called from parallel code.
3119N/Ainline bool ConcurrentMark::mark_and_count(oop obj, HeapRegion* hr) {
3119N/A HeapWord* addr = (HeapWord*)obj;
3119N/A _nextMarkBitMap->mark(addr);
3119N/A // Update the task specific count data for the object.
3119N/A count_object(obj, hr, 0 /* worker_id */);
3119N/A return true;
3119N/A}
3119N/A
3119N/A// As above - but we don't have the heap region containing the
3119N/A// object, so we have to supply it.
3119N/Ainline bool ConcurrentMark::mark_and_count(oop obj) {
3119N/A HeapWord* addr = (HeapWord*)obj;
3119N/A HeapRegion* hr = _g1h->heap_region_containing_raw(addr);
3119N/A return mark_and_count(obj, hr);
3119N/A}
3119N/A
3110N/Ainline bool CMBitMapRO::iterate(BitMapClosure* cl, MemRegion mr) {
3110N/A HeapWord* start_addr = MAX2(startWord(), mr.start());
3110N/A HeapWord* end_addr = MIN2(endWord(), mr.end());
3110N/A
3110N/A if (end_addr > start_addr) {
3110N/A // Right-open interval [start-offset, end-offset).
3110N/A BitMap::idx_t start_offset = heapWordToOffset(start_addr);
3110N/A BitMap::idx_t end_offset = heapWordToOffset(end_addr);
3110N/A
3110N/A start_offset = _bm.get_next_one_offset(start_offset, end_offset);
3110N/A while (start_offset < end_offset) {
3110N/A HeapWord* obj_addr = offsetToHeapWord(start_offset);
3110N/A oop obj = (oop) obj_addr;
3110N/A if (!cl->do_bit(start_offset)) {
3110N/A return false;
3110N/A }
3110N/A HeapWord* next_addr = MIN2(obj_addr + obj->size(), end_addr);
3110N/A BitMap::idx_t next_offset = heapWordToOffset(next_addr);
3110N/A start_offset = _bm.get_next_one_offset(next_offset, end_offset);
3110N/A }
3110N/A }
3110N/A return true;
3110N/A}
3110N/A
3110N/Ainline bool CMBitMapRO::iterate(BitMapClosure* cl) {
3110N/A MemRegion mr(startWord(), sizeInWords());
3110N/A return iterate(cl, mr);
3110N/A}
3110N/A
2596N/Ainline void CMTask::push(oop obj) {
2596N/A HeapWord* objAddr = (HeapWord*) obj;
2596N/A assert(_g1h->is_in_g1_reserved(objAddr), "invariant");
2596N/A assert(!_g1h->is_on_master_free_list(
2596N/A _g1h->heap_region_containing((HeapWord*) objAddr)), "invariant");
2596N/A assert(!_g1h->is_obj_ill(obj), "invariant");
2596N/A assert(_nextMarkBitMap->isMarked(objAddr), "invariant");
2596N/A
2596N/A if (_cm->verbose_high()) {
2596N/A gclog_or_tty->print_cr("[%d] pushing "PTR_FORMAT, _task_id, (void*) obj);
2596N/A }
2596N/A
2596N/A if (!_task_queue->push(obj)) {
2596N/A // The local task queue looks full. We need to push some entries
2596N/A // to the global stack.
2596N/A
2596N/A if (_cm->verbose_medium()) {
2596N/A gclog_or_tty->print_cr("[%d] task queue overflow, "
2596N/A "moving entries to the global stack",
2596N/A _task_id);
2596N/A }
2596N/A move_entries_to_global_stack();
2596N/A
2596N/A // this should succeed since, even if we overflow the global
2596N/A // stack, we should have definitely removed some entries from the
2596N/A // local queue. So, there must be space on it.
2596N/A bool success = _task_queue->push(obj);
2596N/A assert(success, "invariant");
2596N/A }
2596N/A
2596N/A statsOnly( int tmp_size = _task_queue->size();
2601N/A if (tmp_size > _local_max_size) {
2596N/A _local_max_size = tmp_size;
2601N/A }
2596N/A ++_local_pushes );
2596N/A}
2596N/A
2596N/A// This determines whether the method below will check both the local
2596N/A// and global fingers when determining whether to push on the stack a
2596N/A// gray object (value 1) or whether it will only check the global one
2596N/A// (value 0). The tradeoffs are that the former will be a bit more
2596N/A// accurate and possibly push less on the stack, but it might also be
2596N/A// a little bit slower.
2596N/A
2596N/A#define _CHECK_BOTH_FINGERS_ 1
2596N/A
2596N/Ainline void CMTask::deal_with_reference(oop obj) {
2596N/A if (_cm->verbose_high()) {
2596N/A gclog_or_tty->print_cr("[%d] we're dealing with reference = "PTR_FORMAT,
2596N/A _task_id, (void*) obj);
2596N/A }
2596N/A
2596N/A ++_refs_reached;
2596N/A
2596N/A HeapWord* objAddr = (HeapWord*) obj;
2596N/A assert(obj->is_oop_or_null(true /* ignore mark word */), "Error");
3119N/A if (_g1h->is_in_g1_reserved(objAddr)) {
2596N/A assert(obj != NULL, "null check is implicit");
2596N/A if (!_nextMarkBitMap->isMarked(objAddr)) {
2596N/A // Only get the containing region if the object is not marked on the
2596N/A // bitmap (otherwise, it's a waste of time since we won't do
2596N/A // anything with it).
2596N/A HeapRegion* hr = _g1h->heap_region_containing_raw(obj);
2596N/A if (!hr->obj_allocated_since_next_marking(obj)) {
2596N/A if (_cm->verbose_high()) {
2596N/A gclog_or_tty->print_cr("[%d] "PTR_FORMAT" is not considered marked",
2596N/A _task_id, (void*) obj);
2596N/A }
2596N/A
2596N/A // we need to mark it first
3119N/A if (_cm->par_mark_and_count(obj, hr, _marked_bytes_array, _card_bm)) {
2596N/A // No OrderAccess:store_load() is needed. It is implicit in the
3119N/A // CAS done in CMBitMap::parMark() call in the routine above.
2596N/A HeapWord* global_finger = _cm->finger();
2596N/A
2596N/A#if _CHECK_BOTH_FINGERS_
2596N/A // we will check both the local and global fingers
2596N/A
2596N/A if (_finger != NULL && objAddr < _finger) {
2596N/A if (_cm->verbose_high()) {
2596N/A gclog_or_tty->print_cr("[%d] below the local finger ("PTR_FORMAT"), "
2596N/A "pushing it", _task_id, _finger);
2596N/A }
2596N/A push(obj);
2596N/A } else if (_curr_region != NULL && objAddr < _region_limit) {
2596N/A // do nothing
2596N/A } else if (objAddr < global_finger) {
2596N/A // Notice that the global finger might be moving forward
2596N/A // concurrently. This is not a problem. In the worst case, we
2596N/A // mark the object while it is above the global finger and, by
2596N/A // the time we read the global finger, it has moved forward
2596N/A // passed this object. In this case, the object will probably
2596N/A // be visited when a task is scanning the region and will also
2596N/A // be pushed on the stack. So, some duplicate work, but no
2596N/A // correctness problems.
2596N/A
2596N/A if (_cm->verbose_high()) {
2596N/A gclog_or_tty->print_cr("[%d] below the global finger "
2596N/A "("PTR_FORMAT"), pushing it",
2596N/A _task_id, global_finger);
2596N/A }
2596N/A push(obj);
2596N/A } else {
2596N/A // do nothing
2596N/A }
2596N/A#else // _CHECK_BOTH_FINGERS_
2596N/A // we will only check the global finger
2596N/A
2596N/A if (objAddr < global_finger) {
2596N/A // see long comment above
2596N/A
2596N/A if (_cm->verbose_high()) {
2596N/A gclog_or_tty->print_cr("[%d] below the global finger "
2596N/A "("PTR_FORMAT"), pushing it",
2596N/A _task_id, global_finger);
2596N/A }
2596N/A push(obj);
2596N/A }
2596N/A#endif // _CHECK_BOTH_FINGERS_
2596N/A }
2596N/A }
2596N/A }
2596N/A }
2596N/A}
2596N/A
3067N/Ainline void ConcurrentMark::markPrev(oop p) {
3067N/A assert(!_prevMarkBitMap->isMarked((HeapWord*) p), "sanity");
3067N/A // Note we are overriding the read-only view of the prev map here, via
3067N/A // the cast.
3067N/A ((CMBitMap*)_prevMarkBitMap)->mark((HeapWord*) p);
3067N/A}
3067N/A
3120N/Ainline void ConcurrentMark::grayRoot(oop obj, size_t word_size,
3120N/A uint worker_id, HeapRegion* hr) {
3120N/A assert(obj != NULL, "pre-condition");
3067N/A HeapWord* addr = (HeapWord*) obj;
3120N/A if (hr == NULL) {
3120N/A hr = _g1h->heap_region_containing_raw(addr);
3120N/A } else {
3120N/A assert(hr->is_in(addr), "pre-condition");
3120N/A }
3120N/A assert(hr != NULL, "sanity");
3120N/A // Given that we're looking for a region that contains an object
3120N/A // header it's impossible to get back a HC region.
3120N/A assert(!hr->continuesHumongous(), "sanity");
3067N/A
3067N/A // We cannot assert that word_size == obj->size() given that obj
3067N/A // might not be in a consistent state (another thread might be in
3067N/A // the process of copying it). So the best thing we can do is to
3067N/A // assert that word_size is under an upper bound which is its
3067N/A // containing region's capacity.
3067N/A assert(word_size * HeapWordSize <= hr->capacity(),
3067N/A err_msg("size: "SIZE_FORMAT" capacity: "SIZE_FORMAT" "HR_FORMAT,
3067N/A word_size * HeapWordSize, hr->capacity(),
3067N/A HR_FORMAT_PARAMS(hr)));
3067N/A
3120N/A if (addr < hr->next_top_at_mark_start()) {
3120N/A if (!_nextMarkBitMap->isMarked(addr)) {
3120N/A par_mark_and_count(obj, word_size, hr, worker_id);
3120N/A }
3067N/A }
3067N/A}
3067N/A
2596N/A#endif // SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_INLINE_HPP