Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8276670: G1: Move and rename G1CardSetFreePool and related classes #6289

Closed
@@ -25,10 +25,9 @@
#include "precompiled.hpp"

#include "gc/g1/g1CardSetMemory.inline.hpp"
#include "gc/g1/g1CardSetContainers.inline.hpp"
Copy link
Contributor

@tschatzl tschatzl Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include order, must be sorted alphabetically

#include "gc/g1/g1SegmentedArray.inline.hpp"
#include "logging/log.hpp"
#include "runtime/atomic.hpp"
#include "utilities/formatBuffer.hpp"
#include "utilities/ostream.hpp"


@@ -142,168 +141,8 @@ void G1CardSetAllocator<Elem>::print(outputStream* os) {
mem_size());
}

G1CardSetMemoryStats::G1CardSetMemoryStats() {
clear();
}

void G1CardSetMemoryStats::clear() {
for (uint i = 0; i < num_pools(); i++) {
_num_mem_sizes[i] = 0;
_num_buffers[i] = 0;
}
}

void G1CardSetFreePool::update_unlink_processors(G1ReturnMemoryProcessorSet* unlink_processor) {
uint num_free_lists = _freelist_pool.num_free_lists();

for (uint i = 0; i < num_free_lists; i++) {
unlink_processor->at(i)->visit_free_list(_freelist_pool.free_list(i));
}
}

void G1CardSetFreePool::G1ReturnMemoryProcessor::visit_free_list(G1CardSetBufferList* source) {
assert(_source == nullptr, "already visited");
if (_return_to_vm_size > 0) {
_source = source;
} else {
assert(_source == nullptr, "must be");
}
if (source->mem_size() > _return_to_vm_size) {
_first = source->get_all(_num_unlinked, _unlinked_bytes);
} else {
assert(_first == nullptr, "must be");
}
// Above we were racing with other threads getting the contents of the free list,
// so while we might have been asked to return something to the OS initially,
// the free list might be empty anyway. In this case just reset internal values
// used for checking whether there is work available.
if (_first == nullptr) {
_source = nullptr;
_return_to_vm_size = 0;
}
}

bool G1CardSetFreePool::G1ReturnMemoryProcessor::return_to_vm(jlong deadline) {
assert(!finished_return_to_vm(), "already returned everything to the VM");
assert(_first != nullptr, "must have element to return");

size_t keep_size = 0;
size_t keep_num = 0;

G1CardSetBuffer* cur = _first;
G1CardSetBuffer* last = nullptr;

while (cur != nullptr && _return_to_vm_size > 0) {
size_t cur_size = cur->mem_size();
_return_to_vm_size -= MIN2(_return_to_vm_size, cur_size);

keep_size += cur_size;
keep_num++;

last = cur;
cur = cur->next();
// To ensure progress, perform the deadline check here.
if (os::elapsed_counter() > deadline) {
break;
}
}

assert(_first != nullptr, "must be");
assert(last != nullptr, "must be");

last->set_next(nullptr);

// Wait for any in-progress pops to avoid ABA for them.
GlobalCounter::write_synchronize();
_source->bulk_add(*_first, *last, keep_num, keep_size);
_first = cur;

log_trace(gc, task)("Card Set Free Memory: Returned to VM %zu buffers size %zu", keep_num, keep_size);

// _return_to_vm_size may be larger than what is available in the list at the
// time we actually get the list. I.e. the list and _return_to_vm_size may be
// inconsistent.
// So also check if we actually already at the end of the list for the exit
// condition.
if (_return_to_vm_size == 0 || _first == nullptr) {
_source = nullptr;
_return_to_vm_size = 0;
}
return _source != nullptr;
}

bool G1CardSetFreePool::G1ReturnMemoryProcessor::return_to_os(jlong deadline) {
assert(finished_return_to_vm(), "not finished returning to VM");
assert(!finished_return_to_os(), "already returned everything to the OS");

// Now delete the rest.
size_t num_delete = 0;
size_t mem_size_deleted = 0;

while (_first != nullptr) {
G1CardSetBuffer* next = _first->next();
num_delete++;
mem_size_deleted += _first->mem_size();
delete _first;
_first = next;

// To ensure progress, perform the deadline check here.
if (os::elapsed_counter() > deadline) {
break;
}
}

log_trace(gc, task)("Card Set Free Memory: Return to OS %zu buffers size %zu", num_delete, mem_size_deleted);

return _first != nullptr;
}

G1CardSetFreePool G1CardSetFreePool::_freelist_pool(G1CardSetConfiguration::num_mem_object_types());

G1CardSetFreePool::G1CardSetFreePool(uint num_free_lists) :
_num_free_lists(num_free_lists) {

_free_lists = NEW_C_HEAP_ARRAY(G1CardSetBufferList, _num_free_lists, mtGC);
for (uint i = 0; i < _num_free_lists; i++) {
new (&_free_lists[i]) G1CardSetBufferList();
}
}

G1CardSetFreePool::~G1CardSetFreePool() {
for (uint i = 0; i < _num_free_lists; i++) {
_free_lists[i].~G1CardSetBufferList();
}
FREE_C_HEAP_ARRAY(mtGC, _free_lists);
}

G1CardSetMemoryStats G1CardSetFreePool::memory_sizes() const {
G1CardSetMemoryStats free_list_stats;
assert(free_list_stats.num_pools() == num_free_lists(), "must be");
for (uint i = 0; i < num_free_lists(); i++) {
free_list_stats._num_mem_sizes[i] = _free_lists[i].mem_size();
free_list_stats._num_buffers[i] = _free_lists[i].num_buffers();
}
return free_list_stats;
}

size_t G1CardSetFreePool::mem_size() const {
size_t result = 0;
for (uint i = 0; i < _num_free_lists; i++) {
result += _free_lists[i].mem_size();
}
return result;
}

void G1CardSetFreePool::print_on(outputStream* out) {
out->print_cr(" Free Pool: size %zu", free_list_pool()->mem_size());
for (uint i = 0; i < _num_free_lists; i++) {
FormatBuffer<> fmt(" %s", G1CardSetConfiguration::mem_object_type_name_str(i));
_free_lists[i].print_on(out, fmt);
}
}

G1CardSetMemoryManager::G1CardSetMemoryManager(G1CardSetConfiguration* config,
G1CardSetFreePool* free_list_pool) : _config(config) {
G1SegmentedArrayFreePool<mtGCCardSet>* free_list_pool) : _config(config) {

_allocators = NEW_C_HEAP_ARRAY(G1CardSetAllocator<G1CardSetContainer>,
_config->num_mem_object_types(),
@@ -363,8 +202,8 @@ size_t G1CardSetMemoryManager::wasted_mem_size() const {
return result;
}

G1CardSetMemoryStats G1CardSetMemoryManager::memory_stats() const {
G1CardSetMemoryStats result;
G1SegmentedArrayMemoryStats G1CardSetMemoryManager::memory_stats() const {
G1SegmentedArrayMemoryStats result;
for (uint i = 0; i < num_mem_object_types(); i++) {
result._num_mem_sizes[i] += _allocators[i].mem_size();
result._num_buffers[i] += _allocators[i].num_buffers();
@@ -25,9 +25,9 @@
#ifndef SHARE_GC_G1_G1CARDSETMEMORY_HPP
#define SHARE_GC_G1_G1CARDSETMEMORY_HPP

#include "gc/g1/g1SegmentedArrayFreePool.hpp"
#include "gc/g1/g1CardSet.hpp"
#include "gc/g1/g1CardSetContainers.hpp"
#include "gc/g1/g1CardSetContainers.inline.hpp"
#include "gc/g1/g1SegmentedArray.hpp"
#include "memory/allocation.hpp"
#include "utilities/growableArray.hpp"
@@ -136,99 +136,6 @@ class G1CardSetAllocator {
void print(outputStream* os);
};

// Statistics for a fixed set of buffer lists. Contains the number of buffers and memory
// used for each. Note that statistics are typically not taken atomically so there
// can be inconsistencies. The user must be prepared for them.
class G1CardSetMemoryStats {
public:

size_t _num_mem_sizes[G1CardSetConfiguration::num_mem_object_types()];
size_t _num_buffers[G1CardSetConfiguration::num_mem_object_types()];

// Returns all-zero statistics.
G1CardSetMemoryStats();

void add(G1CardSetMemoryStats const other) {
STATIC_ASSERT(ARRAY_SIZE(_num_buffers) == ARRAY_SIZE(_num_mem_sizes));
for (uint i = 0; i < ARRAY_SIZE(_num_mem_sizes); i++) {
_num_mem_sizes[i] += other._num_mem_sizes[i];
_num_buffers[i] += other._num_buffers[i];
}
}

void clear();

uint num_pools() const { return G1CardSetConfiguration::num_mem_object_types(); }
};

// A set of free lists holding memory buffers for use by G1CardSetAllocators.
class G1CardSetFreePool {
// The global free pool.
static G1CardSetFreePool _freelist_pool;

const uint _num_free_lists;
G1CardSetBufferList* _free_lists;

public:
static G1CardSetFreePool* free_list_pool() { return &_freelist_pool; }
static G1CardSetMemoryStats free_list_sizes() { return _freelist_pool.memory_sizes(); }

class G1ReturnMemoryProcessor;
typedef GrowableArrayCHeap<G1ReturnMemoryProcessor*, mtGC> G1ReturnMemoryProcessorSet;

static void update_unlink_processors(G1ReturnMemoryProcessorSet* unlink_processors);

explicit G1CardSetFreePool(uint num_free_lists);
~G1CardSetFreePool();

G1CardSetBufferList* free_list(uint i) {
assert(i < _num_free_lists, "must be");
return &_free_lists[i];
}

uint num_free_lists() const { return _num_free_lists; }

G1CardSetMemoryStats memory_sizes() const;
size_t mem_size() const;

void print_on(outputStream* out);
};

// Data structure containing current in-progress state for returning memory to the
// operating system for a single G1CardSetBufferList.
class G1CardSetFreePool::G1ReturnMemoryProcessor : public CHeapObj<mtGC> {
G1CardSetBufferList* _source;
size_t _return_to_vm_size;

G1CardSetBuffer* _first;
size_t _unlinked_bytes;
size_t _num_unlinked;

public:
explicit G1ReturnMemoryProcessor(size_t return_to_vm) :
_source(nullptr), _return_to_vm_size(return_to_vm), _first(nullptr), _unlinked_bytes(0), _num_unlinked(0) {
}

// Updates the instance members about the given card set buffer list for the purpose
// of giving back memory. Only necessary members are updated, e.g. if there is
// nothing to return to the VM, do not set the source list.
void visit_free_list(G1CardSetBufferList* source);

bool finished_return_to_vm() const { return _return_to_vm_size == 0; }
bool finished_return_to_os() const { return _first == nullptr; }

// Returns memory to the VM until the given deadline expires. Returns true if
// there is no more work. Guarantees forward progress, i.e. at least one buffer
// has been processed after returning.
// return_to_vm() re-adds buffers to the respective free list.
bool return_to_vm(jlong deadline);
// Returns memory to the VM until the given deadline expires. Returns true if
// there is no more work. Guarantees forward progress, i.e. at least one buffer
// has been processed after returning.
// return_to_os() gives back buffers to the OS.
bool return_to_os(jlong deadline);
};

class G1CardSetMemoryManager : public CHeapObj<mtGCCardSet> {
G1CardSetConfiguration* _config;

@@ -237,7 +144,7 @@ class G1CardSetMemoryManager : public CHeapObj<mtGCCardSet> {
uint num_mem_object_types() const;
public:
G1CardSetMemoryManager(G1CardSetConfiguration* config,
G1CardSetFreePool* free_list_pool);
G1SegmentedArrayFreePool<mtGCCardSet>* free_list_pool);

virtual ~G1CardSetMemoryManager();

@@ -256,7 +163,7 @@ class G1CardSetMemoryManager : public CHeapObj<mtGCCardSet> {
size_t mem_size() const;
size_t wasted_mem_size() const;

G1CardSetMemoryStats memory_stats() const;
G1SegmentedArrayMemoryStats memory_stats() const;
};

#endif // SHARE_GC_G1_G1CARDSETMEMORY_HPP
@@ -33,7 +33,7 @@
#include "gc/g1/g1Arguments.hpp"
#include "gc/g1/g1BarrierSet.hpp"
#include "gc/g1/g1BatchedTask.hpp"
#include "gc/g1/g1CardSetFreeMemoryTask.hpp"
#include "gc/g1/g1SegmentedArrayFreeMemoryTask.hpp"
Copy link
Contributor

@tschatzl tschatzl Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include order, must be sorted alphabetically

#include "gc/g1/g1CollectedHeap.inline.hpp"
#include "gc/g1/g1CollectionSet.hpp"
#include "gc/g1/g1CollectionSetCandidates.hpp"
@@ -1431,7 +1431,7 @@ G1CollectedHeap::G1CollectedHeap() :
CollectedHeap(),
_service_thread(NULL),
_periodic_gc_task(NULL),
_free_card_set_memory_task(NULL),
_free_segmented_array_memory_task(NULL),
_workers(NULL),
_card_table(NULL),
_collection_pause_end(Ticks::now()),
@@ -1723,8 +1723,8 @@ jint G1CollectedHeap::initialize() {
_periodic_gc_task = new G1PeriodicGCTask("Periodic GC Task");
_service_thread->register_task(_periodic_gc_task);

_free_card_set_memory_task = new G1CardSetFreeMemoryTask("Card Set Free Memory Task");
_service_thread->register_task(_free_card_set_memory_task);
_free_segmented_array_memory_task = new G1SegmentedArrayFreeMemoryTask("Segmented Array Free Memory Task");
_service_thread->register_task(_free_segmented_array_memory_task);

{
G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
@@ -2616,8 +2616,8 @@ void G1CollectedHeap::gc_epilogue(bool full) {

_collection_pause_end = Ticks::now();

_free_card_set_memory_task->notify_new_stats(&_young_gen_card_set_stats,
&_collection_set_candidates_card_set_stats);
_free_segmented_array_memory_task->notify_new_stats(&_young_gen_card_set_stats,
&_collection_set_candidates_card_set_stats);
}

uint G1CollectedHeap::uncommit_regions(uint region_limit) {
@@ -2941,11 +2941,11 @@ bool G1CollectedHeap::should_sample_collection_set_candidates() const {
return candidates != NULL && candidates->num_remaining() > 0;
}

void G1CollectedHeap::set_collection_set_candidates_stats(G1CardSetMemoryStats& stats) {
void G1CollectedHeap::set_collection_set_candidates_stats(G1SegmentedArrayMemoryStats& stats) {
_collection_set_candidates_card_set_stats = stats;
}

void G1CollectedHeap::set_young_gen_card_set_stats(const G1CardSetMemoryStats& stats) {
void G1CollectedHeap::set_young_gen_card_set_stats(const G1SegmentedArrayMemoryStats& stats) {
_young_gen_card_set_stats = stats;
}