aoqi@0: /* aoqi@0: * Copyright (c) 1997, 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: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "gc_implementation/shared/liveRange.hpp" aoqi@0: #include "gc_implementation/shared/markSweep.hpp" aoqi@0: #include "gc_implementation/shared/spaceDecorator.hpp" jmasa@7031: #include "gc_interface/collectedHeap.inline.hpp" aoqi@0: #include "memory/blockOffsetTable.inline.hpp" aoqi@0: #include "memory/defNewGeneration.hpp" aoqi@0: #include "memory/genCollectedHeap.hpp" aoqi@0: #include "memory/space.hpp" aoqi@0: #include "memory/space.inline.hpp" aoqi@0: #include "memory/universe.inline.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "oops/oop.inline2.hpp" aoqi@0: #include "runtime/java.hpp" goetz@6912: #include "runtime/prefetch.inline.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" aoqi@0: #include "runtime/safepoint.hpp" aoqi@0: #include "utilities/copy.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: HeapWord* DirtyCardToOopClosure::get_actual_top(HeapWord* top, aoqi@0: HeapWord* top_obj) { aoqi@0: if (top_obj != NULL) { aoqi@0: if (_sp->block_is_obj(top_obj)) { aoqi@0: if (_precision == CardTableModRefBS::ObjHeadPreciseArray) { aoqi@0: if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) { aoqi@0: // An arrayOop is starting on the dirty card - since we do exact aoqi@0: // store checks for objArrays we are done. aoqi@0: } else { aoqi@0: // Otherwise, it is possible that the object starting on the dirty aoqi@0: // card spans the entire card, and that the store happened on a aoqi@0: // later card. Figure out where the object ends. aoqi@0: // Use the block_size() method of the space over which aoqi@0: // the iteration is being done. That space (e.g. CMS) may have aoqi@0: // specific requirements on object sizes which will aoqi@0: // be reflected in the block_size() method. aoqi@0: top = top_obj + oop(top_obj)->size(); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: top = top_obj; aoqi@0: } aoqi@0: } else { aoqi@0: assert(top == _sp->end(), "only case where top_obj == NULL"); aoqi@0: } aoqi@0: return top; aoqi@0: } aoqi@0: aoqi@0: void DirtyCardToOopClosure::walk_mem_region(MemRegion mr, aoqi@0: HeapWord* bottom, aoqi@0: HeapWord* top) { aoqi@0: // 1. Blocks may or may not be objects. aoqi@0: // 2. Even when a block_is_obj(), it may not entirely aoqi@0: // occupy the block if the block quantum is larger than aoqi@0: // the object size. aoqi@0: // We can and should try to optimize by calling the non-MemRegion aoqi@0: // version of oop_iterate() for all but the extremal objects aoqi@0: // (for which we need to call the MemRegion version of aoqi@0: // oop_iterate()) To be done post-beta XXX aoqi@0: for (; bottom < top; bottom += _sp->block_size(bottom)) { aoqi@0: // As in the case of contiguous space above, we'd like to aoqi@0: // just use the value returned by oop_iterate to increment the aoqi@0: // current pointer; unfortunately, that won't work in CMS because aoqi@0: // we'd need an interface change (it seems) to have the space aoqi@0: // "adjust the object size" (for instance pad it up to its aoqi@0: // block alignment or minimum block size restrictions. XXX aoqi@0: if (_sp->block_is_obj(bottom) && aoqi@0: !_sp->obj_allocated_since_save_marks(oop(bottom))) { aoqi@0: oop(bottom)->oop_iterate(_cl, mr); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We get called with "mr" representing the dirty region aoqi@0: // that we want to process. Because of imprecise marking, aoqi@0: // we may need to extend the incoming "mr" to the right, aoqi@0: // and scan more. However, because we may already have aoqi@0: // scanned some of that extended region, we may need to aoqi@0: // trim its right-end back some so we do not scan what aoqi@0: // we (or another worker thread) may already have scanned aoqi@0: // or planning to scan. aoqi@0: void DirtyCardToOopClosure::do_MemRegion(MemRegion mr) { aoqi@0: aoqi@0: // Some collectors need to do special things whenever their dirty aoqi@0: // cards are processed. For instance, CMS must remember mutator updates aoqi@0: // (i.e. dirty cards) so as to re-scan mutated objects. aoqi@0: // Such work can be piggy-backed here on dirty card scanning, so as to make aoqi@0: // it slightly more efficient than doing a complete non-detructive pre-scan aoqi@0: // of the card table. aoqi@0: MemRegionClosure* pCl = _sp->preconsumptionDirtyCardClosure(); aoqi@0: if (pCl != NULL) { aoqi@0: pCl->do_MemRegion(mr); aoqi@0: } aoqi@0: aoqi@0: HeapWord* bottom = mr.start(); aoqi@0: HeapWord* last = mr.last(); aoqi@0: HeapWord* top = mr.end(); aoqi@0: HeapWord* bottom_obj; aoqi@0: HeapWord* top_obj; aoqi@0: aoqi@0: assert(_precision == CardTableModRefBS::ObjHeadPreciseArray || aoqi@0: _precision == CardTableModRefBS::Precise, aoqi@0: "Only ones we deal with for now."); aoqi@0: aoqi@0: assert(_precision != CardTableModRefBS::ObjHeadPreciseArray || aoqi@0: _cl->idempotent() || _last_bottom == NULL || aoqi@0: top <= _last_bottom, aoqi@0: "Not decreasing"); aoqi@0: NOT_PRODUCT(_last_bottom = mr.start()); aoqi@0: aoqi@0: bottom_obj = _sp->block_start(bottom); aoqi@0: top_obj = _sp->block_start(last); aoqi@0: aoqi@0: assert(bottom_obj <= bottom, "just checking"); aoqi@0: assert(top_obj <= top, "just checking"); aoqi@0: aoqi@0: // Given what we think is the top of the memory region and aoqi@0: // the start of the object at the top, get the actual aoqi@0: // value of the top. aoqi@0: top = get_actual_top(top, top_obj); aoqi@0: aoqi@0: // If the previous call did some part of this region, don't redo. aoqi@0: if (_precision == CardTableModRefBS::ObjHeadPreciseArray && aoqi@0: _min_done != NULL && aoqi@0: _min_done < top) { aoqi@0: top = _min_done; aoqi@0: } aoqi@0: aoqi@0: // Top may have been reset, and in fact may be below bottom, aoqi@0: // e.g. the dirty card region is entirely in a now free object aoqi@0: // -- something that could happen with a concurrent sweeper. aoqi@0: bottom = MIN2(bottom, top); aoqi@0: MemRegion extended_mr = MemRegion(bottom, top); aoqi@0: assert(bottom <= top && aoqi@0: (_precision != CardTableModRefBS::ObjHeadPreciseArray || aoqi@0: _min_done == NULL || aoqi@0: top <= _min_done), aoqi@0: "overlap!"); aoqi@0: aoqi@0: // Walk the region if it is not empty; otherwise there is nothing to do. aoqi@0: if (!extended_mr.is_empty()) { aoqi@0: walk_mem_region(extended_mr, bottom_obj, top); aoqi@0: } aoqi@0: aoqi@0: // An idempotent closure might be applied in any order, so we don't aoqi@0: // record a _min_done for it. aoqi@0: if (!_cl->idempotent()) { aoqi@0: _min_done = bottom; aoqi@0: } else { aoqi@0: assert(_min_done == _last_explicit_min_done, aoqi@0: "Don't update _min_done for idempotent cl"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: DirtyCardToOopClosure* Space::new_dcto_cl(ExtendedOopClosure* cl, aoqi@0: CardTableModRefBS::PrecisionStyle precision, aoqi@0: HeapWord* boundary) { aoqi@0: return new DirtyCardToOopClosure(this, cl, precision, boundary); aoqi@0: } aoqi@0: aoqi@0: HeapWord* ContiguousSpaceDCTOC::get_actual_top(HeapWord* top, aoqi@0: HeapWord* top_obj) { aoqi@0: if (top_obj != NULL && top_obj < (_sp->toContiguousSpace())->top()) { aoqi@0: if (_precision == CardTableModRefBS::ObjHeadPreciseArray) { aoqi@0: if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) { aoqi@0: // An arrayOop is starting on the dirty card - since we do exact aoqi@0: // store checks for objArrays we are done. aoqi@0: } else { aoqi@0: // Otherwise, it is possible that the object starting on the dirty aoqi@0: // card spans the entire card, and that the store happened on a aoqi@0: // later card. Figure out where the object ends. aoqi@0: assert(_sp->block_size(top_obj) == (size_t) oop(top_obj)->size(), aoqi@0: "Block size and object size mismatch"); aoqi@0: top = top_obj + oop(top_obj)->size(); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: top = (_sp->toContiguousSpace())->top(); aoqi@0: } aoqi@0: return top; aoqi@0: } aoqi@0: aoqi@0: void Filtering_DCTOC::walk_mem_region(MemRegion mr, aoqi@0: HeapWord* bottom, aoqi@0: HeapWord* top) { aoqi@0: // Note that this assumption won't hold if we have a concurrent aoqi@0: // collector in this space, which may have freed up objects after aoqi@0: // they were dirtied and before the stop-the-world GC that is aoqi@0: // examining cards here. aoqi@0: assert(bottom < top, "ought to be at least one obj on a dirty card."); aoqi@0: aoqi@0: if (_boundary != NULL) { aoqi@0: // We have a boundary outside of which we don't want to look aoqi@0: // at objects, so create a filtering closure around the aoqi@0: // oop closure before walking the region. aoqi@0: FilteringClosure filter(_boundary, _cl); aoqi@0: walk_mem_region_with_cl(mr, bottom, top, &filter); aoqi@0: } else { aoqi@0: // No boundary, simply walk the heap with the oop closure. aoqi@0: walk_mem_region_with_cl(mr, bottom, top, _cl); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: // We must replicate this so that the static type of "FilteringClosure" aoqi@0: // (see above) is apparent at the oop_iterate calls. aoqi@0: #define ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ClosureType) \ aoqi@0: void ContiguousSpaceDCTOC::walk_mem_region_with_cl(MemRegion mr, \ aoqi@0: HeapWord* bottom, \ aoqi@0: HeapWord* top, \ aoqi@0: ClosureType* cl) { \ aoqi@0: bottom += oop(bottom)->oop_iterate(cl, mr); \ aoqi@0: if (bottom < top) { \ aoqi@0: HeapWord* next_obj = bottom + oop(bottom)->size(); \ aoqi@0: while (next_obj < top) { \ aoqi@0: /* Bottom lies entirely below top, so we can call the */ \ aoqi@0: /* non-memRegion version of oop_iterate below. */ \ aoqi@0: oop(bottom)->oop_iterate(cl); \ aoqi@0: bottom = next_obj; \ aoqi@0: next_obj = bottom + oop(bottom)->size(); \ aoqi@0: } \ aoqi@0: /* Last object. */ \ aoqi@0: oop(bottom)->oop_iterate(cl, mr); \ aoqi@0: } \ aoqi@0: } aoqi@0: aoqi@0: // (There are only two of these, rather than N, because the split is due aoqi@0: // only to the introduction of the FilteringClosure, a local part of the aoqi@0: // impl of this abstraction.) aoqi@0: ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ExtendedOopClosure) aoqi@0: ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(FilteringClosure) aoqi@0: aoqi@0: DirtyCardToOopClosure* aoqi@0: ContiguousSpace::new_dcto_cl(ExtendedOopClosure* cl, aoqi@0: CardTableModRefBS::PrecisionStyle precision, aoqi@0: HeapWord* boundary) { aoqi@0: return new ContiguousSpaceDCTOC(this, cl, precision, boundary); aoqi@0: } aoqi@0: aoqi@0: void Space::initialize(MemRegion mr, aoqi@0: bool clear_space, aoqi@0: bool mangle_space) { aoqi@0: HeapWord* bottom = mr.start(); aoqi@0: HeapWord* end = mr.end(); aoqi@0: assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end), aoqi@0: "invalid space boundaries"); aoqi@0: set_bottom(bottom); aoqi@0: set_end(end); aoqi@0: if (clear_space) clear(mangle_space); aoqi@0: } aoqi@0: aoqi@0: void Space::clear(bool mangle_space) { aoqi@0: if (ZapUnusedHeapArea && mangle_space) { aoqi@0: mangle_unused_area(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ContiguousSpace::ContiguousSpace(): CompactibleSpace(), _top(NULL), aoqi@0: _concurrent_iteration_safe_limit(NULL) { aoqi@0: _mangler = new GenSpaceMangler(this); aoqi@0: } aoqi@0: aoqi@0: ContiguousSpace::~ContiguousSpace() { aoqi@0: delete _mangler; aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::initialize(MemRegion mr, aoqi@0: bool clear_space, aoqi@0: bool mangle_space) aoqi@0: { aoqi@0: CompactibleSpace::initialize(mr, clear_space, mangle_space); aoqi@0: set_concurrent_iteration_safe_limit(top()); aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::clear(bool mangle_space) { aoqi@0: set_top(bottom()); aoqi@0: set_saved_mark(); aoqi@0: CompactibleSpace::clear(mangle_space); aoqi@0: } aoqi@0: aoqi@0: bool ContiguousSpace::is_free_block(const HeapWord* p) const { aoqi@0: return p >= _top; aoqi@0: } aoqi@0: aoqi@0: void OffsetTableContigSpace::clear(bool mangle_space) { aoqi@0: ContiguousSpace::clear(mangle_space); aoqi@0: _offsets.initialize_threshold(); aoqi@0: } aoqi@0: aoqi@0: void OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) { aoqi@0: Space::set_bottom(new_bottom); aoqi@0: _offsets.set_bottom(new_bottom); aoqi@0: } aoqi@0: aoqi@0: void OffsetTableContigSpace::set_end(HeapWord* new_end) { aoqi@0: // Space should not advertize an increase in size aoqi@0: // until after the underlying offest table has been enlarged. aoqi@0: _offsets.resize(pointer_delta(new_end, bottom())); aoqi@0: Space::set_end(new_end); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: void ContiguousSpace::set_top_for_allocations(HeapWord* v) { aoqi@0: mangler()->set_top_for_allocations(v); aoqi@0: } aoqi@0: void ContiguousSpace::set_top_for_allocations() { aoqi@0: mangler()->set_top_for_allocations(top()); aoqi@0: } aoqi@0: void ContiguousSpace::check_mangled_unused_area(HeapWord* limit) { aoqi@0: mangler()->check_mangled_unused_area(limit); aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::check_mangled_unused_area_complete() { aoqi@0: mangler()->check_mangled_unused_area_complete(); aoqi@0: } aoqi@0: aoqi@0: // Mangled only the unused space that has not previously aoqi@0: // been mangled and that has not been allocated since being aoqi@0: // mangled. aoqi@0: void ContiguousSpace::mangle_unused_area() { aoqi@0: mangler()->mangle_unused_area(); aoqi@0: } aoqi@0: void ContiguousSpace::mangle_unused_area_complete() { aoqi@0: mangler()->mangle_unused_area_complete(); aoqi@0: } aoqi@0: void ContiguousSpace::mangle_region(MemRegion mr) { aoqi@0: // Although this method uses SpaceMangler::mangle_region() which aoqi@0: // is not specific to a space, the when the ContiguousSpace version aoqi@0: // is called, it is always with regard to a space and this aoqi@0: // bounds checking is appropriate. aoqi@0: MemRegion space_mr(bottom(), end()); aoqi@0: assert(space_mr.contains(mr), "Mangling outside space"); aoqi@0: SpaceMangler::mangle_region(mr); aoqi@0: } aoqi@0: #endif // NOT_PRODUCT aoqi@0: aoqi@0: void CompactibleSpace::initialize(MemRegion mr, aoqi@0: bool clear_space, aoqi@0: bool mangle_space) { aoqi@0: Space::initialize(mr, clear_space, mangle_space); aoqi@0: set_compaction_top(bottom()); aoqi@0: _next_compaction_space = NULL; aoqi@0: } aoqi@0: aoqi@0: void CompactibleSpace::clear(bool mangle_space) { aoqi@0: Space::clear(mangle_space); aoqi@0: _compaction_top = bottom(); aoqi@0: } aoqi@0: aoqi@0: HeapWord* CompactibleSpace::forward(oop q, size_t size, aoqi@0: CompactPoint* cp, HeapWord* compact_top) { aoqi@0: // q is alive aoqi@0: // First check if we should switch compaction space aoqi@0: assert(this == cp->space, "'this' should be current compaction space."); aoqi@0: size_t compaction_max_size = pointer_delta(end(), compact_top); aoqi@0: while (size > compaction_max_size) { aoqi@0: // switch to next compaction space aoqi@0: cp->space->set_compaction_top(compact_top); aoqi@0: cp->space = cp->space->next_compaction_space(); aoqi@0: if (cp->space == NULL) { aoqi@0: cp->gen = GenCollectedHeap::heap()->prev_gen(cp->gen); aoqi@0: assert(cp->gen != NULL, "compaction must succeed"); aoqi@0: cp->space = cp->gen->first_compaction_space(); aoqi@0: assert(cp->space != NULL, "generation must have a first compaction space"); aoqi@0: } aoqi@0: compact_top = cp->space->bottom(); aoqi@0: cp->space->set_compaction_top(compact_top); aoqi@0: cp->threshold = cp->space->initialize_threshold(); aoqi@0: compaction_max_size = pointer_delta(cp->space->end(), compact_top); aoqi@0: } aoqi@0: aoqi@0: // store the forwarding pointer into the mark word aoqi@0: if ((HeapWord*)q != compact_top) { aoqi@0: q->forward_to(oop(compact_top)); aoqi@0: assert(q->is_gc_marked(), "encoding the pointer should preserve the mark"); aoqi@0: } else { aoqi@0: // if the object isn't moving we can just set the mark to the default aoqi@0: // mark and handle it specially later on. aoqi@0: q->init_mark(); aoqi@0: assert(q->forwardee() == NULL, "should be forwarded to NULL"); aoqi@0: } aoqi@0: aoqi@0: compact_top += size; aoqi@0: aoqi@0: // we need to update the offset table so that the beginnings of objects can be aoqi@0: // found during scavenge. Note that we are updating the offset table based on aoqi@0: // where the object will be once the compaction phase finishes. aoqi@0: if (compact_top > cp->threshold) aoqi@0: cp->threshold = aoqi@0: cp->space->cross_threshold(compact_top - size, compact_top); aoqi@0: return compact_top; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool CompactibleSpace::insert_deadspace(size_t& allowed_deadspace_words, aoqi@0: HeapWord* q, size_t deadlength) { aoqi@0: if (allowed_deadspace_words >= deadlength) { aoqi@0: allowed_deadspace_words -= deadlength; aoqi@0: CollectedHeap::fill_with_object(q, deadlength); aoqi@0: oop(q)->set_mark(oop(q)->mark()->set_marked()); aoqi@0: assert((int) deadlength == oop(q)->size(), "bad filler object size"); aoqi@0: // Recall that we required "q == compaction_top". aoqi@0: return true; aoqi@0: } else { aoqi@0: allowed_deadspace_words = 0; aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #define block_is_always_obj(q) true aoqi@0: #define obj_size(q) oop(q)->size() aoqi@0: #define adjust_obj_size(s) s aoqi@0: aoqi@0: void CompactibleSpace::prepare_for_compaction(CompactPoint* cp) { aoqi@0: SCAN_AND_FORWARD(cp, end, block_is_obj, block_size); aoqi@0: } aoqi@0: aoqi@0: // Faster object search. aoqi@0: void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) { aoqi@0: SCAN_AND_FORWARD(cp, top, block_is_always_obj, obj_size); aoqi@0: } aoqi@0: aoqi@0: void Space::adjust_pointers() { aoqi@0: // adjust all the interior pointers to point at the new locations of objects aoqi@0: // Used by MarkSweep::mark_sweep_phase3() aoqi@0: aoqi@0: // First check to see if there is any work to be done. aoqi@0: if (used() == 0) { aoqi@0: return; // Nothing to do. aoqi@0: } aoqi@0: aoqi@0: // Otherwise... aoqi@0: HeapWord* q = bottom(); aoqi@0: HeapWord* t = end(); aoqi@0: aoqi@0: debug_only(HeapWord* prev_q = NULL); aoqi@0: while (q < t) { aoqi@0: if (oop(q)->is_gc_marked()) { aoqi@0: // q is alive aoqi@0: aoqi@0: // point all the oops to the new location aoqi@0: size_t size = oop(q)->adjust_pointers(); aoqi@0: aoqi@0: debug_only(prev_q = q); aoqi@0: aoqi@0: q += size; aoqi@0: } else { aoqi@0: // q is not a live object. But we're not in a compactible space, aoqi@0: // So we don't have live ranges. aoqi@0: debug_only(prev_q = q); aoqi@0: q += block_size(q); aoqi@0: assert(q > prev_q, "we should be moving forward through memory"); aoqi@0: } aoqi@0: } aoqi@0: assert(q == t, "just checking"); aoqi@0: } aoqi@0: aoqi@0: void CompactibleSpace::adjust_pointers() { aoqi@0: // Check first is there is any work to do. aoqi@0: if (used() == 0) { aoqi@0: return; // Nothing to do. aoqi@0: } aoqi@0: aoqi@0: SCAN_AND_ADJUST_POINTERS(adjust_obj_size); aoqi@0: } aoqi@0: aoqi@0: void CompactibleSpace::compact() { aoqi@0: SCAN_AND_COMPACT(obj_size); aoqi@0: } aoqi@0: aoqi@0: void Space::print_short() const { print_short_on(tty); } aoqi@0: aoqi@0: void Space::print_short_on(outputStream* st) const { aoqi@0: st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K, aoqi@0: (int) ((double) used() * 100 / capacity())); aoqi@0: } aoqi@0: aoqi@0: void Space::print() const { print_on(tty); } aoqi@0: aoqi@0: void Space::print_on(outputStream* st) const { aoqi@0: print_short_on(st); aoqi@0: st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ")", aoqi@0: bottom(), end()); aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::print_on(outputStream* st) const { aoqi@0: print_short_on(st); aoqi@0: st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", aoqi@0: bottom(), top(), end()); aoqi@0: } aoqi@0: aoqi@0: void OffsetTableContigSpace::print_on(outputStream* st) const { aoqi@0: print_short_on(st); aoqi@0: st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " aoqi@0: INTPTR_FORMAT ", " INTPTR_FORMAT ")", aoqi@0: bottom(), top(), _offsets.threshold(), end()); aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::verify() const { aoqi@0: HeapWord* p = bottom(); aoqi@0: HeapWord* t = top(); aoqi@0: HeapWord* prev_p = NULL; aoqi@0: while (p < t) { aoqi@0: oop(p)->verify(); aoqi@0: prev_p = p; aoqi@0: p += oop(p)->size(); aoqi@0: } aoqi@0: guarantee(p == top(), "end of last object must match end of space"); aoqi@0: if (top() != end()) { aoqi@0: guarantee(top() == block_start_const(end()-1) && aoqi@0: top() == block_start_const(top()), aoqi@0: "top should be start of unallocated block, if it exists"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void Space::oop_iterate(ExtendedOopClosure* blk) { aoqi@0: ObjectToOopClosure blk2(blk); aoqi@0: object_iterate(&blk2); aoqi@0: } aoqi@0: aoqi@0: bool Space::obj_is_alive(const HeapWord* p) const { aoqi@0: assert (block_is_obj(p), "The address should point to an object"); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: #define ContigSpace_PAR_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \ aoqi@0: \ aoqi@0: void ContiguousSpace::par_oop_iterate(MemRegion mr, OopClosureType* blk) {\ aoqi@0: HeapWord* obj_addr = mr.start(); \ aoqi@0: HeapWord* t = mr.end(); \ aoqi@0: while (obj_addr < t) { \ aoqi@0: assert(oop(obj_addr)->is_oop(), "Should be an oop"); \ aoqi@0: obj_addr += oop(obj_addr)->oop_iterate(blk); \ aoqi@0: } \ aoqi@0: } aoqi@0: aoqi@0: ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DEFN) aoqi@0: aoqi@0: #undef ContigSpace_PAR_OOP_ITERATE_DEFN aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: void ContiguousSpace::oop_iterate(ExtendedOopClosure* blk) { aoqi@0: if (is_empty()) return; aoqi@0: HeapWord* obj_addr = bottom(); aoqi@0: HeapWord* t = top(); aoqi@0: // Could call objects iterate, but this is easier. aoqi@0: while (obj_addr < t) { aoqi@0: obj_addr += oop(obj_addr)->oop_iterate(blk); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::object_iterate(ObjectClosure* blk) { aoqi@0: if (is_empty()) return; aoqi@0: WaterMark bm = bottom_mark(); aoqi@0: object_iterate_from(bm, blk); aoqi@0: } aoqi@0: aoqi@0: // For a continguous space object_iterate() and safe_object_iterate() aoqi@0: // are the same. aoqi@0: void ContiguousSpace::safe_object_iterate(ObjectClosure* blk) { aoqi@0: object_iterate(blk); aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::object_iterate_from(WaterMark mark, ObjectClosure* blk) { aoqi@0: assert(mark.space() == this, "Mark does not match space"); aoqi@0: HeapWord* p = mark.point(); aoqi@0: while (p < top()) { aoqi@0: blk->do_object(oop(p)); aoqi@0: p += oop(p)->size(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: HeapWord* aoqi@0: ContiguousSpace::object_iterate_careful(ObjectClosureCareful* blk) { aoqi@0: HeapWord * limit = concurrent_iteration_safe_limit(); aoqi@0: assert(limit <= top(), "sanity check"); aoqi@0: for (HeapWord* p = bottom(); p < limit;) { aoqi@0: size_t size = blk->do_object_careful(oop(p)); aoqi@0: if (size == 0) { aoqi@0: return p; // failed at p aoqi@0: } else { aoqi@0: p += size; aoqi@0: } aoqi@0: } aoqi@0: return NULL; // all done aoqi@0: } aoqi@0: aoqi@0: #define ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \ aoqi@0: \ aoqi@0: void ContiguousSpace:: \ aoqi@0: oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) { \ aoqi@0: HeapWord* t; \ aoqi@0: HeapWord* p = saved_mark_word(); \ aoqi@0: assert(p != NULL, "expected saved mark"); \ aoqi@0: \ aoqi@0: const intx interval = PrefetchScanIntervalInBytes; \ aoqi@0: do { \ aoqi@0: t = top(); \ aoqi@0: while (p < t) { \ aoqi@0: Prefetch::write(p, interval); \ aoqi@0: debug_only(HeapWord* prev = p); \ aoqi@0: oop m = oop(p); \ aoqi@0: p += m->oop_iterate(blk); \ aoqi@0: } \ aoqi@0: } while (t < top()); \ aoqi@0: \ aoqi@0: set_saved_mark_word(p); \ aoqi@0: } aoqi@0: aoqi@0: ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN) aoqi@0: aoqi@0: #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN aoqi@0: aoqi@0: // Very general, slow implementation. aoqi@0: HeapWord* ContiguousSpace::block_start_const(const void* p) const { aoqi@0: assert(MemRegion(bottom(), end()).contains(p), aoqi@0: err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")", aoqi@0: p, bottom(), end())); aoqi@0: if (p >= top()) { aoqi@0: return top(); aoqi@0: } else { aoqi@0: HeapWord* last = bottom(); aoqi@0: HeapWord* cur = last; aoqi@0: while (cur <= p) { aoqi@0: last = cur; aoqi@0: cur += oop(cur)->size(); aoqi@0: } aoqi@0: assert(oop(last)->is_oop(), aoqi@0: err_msg(PTR_FORMAT " should be an object start", last)); aoqi@0: return last; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: size_t ContiguousSpace::block_size(const HeapWord* p) const { aoqi@0: assert(MemRegion(bottom(), end()).contains(p), aoqi@0: err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")", aoqi@0: p, bottom(), end())); aoqi@0: HeapWord* current_top = top(); aoqi@0: assert(p <= current_top, aoqi@0: err_msg("p > current top - p: " PTR_FORMAT ", current top: " PTR_FORMAT, aoqi@0: p, current_top)); aoqi@0: assert(p == current_top || oop(p)->is_oop(), aoqi@0: err_msg("p (" PTR_FORMAT ") is not a block start - " aoqi@0: "current_top: " PTR_FORMAT ", is_oop: %s", aoqi@0: p, current_top, BOOL_TO_STR(oop(p)->is_oop()))); aoqi@0: if (p < current_top) { aoqi@0: return oop(p)->size(); aoqi@0: } else { aoqi@0: assert(p == current_top, "just checking"); aoqi@0: return pointer_delta(end(), (HeapWord*) p); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // This version requires locking. aoqi@0: inline HeapWord* ContiguousSpace::allocate_impl(size_t size, aoqi@0: HeapWord* const end_value) { aoqi@0: assert(Heap_lock->owned_by_self() || mgerdin@6990: (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()), aoqi@0: "not locked"); aoqi@0: HeapWord* obj = top(); aoqi@0: if (pointer_delta(end_value, obj) >= size) { aoqi@0: HeapWord* new_top = obj + size; aoqi@0: set_top(new_top); aoqi@0: assert(is_aligned(obj) && is_aligned(new_top), "checking alignment"); aoqi@0: return obj; aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // This version is lock-free. aoqi@0: inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size, aoqi@0: HeapWord* const end_value) { aoqi@0: do { aoqi@0: HeapWord* obj = top(); aoqi@0: if (pointer_delta(end_value, obj) >= size) { aoqi@0: HeapWord* new_top = obj + size; aoqi@0: HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj); aoqi@0: // result can be one of two: aoqi@0: // the old top value: the exchange succeeded aoqi@0: // otherwise: the new value of the top is returned. aoqi@0: if (result == obj) { aoqi@0: assert(is_aligned(obj) && is_aligned(new_top), "checking alignment"); aoqi@0: return obj; aoqi@0: } aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } while (true); aoqi@0: } aoqi@0: jmasa@7031: HeapWord* ContiguousSpace::allocate_aligned(size_t size) { jmasa@7031: assert(Heap_lock->owned_by_self() || (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()), "not locked"); jmasa@7031: HeapWord* end_value = end(); jmasa@7031: jmasa@7031: HeapWord* obj = CollectedHeap::align_allocation_or_fail(top(), end_value, SurvivorAlignmentInBytes); jmasa@7031: if (obj == NULL) { jmasa@7031: return NULL; jmasa@7031: } jmasa@7031: jmasa@7031: if (pointer_delta(end_value, obj) >= size) { jmasa@7031: HeapWord* new_top = obj + size; jmasa@7031: set_top(new_top); jmasa@7031: assert(is_ptr_aligned(obj, SurvivorAlignmentInBytes) && is_aligned(new_top), jmasa@7031: "checking alignment"); jmasa@7031: return obj; jmasa@7031: } else { jmasa@7031: set_top(obj); jmasa@7031: return NULL; jmasa@7031: } jmasa@7031: } jmasa@7031: aoqi@0: // Requires locking. aoqi@0: HeapWord* ContiguousSpace::allocate(size_t size) { aoqi@0: return allocate_impl(size, end()); aoqi@0: } aoqi@0: aoqi@0: // Lock-free. aoqi@0: HeapWord* ContiguousSpace::par_allocate(size_t size) { aoqi@0: return par_allocate_impl(size, end()); aoqi@0: } aoqi@0: aoqi@0: void ContiguousSpace::allocate_temporary_filler(int factor) { aoqi@0: // allocate temporary type array decreasing free size with factor 'factor' aoqi@0: assert(factor >= 0, "just checking"); aoqi@0: size_t size = pointer_delta(end(), top()); aoqi@0: aoqi@0: // if space is full, return aoqi@0: if (size == 0) return; aoqi@0: aoqi@0: if (factor > 0) { aoqi@0: size -= size/factor; aoqi@0: } aoqi@0: size = align_object_size(size); aoqi@0: aoqi@0: const size_t array_header_size = typeArrayOopDesc::header_size(T_INT); aoqi@0: if (size >= (size_t)align_object_size(array_header_size)) { aoqi@0: size_t length = (size - array_header_size) * (HeapWordSize / sizeof(jint)); aoqi@0: // allocate uninitialized int array aoqi@0: typeArrayOop t = (typeArrayOop) allocate(size); aoqi@0: assert(t != NULL, "allocation should succeed"); aoqi@0: t->set_mark(markOopDesc::prototype()); aoqi@0: t->set_klass(Universe::intArrayKlassObj()); aoqi@0: t->set_length((int)length); aoqi@0: } else { aoqi@0: assert(size == CollectedHeap::min_fill_size(), aoqi@0: "size for smallest fake object doesn't match"); aoqi@0: instanceOop obj = (instanceOop) allocate(size); aoqi@0: obj->set_mark(markOopDesc::prototype()); aoqi@0: obj->set_klass_gap(0); aoqi@0: obj->set_klass(SystemDictionary::Object_klass()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void EdenSpace::clear(bool mangle_space) { aoqi@0: ContiguousSpace::clear(mangle_space); aoqi@0: set_soft_end(end()); aoqi@0: } aoqi@0: aoqi@0: // Requires locking. aoqi@0: HeapWord* EdenSpace::allocate(size_t size) { aoqi@0: return allocate_impl(size, soft_end()); aoqi@0: } aoqi@0: aoqi@0: // Lock-free. aoqi@0: HeapWord* EdenSpace::par_allocate(size_t size) { aoqi@0: return par_allocate_impl(size, soft_end()); aoqi@0: } aoqi@0: aoqi@0: HeapWord* ConcEdenSpace::par_allocate(size_t size) aoqi@0: { aoqi@0: do { aoqi@0: // The invariant is top() should be read before end() because aoqi@0: // top() can't be greater than end(), so if an update of _soft_end aoqi@0: // occurs between 'end_val = end();' and 'top_val = top();' top() aoqi@0: // also can grow up to the new end() and the condition aoqi@0: // 'top_val > end_val' is true. To ensure the loading order aoqi@0: // OrderAccess::loadload() is required after top() read. aoqi@0: HeapWord* obj = top(); aoqi@0: OrderAccess::loadload(); aoqi@0: if (pointer_delta(*soft_end_addr(), obj) >= size) { aoqi@0: HeapWord* new_top = obj + size; aoqi@0: HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj); aoqi@0: // result can be one of two: aoqi@0: // the old top value: the exchange succeeded aoqi@0: // otherwise: the new value of the top is returned. aoqi@0: if (result == obj) { aoqi@0: assert(is_aligned(obj) && is_aligned(new_top), "checking alignment"); aoqi@0: return obj; aoqi@0: } aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } while (true); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: HeapWord* OffsetTableContigSpace::initialize_threshold() { aoqi@0: return _offsets.initialize_threshold(); aoqi@0: } aoqi@0: aoqi@0: HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) { aoqi@0: _offsets.alloc_block(start, end); aoqi@0: return _offsets.threshold(); aoqi@0: } aoqi@0: aoqi@0: OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray, aoqi@0: MemRegion mr) : aoqi@0: _offsets(sharedOffsetArray, mr), aoqi@0: _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true) aoqi@0: { aoqi@0: _offsets.set_contig_space(this); aoqi@0: initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle); aoqi@0: } aoqi@0: aoqi@0: #define OBJ_SAMPLE_INTERVAL 0 aoqi@0: #define BLOCK_SAMPLE_INTERVAL 100 aoqi@0: aoqi@0: void OffsetTableContigSpace::verify() const { aoqi@0: HeapWord* p = bottom(); aoqi@0: HeapWord* prev_p = NULL; aoqi@0: int objs = 0; aoqi@0: int blocks = 0; aoqi@0: aoqi@0: if (VerifyObjectStartArray) { aoqi@0: _offsets.verify(); aoqi@0: } aoqi@0: aoqi@0: while (p < top()) { aoqi@0: size_t size = oop(p)->size(); aoqi@0: // For a sampling of objects in the space, find it using the aoqi@0: // block offset table. aoqi@0: if (blocks == BLOCK_SAMPLE_INTERVAL) { aoqi@0: guarantee(p == block_start_const(p + (size/2)), aoqi@0: "check offset computation"); aoqi@0: blocks = 0; aoqi@0: } else { aoqi@0: blocks++; aoqi@0: } aoqi@0: aoqi@0: if (objs == OBJ_SAMPLE_INTERVAL) { aoqi@0: oop(p)->verify(); aoqi@0: objs = 0; aoqi@0: } else { aoqi@0: objs++; aoqi@0: } aoqi@0: prev_p = p; aoqi@0: p += size; aoqi@0: } aoqi@0: guarantee(p == top(), "end of last object must match end of space"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: size_t TenuredSpace::allowed_dead_ratio() const { aoqi@0: return MarkSweepDeadRatio; aoqi@0: }