Path: blob/master/src/hotspot/share/gc/parallel/psParallelCompact.hpp
41152 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_GC_PARALLEL_PSPARALLELCOMPACT_HPP25#define SHARE_GC_PARALLEL_PSPARALLELCOMPACT_HPP2627#include "gc/parallel/mutableSpace.hpp"28#include "gc/parallel/objectStartArray.hpp"29#include "gc/parallel/parallelScavengeHeap.hpp"30#include "gc/parallel/parMarkBitMap.hpp"31#include "gc/shared/collectedHeap.hpp"32#include "gc/shared/collectorCounters.hpp"33#include "gc/shared/taskTerminator.hpp"34#include "oops/oop.hpp"35#include "runtime/atomic.hpp"36#include "runtime/orderAccess.hpp"3738class ParallelScavengeHeap;39class PSAdaptiveSizePolicy;40class PSYoungGen;41class PSOldGen;42class ParCompactionManager;43class PSParallelCompact;44class MoveAndUpdateClosure;45class RefProcTaskExecutor;46class ParallelOldTracer;47class STWGCTimer;4849// The SplitInfo class holds the information needed to 'split' a source region50// so that the live data can be copied to two destination *spaces*. Normally,51// all the live data in a region is copied to a single destination space (e.g.,52// everything live in a region in eden is copied entirely into the old gen).53// However, when the heap is nearly full, all the live data in eden may not fit54// into the old gen. Copying only some of the regions from eden to old gen55// requires finding a region that does not contain a partial object (i.e., no56// live object crosses the region boundary) somewhere near the last object that57// does fit into the old gen. Since it's not always possible to find such a58// region, splitting is necessary for predictable behavior.59//60// A region is always split at the end of the partial object. This avoids61// additional tests when calculating the new location of a pointer, which is a62// very hot code path. The partial object and everything to its left will be63// copied to another space (call it dest_space_1). The live data to the right64// of the partial object will be copied either within the space itself, or to a65// different destination space (distinct from dest_space_1).66//67// Split points are identified during the summary phase, when region68// destinations are computed: data about the split, including the69// partial_object_size, is recorded in a SplitInfo record and the70// partial_object_size field in the summary data is set to zero. The zeroing is71// possible (and necessary) since the partial object will move to a different72// destination space than anything to its right, thus the partial object should73// not affect the locations of any objects to its right.74//75// The recorded data is used during the compaction phase, but only rarely: when76// the partial object on the split region will be copied across a destination77// region boundary. This test is made once each time a region is filled, and is78// a simple address comparison, so the overhead is negligible (see79// PSParallelCompact::first_src_addr()).80//81// Notes:82//83// Only regions with partial objects are split; a region without a partial84// object does not need any extra bookkeeping.85//86// At most one region is split per space, so the amount of data required is87// constant.88//89// A region is split only when the destination space would overflow. Once that90// happens, the destination space is abandoned and no other data (even from91// other source spaces) is targeted to that destination space. Abandoning the92// destination space may leave a somewhat large unused area at the end, if a93// large object caused the overflow.94//95// Future work:96//97// More bookkeeping would be required to continue to use the destination space.98// The most general solution would allow data from regions in two different99// source spaces to be "joined" in a single destination region. At the very100// least, additional code would be required in next_src_region() to detect the101// join and skip to an out-of-order source region. If the join region was also102// the last destination region to which a split region was copied (the most103// likely case), then additional work would be needed to get fill_region() to104// stop iteration and switch to a new source region at the right point. Basic105// idea would be to use a fake value for the top of the source space. It is106// doable, if a bit tricky.107//108// A simpler (but less general) solution would fill the remainder of the109// destination region with a dummy object and continue filling the next110// destination region.111112class SplitInfo113{114public:115// Return true if this split info is valid (i.e., if a split has been116// recorded). The very first region cannot have a partial object and thus is117// never split, so 0 is the 'invalid' value.118bool is_valid() const { return _src_region_idx > 0; }119120// Return true if this split holds data for the specified source region.121inline bool is_split(size_t source_region) const;122123// The index of the split region, the size of the partial object on that124// region and the destination of the partial object.125size_t src_region_idx() const { return _src_region_idx; }126size_t partial_obj_size() const { return _partial_obj_size; }127HeapWord* destination() const { return _destination; }128129// The destination count of the partial object referenced by this split130// (either 1 or 2). This must be added to the destination count of the131// remainder of the source region.132unsigned int destination_count() const { return _destination_count; }133134// If a word within the partial object will be written to the first word of a135// destination region, this is the address of the destination region;136// otherwise this is NULL.137HeapWord* dest_region_addr() const { return _dest_region_addr; }138139// If a word within the partial object will be written to the first word of a140// destination region, this is the address of that word within the partial141// object; otherwise this is NULL.142HeapWord* first_src_addr() const { return _first_src_addr; }143144// Record the data necessary to split the region src_region_idx.145void record(size_t src_region_idx, size_t partial_obj_size,146HeapWord* destination);147148void clear();149150DEBUG_ONLY(void verify_clear();)151152private:153size_t _src_region_idx;154size_t _partial_obj_size;155HeapWord* _destination;156unsigned int _destination_count;157HeapWord* _dest_region_addr;158HeapWord* _first_src_addr;159};160161inline bool SplitInfo::is_split(size_t region_idx) const162{163return _src_region_idx == region_idx && is_valid();164}165166class SpaceInfo167{168public:169MutableSpace* space() const { return _space; }170171// Where the free space will start after the collection. Valid only after the172// summary phase completes.173HeapWord* new_top() const { return _new_top; }174175// Allows new_top to be set.176HeapWord** new_top_addr() { return &_new_top; }177178// Where the smallest allowable dense prefix ends (used only for perm gen).179HeapWord* min_dense_prefix() const { return _min_dense_prefix; }180181// Where the dense prefix ends, or the compacted region begins.182HeapWord* dense_prefix() const { return _dense_prefix; }183184// The start array for the (generation containing the) space, or NULL if there185// is no start array.186ObjectStartArray* start_array() const { return _start_array; }187188SplitInfo& split_info() { return _split_info; }189190void set_space(MutableSpace* s) { _space = s; }191void set_new_top(HeapWord* addr) { _new_top = addr; }192void set_min_dense_prefix(HeapWord* addr) { _min_dense_prefix = addr; }193void set_dense_prefix(HeapWord* addr) { _dense_prefix = addr; }194void set_start_array(ObjectStartArray* s) { _start_array = s; }195196void publish_new_top() const { _space->set_top(_new_top); }197198private:199MutableSpace* _space;200HeapWord* _new_top;201HeapWord* _min_dense_prefix;202HeapWord* _dense_prefix;203ObjectStartArray* _start_array;204SplitInfo _split_info;205};206207class ParallelCompactData208{209public:210// Sizes are in HeapWords, unless indicated otherwise.211static const size_t Log2RegionSize;212static const size_t RegionSize;213static const size_t RegionSizeBytes;214215// Mask for the bits in a size_t to get an offset within a region.216static const size_t RegionSizeOffsetMask;217// Mask for the bits in a pointer to get an offset within a region.218static const size_t RegionAddrOffsetMask;219// Mask for the bits in a pointer to get the address of the start of a region.220static const size_t RegionAddrMask;221222static const size_t Log2BlockSize;223static const size_t BlockSize;224static const size_t BlockSizeBytes;225226static const size_t BlockSizeOffsetMask;227static const size_t BlockAddrOffsetMask;228static const size_t BlockAddrMask;229230static const size_t BlocksPerRegion;231static const size_t Log2BlocksPerRegion;232233class RegionData234{235public:236// Destination address of the region.237HeapWord* destination() const { return _destination; }238239// The first region containing data destined for this region.240size_t source_region() const { return _source_region; }241242// Reuse _source_region to store the corresponding shadow region index243size_t shadow_region() const { return _source_region; }244245// The object (if any) starting in this region and ending in a different246// region that could not be updated during the main (parallel) compaction247// phase. This is different from _partial_obj_addr, which is an object that248// extends onto a source region. However, the two uses do not overlap in249// time, so the same field is used to save space.250HeapWord* deferred_obj_addr() const { return _partial_obj_addr; }251252// The starting address of the partial object extending onto the region.253HeapWord* partial_obj_addr() const { return _partial_obj_addr; }254255// Size of the partial object extending onto the region (words).256size_t partial_obj_size() const { return _partial_obj_size; }257258// Size of live data that lies within this region due to objects that start259// in this region (words). This does not include the partial object260// extending onto the region (if any), or the part of an object that extends261// onto the next region (if any).262size_t live_obj_size() const { return _dc_and_los & los_mask; }263264// Total live data that lies within the region (words).265size_t data_size() const { return partial_obj_size() + live_obj_size(); }266267// The destination_count is the number of other regions to which data from268// this region will be copied. At the end of the summary phase, the valid269// values of destination_count are270//271// 0 - data from the region will be compacted completely into itself, or the272// region is empty. The region can be claimed and then filled.273// 1 - data from the region will be compacted into 1 other region; some274// data from the region may also be compacted into the region itself.275// 2 - data from the region will be copied to 2 other regions.276//277// During compaction as regions are emptied, the destination_count is278// decremented (atomically) and when it reaches 0, it can be claimed and279// then filled.280//281// A region is claimed for processing by atomically changing the282// destination_count to the claimed value (dc_claimed). After a region has283// been filled, the destination_count should be set to the completed value284// (dc_completed).285inline uint destination_count() const;286inline uint destination_count_raw() const;287288// Whether the block table for this region has been filled.289inline bool blocks_filled() const;290291// Number of times the block table was filled.292DEBUG_ONLY(inline size_t blocks_filled_count() const;)293294// The location of the java heap data that corresponds to this region.295inline HeapWord* data_location() const;296297// The highest address referenced by objects in this region.298inline HeapWord* highest_ref() const;299300// Whether this region is available to be claimed, has been claimed, or has301// been completed.302//303// Minor subtlety: claimed() returns true if the region is marked304// completed(), which is desirable since a region must be claimed before it305// can be completed.306bool available() const { return _dc_and_los < dc_one; }307bool claimed() const { return _dc_and_los >= dc_claimed; }308bool completed() const { return _dc_and_los >= dc_completed; }309310// These are not atomic.311void set_destination(HeapWord* addr) { _destination = addr; }312void set_source_region(size_t region) { _source_region = region; }313void set_shadow_region(size_t region) { _source_region = region; }314void set_deferred_obj_addr(HeapWord* addr) { _partial_obj_addr = addr; }315void set_partial_obj_addr(HeapWord* addr) { _partial_obj_addr = addr; }316void set_partial_obj_size(size_t words) {317_partial_obj_size = (region_sz_t) words;318}319inline void set_blocks_filled();320321inline void set_destination_count(uint count);322inline void set_live_obj_size(size_t words);323inline void set_data_location(HeapWord* addr);324inline void set_completed();325inline bool claim_unsafe();326327// These are atomic.328inline void add_live_obj(size_t words);329inline void set_highest_ref(HeapWord* addr);330inline void decrement_destination_count();331inline bool claim();332333// Possible values of _shadow_state, and transition is as follows334// Normal Path:335// UnusedRegion -> mark_normal() -> NormalRegion336// Shadow Path:337// UnusedRegion -> mark_shadow() -> ShadowRegion ->338// mark_filled() -> FilledShadow -> mark_copied() -> CopiedShadow339static const int UnusedRegion = 0; // The region is not collected yet340static const int ShadowRegion = 1; // Stolen by an idle thread, and a shadow region is created for it341static const int FilledShadow = 2; // Its shadow region has been filled and ready to be copied back342static const int CopiedShadow = 3; // The data of the shadow region has been copied back343static const int NormalRegion = 4; // The region will be collected by the original parallel algorithm344345// Mark the current region as normal or shadow to enter different processing paths346inline bool mark_normal();347inline bool mark_shadow();348// Mark the shadow region as filled and ready to be copied back349inline void mark_filled();350// Mark the shadow region as copied back to avoid double copying.351inline bool mark_copied();352// Special case: see the comment in PSParallelCompact::fill_and_update_shadow_region.353// Return to the normal path here354inline void shadow_to_normal();355356357int shadow_state() { return _shadow_state; }358359private:360// The type used to represent object sizes within a region.361typedef uint region_sz_t;362363// Constants for manipulating the _dc_and_los field, which holds both the364// destination count and live obj size. The live obj size lives at the365// least significant end so no masking is necessary when adding.366static const region_sz_t dc_shift; // Shift amount.367static const region_sz_t dc_mask; // Mask for destination count.368static const region_sz_t dc_one; // 1, shifted appropriately.369static const region_sz_t dc_claimed; // Region has been claimed.370static const region_sz_t dc_completed; // Region has been completed.371static const region_sz_t los_mask; // Mask for live obj size.372373HeapWord* _destination;374size_t _source_region;375HeapWord* _partial_obj_addr;376region_sz_t _partial_obj_size;377region_sz_t volatile _dc_and_los;378bool volatile _blocks_filled;379int volatile _shadow_state;380381#ifdef ASSERT382size_t _blocks_filled_count; // Number of block table fills.383384// These enable optimizations that are only partially implemented. Use385// debug builds to prevent the code fragments from breaking.386HeapWord* _data_location;387HeapWord* _highest_ref;388#endif // #ifdef ASSERT389390#ifdef ASSERT391public:392uint _pushed; // 0 until region is pushed onto a stack393private:394#endif395};396397// "Blocks" allow shorter sections of the bitmap to be searched. Each Block398// holds an offset, which is the amount of live data in the Region to the left399// of the first live object that starts in the Block.400class BlockData401{402public:403typedef unsigned short int blk_ofs_t;404405blk_ofs_t offset() const { return _offset; }406void set_offset(size_t val) { _offset = (blk_ofs_t)val; }407408private:409blk_ofs_t _offset;410};411412public:413ParallelCompactData();414bool initialize(MemRegion covered_region);415416size_t region_count() const { return _region_count; }417size_t reserved_byte_size() const { return _reserved_byte_size; }418419// Convert region indices to/from RegionData pointers.420inline RegionData* region(size_t region_idx) const;421inline size_t region(const RegionData* const region_ptr) const;422423size_t block_count() const { return _block_count; }424inline BlockData* block(size_t block_idx) const;425inline size_t block(const BlockData* block_ptr) const;426427void add_obj(HeapWord* addr, size_t len);428void add_obj(oop p, size_t len) { add_obj(cast_from_oop<HeapWord*>(p), len); }429430// Fill in the regions covering [beg, end) so that no data moves; i.e., the431// destination of region n is simply the start of region n. Both arguments432// beg and end must be region-aligned.433void summarize_dense_prefix(HeapWord* beg, HeapWord* end);434435HeapWord* summarize_split_space(size_t src_region, SplitInfo& split_info,436HeapWord* destination, HeapWord* target_end,437HeapWord** target_next);438bool summarize(SplitInfo& split_info,439HeapWord* source_beg, HeapWord* source_end,440HeapWord** source_next,441HeapWord* target_beg, HeapWord* target_end,442HeapWord** target_next);443444void clear();445void clear_range(size_t beg_region, size_t end_region);446void clear_range(HeapWord* beg, HeapWord* end) {447clear_range(addr_to_region_idx(beg), addr_to_region_idx(end));448}449450// Return the number of words between addr and the start of the region451// containing addr.452inline size_t region_offset(const HeapWord* addr) const;453454// Convert addresses to/from a region index or region pointer.455inline size_t addr_to_region_idx(const HeapWord* addr) const;456inline RegionData* addr_to_region_ptr(const HeapWord* addr) const;457inline HeapWord* region_to_addr(size_t region) const;458inline HeapWord* region_to_addr(size_t region, size_t offset) const;459inline HeapWord* region_to_addr(const RegionData* region) const;460461inline HeapWord* region_align_down(HeapWord* addr) const;462inline HeapWord* region_align_up(HeapWord* addr) const;463inline bool is_region_aligned(HeapWord* addr) const;464465// Analogous to region_offset() for blocks.466size_t block_offset(const HeapWord* addr) const;467size_t addr_to_block_idx(const HeapWord* addr) const;468size_t addr_to_block_idx(const oop obj) const {469return addr_to_block_idx(cast_from_oop<HeapWord*>(obj));470}471inline BlockData* addr_to_block_ptr(const HeapWord* addr) const;472inline HeapWord* block_to_addr(size_t block) const;473inline size_t region_to_block_idx(size_t region) const;474475inline HeapWord* block_align_down(HeapWord* addr) const;476inline HeapWord* block_align_up(HeapWord* addr) const;477inline bool is_block_aligned(HeapWord* addr) const;478479// Return the address one past the end of the partial object.480HeapWord* partial_obj_end(size_t region_idx) const;481482// Return the location of the object after compaction.483HeapWord* calc_new_pointer(HeapWord* addr, ParCompactionManager* cm) const;484485HeapWord* calc_new_pointer(oop p, ParCompactionManager* cm) const {486return calc_new_pointer(cast_from_oop<HeapWord*>(p), cm);487}488489#ifdef ASSERT490void verify_clear(const PSVirtualSpace* vspace);491void verify_clear();492#endif // #ifdef ASSERT493494private:495bool initialize_block_data();496bool initialize_region_data(size_t region_size);497PSVirtualSpace* create_vspace(size_t count, size_t element_size);498499private:500HeapWord* _region_start;501#ifdef ASSERT502HeapWord* _region_end;503#endif // #ifdef ASSERT504505PSVirtualSpace* _region_vspace;506size_t _reserved_byte_size;507RegionData* _region_data;508size_t _region_count;509510PSVirtualSpace* _block_vspace;511BlockData* _block_data;512size_t _block_count;513};514515inline uint516ParallelCompactData::RegionData::destination_count_raw() const517{518return _dc_and_los & dc_mask;519}520521inline uint522ParallelCompactData::RegionData::destination_count() const523{524return destination_count_raw() >> dc_shift;525}526527inline bool528ParallelCompactData::RegionData::blocks_filled() const529{530bool result = _blocks_filled;531OrderAccess::acquire();532return result;533}534535#ifdef ASSERT536inline size_t537ParallelCompactData::RegionData::blocks_filled_count() const538{539return _blocks_filled_count;540}541#endif // #ifdef ASSERT542543inline void544ParallelCompactData::RegionData::set_blocks_filled()545{546OrderAccess::release();547_blocks_filled = true;548// Debug builds count the number of times the table was filled.549DEBUG_ONLY(Atomic::inc(&_blocks_filled_count));550}551552inline void553ParallelCompactData::RegionData::set_destination_count(uint count)554{555assert(count <= (dc_completed >> dc_shift), "count too large");556const region_sz_t live_sz = (region_sz_t) live_obj_size();557_dc_and_los = (count << dc_shift) | live_sz;558}559560inline void ParallelCompactData::RegionData::set_live_obj_size(size_t words)561{562assert(words <= los_mask, "would overflow");563_dc_and_los = destination_count_raw() | (region_sz_t)words;564}565566inline void ParallelCompactData::RegionData::decrement_destination_count()567{568assert(_dc_and_los < dc_claimed, "already claimed");569assert(_dc_and_los >= dc_one, "count would go negative");570Atomic::add(&_dc_and_los, dc_mask);571}572573inline HeapWord* ParallelCompactData::RegionData::data_location() const574{575DEBUG_ONLY(return _data_location;)576NOT_DEBUG(return NULL;)577}578579inline HeapWord* ParallelCompactData::RegionData::highest_ref() const580{581DEBUG_ONLY(return _highest_ref;)582NOT_DEBUG(return NULL;)583}584585inline void ParallelCompactData::RegionData::set_data_location(HeapWord* addr)586{587DEBUG_ONLY(_data_location = addr;)588}589590inline void ParallelCompactData::RegionData::set_completed()591{592assert(claimed(), "must be claimed first");593_dc_and_los = dc_completed | (region_sz_t) live_obj_size();594}595596// MT-unsafe claiming of a region. Should only be used during single threaded597// execution.598inline bool ParallelCompactData::RegionData::claim_unsafe()599{600if (available()) {601_dc_and_los |= dc_claimed;602return true;603}604return false;605}606607inline void ParallelCompactData::RegionData::add_live_obj(size_t words)608{609assert(words <= (size_t)los_mask - live_obj_size(), "overflow");610Atomic::add(&_dc_and_los, static_cast<region_sz_t>(words));611}612613inline void ParallelCompactData::RegionData::set_highest_ref(HeapWord* addr)614{615#ifdef ASSERT616HeapWord* tmp = _highest_ref;617while (addr > tmp) {618tmp = Atomic::cmpxchg(&_highest_ref, tmp, addr);619}620#endif // #ifdef ASSERT621}622623inline bool ParallelCompactData::RegionData::claim()624{625const region_sz_t los = static_cast<region_sz_t>(live_obj_size());626const region_sz_t old = Atomic::cmpxchg(&_dc_and_los, los, dc_claimed | los);627return old == los;628}629630inline bool ParallelCompactData::RegionData::mark_normal() {631return Atomic::cmpxchg(&_shadow_state, UnusedRegion, NormalRegion) == UnusedRegion;632}633634inline bool ParallelCompactData::RegionData::mark_shadow() {635if (_shadow_state != UnusedRegion) return false;636return Atomic::cmpxchg(&_shadow_state, UnusedRegion, ShadowRegion) == UnusedRegion;637}638639inline void ParallelCompactData::RegionData::mark_filled() {640int old = Atomic::cmpxchg(&_shadow_state, ShadowRegion, FilledShadow);641assert(old == ShadowRegion, "Fail to mark the region as filled");642}643644inline bool ParallelCompactData::RegionData::mark_copied() {645return Atomic::cmpxchg(&_shadow_state, FilledShadow, CopiedShadow) == FilledShadow;646}647648void ParallelCompactData::RegionData::shadow_to_normal() {649int old = Atomic::cmpxchg(&_shadow_state, ShadowRegion, NormalRegion);650assert(old == ShadowRegion, "Fail to mark the region as finish");651}652653inline ParallelCompactData::RegionData*654ParallelCompactData::region(size_t region_idx) const655{656assert(region_idx <= region_count(), "bad arg");657return _region_data + region_idx;658}659660inline size_t661ParallelCompactData::region(const RegionData* const region_ptr) const662{663assert(region_ptr >= _region_data, "bad arg");664assert(region_ptr <= _region_data + region_count(), "bad arg");665return pointer_delta(region_ptr, _region_data, sizeof(RegionData));666}667668inline ParallelCompactData::BlockData*669ParallelCompactData::block(size_t n) const {670assert(n < block_count(), "bad arg");671return _block_data + n;672}673674inline size_t675ParallelCompactData::region_offset(const HeapWord* addr) const676{677assert(addr >= _region_start, "bad addr");678// would mistakenly return 0 for _region_end679assert(addr < _region_end, "bad addr");680return (size_t(addr) & RegionAddrOffsetMask) >> LogHeapWordSize;681}682683inline size_t684ParallelCompactData::addr_to_region_idx(const HeapWord* addr) const685{686assert(addr >= _region_start, "bad addr " PTR_FORMAT " _region_start " PTR_FORMAT, p2i(addr), p2i(_region_start));687assert(addr <= _region_end, "bad addr " PTR_FORMAT " _region_end " PTR_FORMAT, p2i(addr), p2i(_region_end));688return pointer_delta(addr, _region_start) >> Log2RegionSize;689}690691inline ParallelCompactData::RegionData*692ParallelCompactData::addr_to_region_ptr(const HeapWord* addr) const693{694return region(addr_to_region_idx(addr));695}696697inline HeapWord*698ParallelCompactData::region_to_addr(size_t region) const699{700assert(region <= _region_count, "region out of range");701return _region_start + (region << Log2RegionSize);702}703704inline HeapWord*705ParallelCompactData::region_to_addr(const RegionData* region) const706{707return region_to_addr(pointer_delta(region, _region_data,708sizeof(RegionData)));709}710711inline HeapWord*712ParallelCompactData::region_to_addr(size_t region, size_t offset) const713{714assert(region <= _region_count, "region out of range");715assert(offset < RegionSize, "offset too big"); // This may be too strict.716return region_to_addr(region) + offset;717}718719inline HeapWord*720ParallelCompactData::region_align_down(HeapWord* addr) const721{722assert(addr >= _region_start, "bad addr");723assert(addr < _region_end + RegionSize, "bad addr");724return (HeapWord*)(size_t(addr) & RegionAddrMask);725}726727inline HeapWord*728ParallelCompactData::region_align_up(HeapWord* addr) const729{730assert(addr >= _region_start, "bad addr");731assert(addr <= _region_end, "bad addr");732return region_align_down(addr + RegionSizeOffsetMask);733}734735inline bool736ParallelCompactData::is_region_aligned(HeapWord* addr) const737{738return (size_t(addr) & RegionAddrOffsetMask) == 0;739}740741inline size_t742ParallelCompactData::block_offset(const HeapWord* addr) const743{744assert(addr >= _region_start, "bad addr");745assert(addr <= _region_end, "bad addr");746return (size_t(addr) & BlockAddrOffsetMask) >> LogHeapWordSize;747}748749inline size_t750ParallelCompactData::addr_to_block_idx(const HeapWord* addr) const751{752assert(addr >= _region_start, "bad addr");753assert(addr <= _region_end, "bad addr");754return pointer_delta(addr, _region_start) >> Log2BlockSize;755}756757inline ParallelCompactData::BlockData*758ParallelCompactData::addr_to_block_ptr(const HeapWord* addr) const759{760return block(addr_to_block_idx(addr));761}762763inline HeapWord*764ParallelCompactData::block_to_addr(size_t block) const765{766assert(block < _block_count, "block out of range");767return _region_start + (block << Log2BlockSize);768}769770inline size_t771ParallelCompactData::region_to_block_idx(size_t region) const772{773return region << Log2BlocksPerRegion;774}775776inline HeapWord*777ParallelCompactData::block_align_down(HeapWord* addr) const778{779assert(addr >= _region_start, "bad addr");780assert(addr < _region_end + RegionSize, "bad addr");781return (HeapWord*)(size_t(addr) & BlockAddrMask);782}783784inline HeapWord*785ParallelCompactData::block_align_up(HeapWord* addr) const786{787assert(addr >= _region_start, "bad addr");788assert(addr <= _region_end, "bad addr");789return block_align_down(addr + BlockSizeOffsetMask);790}791792inline bool793ParallelCompactData::is_block_aligned(HeapWord* addr) const794{795return block_offset(addr) == 0;796}797798// Abstract closure for use with ParMarkBitMap::iterate(), which will invoke the799// do_addr() method.800//801// The closure is initialized with the number of heap words to process802// (words_remaining()), and becomes 'full' when it reaches 0. The do_addr()803// methods in subclasses should update the total as words are processed. Since804// only one subclass actually uses this mechanism to terminate iteration, the805// default initial value is > 0. The implementation is here and not in the806// single subclass that uses it to avoid making is_full() virtual, and thus807// adding a virtual call per live object.808809class ParMarkBitMapClosure: public StackObj {810public:811typedef ParMarkBitMap::idx_t idx_t;812typedef ParMarkBitMap::IterationStatus IterationStatus;813814public:815inline ParMarkBitMapClosure(ParMarkBitMap* mbm, ParCompactionManager* cm,816size_t words = max_uintx);817818inline ParCompactionManager* compaction_manager() const;819inline ParMarkBitMap* bitmap() const;820inline size_t words_remaining() const;821inline bool is_full() const;822inline HeapWord* source() const;823824inline void set_source(HeapWord* addr);825826virtual IterationStatus do_addr(HeapWord* addr, size_t words) = 0;827828protected:829inline void decrement_words_remaining(size_t words);830831private:832ParMarkBitMap* const _bitmap;833ParCompactionManager* const _compaction_manager;834DEBUG_ONLY(const size_t _initial_words_remaining;) // Useful in debugger.835size_t _words_remaining; // Words left to copy.836837protected:838HeapWord* _source; // Next addr that would be read.839};840841inline842ParMarkBitMapClosure::ParMarkBitMapClosure(ParMarkBitMap* bitmap,843ParCompactionManager* cm,844size_t words):845_bitmap(bitmap), _compaction_manager(cm)846#ifdef ASSERT847, _initial_words_remaining(words)848#endif849{850_words_remaining = words;851_source = NULL;852}853854inline ParCompactionManager* ParMarkBitMapClosure::compaction_manager() const {855return _compaction_manager;856}857858inline ParMarkBitMap* ParMarkBitMapClosure::bitmap() const {859return _bitmap;860}861862inline size_t ParMarkBitMapClosure::words_remaining() const {863return _words_remaining;864}865866inline bool ParMarkBitMapClosure::is_full() const {867return words_remaining() == 0;868}869870inline HeapWord* ParMarkBitMapClosure::source() const {871return _source;872}873874inline void ParMarkBitMapClosure::set_source(HeapWord* addr) {875_source = addr;876}877878inline void ParMarkBitMapClosure::decrement_words_remaining(size_t words) {879assert(_words_remaining >= words, "processed too many words");880_words_remaining -= words;881}882883// The Parallel collector is a stop-the-world garbage collector that884// does parts of the collection using parallel threads. The collection includes885// the tenured generation and the young generation.886//887// There are four phases of the collection.888//889// - marking phase890// - summary phase891// - compacting phase892// - clean up phase893//894// Roughly speaking these phases correspond, respectively, to895// - mark all the live objects896// - calculate the destination of each object at the end of the collection897// - move the objects to their destination898// - update some references and reinitialize some variables899//900// These three phases are invoked in PSParallelCompact::invoke_no_policy(). The901// marking phase is implemented in PSParallelCompact::marking_phase() and does a902// complete marking of the heap. The summary phase is implemented in903// PSParallelCompact::summary_phase(). The move and update phase is implemented904// in PSParallelCompact::compact().905//906// A space that is being collected is divided into regions and with each region907// is associated an object of type ParallelCompactData. Each region is of a908// fixed size and typically will contain more than 1 object and may have parts909// of objects at the front and back of the region.910//911// region -----+---------------------+----------912// objects covered [ AAA )[ BBB )[ CCC )[ DDD )913//914// The marking phase does a complete marking of all live objects in the heap.915// The marking also compiles the size of the data for all live objects covered916// by the region. This size includes the part of any live object spanning onto917// the region (part of AAA if it is live) from the front, all live objects918// contained in the region (BBB and/or CCC if they are live), and the part of919// any live objects covered by the region that extends off the region (part of920// DDD if it is live). The marking phase uses multiple GC threads and marking921// is done in a bit array of type ParMarkBitMap. The marking of the bit map is922// done atomically as is the accumulation of the size of the live objects923// covered by a region.924//925// The summary phase calculates the total live data to the left of each region926// XXX. Based on that total and the bottom of the space, it can calculate the927// starting location of the live data in XXX. The summary phase calculates for928// each region XXX quantities such as929//930// - the amount of live data at the beginning of a region from an object931// entering the region.932// - the location of the first live data on the region933// - a count of the number of regions receiving live data from XXX.934//935// See ParallelCompactData for precise details. The summary phase also936// calculates the dense prefix for the compaction. The dense prefix is a937// portion at the beginning of the space that is not moved. The objects in the938// dense prefix do need to have their object references updated. See method939// summarize_dense_prefix().940//941// The summary phase is done using 1 GC thread.942//943// The compaction phase moves objects to their new location and updates all944// references in the object.945//946// A current exception is that objects that cross a region boundary are moved947// but do not have their references updated. References are not updated because948// it cannot easily be determined if the klass pointer KKK for the object AAA949// has been updated. KKK likely resides in a region to the left of the region950// containing AAA. These AAA's have there references updated at the end in a951// clean up phase. See the method PSParallelCompact::update_deferred_objects().952// An alternate strategy is being investigated for this deferral of updating.953//954// Compaction is done on a region basis. A region that is ready to be filled is955// put on a ready list and GC threads take region off the list and fill them. A956// region is ready to be filled if it empty of live objects. Such a region may957// have been initially empty (only contained dead objects) or may have had all958// its live objects copied out already. A region that compacts into itself is959// also ready for filling. The ready list is initially filled with empty960// regions and regions compacting into themselves. There is always at least 1961// region that can be put on the ready list. The regions are atomically added962// and removed from the ready list.963964class TaskQueue;965966class PSParallelCompact : AllStatic {967public:968// Convenient access to type names.969typedef ParMarkBitMap::idx_t idx_t;970typedef ParallelCompactData::RegionData RegionData;971typedef ParallelCompactData::BlockData BlockData;972973typedef enum {974old_space_id, eden_space_id,975from_space_id, to_space_id, last_space_id976} SpaceId;977978struct UpdateDensePrefixTask : public CHeapObj<mtGC> {979SpaceId _space_id;980size_t _region_index_start;981size_t _region_index_end;982983UpdateDensePrefixTask() :984_space_id(SpaceId(0)),985_region_index_start(0),986_region_index_end(0) {}987988UpdateDensePrefixTask(SpaceId space_id,989size_t region_index_start,990size_t region_index_end) :991_space_id(space_id),992_region_index_start(region_index_start),993_region_index_end(region_index_end) {}994};995996public:997// Inline closure decls998//999class IsAliveClosure: public BoolObjectClosure {1000public:1001virtual bool do_object_b(oop p);1002};10031004friend class RefProcTaskProxy;1005friend class PSParallelCompactTest;10061007private:1008static STWGCTimer _gc_timer;1009static ParallelOldTracer _gc_tracer;1010static elapsedTimer _accumulated_time;1011static unsigned int _total_invocations;1012static unsigned int _maximum_compaction_gc_num;1013static CollectorCounters* _counters;1014static ParMarkBitMap _mark_bitmap;1015static ParallelCompactData _summary_data;1016static IsAliveClosure _is_alive_closure;1017static SpaceInfo _space_info[last_space_id];10181019// Reference processing (used in ...follow_contents)1020static SpanSubjectToDiscoveryClosure _span_based_discoverer;1021static ReferenceProcessor* _ref_processor;10221023// Values computed at initialization and used by dead_wood_limiter().1024static double _dwl_mean;1025static double _dwl_std_dev;1026static double _dwl_first_term;1027static double _dwl_adjustment;1028#ifdef ASSERT1029static bool _dwl_initialized;1030#endif // #ifdef ASSERT10311032public:1033static ParallelOldTracer* gc_tracer() { return &_gc_tracer; }10341035private:10361037static void initialize_space_info();10381039// Clear the marking bitmap and summary data that cover the specified space.1040static void clear_data_covering_space(SpaceId id);10411042static void pre_compact();1043static void post_compact();10441045// Mark live objects1046static void marking_phase(ParCompactionManager* cm,1047bool maximum_heap_compaction,1048ParallelOldTracer *gc_tracer);10491050// Compute the dense prefix for the designated space. This is an experimental1051// implementation currently not used in production.1052static HeapWord* compute_dense_prefix_via_density(const SpaceId id,1053bool maximum_compaction);10541055// Methods used to compute the dense prefix.10561057// Compute the value of the normal distribution at x = density. The mean and1058// standard deviation are values saved by initialize_dead_wood_limiter().1059static inline double normal_distribution(double density);10601061// Initialize the static vars used by dead_wood_limiter().1062static void initialize_dead_wood_limiter();10631064// Return the percentage of space that can be treated as "dead wood" (i.e.,1065// not reclaimed).1066static double dead_wood_limiter(double density, size_t min_percent);10671068// Find the first (left-most) region in the range [beg, end) that has at least1069// dead_words of dead space to the left. The argument beg must be the first1070// region in the space that is not completely live.1071static RegionData* dead_wood_limit_region(const RegionData* beg,1072const RegionData* end,1073size_t dead_words);10741075// Return a pointer to the first region in the range [beg, end) that is not1076// completely full.1077static RegionData* first_dead_space_region(const RegionData* beg,1078const RegionData* end);10791080// Return a value indicating the benefit or 'yield' if the compacted region1081// were to start (or equivalently if the dense prefix were to end) at the1082// candidate region. Higher values are better.1083//1084// The value is based on the amount of space reclaimed vs. the costs of (a)1085// updating references in the dense prefix plus (b) copying objects and1086// updating references in the compacted region.1087static inline double reclaimed_ratio(const RegionData* const candidate,1088HeapWord* const bottom,1089HeapWord* const top,1090HeapWord* const new_top);10911092// Compute the dense prefix for the designated space.1093static HeapWord* compute_dense_prefix(const SpaceId id,1094bool maximum_compaction);10951096// Return true if dead space crosses onto the specified Region; bit must be1097// the bit index corresponding to the first word of the Region.1098static inline bool dead_space_crosses_boundary(const RegionData* region,1099idx_t bit);11001101// Summary phase utility routine to fill dead space (if any) at the dense1102// prefix boundary. Should only be called if the the dense prefix is1103// non-empty.1104static void fill_dense_prefix_end(SpaceId id);11051106static void summarize_spaces_quick();1107static void summarize_space(SpaceId id, bool maximum_compaction);1108static void summary_phase(ParCompactionManager* cm, bool maximum_compaction);11091110// Adjust addresses in roots. Does not adjust addresses in heap.1111static void adjust_roots();11121113DEBUG_ONLY(static void write_block_fill_histogram();)11141115// Move objects to new locations.1116static void compact_perm(ParCompactionManager* cm);1117static void compact();11181119// Add available regions to the stack and draining tasks to the task queue.1120static void prepare_region_draining_tasks(uint parallel_gc_threads);11211122// Add dense prefix update tasks to the task queue.1123static void enqueue_dense_prefix_tasks(TaskQueue& task_queue,1124uint parallel_gc_threads);11251126#ifndef PRODUCT1127// Print generic summary data1128static void print_generic_summary_data(ParallelCompactData& summary_data,1129HeapWord* const beg_addr,1130HeapWord* const end_addr);1131#endif // #ifndef PRODUCT11321133public:11341135PSParallelCompact();11361137static void invoke(bool maximum_heap_compaction);1138static bool invoke_no_policy(bool maximum_heap_compaction);11391140static void post_initialize();1141// Perform initialization for PSParallelCompact that requires1142// allocations. This should be called during the VM initialization1143// at a pointer where it would be appropriate to return a JNI_ENOMEM1144// in the event of a failure.1145static bool initialize();11461147// Closure accessors1148static BoolObjectClosure* is_alive_closure() { return &_is_alive_closure; }11491150// Public accessors1151static elapsedTimer* accumulated_time() { return &_accumulated_time; }1152static unsigned int total_invocations() { return _total_invocations; }1153static CollectorCounters* counters() { return _counters; }11541155// Marking support1156static inline bool mark_obj(oop obj);1157static inline bool is_marked(oop obj);11581159template <class T> static inline void adjust_pointer(T* p, ParCompactionManager* cm);11601161// Compaction support.1162// Return true if p is in the range [beg_addr, end_addr).1163static inline bool is_in(HeapWord* p, HeapWord* beg_addr, HeapWord* end_addr);1164static inline bool is_in(oop* p, HeapWord* beg_addr, HeapWord* end_addr);11651166// Convenience wrappers for per-space data kept in _space_info.1167static inline MutableSpace* space(SpaceId space_id);1168static inline HeapWord* new_top(SpaceId space_id);1169static inline HeapWord* dense_prefix(SpaceId space_id);1170static inline ObjectStartArray* start_array(SpaceId space_id);11711172// Process the end of the given region range in the dense prefix.1173// This includes saving any object not updated.1174static void dense_prefix_regions_epilogue(ParCompactionManager* cm,1175size_t region_start_index,1176size_t region_end_index,1177idx_t exiting_object_offset,1178idx_t region_offset_start,1179idx_t region_offset_end);11801181// Update a region in the dense prefix. For each live object1182// in the region, update it's interior references. For each1183// dead object, fill it with deadwood. Dead space at the end1184// of a region range will be filled to the start of the next1185// live object regardless of the region_index_end. None of the1186// objects in the dense prefix move and dead space is dead1187// (holds only dead objects that don't need any processing), so1188// dead space can be filled in any order.1189static void update_and_deadwood_in_dense_prefix(ParCompactionManager* cm,1190SpaceId space_id,1191size_t region_index_start,1192size_t region_index_end);11931194// Return the address of the count + 1st live word in the range [beg, end).1195static HeapWord* skip_live_words(HeapWord* beg, HeapWord* end, size_t count);11961197// Return the address of the word to be copied to dest_addr, which must be1198// aligned to a region boundary.1199static HeapWord* first_src_addr(HeapWord* const dest_addr,1200SpaceId src_space_id,1201size_t src_region_idx);12021203// Determine the next source region, set closure.source() to the start of the1204// new region return the region index. Parameter end_addr is the address one1205// beyond the end of source range just processed. If necessary, switch to a1206// new source space and set src_space_id (in-out parameter) and src_space_top1207// (out parameter) accordingly.1208static size_t next_src_region(MoveAndUpdateClosure& closure,1209SpaceId& src_space_id,1210HeapWord*& src_space_top,1211HeapWord* end_addr);12121213// Decrement the destination count for each non-empty source region in the1214// range [beg_region, region(region_align_up(end_addr))). If the destination1215// count for a region goes to 0 and it needs to be filled, enqueue it.1216static void decrement_destination_counts(ParCompactionManager* cm,1217SpaceId src_space_id,1218size_t beg_region,1219HeapWord* end_addr);12201221static void fill_region(ParCompactionManager* cm, MoveAndUpdateClosure& closure, size_t region);1222static void fill_and_update_region(ParCompactionManager* cm, size_t region);12231224static bool steal_unavailable_region(ParCompactionManager* cm, size_t& region_idx);1225static void fill_and_update_shadow_region(ParCompactionManager* cm, size_t region);1226// Copy the content of a shadow region back to its corresponding heap region1227static void copy_back(HeapWord* shadow_addr, HeapWord* region_addr);1228// Collect empty regions as shadow regions and initialize the1229// _next_shadow_region filed for each compact manager1230static void initialize_shadow_regions(uint parallel_gc_threads);12311232// Fill in the block table for the specified region.1233static void fill_blocks(size_t region_idx);12341235// Update the deferred objects in the space.1236static void update_deferred_objects(ParCompactionManager* cm, SpaceId id);12371238static ParMarkBitMap* mark_bitmap() { return &_mark_bitmap; }1239static ParallelCompactData& summary_data() { return _summary_data; }12401241// Reference Processing1242static ReferenceProcessor* const ref_processor() { return _ref_processor; }12431244static STWGCTimer* gc_timer() { return &_gc_timer; }12451246// Return the SpaceId for the given address.1247static SpaceId space_id(HeapWord* addr);12481249static void print_on_error(outputStream* st);12501251#ifndef PRODUCT1252// Debugging support.1253static const char* space_names[last_space_id];1254static void print_region_ranges();1255static void print_dense_prefix_stats(const char* const algorithm,1256const SpaceId id,1257const bool maximum_compaction,1258HeapWord* const addr);1259static void summary_phase_msg(SpaceId dst_space_id,1260HeapWord* dst_beg, HeapWord* dst_end,1261SpaceId src_space_id,1262HeapWord* src_beg, HeapWord* src_end);1263#endif // #ifndef PRODUCT12641265#ifdef ASSERT1266// Sanity check the new location of a word in the heap.1267static inline void check_new_location(HeapWord* old_addr, HeapWord* new_addr);1268// Verify that all the regions have been emptied.1269static void verify_complete(SpaceId space_id);1270#endif // #ifdef ASSERT1271};12721273class MoveAndUpdateClosure: public ParMarkBitMapClosure {1274static inline size_t calculate_words_remaining(size_t region);1275public:1276inline MoveAndUpdateClosure(ParMarkBitMap* bitmap, ParCompactionManager* cm,1277size_t region);12781279// Accessors.1280HeapWord* destination() const { return _destination; }1281HeapWord* copy_destination() const { return _destination + _offset; }12821283// If the object will fit (size <= words_remaining()), copy it to the current1284// destination, update the interior oops and the start array and return either1285// full (if the closure is full) or incomplete. If the object will not fit,1286// return would_overflow.1287IterationStatus do_addr(HeapWord* addr, size_t size);12881289// Copy enough words to fill this closure, starting at source(). Interior1290// oops and the start array are not updated. Return full.1291IterationStatus copy_until_full();12921293// Copy enough words to fill this closure or to the end of an object,1294// whichever is smaller, starting at source(). Interior oops and the start1295// array are not updated.1296void copy_partial_obj();12971298virtual void complete_region(ParCompactionManager* cm, HeapWord* dest_addr,1299PSParallelCompact::RegionData* region_ptr);13001301protected:1302// Update variables to indicate that word_count words were processed.1303inline void update_state(size_t word_count);13041305protected:1306HeapWord* _destination; // Next addr to be written.1307ObjectStartArray* const _start_array;1308size_t _offset;1309};13101311inline size_t MoveAndUpdateClosure::calculate_words_remaining(size_t region) {1312HeapWord* dest_addr = PSParallelCompact::summary_data().region_to_addr(region);1313PSParallelCompact::SpaceId dest_space_id = PSParallelCompact::space_id(dest_addr);1314HeapWord* new_top = PSParallelCompact::new_top(dest_space_id);1315assert(dest_addr < new_top, "sanity");13161317return MIN2(pointer_delta(new_top, dest_addr), ParallelCompactData::RegionSize);1318}13191320inline1321MoveAndUpdateClosure::MoveAndUpdateClosure(ParMarkBitMap* bitmap,1322ParCompactionManager* cm,1323size_t region_idx) :1324ParMarkBitMapClosure(bitmap, cm, calculate_words_remaining(region_idx)),1325_destination(PSParallelCompact::summary_data().region_to_addr(region_idx)),1326_start_array(PSParallelCompact::start_array(PSParallelCompact::space_id(_destination))),1327_offset(0) { }132813291330inline void MoveAndUpdateClosure::update_state(size_t words)1331{1332decrement_words_remaining(words);1333_source += words;1334_destination += words;1335}13361337class MoveAndUpdateShadowClosure: public MoveAndUpdateClosure {1338inline size_t calculate_shadow_offset(size_t region_idx, size_t shadow_idx);1339public:1340inline MoveAndUpdateShadowClosure(ParMarkBitMap* bitmap, ParCompactionManager* cm,1341size_t region, size_t shadow);13421343virtual void complete_region(ParCompactionManager* cm, HeapWord* dest_addr,1344PSParallelCompact::RegionData* region_ptr);13451346private:1347size_t _shadow;1348};13491350inline size_t MoveAndUpdateShadowClosure::calculate_shadow_offset(size_t region_idx, size_t shadow_idx) {1351ParallelCompactData& sd = PSParallelCompact::summary_data();1352HeapWord* dest_addr = sd.region_to_addr(region_idx);1353HeapWord* shadow_addr = sd.region_to_addr(shadow_idx);1354return pointer_delta(shadow_addr, dest_addr);1355}13561357inline1358MoveAndUpdateShadowClosure::MoveAndUpdateShadowClosure(ParMarkBitMap *bitmap,1359ParCompactionManager *cm,1360size_t region,1361size_t shadow) :1362MoveAndUpdateClosure(bitmap, cm, region),1363_shadow(shadow) {1364_offset = calculate_shadow_offset(region, shadow);1365}13661367class UpdateOnlyClosure: public ParMarkBitMapClosure {1368private:1369const PSParallelCompact::SpaceId _space_id;1370ObjectStartArray* const _start_array;13711372public:1373UpdateOnlyClosure(ParMarkBitMap* mbm,1374ParCompactionManager* cm,1375PSParallelCompact::SpaceId space_id);13761377// Update the object.1378virtual IterationStatus do_addr(HeapWord* addr, size_t words);13791380inline void do_addr(HeapWord* addr);1381};13821383class FillClosure: public ParMarkBitMapClosure {1384public:1385FillClosure(ParCompactionManager* cm, PSParallelCompact::SpaceId space_id);13861387virtual IterationStatus do_addr(HeapWord* addr, size_t size);13881389private:1390ObjectStartArray* const _start_array;1391};13921393void steal_marking_work(TaskTerminator& terminator, uint worker_id);13941395#endif // SHARE_GC_PARALLEL_PSPARALLELCOMPACT_HPP139613971398