/* * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
// These fields may not be updated below, so make sure they're clear.
assert(_dest_region_addr == NULL, "should have been cleared");
assert(_first_src_addr == NULL, "should have been cleared");
// Determine the number of destination regions for the partial object.
HeapWord* const last_word = destination + partial_obj_size - 1; const ParallelCompactData& sd = PSParallelCompact::summary_data();
HeapWord* const beg_region_addr = sd.region_align_down(destination);
HeapWord* const end_region_addr = sd.region_align_down(last_word);
if (beg_region_addr == end_region_addr) { // One destination region.
_destination_count = 1; if (end_region_addr == destination) { // The destination falls on a region boundary, thus the first word of the // partial object will be the first word copied to the destination region.
_dest_region_addr = end_region_addr;
_first_src_addr = sd.region_to_addr(src_region_idx);
}
} else { // Two destination regions. When copied, the partial object will cross a // destination region boundary, so a word somewhere within the partial // object will be the first word copied to the second destination region.
_destination_count = 2;
_dest_region_addr = end_region_addr; const size_t ofs = pointer_delta(end_region_addr, destination);
assert(ofs < _partial_obj_size, "sanity");
_first_src_addr = sd.region_to_addr(src_region_idx) + ofs;
}
}
// Print the 'reclaimed ratio' for regions while there is something live in // the region or to the right of it. The remaining regions are empty (and // uninteresting), and computing the ratio will result in division by 0. while (i < end_region && live_to_right > 0) {
c = summary_data.region(i);
HeapWord* const region_addr = summary_data.region_to_addr(i); const size_t used_to_right = pointer_delta(space->top(), region_addr); const size_t dead_to_right = used_to_right - live_to_right; constdouble reclaimed_ratio = double(dead_to_right) / live_to_right;
// Any remaining regions are empty. Print one more if there is one. if (i < end_region) {
ParallelCompactData::RegionData* c = summary_data.region(i);
log_develop_trace(gc, compaction)(
SIZE_FORMAT_W(5) " " PTR_FORMAT " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " %d",
i, p2i(c->destination()),
c->partial_obj_size(), c->live_obj_size(),
c->data_size(), c->source_region(), c->destination_count());
}
unsignedint id = PSParallelCompact::old_space_id; const MutableSpace* space; do {
space = space_info[id].space();
print_initial_summary_data(summary_data, space);
} while (++id < PSParallelCompact::eden_space_id);
do {
space = space_info[id].space();
print_generic_summary_data(summary_data, space->bottom(), space->top());
} while (++id < PSParallelCompact::last_space_id);
} #endif// #ifndef PRODUCT
void ParallelCompactData::clear_range(size_t beg_region, size_t end_region) {
assert(beg_region <= _region_count, "beg_region out of range");
assert(end_region <= _region_count, "end_region out of range");
assert(RegionSize % BlockSize == 0, "RegionSize not a multiple of BlockSize");
// Middle regions--completely spanned by this object. for (size_t region = beg_region + 1; region < end_region; ++region) {
_region_data[region].set_partial_obj_size(RegionSize);
_region_data[region].set_partial_obj_addr(addr);
}
// Last region. const size_t end_ofs = region_offset(addr + len - 1);
_region_data[end_region].set_partial_obj_size(end_ofs + 1);
_region_data[end_region].set_partial_obj_addr(addr);
}
// Update live_obj_size so the region appears completely full.
size_t live_size = RegionSize - _region_data[cur_region].partial_obj_size();
_region_data[cur_region].set_live_obj_size(live_size);
++cur_region;
addr += RegionSize;
}
}
// Find the point at which a space can be split and, if necessary, record the // split point. // // If the current src region (which overflowed the destination space) doesn't // have a partial object, the split point is at the beginning of the current src // region (an "easy" split, no extra bookkeeping required). // // If the current src region has a partial object, the split point is in the // region where that partial object starts (call it the split_region). If // split_region has a partial object, then the split point is just after that // partial object (a "hard" split where we have to record the split data and // zero the partial_obj_size field). With a "hard" split, we know that the // partial_obj ends within split_region because the partial object that caused // the overflow starts in split_region. If split_region doesn't have a partial // obj, then the split is at the beginning of split_region (another "easy" // split).
HeapWord*
ParallelCompactData::summarize_split_space(size_t src_region,
SplitInfo& split_info,
HeapWord* destination,
HeapWord* target_end,
HeapWord** target_next)
{
assert(destination <= target_end, "sanity");
assert(destination + _region_data[src_region].data_size() > target_end, "region should not fit into target space");
assert(is_region_aligned(target_end), "sanity");
if (destination + partial_obj_size > target_end) { // The split point is just after the partial object (if any) in the // src_region that contains the start of the object that overflowed the // destination space. // // Find the start of the "overflow" object and set split_region to the // region containing it.
HeapWord* const overflow_obj = _region_data[src_region].partial_obj_addr();
split_region = addr_to_region_idx(overflow_obj);
// Clear the source_region field of all destination regions whose first word // came from data after the split point (a non-null source_region field // implies a region must be filled). // // An alternative to the simple loop below: clear during post_compact(), // which uses memcpy instead of individual stores, and is easy to // parallelize. (The downside is that it clears the entire RegionData // object as opposed to just one field.) // // post_compact() would have to clear the summary data up to the highest // address that was written during the summary phase, which would be // // max(top, max(new_top, clear_top)) // // where clear_top is a new field in SpaceInfo. Would have to set clear_top // to target_end. const RegionData* const sr = region(split_region); const size_t beg_idx =
addr_to_region_idx(region_align_up(sr->destination() +
sr->partial_obj_size())); const size_t end_idx = addr_to_region_idx(target_end);
log_develop_trace(gc, compaction)("split: clearing source_region field in [" SIZE_FORMAT ", " SIZE_FORMAT ")", beg_idx, end_idx); for (size_t idx = beg_idx; idx < end_idx; ++idx) {
_region_data[idx].set_source_region(0);
}
// Set split_destination and partial_obj_size to reflect the split region.
split_destination = sr->destination();
partial_obj_size = sr->partial_obj_size();
}
// The split is recorded only if a partial object extends onto the region. if (partial_obj_size != 0) {
_region_data[split_region].set_partial_obj_size(0);
split_info.record(split_region, partial_obj_size, split_destination);
}
HeapWord *dest_addr = target_beg; while (cur_region < end_region) { // The destination must be set even if the region has no data.
_region_data[cur_region].set_destination(dest_addr);
size_t words = _region_data[cur_region].data_size(); if (words > 0) { // If cur_region does not fit entirely into the target space, find a point // at which the source space can be 'split' so that part is copied to the // target space and the rest is copied elsewhere. if (dest_addr + words > target_end) {
assert(source_next != NULL, "source_next is NULL when splitting");
*source_next = summarize_split_space(cur_region, split_info, dest_addr,
target_end, target_next); returnfalse;
}
// Compute the destination_count for cur_region, and if necessary, update // source_region for a destination region. The source_region field is // updated if cur_region is the first (left-most) region to be copied to a // destination region. // // The destination_count calculation is a bit subtle. A region that has // data that compacts into itself does not count itself as a destination. // This maintains the invariant that a zero count means the region is // available and can be claimed and then filled.
uint destination_count = 0; if (split_info.is_split(cur_region)) { // The current region has been split: the partial object will be copied // to one destination space and the remaining data will be copied to // another destination space. Adjust the initial destination_count and, // if necessary, set the source_region field if the partial object will // cross a destination region boundary.
destination_count = split_info.destination_count(); if (destination_count == 2) {
size_t dest_idx = addr_to_region_idx(split_info.dest_region_addr());
_region_data[dest_idx].set_source_region(cur_region);
}
}
// Initially assume that the destination regions will be the same and // adjust the value below if necessary. Under this assumption, if // cur_region == dest_region_2, then cur_region will be compacted // completely into itself.
destination_count += cur_region == dest_region_2 ? 0 : 1; if (dest_region_1 != dest_region_2) { // Destination regions differ; adjust destination_count.
destination_count += 1; // Data from cur_region will be copied to the start of dest_region_2.
_region_data[dest_region_2].set_source_region(cur_region);
} elseif (is_region_aligned(dest_addr)) { // Data from cur_region will be copied to the start of the destination // region.
_region_data[dest_region_1].set_source_region(cur_region);
}
// Region covering the object.
RegionData* const region_ptr = addr_to_region_ptr(addr);
HeapWord* result = region_ptr->destination();
// If the entire Region is live, the new location is region->destination + the // offset of the object within in the Region.
// Run some performance tests to determine if this special case pays off. It // is worth it for pointers into the dense prefix. If the optimization to // avoid pointer updates in regions that only point to the dense prefix is // ever implemented, this should be revisited. if (region_ptr->data_size() == RegionSize) {
result += region_offset(addr); return result;
}
// Otherwise, the new location is region->destination + block offset + the // number of live words in the Block that are (a) to the left of addr and (b) // due to objects that start in the Block.
// Fill in the block table if necessary. This is unsynchronized, so multiple // threads may fill the block table for a region (harmless, since it is // idempotent). if (!region_ptr->blocks_filled()) {
PSParallelCompact::fill_blocks(addr_to_region_idx(addr));
region_ptr->set_blocks_filled();
}
void
PSParallelCompact::clear_data_covering_space(SpaceId id)
{ // At this point, top is the value before GC, new_top() is the value that will // be set at the end of GC. The marking bitmap is cleared to top; nothing // should be marked above top. The summary data is cleared to the larger of // top & new_top.
MutableSpace* const space = _space_info[id].space();
HeapWord* const bot = space->bottom();
HeapWord* const top = space->top();
HeapWord* const max_top = MAX2(top, _space_info[id].new_top());
// Clear the data used to 'split' regions.
SplitInfo& split_info = _space_info[id].split_info(); if (split_info.is_valid()) {
split_info.clear();
}
DEBUG_ONLY(split_info.verify_clear();)
}
void PSParallelCompact::pre_compact()
{ // Update the from & to space pointers in space_info, since they are swapped // at each young gen gc. Do the update unconditionally (even though a // promotion failure does not swap spaces) because an unknown number of young // collections will have swapped the spaces an unknown number of times.
GCTraceTime(Debug, gc, phases) tm("Pre Compact", &_gc_timer);
ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
_space_info[from_space_id].set_space(heap->young_gen()->from_space());
_space_info[to_space_id].set_space(heap->young_gen()->to_space());
// Increment the invocation count
heap->increment_total_collections(true);
for (unsignedint id = old_space_id; id < last_space_id; ++id) { // Clear the marking bitmap, summary data and split info.
clear_data_covering_space(SpaceId(id)); // Update top(). Must be done after clearing the bitmap and summary data.
_space_info[id].publish_new_top();
}
// Update heap occupancy information which is used as input to the soft ref // clearing policy at the next gc.
Universe::heap()->update_capacity_and_used_at_gc();
// Delete metaspaces for unloaded class loaders and clean up loader_data graph
ClassLoaderDataGraph::purge(/*at_safepoint*/true);
DEBUG_ONLY(MetaspaceUtils::verify();)
// Need to clear claim bits for the next mark.
ClassLoaderDataGraph::clear_claimed_marks();
// Skip full regions at the beginning of the space--they are necessarily part // of the dense prefix.
size_t full_count = 0; const RegionData* cp; for (cp = beg_cp; cp < end_cp && cp->data_size() == region_size; ++cp) {
++full_count;
}
if (cur_deadwood >= deadwood_goal) { // Found the region that has the correct amount of deadwood to the left. // This typically occurs after crossing a fairly sparse set of regions, so // iterate backwards over those sparse regions, looking for the region // that has the lowest density of live objects 'to the right.'
size_t space_to_left = sd.region(cp) * region_size;
size_t live_to_left = space_to_left - cur_deadwood;
size_t space_to_right = space_capacity - space_to_left;
size_t live_to_right = space_live - live_to_left; double density_to_right = double(live_to_right) / space_to_right; while (cp > full_cp) {
--cp; const size_t prev_region_live_to_right = live_to_right -
cp->data_size(); const size_t prev_region_space_to_right = space_to_right + region_size; double prev_region_density_to_right = double(prev_region_live_to_right) / prev_region_space_to_right; if (density_to_right <= prev_region_density_to_right) { return dense_prefix;
}
log_develop_trace(gc, compaction)( "backing up from c=" SIZE_FORMAT_W(4) " d2r=%10.8f " "pc_d2r=%10.8f",
sd.region(cp), density_to_right,
prev_region_density_to_right);
// The raw limit is the value of the normal distribution at x = density. constdouble raw_limit = normal_distribution(density);
// Adjust the raw limit so it becomes the minimum when the density is 1. // // First subtract the adjustment value (which is simply the precomputed value // normal_distribution(1.0)); this yields a value of 0 when the density is 1. // Then add the minimum value, so the minimum is returned when the density is // 1. Finally, prevent negative values, which occur when the mean is not 0.5. constdouble min = double(min_percent) / 100.0; constdouble limit = raw_limit - _dwl_adjustment + min; return MAX2(limit, 0.0);
}
ParallelCompactData::RegionData*
PSParallelCompact::first_dead_space_region(const RegionData* beg, const RegionData* end)
{ const size_t region_size = ParallelCompactData::RegionSize;
ParallelCompactData& sd = summary_data();
size_t left = sd.region(beg);
size_t right = end > beg ? sd.region(end) - 1 : left;
// Binary search. while (left < right) { // Equivalent to (left + right) / 2, but does not overflow. const size_t middle = left + (right - left) / 2;
RegionData* const middle_ptr = sd.region(middle);
HeapWord* const dest = middle_ptr->destination();
HeapWord* const addr = sd.region_to_addr(middle);
assert(dest != NULL, "sanity");
assert(dest <= addr, "must move left");
if (middle > left && dest < addr) {
right = middle - 1;
} elseif (middle < right && middle_ptr->data_size() == region_size) {
left = middle + 1;
} else { return middle_ptr;
}
} return sd.region(left);
}
ParallelCompactData::RegionData*
PSParallelCompact::dead_wood_limit_region(const RegionData* beg, const RegionData* end,
size_t dead_words)
{
ParallelCompactData& sd = summary_data();
size_t left = sd.region(beg);
size_t right = end > beg ? sd.region(end) - 1 : left;
// Binary search. while (left < right) { // Equivalent to (left + right) / 2, but does not overflow. const size_t middle = left + (right - left) / 2;
RegionData* const middle_ptr = sd.region(middle);
HeapWord* const dest = middle_ptr->destination();
HeapWord* const addr = sd.region_to_addr(middle);
assert(dest != NULL, "sanity");
assert(dest <= addr, "must move left");
const size_t dead_to_left = pointer_delta(addr, dest); if (middle > left && dead_to_left > dead_words) {
right = middle - 1;
} elseif (middle < right && dead_to_left < dead_words) {
left = middle + 1;
} else { return middle_ptr;
}
} return sd.region(left);
}
// The result is valid during the summary phase, after the initial summarization // of each space into itself, and before final summarization. inlinedouble
PSParallelCompact::reclaimed_ratio(const RegionData* const cp,
HeapWord* const bottom,
HeapWord* const top,
HeapWord* const new_top)
{
ParallelCompactData& sd = summary_data();
assert(cp != NULL, "sanity");
assert(bottom != NULL, "sanity");
assert(top != NULL, "sanity");
assert(new_top != NULL, "sanity");
assert(top >= new_top, "summary data problem?");
assert(new_top > bottom, "space is empty; should not be here");
assert(new_top >= cp->destination(), "sanity");
assert(top >= sd.region_to_addr(cp), "sanity");
// Return the address of the end of the dense prefix, a.k.a. the start of the // compacted region. The address is always on a region boundary. // // Completely full regions at the left are skipped, since no compaction can // occur in those regions. Then the maximum amount of dead wood to allow is // computed, based on the density (amount live / capacity) of the generation; // the region with approximately that amount of dead space to the left is // identified as the limit region. Regions between the last completely full // region and the limit region are scanned and the one that has the best // (maximum) reclaimed_ratio() is selected.
HeapWord*
PSParallelCompact::compute_dense_prefix(const SpaceId id, bool maximum_compaction)
{ const size_t region_size = ParallelCompactData::RegionSize; const ParallelCompactData& sd = summary_data();
// Skip full regions at the beginning of the space--they are necessarily part // of the dense prefix. const RegionData* const full_cp = first_dead_space_region(beg_cp, new_top_cp);
assert(full_cp->destination() == sd.region_to_addr(full_cp) ||
space->is_empty(), "no dead space allowed to the left");
assert(full_cp->data_size() < region_size || full_cp == new_top_cp - 1, "region must have dead space");
// The gc number is saved whenever a maximum compaction is done, and used to // determine when the maximum compaction interval has expired. This avoids // successive max compactions for different reasons.
assert(total_invocations() >= _maximum_compaction_gc_num, "sanity"); const size_t gcs_since_max = total_invocations() - _maximum_compaction_gc_num; constbool interval_ended = gcs_since_max > HeapMaximumCompactionInterval ||
total_invocations() == HeapFirstMaximumCompactionCount; if (maximum_compaction || full_cp == top_cp || interval_ended) {
_maximum_compaction_gc_num = total_invocations(); return sd.region_to_addr(full_cp);
}
// Locate the region with the desired amount of dead space to the left. const RegionData* const limit_cp =
dead_wood_limit_region(full_cp, top_cp, dead_wood_limit);
// Scan from the first region with dead space to the limit region and find the // one with the best (largest) reclaimed ratio. double best_ratio = 0.0; const RegionData* best_cp = full_cp; for (const RegionData* cp = full_cp; cp < limit_cp; ++cp) { double tmp_ratio = reclaimed_ratio(cp, bottom, top, new_top); if (tmp_ratio > best_ratio) {
best_cp = cp;
best_ratio = tmp_ratio;
}
}
return sd.region_to_addr(best_cp);
}
void PSParallelCompact::summarize_spaces_quick()
{ for (unsignedint i = 0; i < last_space_id; ++i) { const MutableSpace* space = _space_info[i].space();
HeapWord** nta = _space_info[i].new_top_addr(); bool result = _summary_data.summarize(_space_info[i].split_info(),
space->bottom(), space->top(), NULL,
space->bottom(), space->end(), nta);
assert(result, "space must fit into itself");
_space_info[i].set_dense_prefix(space->bottom());
}
}
void PSParallelCompact::fill_dense_prefix_end(SpaceId id)
{
HeapWord* const dense_prefix_end = dense_prefix(id); const RegionData* region = _summary_data.addr_to_region_ptr(dense_prefix_end); const idx_t dense_prefix_bit = _mark_bitmap.addr_to_bit(dense_prefix_end); if (dead_space_crosses_boundary(region, dense_prefix_bit)) { // Only enough dead space is filled so that any remaining dead space to the // left is larger than the minimum filler object. (The remainder is filled // during the copy/update phase.) // // The size of the dead space to the right of the boundary is not a // concern, since compaction will be able to use whatever space is // available. // // Here '||' is the boundary, 'x' represents a don't care bit and a box // surrounds the space to be filled with an object. // // In the 32-bit VM, each bit represents two 32-bit words: // +---+ // a) beg_bits: ... x x x | 0 | || 0 x x ... // end_bits: ... x x x | 0 | || 0 x x ... // +---+ // // In the 64-bit VM, each bit represents one 64-bit word: // +------------+ // b) beg_bits: ... x x x | 0 || 0 | x x ... // end_bits: ... x x 1 | 0 || 0 | x x ... // +------------+ // +-------+ // c) beg_bits: ... x x | 0 0 | || 0 x x ... // end_bits: ... x 1 | 0 0 | || 0 x x ... // +-------+ // +-----------+ // d) beg_bits: ... x | 0 0 0 | || 0 x x ... // end_bits: ... 1 | 0 0 0 | || 0 x x ... // +-----------+ // +-------+ // e) beg_bits: ... 0 0 | 0 0 | || 0 x x ... // end_bits: ... 0 0 | 0 0 | || 0 x x ... // +-------+
// Initially assume case a, c or e will apply.
size_t obj_len = CollectedHeap::min_fill_size();
HeapWord* obj_beg = dense_prefix_end - obj_len;
#ifdef _LP64 if (MinObjAlignment > 1) { // object alignment > heap word size // Cases a, c or e.
} elseif (_mark_bitmap.is_obj_end(dense_prefix_bit - 2)) { // Case b above.
obj_beg = dense_prefix_end - 1;
} elseif (!_mark_bitmap.is_obj_end(dense_prefix_bit - 3) &&
_mark_bitmap.is_obj_end(dense_prefix_bit - 4)) { // Case d above.
obj_beg = dense_prefix_end - 3;
obj_len = 3;
} #endif// #ifdef _LP64
void
PSParallelCompact::summarize_space(SpaceId id, bool maximum_compaction)
{
assert(id < last_space_id, "id out of range");
assert(_space_info[id].dense_prefix() == _space_info[id].space()->bottom(), "should have been reset in summarize_spaces_quick()");
const MutableSpace* space = _space_info[id].space(); if (_space_info[id].new_top() != space->bottom()) {
HeapWord* dense_prefix_end = compute_dense_prefix(id, maximum_compaction);
_space_info[id].set_dense_prefix(dense_prefix_end);
// Recompute the summary data, taking into account the dense prefix. If // every last byte will be reclaimed, then the existing summary data which // compacts everything can be left in place. if (!maximum_compaction && dense_prefix_end != space->bottom()) { // If dead space crosses the dense prefix boundary, it is (at least // partially) filled with a dummy object, marked live and added to the // summary data. This simplifies the copy/update phase and must be done // before the final locations of objects are determined, to prevent // leaving a fragment of dead space that is too small to fill.
fill_dense_prefix_end(id);
// Compute the destination of each Region, and thus each object.
_summary_data.summarize_dense_prefix(space->bottom(), dense_prefix_end);
_summary_data.summarize(_space_info[id].split_info(),
dense_prefix_end, space->top(), NULL,
dense_prefix_end, space->end(),
_space_info[id].new_top_addr());
}
}
// Quick summarization of each space into itself, to see how much is live.
summarize_spaces_quick();
log_develop_trace(gc, compaction)("summary phase: after summarizing each space to self");
NOT_PRODUCT(print_region_ranges());
NOT_PRODUCT(print_initial_summary_data(_summary_data, _space_info));
// The amount of live data that will end up in old space (assuming it fits).
size_t old_space_total_live = 0; for (unsignedint id = old_space_id; id < last_space_id; ++id) {
old_space_total_live += pointer_delta(_space_info[id].new_top(),
_space_info[id].space()->bottom());
--> --------------------
--> maximum size reached
--> --------------------
Messung V0.5
¤ Dauer der Verarbeitung: 0.29 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.