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