jdk/src/hotspot/share/gc/z/zRelocationSetSelector.cpp
Per Liden 2c9dfc73f9 8255234: ZGC: Bulk allocate forwarding data structures
Co-authored-by: Albert Mingkun Yang <ayang@openjdk.org>
Co-authored-by: Per Liden <pliden@openjdk.org>
Reviewed-by: ayang, stefank
2020-10-28 08:56:57 +00:00

274 lines
9.5 KiB
C++

/*
* Copyright (c) 2017, 2020, 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.
*/
#include "precompiled.hpp"
#include "gc/z/zArray.inline.hpp"
#include "gc/z/zForwarding.inline.hpp"
#include "gc/z/zPage.inline.hpp"
#include "gc/z/zRelocationSet.hpp"
#include "gc/z/zRelocationSetSelector.inline.hpp"
#include "jfr/jfrEvents.hpp"
#include "logging/log.hpp"
#include "runtime/globals.hpp"
#include "utilities/debug.hpp"
#include "utilities/powerOfTwo.hpp"
ZRelocationSetSelectorGroupStats::ZRelocationSetSelectorGroupStats() :
_npages(0),
_total(0),
_live(0),
_garbage(0),
_empty(0),
_compacting_from(0),
_compacting_to(0) {}
ZRelocationSetSelectorGroup::ZRelocationSetSelectorGroup(const char* name,
uint8_t page_type,
size_t page_size,
size_t object_size_limit) :
_name(name),
_page_type(page_type),
_page_size(page_size),
_object_size_limit(object_size_limit),
_fragmentation_limit(page_size * (ZFragmentationLimit / 100)),
_registered_pages(),
_sorted_pages(NULL),
_nselected(0),
_forwarding_entries(0),
_stats() {}
ZRelocationSetSelectorGroup::~ZRelocationSetSelectorGroup() {
FREE_C_HEAP_ARRAY(ZPage*, _sorted_pages);
}
void ZRelocationSetSelectorGroup::register_live_page(ZPage* page) {
const uint8_t type = page->type();
const size_t size = page->size();
const size_t live = page->live_bytes();
const size_t garbage = size - live;
if (garbage > _fragmentation_limit) {
_registered_pages.append(page);
}
_stats._npages++;
_stats._total += size;
_stats._live += live;
_stats._garbage += garbage;
}
void ZRelocationSetSelectorGroup::register_garbage_page(ZPage* page) {
const size_t size = page->size();
_stats._npages++;
_stats._total += size;
_stats._garbage += size;
_stats._empty += size;
}
bool ZRelocationSetSelectorGroup::is_disabled() {
// Medium pages are disabled when their page size is zero
return _page_type == ZPageTypeMedium && _page_size == 0;
}
bool ZRelocationSetSelectorGroup::is_selectable() {
// Large pages are not selectable
return _page_type != ZPageTypeLarge;
}
void ZRelocationSetSelectorGroup::semi_sort() {
// Semi-sort registered pages by live bytes in ascending order
const size_t npartitions_shift = 11;
const size_t npartitions = (size_t)1 << npartitions_shift;
const size_t partition_size = _page_size >> npartitions_shift;
const size_t partition_size_shift = exact_log2(partition_size);
const size_t npages = _registered_pages.length();
// Partition slots/fingers
size_t partitions[npartitions];
// Allocate destination array
assert(_sorted_pages == NULL, "Already initialized");
_sorted_pages = NEW_C_HEAP_ARRAY(ZPage*, npages, mtGC);
debug_only(memset(_sorted_pages, 0, npages * sizeof(ZPage*)));
// Calculate partition slots
memset(partitions, 0, sizeof(partitions));
ZArrayIterator<ZPage*> iter1(&_registered_pages);
for (ZPage* page; iter1.next(&page);) {
const size_t index = page->live_bytes() >> partition_size_shift;
partitions[index]++;
}
// Calculate partition fingers
size_t finger = 0;
for (size_t i = 0; i < npartitions; i++) {
const size_t slots = partitions[i];
partitions[i] = finger;
finger += slots;
}
// Sort pages into partitions
ZArrayIterator<ZPage*> iter2(&_registered_pages);
for (ZPage* page; iter2.next(&page);) {
const size_t index = page->live_bytes() >> partition_size_shift;
const size_t finger = partitions[index]++;
assert(_sorted_pages[finger] == NULL, "Invalid finger");
_sorted_pages[finger] = page;
}
}
void ZRelocationSetSelectorGroup::select_inner() {
// Calculate the number of pages to relocate by successively including pages in
// a candidate relocation set and calculate the maximum space requirement for
// their live objects.
const size_t npages = _registered_pages.length();
size_t selected_from = 0;
size_t selected_to = 0;
size_t selected_forwarding_entries = 0;
size_t from_live_bytes = 0;
size_t from_forwarding_entries = 0;
semi_sort();
for (size_t from = 1; from <= npages; from++) {
// Add page to the candidate relocation set
ZPage* const page = _sorted_pages[from - 1];
from_live_bytes += page->live_bytes();
from_forwarding_entries += ZForwarding::nentries(page);
// Calculate the maximum number of pages needed by the candidate relocation set.
// By subtracting the object size limit from the pages size we get the maximum
// number of pages that the relocation set is guaranteed to fit in, regardless
// of in which order the objects are relocated.
const size_t to = ceil((double)(from_live_bytes) / (double)(_page_size - _object_size_limit));
// Calculate the relative difference in reclaimable space compared to our
// currently selected final relocation set. If this number is larger than the
// acceptable fragmentation limit, then the current candidate relocation set
// becomes our new final relocation set.
const size_t diff_from = from - selected_from;
const size_t diff_to = to - selected_to;
const double diff_reclaimable = 100 - percent_of(diff_to, diff_from);
if (diff_reclaimable > ZFragmentationLimit) {
selected_from = from;
selected_to = to;
selected_forwarding_entries = from_forwarding_entries;
}
log_trace(gc, reloc)("Candidate Relocation Set (%s Pages): " SIZE_FORMAT "->" SIZE_FORMAT ", "
"%.1f%% relative defragmentation, " SIZE_FORMAT " forwarding entries, %s",
_name, from, to, diff_reclaimable, from_forwarding_entries,
(selected_from == from) ? "Selected" : "Rejected");
}
// Finalize selection
_nselected = selected_from;
_forwarding_entries = selected_forwarding_entries;
// Update statistics
_stats._compacting_from = selected_from * _page_size;
_stats._compacting_to = selected_to * _page_size;
log_trace(gc, reloc)("Relocation Set (%s Pages): " SIZE_FORMAT "->" SIZE_FORMAT ", "
SIZE_FORMAT " skipped, " SIZE_FORMAT " forwarding entries",
_name, selected_from, selected_to, npages - selected_from,
selected_forwarding_entries);
}
void ZRelocationSetSelectorGroup::select() {
if (is_disabled()) {
return;
}
EventZRelocationSetGroup event;
if (is_selectable()) {
select_inner();
}
// Send event
event.commit(_page_type, _stats.npages(), _stats.total(), _stats.empty(),
_stats.compacting_from(), _stats.compacting_to());
}
ZRelocationSetSelector::ZRelocationSetSelector() :
_small("Small", ZPageTypeSmall, ZPageSizeSmall, ZObjectSizeLimitSmall),
_medium("Medium", ZPageTypeMedium, ZPageSizeMedium, ZObjectSizeLimitMedium),
_large("Large", ZPageTypeLarge, 0 /* page_size */, 0 /* object_size_limit */) {}
void ZRelocationSetSelector::register_live_page(ZPage* page) {
const uint8_t type = page->type();
if (type == ZPageTypeSmall) {
_small.register_live_page(page);
} else if (type == ZPageTypeMedium) {
_medium.register_live_page(page);
} else {
_large.register_live_page(page);
}
}
void ZRelocationSetSelector::register_garbage_page(ZPage* page) {
const uint8_t type = page->type();
if (type == ZPageTypeSmall) {
_small.register_garbage_page(page);
} else if (type == ZPageTypeMedium) {
_medium.register_garbage_page(page);
} else {
_large.register_garbage_page(page);
}
}
void ZRelocationSetSelector::select(ZRelocationSet* relocation_set) {
// Select pages to relocate. The resulting relocation set will be
// sorted such that medium pages comes first, followed by small
// pages. Pages within each page group will be semi-sorted by live
// bytes in ascending order. Relocating pages in this order allows
// us to start reclaiming memory more quickly.
EventZRelocationSet event;
// Select pages from each group
_large.select();
_medium.select();
_small.select();
// Populate relocation set
relocation_set->populate(_small.selected(), _small.nselected(),
_medium.selected(), _medium.nselected(),
forwarding_entries());
// Send event
event.commit(total(), empty(), compacting_from(), compacting_to());
}
ZRelocationSetSelectorStats ZRelocationSetSelector::stats() const {
ZRelocationSetSelectorStats stats;
stats._small = _small.stats();
stats._medium = _medium.stats();
stats._large = _large.stats();
return stats;
}