ysr@777: /* ysr@777: * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * ysr@777: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, ysr@777: * CA 95054 USA or visit www.sun.com if you need additional information or ysr@777: * have any questions. ysr@777: * ysr@777: */ ysr@777: ysr@777: #include "incls/_precompiled.incl" ysr@777: #include "incls/_heapRegion.cpp.incl" ysr@777: ysr@777: HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1, ysr@777: HeapRegion* hr, OopClosure* cl, ysr@777: CardTableModRefBS::PrecisionStyle precision, ysr@777: FilterKind fk) : ysr@777: ContiguousSpaceDCTOC(hr, cl, precision, NULL), ysr@777: _hr(hr), _fk(fk), _g1(g1) ysr@777: {} ysr@777: ysr@777: FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r, ysr@777: OopClosure* oc) : ysr@777: _r_bottom(r->bottom()), _r_end(r->end()), ysr@777: _oc(oc), _out_of_region(0) ysr@777: {} ysr@777: ysr@777: class VerifyLiveClosure: public OopClosure { ysr@777: G1CollectedHeap* _g1h; ysr@777: CardTableModRefBS* _bs; ysr@777: oop _containing_obj; ysr@777: bool _failures; ysr@777: int _n_failures; ysr@777: public: ysr@777: VerifyLiveClosure(G1CollectedHeap* g1h) : ysr@777: _g1h(g1h), _bs(NULL), _containing_obj(NULL), ysr@777: _failures(false), _n_failures(0) ysr@777: { ysr@777: BarrierSet* bs = _g1h->barrier_set(); ysr@777: if (bs->is_a(BarrierSet::CardTableModRef)) ysr@777: _bs = (CardTableModRefBS*)bs; ysr@777: } ysr@777: ysr@777: void set_containing_obj(oop obj) { ysr@777: _containing_obj = obj; ysr@777: } ysr@777: ysr@777: bool failures() { return _failures; } ysr@777: int n_failures() { return _n_failures; } ysr@777: ysr@777: virtual void do_oop(narrowOop* p) { ysr@777: guarantee(false, "NYI"); ysr@777: } ysr@777: ysr@777: void do_oop(oop* p) { ysr@777: assert(_containing_obj != NULL, "Precondition"); ysr@777: assert(!_g1h->is_obj_dead(_containing_obj), "Precondition"); ysr@777: oop obj = *p; ysr@777: if (obj != NULL) { ysr@777: bool failed = false; ysr@777: if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead(obj)) { ysr@777: if (!_failures) { ysr@777: gclog_or_tty->print_cr(""); ysr@777: gclog_or_tty->print_cr("----------"); ysr@777: } ysr@777: if (!_g1h->is_in_closed_subset(obj)) { ysr@777: gclog_or_tty->print_cr("Field "PTR_FORMAT ysr@777: " of live obj "PTR_FORMAT ysr@777: " points to obj "PTR_FORMAT ysr@777: " not in the heap.", ysr@777: p, (void*) _containing_obj, (void*) obj); ysr@777: } else { ysr@777: gclog_or_tty->print_cr("Field "PTR_FORMAT ysr@777: " of live obj "PTR_FORMAT ysr@777: " points to dead obj "PTR_FORMAT".", ysr@777: p, (void*) _containing_obj, (void*) obj); ysr@777: } ysr@777: gclog_or_tty->print_cr("Live obj:"); ysr@777: _containing_obj->print_on(gclog_or_tty); ysr@777: gclog_or_tty->print_cr("Bad referent:"); ysr@777: obj->print_on(gclog_or_tty); ysr@777: gclog_or_tty->print_cr("----------"); ysr@777: _failures = true; ysr@777: failed = true; ysr@777: _n_failures++; ysr@777: } ysr@777: ysr@777: if (!_g1h->full_collection()) { ysr@777: HeapRegion* from = _g1h->heap_region_containing(p); ysr@777: HeapRegion* to = _g1h->heap_region_containing(*p); ysr@777: if (from != NULL && to != NULL && ysr@777: from != to && ysr@777: !to->popular() && ysr@777: !to->isHumongous()) { ysr@777: jbyte cv_obj = *_bs->byte_for_const(_containing_obj); ysr@777: jbyte cv_field = *_bs->byte_for_const(p); ysr@777: const jbyte dirty = CardTableModRefBS::dirty_card_val(); ysr@777: ysr@777: bool is_bad = !(from->is_young() ysr@777: || to->rem_set()->contains_reference(p) ysr@777: || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed ysr@777: (_containing_obj->is_objArray() ? ysr@777: cv_field == dirty ysr@777: : cv_obj == dirty || cv_field == dirty)); ysr@777: if (is_bad) { ysr@777: if (!_failures) { ysr@777: gclog_or_tty->print_cr(""); ysr@777: gclog_or_tty->print_cr("----------"); ysr@777: } ysr@777: gclog_or_tty->print_cr("Missing rem set entry:"); ysr@777: gclog_or_tty->print_cr("Field "PTR_FORMAT ysr@777: " of obj "PTR_FORMAT ysr@777: ", in region %d ["PTR_FORMAT ysr@777: ", "PTR_FORMAT"),", ysr@777: p, (void*) _containing_obj, ysr@777: from->hrs_index(), ysr@777: from->bottom(), ysr@777: from->end()); ysr@777: _containing_obj->print_on(gclog_or_tty); ysr@777: gclog_or_tty->print_cr("points to obj "PTR_FORMAT ysr@777: " in region %d ["PTR_FORMAT ysr@777: ", "PTR_FORMAT").", ysr@777: (void*) obj, to->hrs_index(), ysr@777: to->bottom(), to->end()); ysr@777: obj->print_on(gclog_or_tty); ysr@777: gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.", ysr@777: cv_obj, cv_field); ysr@777: gclog_or_tty->print_cr("----------"); ysr@777: _failures = true; ysr@777: if (!failed) _n_failures++; ysr@777: } ysr@777: } ysr@777: } ysr@777: } ysr@777: } ysr@777: }; ysr@777: ysr@777: template ysr@777: HeapWord* walk_mem_region_loop(ClosureType* cl, G1CollectedHeap* g1h, ysr@777: HeapRegion* hr, ysr@777: HeapWord* cur, HeapWord* top) { ysr@777: oop cur_oop = oop(cur); ysr@777: int oop_size = cur_oop->size(); ysr@777: HeapWord* next_obj = cur + oop_size; ysr@777: while (next_obj < top) { ysr@777: // Keep filtering the remembered set. ysr@777: if (!g1h->is_obj_dead(cur_oop, hr)) { ysr@777: // Bottom lies entirely below top, so we can call the ysr@777: // non-memRegion version of oop_iterate below. ysr@777: #ifndef PRODUCT ysr@777: if (G1VerifyMarkingInEvac) { ysr@777: VerifyLiveClosure vl_cl(g1h); ysr@777: cur_oop->oop_iterate(&vl_cl); ysr@777: } ysr@777: #endif ysr@777: cur_oop->oop_iterate(cl); ysr@777: } ysr@777: cur = next_obj; ysr@777: cur_oop = oop(cur); ysr@777: oop_size = cur_oop->size(); ysr@777: next_obj = cur + oop_size; ysr@777: } ysr@777: return cur; ysr@777: } ysr@777: ysr@777: void HeapRegionDCTOC::walk_mem_region_with_cl(MemRegion mr, ysr@777: HeapWord* bottom, ysr@777: HeapWord* top, ysr@777: OopClosure* cl) { ysr@777: G1CollectedHeap* g1h = _g1; ysr@777: ysr@777: int oop_size; ysr@777: ysr@777: OopClosure* cl2 = cl; ysr@777: FilterIntoCSClosure intoCSFilt(this, g1h, cl); ysr@777: FilterOutOfRegionClosure outOfRegionFilt(_hr, cl); ysr@777: switch (_fk) { ysr@777: case IntoCSFilterKind: cl2 = &intoCSFilt; break; ysr@777: case OutOfRegionFilterKind: cl2 = &outOfRegionFilt; break; ysr@777: } ysr@777: ysr@777: // Start filtering what we add to the remembered set. If the object is ysr@777: // not considered dead, either because it is marked (in the mark bitmap) ysr@777: // or it was allocated after marking finished, then we add it. Otherwise ysr@777: // we can safely ignore the object. ysr@777: if (!g1h->is_obj_dead(oop(bottom), _hr)) { ysr@777: #ifndef PRODUCT ysr@777: if (G1VerifyMarkingInEvac) { ysr@777: VerifyLiveClosure vl_cl(g1h); ysr@777: oop(bottom)->oop_iterate(&vl_cl, mr); ysr@777: } ysr@777: #endif ysr@777: oop_size = oop(bottom)->oop_iterate(cl2, mr); ysr@777: } else { ysr@777: oop_size = oop(bottom)->size(); ysr@777: } ysr@777: ysr@777: bottom += oop_size; ysr@777: ysr@777: if (bottom < top) { ysr@777: // We replicate the loop below for several kinds of possible filters. ysr@777: switch (_fk) { ysr@777: case NoFilterKind: ysr@777: bottom = walk_mem_region_loop(cl, g1h, _hr, bottom, top); ysr@777: break; ysr@777: case IntoCSFilterKind: { ysr@777: FilterIntoCSClosure filt(this, g1h, cl); ysr@777: bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top); ysr@777: break; ysr@777: } ysr@777: case OutOfRegionFilterKind: { ysr@777: FilterOutOfRegionClosure filt(_hr, cl); ysr@777: bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top); ysr@777: break; ysr@777: } ysr@777: default: ysr@777: ShouldNotReachHere(); ysr@777: } ysr@777: ysr@777: // Last object. Need to do dead-obj filtering here too. ysr@777: if (!g1h->is_obj_dead(oop(bottom), _hr)) { ysr@777: #ifndef PRODUCT ysr@777: if (G1VerifyMarkingInEvac) { ysr@777: VerifyLiveClosure vl_cl(g1h); ysr@777: oop(bottom)->oop_iterate(&vl_cl, mr); ysr@777: } ysr@777: #endif ysr@777: oop(bottom)->oop_iterate(cl2, mr); ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: void HeapRegion::reset_after_compaction() { ysr@777: G1OffsetTableContigSpace::reset_after_compaction(); ysr@777: // After a compaction the mark bitmap is invalid, so we must ysr@777: // treat all objects as being inside the unmarked area. ysr@777: zero_marked_bytes(); ysr@777: init_top_at_mark_start(); ysr@777: } ysr@777: ysr@777: ysr@777: ysr@777: DirtyCardToOopClosure* ysr@777: HeapRegion::new_dcto_closure(OopClosure* cl, ysr@777: CardTableModRefBS::PrecisionStyle precision, ysr@777: HeapRegionDCTOC::FilterKind fk) { ysr@777: return new HeapRegionDCTOC(G1CollectedHeap::heap(), ysr@777: this, cl, precision, fk); ysr@777: } ysr@777: ysr@777: void HeapRegion::hr_clear(bool par, bool clear_space) { tonyp@790: _humongous_type = NotHumongous; ysr@777: _humongous_start_region = NULL; ysr@777: _in_collection_set = false; ysr@777: _is_gc_alloc_region = false; ysr@777: ysr@777: // Age stuff (if parallel, this will be done separately, since it needs ysr@777: // to be sequential). ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: ysr@777: set_young_index_in_cset(-1); ysr@777: uninstall_surv_rate_group(); ysr@777: set_young_type(NotYoung); ysr@777: ysr@777: // In case it had been the start of a humongous sequence, reset its end. ysr@777: set_end(_orig_end); ysr@777: ysr@777: if (!par) { ysr@777: // If this is parallel, this will be done later. ysr@777: HeapRegionRemSet* hrrs = rem_set(); ysr@777: if (hrrs != NULL) hrrs->clear(); tonyp@790: _claimed = InitialClaimValue; ysr@777: } ysr@777: zero_marked_bytes(); ysr@777: set_sort_index(-1); ysr@777: if ((uintptr_t)bottom() >= (uintptr_t)g1h->popular_object_boundary()) ysr@777: set_popular(false); ysr@777: ysr@777: _offsets.resize(HeapRegion::GrainWords); ysr@777: init_top_at_mark_start(); ysr@777: if (clear_space) clear(); ysr@777: } ysr@777: ysr@777: // ysr@777: void HeapRegion::calc_gc_efficiency() { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: _gc_efficiency = (double) garbage_bytes() / ysr@777: g1h->predict_region_elapsed_time_ms(this, false); ysr@777: } ysr@777: // ysr@777: ysr@777: void HeapRegion::set_startsHumongous() { tonyp@790: _humongous_type = StartsHumongous; ysr@777: _humongous_start_region = this; ysr@777: assert(end() == _orig_end, "Should be normal before alloc."); ysr@777: } ysr@777: ysr@777: bool HeapRegion::claimHeapRegion(jint claimValue) { ysr@777: jint current = _claimed; ysr@777: if (current != claimValue) { ysr@777: jint res = Atomic::cmpxchg(claimValue, &_claimed, current); ysr@777: if (res == current) { ysr@777: return true; ysr@777: } ysr@777: } ysr@777: return false; ysr@777: } ysr@777: ysr@777: HeapWord* HeapRegion::next_block_start_careful(HeapWord* addr) { ysr@777: HeapWord* low = addr; ysr@777: HeapWord* high = end(); ysr@777: while (low < high) { ysr@777: size_t diff = pointer_delta(high, low); ysr@777: // Must add one below to bias toward the high amount. Otherwise, if ysr@777: // "high" were at the desired value, and "low" were one less, we ysr@777: // would not converge on "high". This is not symmetric, because ysr@777: // we set "high" to a block start, which might be the right one, ysr@777: // which we don't do for "low". ysr@777: HeapWord* middle = low + (diff+1)/2; ysr@777: if (middle == high) return high; ysr@777: HeapWord* mid_bs = block_start_careful(middle); ysr@777: if (mid_bs < addr) { ysr@777: low = middle; ysr@777: } else { ysr@777: high = mid_bs; ysr@777: } ysr@777: } ysr@777: assert(low == high && low >= addr, "Didn't work."); ysr@777: return low; ysr@777: } ysr@777: ysr@777: void HeapRegion::set_next_on_unclean_list(HeapRegion* r) { ysr@777: assert(r == NULL || r->is_on_unclean_list(), "Malformed unclean list."); ysr@777: _next_in_special_set = r; ysr@777: } ysr@777: ysr@777: void HeapRegion::set_on_unclean_list(bool b) { ysr@777: _is_on_unclean_list = b; ysr@777: } ysr@777: ysr@777: void HeapRegion::initialize(MemRegion mr, bool clear_space) { ysr@777: G1OffsetTableContigSpace::initialize(mr, false); ysr@777: hr_clear(false/*par*/, clear_space); ysr@777: } ysr@777: #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away ysr@777: #pragma warning( disable:4355 ) // 'this' : used in base member initializer list ysr@777: #endif // _MSC_VER ysr@777: ysr@777: ysr@777: HeapRegion:: ysr@777: HeapRegion(G1BlockOffsetSharedArray* sharedOffsetArray, ysr@777: MemRegion mr, bool is_zeroed) ysr@777: : G1OffsetTableContigSpace(sharedOffsetArray, mr, is_zeroed), ysr@777: _next_fk(HeapRegionDCTOC::NoFilterKind), ysr@777: _hrs_index(-1), tonyp@790: _humongous_type(NotHumongous), _humongous_start_region(NULL), ysr@777: _in_collection_set(false), _is_gc_alloc_region(false), ysr@777: _is_on_free_list(false), _is_on_unclean_list(false), ysr@777: _next_in_special_set(NULL), _orig_end(NULL), tonyp@790: _claimed(InitialClaimValue), _evacuation_failed(false), ysr@777: _prev_marked_bytes(0), _next_marked_bytes(0), _sort_index(-1), ysr@777: _popularity(NotPopular), ysr@777: _young_type(NotYoung), _next_young_region(NULL), ysr@777: _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1), ysr@777: _rem_set(NULL), _zfs(NotZeroFilled) ysr@777: { ysr@777: _orig_end = mr.end(); ysr@777: // Note that initialize() will set the start of the unmarked area of the ysr@777: // region. ysr@777: this->initialize(mr, !is_zeroed); ysr@777: ysr@777: _rem_set = new HeapRegionRemSet(sharedOffsetArray, this); ysr@777: ysr@777: assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant."); ysr@777: // In case the region is allocated during a pause, note the top. ysr@777: // We haven't done any counting on a brand new region. ysr@777: _top_at_conc_mark_count = bottom(); ysr@777: } ysr@777: ysr@777: class NextCompactionHeapRegionClosure: public HeapRegionClosure { ysr@777: const HeapRegion* _target; ysr@777: bool _target_seen; ysr@777: HeapRegion* _last; ysr@777: CompactibleSpace* _res; ysr@777: public: ysr@777: NextCompactionHeapRegionClosure(const HeapRegion* target) : ysr@777: _target(target), _target_seen(false), _res(NULL) {} ysr@777: bool doHeapRegion(HeapRegion* cur) { ysr@777: if (_target_seen) { ysr@777: if (!cur->isHumongous()) { ysr@777: _res = cur; ysr@777: return true; ysr@777: } ysr@777: } else if (cur == _target) { ysr@777: _target_seen = true; ysr@777: } ysr@777: return false; ysr@777: } ysr@777: CompactibleSpace* result() { return _res; } ysr@777: }; ysr@777: ysr@777: CompactibleSpace* HeapRegion::next_compaction_space() const { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: // cast away const-ness ysr@777: HeapRegion* r = (HeapRegion*) this; ysr@777: NextCompactionHeapRegionClosure blk(r); ysr@777: g1h->heap_region_iterate_from(r, &blk); ysr@777: return blk.result(); ysr@777: } ysr@777: ysr@777: void HeapRegion::set_continuesHumongous(HeapRegion* start) { ysr@777: // The order is important here. ysr@777: start->add_continuingHumongousRegion(this); tonyp@790: _humongous_type = ContinuesHumongous; ysr@777: _humongous_start_region = start; ysr@777: } ysr@777: ysr@777: void HeapRegion::add_continuingHumongousRegion(HeapRegion* cont) { ysr@777: // Must join the blocks of the current H region seq with the block of the ysr@777: // added region. ysr@777: offsets()->join_blocks(bottom(), cont->bottom()); ysr@777: arrayOop obj = (arrayOop)(bottom()); ysr@777: obj->set_length((int) (obj->length() + cont->capacity()/jintSize)); ysr@777: set_end(cont->end()); ysr@777: set_top(cont->end()); ysr@777: } ysr@777: ysr@777: void HeapRegion::save_marks() { ysr@777: set_saved_mark(); ysr@777: } ysr@777: ysr@777: void HeapRegion::oops_in_mr_iterate(MemRegion mr, OopClosure* cl) { ysr@777: HeapWord* p = mr.start(); ysr@777: HeapWord* e = mr.end(); ysr@777: oop obj; ysr@777: while (p < e) { ysr@777: obj = oop(p); ysr@777: p += obj->oop_iterate(cl); ysr@777: } ysr@777: assert(p == e, "bad memregion: doesn't end on obj boundary"); ysr@777: } ysr@777: ysr@777: #define HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \ ysr@777: void HeapRegion::oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \ ysr@777: ContiguousSpace::oop_since_save_marks_iterate##nv_suffix(cl); \ ysr@777: } ysr@777: SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN) ysr@777: ysr@777: ysr@777: void HeapRegion::oop_before_save_marks_iterate(OopClosure* cl) { ysr@777: oops_in_mr_iterate(MemRegion(bottom(), saved_mark_word()), cl); ysr@777: } ysr@777: ysr@777: #ifdef DEBUG ysr@777: HeapWord* HeapRegion::allocate(size_t size) { ysr@777: jint state = zero_fill_state(); ysr@777: assert(!G1CollectedHeap::heap()->allocs_are_zero_filled() || ysr@777: zero_fill_is_allocated(), ysr@777: "When ZF is on, only alloc in ZF'd regions"); ysr@777: return G1OffsetTableContigSpace::allocate(size); ysr@777: } ysr@777: #endif ysr@777: ysr@777: void HeapRegion::set_zero_fill_state_work(ZeroFillState zfs) { ysr@777: assert(top() == bottom() || zfs == Allocated, ysr@777: "Region must be empty, or we must be setting it to allocated."); ysr@777: assert(ZF_mon->owned_by_self() || ysr@777: Universe::heap()->is_gc_active(), ysr@777: "Must hold the lock or be a full GC to modify."); ysr@777: _zfs = zfs; ysr@777: } ysr@777: ysr@777: void HeapRegion::set_zero_fill_complete() { ysr@777: set_zero_fill_state_work(ZeroFilled); ysr@777: if (ZF_mon->owned_by_self()) { ysr@777: ZF_mon->notify_all(); ysr@777: } ysr@777: } ysr@777: ysr@777: ysr@777: void HeapRegion::ensure_zero_filled() { ysr@777: MutexLockerEx x(ZF_mon, Mutex::_no_safepoint_check_flag); ysr@777: ensure_zero_filled_locked(); ysr@777: } ysr@777: ysr@777: void HeapRegion::ensure_zero_filled_locked() { ysr@777: assert(ZF_mon->owned_by_self(), "Precondition"); ysr@777: bool should_ignore_zf = SafepointSynchronize::is_at_safepoint(); ysr@777: assert(should_ignore_zf || Heap_lock->is_locked(), ysr@777: "Either we're in a GC or we're allocating a region."); ysr@777: switch (zero_fill_state()) { ysr@777: case HeapRegion::NotZeroFilled: ysr@777: set_zero_fill_in_progress(Thread::current()); ysr@777: { ysr@777: ZF_mon->unlock(); ysr@777: Copy::fill_to_words(bottom(), capacity()/HeapWordSize); ysr@777: ZF_mon->lock_without_safepoint_check(); ysr@777: } ysr@777: // A trap. ysr@777: guarantee(zero_fill_state() == HeapRegion::ZeroFilling ysr@777: && zero_filler() == Thread::current(), ysr@777: "AHA! Tell Dave D if you see this..."); ysr@777: set_zero_fill_complete(); ysr@777: // gclog_or_tty->print_cr("Did sync ZF."); ysr@777: ConcurrentZFThread::note_sync_zfs(); ysr@777: break; ysr@777: case HeapRegion::ZeroFilling: ysr@777: if (should_ignore_zf) { ysr@777: // We can "break" the lock and take over the work. ysr@777: Copy::fill_to_words(bottom(), capacity()/HeapWordSize); ysr@777: set_zero_fill_complete(); ysr@777: ConcurrentZFThread::note_sync_zfs(); ysr@777: break; ysr@777: } else { ysr@777: ConcurrentZFThread::wait_for_ZF_completed(this); ysr@777: } ysr@777: case HeapRegion::ZeroFilled: ysr@777: // Nothing to do. ysr@777: break; ysr@777: case HeapRegion::Allocated: ysr@777: guarantee(false, "Should not call on allocated regions."); ysr@777: } ysr@777: assert(zero_fill_state() == HeapRegion::ZeroFilled, "Post"); ysr@777: } ysr@777: ysr@777: HeapWord* ysr@777: HeapRegion::object_iterate_mem_careful(MemRegion mr, ysr@777: ObjectClosure* cl) { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: // We used to use "block_start_careful" here. But we're actually happy ysr@777: // to update the BOT while we do this... ysr@777: HeapWord* cur = block_start(mr.start()); ysr@777: mr = mr.intersection(used_region()); ysr@777: if (mr.is_empty()) return NULL; ysr@777: // Otherwise, find the obj that extends onto mr.start(). ysr@777: ysr@777: assert(cur <= mr.start() ysr@777: && (oop(cur)->klass() == NULL || ysr@777: cur + oop(cur)->size() > mr.start()), ysr@777: "postcondition of block_start"); ysr@777: oop obj; ysr@777: while (cur < mr.end()) { ysr@777: obj = oop(cur); ysr@777: if (obj->klass() == NULL) { ysr@777: // Ran into an unparseable point. ysr@777: return cur; ysr@777: } else if (!g1h->is_obj_dead(obj)) { ysr@777: cl->do_object(obj); ysr@777: } ysr@777: if (cl->abort()) return cur; ysr@777: // The check above must occur before the operation below, since an ysr@777: // abort might invalidate the "size" operation. ysr@777: cur += obj->size(); ysr@777: } ysr@777: return NULL; ysr@777: } ysr@777: ysr@777: HeapWord* ysr@777: HeapRegion:: ysr@777: oops_on_card_seq_iterate_careful(MemRegion mr, ysr@777: FilterOutOfRegionClosure* cl) { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: ysr@777: // If we're within a stop-world GC, then we might look at a card in a ysr@777: // GC alloc region that extends onto a GC LAB, which may not be ysr@777: // parseable. Stop such at the "saved_mark" of the region. ysr@777: if (G1CollectedHeap::heap()->is_gc_active()) { ysr@777: mr = mr.intersection(used_region_at_save_marks()); ysr@777: } else { ysr@777: mr = mr.intersection(used_region()); ysr@777: } ysr@777: if (mr.is_empty()) return NULL; ysr@777: // Otherwise, find the obj that extends onto mr.start(). ysr@777: ysr@777: // We used to use "block_start_careful" here. But we're actually happy ysr@777: // to update the BOT while we do this... ysr@777: HeapWord* cur = block_start(mr.start()); ysr@777: assert(cur <= mr.start(), "Postcondition"); ysr@777: ysr@777: while (cur <= mr.start()) { ysr@777: if (oop(cur)->klass() == NULL) { ysr@777: // Ran into an unparseable point. ysr@777: return cur; ysr@777: } ysr@777: // Otherwise... ysr@777: int sz = oop(cur)->size(); ysr@777: if (cur + sz > mr.start()) break; ysr@777: // Otherwise, go on. ysr@777: cur = cur + sz; ysr@777: } ysr@777: oop obj; ysr@777: obj = oop(cur); ysr@777: // If we finish this loop... ysr@777: assert(cur <= mr.start() ysr@777: && obj->klass() != NULL ysr@777: && cur + obj->size() > mr.start(), ysr@777: "Loop postcondition"); ysr@777: if (!g1h->is_obj_dead(obj)) { ysr@777: obj->oop_iterate(cl, mr); ysr@777: } ysr@777: ysr@777: HeapWord* next; ysr@777: while (cur < mr.end()) { ysr@777: obj = oop(cur); ysr@777: if (obj->klass() == NULL) { ysr@777: // Ran into an unparseable point. ysr@777: return cur; ysr@777: }; ysr@777: // Otherwise: ysr@777: next = (cur + obj->size()); ysr@777: if (!g1h->is_obj_dead(obj)) { ysr@777: if (next < mr.end()) { ysr@777: obj->oop_iterate(cl); ysr@777: } else { ysr@777: // this obj spans the boundary. If it's an array, stop at the ysr@777: // boundary. ysr@777: if (obj->is_objArray()) { ysr@777: obj->oop_iterate(cl, mr); ysr@777: } else { ysr@777: obj->oop_iterate(cl); ysr@777: } ysr@777: } ysr@777: } ysr@777: cur = next; ysr@777: } ysr@777: return NULL; ysr@777: } ysr@777: ysr@777: void HeapRegion::print() const { print_on(gclog_or_tty); } ysr@777: void HeapRegion::print_on(outputStream* st) const { ysr@777: if (isHumongous()) { ysr@777: if (startsHumongous()) ysr@777: st->print(" HS"); ysr@777: else ysr@777: st->print(" HC"); ysr@777: } else { ysr@777: st->print(" "); ysr@777: } ysr@777: if (in_collection_set()) ysr@777: st->print(" CS"); ysr@777: else if (is_gc_alloc_region()) ysr@777: st->print(" A "); ysr@777: else ysr@777: st->print(" "); ysr@777: if (is_young()) ysr@777: st->print(is_scan_only() ? " SO" : (is_survivor() ? " SU" : " Y ")); ysr@777: else ysr@777: st->print(" "); ysr@777: if (is_empty()) ysr@777: st->print(" F"); ysr@777: else ysr@777: st->print(" "); ysr@777: st->print(" %d", _gc_time_stamp); ysr@777: G1OffsetTableContigSpace::print_on(st); ysr@777: } ysr@777: ysr@777: #define OBJ_SAMPLE_INTERVAL 0 ysr@777: #define BLOCK_SAMPLE_INTERVAL 100 ysr@777: ysr@777: // This really ought to be commoned up into OffsetTableContigSpace somehow. ysr@777: // We would need a mechanism to make that code skip dead objects. ysr@777: ysr@777: void HeapRegion::verify(bool allow_dirty) const { ysr@777: G1CollectedHeap* g1 = G1CollectedHeap::heap(); ysr@777: HeapWord* p = bottom(); ysr@777: HeapWord* prev_p = NULL; ysr@777: int objs = 0; ysr@777: int blocks = 0; ysr@777: VerifyLiveClosure vl_cl(g1); ysr@777: while (p < top()) { ysr@777: size_t size = oop(p)->size(); ysr@777: if (blocks == BLOCK_SAMPLE_INTERVAL) { ysr@777: guarantee(p == block_start_const(p + (size/2)), ysr@777: "check offset computation"); ysr@777: blocks = 0; ysr@777: } else { ysr@777: blocks++; ysr@777: } ysr@777: if (objs == OBJ_SAMPLE_INTERVAL) { ysr@777: oop obj = oop(p); ysr@777: if (!g1->is_obj_dead(obj, this)) { ysr@777: obj->verify(); ysr@777: vl_cl.set_containing_obj(obj); ysr@777: obj->oop_iterate(&vl_cl); ysr@777: if (G1MaxVerifyFailures >= 0 ysr@777: && vl_cl.n_failures() >= G1MaxVerifyFailures) break; ysr@777: } ysr@777: objs = 0; ysr@777: } else { ysr@777: objs++; ysr@777: } ysr@777: prev_p = p; ysr@777: p += size; ysr@777: } ysr@777: HeapWord* rend = end(); ysr@777: HeapWord* rtop = top(); ysr@777: if (rtop < rend) { ysr@777: guarantee(block_start_const(rtop + (rend - rtop) / 2) == rtop, ysr@777: "check offset computation"); ysr@777: } ysr@777: if (vl_cl.failures()) { ysr@777: gclog_or_tty->print_cr("Heap:"); ysr@777: G1CollectedHeap::heap()->print(); ysr@777: gclog_or_tty->print_cr(""); ysr@777: } ysr@777: if (G1VerifyConcMark && ysr@777: G1VerifyConcMarkPrintReachable && ysr@777: vl_cl.failures()) { ysr@777: g1->concurrent_mark()->print_prev_bitmap_reachable(); ysr@777: } ysr@777: guarantee(!vl_cl.failures(), "should not have had any failures"); ysr@777: guarantee(p == top(), "end of last object must match end of space"); ysr@777: } ysr@777: ysr@777: // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go ysr@777: // away eventually. ysr@777: ysr@777: void G1OffsetTableContigSpace::initialize(MemRegion mr, bool clear_space) { ysr@777: // false ==> we'll do the clearing if there's clearing to be done. ysr@777: ContiguousSpace::initialize(mr, false); ysr@777: _offsets.zero_bottom_entry(); ysr@777: _offsets.initialize_threshold(); ysr@777: if (clear_space) clear(); ysr@777: } ysr@777: ysr@777: void G1OffsetTableContigSpace::clear() { ysr@777: ContiguousSpace::clear(); ysr@777: _offsets.zero_bottom_entry(); ysr@777: _offsets.initialize_threshold(); ysr@777: } ysr@777: ysr@777: void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) { ysr@777: Space::set_bottom(new_bottom); ysr@777: _offsets.set_bottom(new_bottom); ysr@777: } ysr@777: ysr@777: void G1OffsetTableContigSpace::set_end(HeapWord* new_end) { ysr@777: Space::set_end(new_end); ysr@777: _offsets.resize(new_end - bottom()); ysr@777: } ysr@777: ysr@777: void G1OffsetTableContigSpace::print() const { ysr@777: print_short(); ysr@777: gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " ysr@777: INTPTR_FORMAT ", " INTPTR_FORMAT ")", ysr@777: bottom(), top(), _offsets.threshold(), end()); ysr@777: } ysr@777: ysr@777: HeapWord* G1OffsetTableContigSpace::initialize_threshold() { ysr@777: return _offsets.initialize_threshold(); ysr@777: } ysr@777: ysr@777: HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start, ysr@777: HeapWord* end) { ysr@777: _offsets.alloc_block(start, end); ysr@777: return _offsets.threshold(); ysr@777: } ysr@777: ysr@777: HeapWord* G1OffsetTableContigSpace::saved_mark_word() const { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: assert( _gc_time_stamp <= g1h->get_gc_time_stamp(), "invariant" ); ysr@777: if (_gc_time_stamp < g1h->get_gc_time_stamp()) ysr@777: return top(); ysr@777: else ysr@777: return ContiguousSpace::saved_mark_word(); ysr@777: } ysr@777: ysr@777: void G1OffsetTableContigSpace::set_saved_mark() { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: unsigned curr_gc_time_stamp = g1h->get_gc_time_stamp(); ysr@777: ysr@777: if (_gc_time_stamp < curr_gc_time_stamp) { ysr@777: // The order of these is important, as another thread might be ysr@777: // about to start scanning this region. If it does so after ysr@777: // set_saved_mark and before _gc_time_stamp = ..., then the latter ysr@777: // will be false, and it will pick up top() as the high water mark ysr@777: // of region. If it does so after _gc_time_stamp = ..., then it ysr@777: // will pick up the right saved_mark_word() as the high water mark ysr@777: // of the region. Either way, the behaviour will be correct. ysr@777: ContiguousSpace::set_saved_mark(); iveresov@788: _gc_time_stamp = curr_gc_time_stamp; iveresov@788: OrderAccess::fence(); ysr@777: } ysr@777: } ysr@777: ysr@777: G1OffsetTableContigSpace:: ysr@777: G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray, ysr@777: MemRegion mr, bool is_zeroed) : ysr@777: _offsets(sharedOffsetArray, mr), ysr@777: _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true), ysr@777: _gc_time_stamp(0) ysr@777: { ysr@777: _offsets.set_space(this); ysr@777: initialize(mr, !is_zeroed); ysr@777: } ysr@777: ysr@777: size_t RegionList::length() { ysr@777: size_t len = 0; ysr@777: HeapRegion* cur = hd(); ysr@777: DEBUG_ONLY(HeapRegion* last = NULL); ysr@777: while (cur != NULL) { ysr@777: len++; ysr@777: DEBUG_ONLY(last = cur); ysr@777: cur = get_next(cur); ysr@777: } ysr@777: assert(last == tl(), "Invariant"); ysr@777: return len; ysr@777: } ysr@777: ysr@777: void RegionList::insert_before_head(HeapRegion* r) { ysr@777: assert(well_formed(), "Inv"); ysr@777: set_next(r, hd()); ysr@777: _hd = r; ysr@777: _sz++; ysr@777: if (tl() == NULL) _tl = r; ysr@777: assert(well_formed(), "Inv"); ysr@777: } ysr@777: ysr@777: void RegionList::prepend_list(RegionList* new_list) { ysr@777: assert(well_formed(), "Precondition"); ysr@777: assert(new_list->well_formed(), "Precondition"); ysr@777: HeapRegion* new_tl = new_list->tl(); ysr@777: if (new_tl != NULL) { ysr@777: set_next(new_tl, hd()); ysr@777: _hd = new_list->hd(); ysr@777: _sz += new_list->sz(); ysr@777: if (tl() == NULL) _tl = new_list->tl(); ysr@777: } else { ysr@777: assert(new_list->hd() == NULL && new_list->sz() == 0, "Inv"); ysr@777: } ysr@777: assert(well_formed(), "Inv"); ysr@777: } ysr@777: ysr@777: void RegionList::delete_after(HeapRegion* r) { ysr@777: assert(well_formed(), "Precondition"); ysr@777: HeapRegion* next = get_next(r); ysr@777: assert(r != NULL, "Precondition"); ysr@777: HeapRegion* next_tl = get_next(next); ysr@777: set_next(r, next_tl); ysr@777: dec_sz(); ysr@777: if (next == tl()) { ysr@777: assert(next_tl == NULL, "Inv"); ysr@777: _tl = r; ysr@777: } ysr@777: assert(well_formed(), "Inv"); ysr@777: } ysr@777: ysr@777: HeapRegion* RegionList::pop() { ysr@777: assert(well_formed(), "Inv"); ysr@777: HeapRegion* res = hd(); ysr@777: if (res != NULL) { ysr@777: _hd = get_next(res); ysr@777: _sz--; ysr@777: set_next(res, NULL); ysr@777: if (sz() == 0) _tl = NULL; ysr@777: } ysr@777: assert(well_formed(), "Inv"); ysr@777: return res; ysr@777: }