Path: blob/master/test/hotspot/gtest/gc/shared/test_collectorPolicy.cpp
41152 views
/*1* Copyright (c) 2016, 2020, 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*/2223#include "precompiled.hpp"24#include "gc/serial/serialArguments.hpp"25#include "runtime/arguments.hpp"26#include "runtime/flags/flagSetting.hpp"27#include "runtime/globals_extension.hpp"28#include "utilities/align.hpp"29#include "utilities/globalDefinitions.hpp"30#include "utilities/macros.hpp"31#include "unittest.hpp"3233class TestGenCollectorPolicy {34public:3536class Executor {37public:38virtual void execute() = 0;39};4041class UnaryExecutor : public Executor {42protected:43const size_t param;44public:45UnaryExecutor(size_t val) : param(val) { }46};4748class BinaryExecutor : public Executor {49protected:50const size_t param1;51const size_t param2;52public:53BinaryExecutor(size_t val1, size_t val2) : param1(val1), param2(val2) { }54};5556class TestWrapper {57public:58static void test(Executor* setter1, Executor* setter2, Executor* checker) {59AutoSaveRestore<size_t> FLAG_GUARD(MinHeapSize);60AutoSaveRestore<size_t> FLAG_GUARD(InitialHeapSize);61AutoSaveRestore<size_t> FLAG_GUARD(MaxHeapSize);62AutoSaveRestore<size_t> FLAG_GUARD(MaxNewSize);63AutoSaveRestore<size_t> FLAG_GUARD(MinHeapDeltaBytes);64AutoSaveRestore<size_t> FLAG_GUARD(NewSize);65AutoSaveRestore<size_t> FLAG_GUARD(OldSize);6667MinHeapSize = 40 * M;68FLAG_SET_ERGO(InitialHeapSize, 100 * M);69FLAG_SET_ERGO(OldSize, 4 * M);70FLAG_SET_ERGO(NewSize, 1 * M);71FLAG_SET_ERGO(MaxNewSize, 80 * M);7273ASSERT_NO_FATAL_FAILURE(setter1->execute());7475if (setter2 != NULL) {76ASSERT_NO_FATAL_FAILURE(setter2->execute());77}7879ASSERT_NO_FATAL_FAILURE(checker->execute());80}81static void test(Executor* setter, Executor* checker) {82test(setter, NULL, checker);83}84};8586class SetNewSizeErgo : public UnaryExecutor {87public:88SetNewSizeErgo(size_t param) : UnaryExecutor(param) { }89void execute() {90FLAG_SET_ERGO(NewSize, param);91}92};9394class CheckYoungMin : public UnaryExecutor {95public:96CheckYoungMin(size_t param) : UnaryExecutor(param) { }97void execute() {98SerialArguments sa;99sa.initialize_heap_sizes();100ASSERT_LE(MinNewSize, param);101}102};103104static size_t scale_by_NewRatio_aligned(size_t value, size_t alignment) {105// Accessible via friend declaration106return GenArguments::scale_by_NewRatio_aligned(value, alignment);107}108109class CheckScaledYoungInitial : public Executor {110public:111void execute() {112size_t initial_heap_size = InitialHeapSize;113SerialArguments sa;114sa.initialize_heap_sizes();115116if (InitialHeapSize > initial_heap_size) {117// InitialHeapSize was adapted by sa.initialize_heap_sizes, e.g. due to alignment118// caused by 64K page size.119initial_heap_size = InitialHeapSize;120}121122size_t expected = scale_by_NewRatio_aligned(initial_heap_size, GenAlignment);123ASSERT_EQ(expected, NewSize);124}125};126127class SetNewSizeCmd : public UnaryExecutor {128public:129SetNewSizeCmd(size_t param) : UnaryExecutor(param) { }130void execute() {131FLAG_SET_CMDLINE(NewSize, param);132}133};134135class CheckYoungInitial : public UnaryExecutor {136public:137CheckYoungInitial(size_t param) : UnaryExecutor(param) { }138void execute() {139SerialArguments sa;140sa.initialize_heap_sizes();141142ASSERT_EQ(param, NewSize);143}144};145146class SetOldSizeCmd : public UnaryExecutor {147public:148SetOldSizeCmd(size_t param) : UnaryExecutor(param) { }149void execute() {150FLAG_SET_CMDLINE(OldSize, param);151}152};153154class SetMaxNewSizeCmd : public BinaryExecutor {155public:156SetMaxNewSizeCmd(size_t param1, size_t param2) : BinaryExecutor(param1, param2) { }157void execute() {158size_t heap_alignment = GCArguments::compute_heap_alignment();159size_t new_size_value = align_up(MaxHeapSize, heap_alignment)160- param1 + param2;161FLAG_SET_CMDLINE(MaxNewSize, new_size_value);162}163};164165class CheckOldMin : public UnaryExecutor {166public:167CheckOldMin(size_t param) : UnaryExecutor(param) { }168void execute() {169SerialArguments sa;170sa.initialize_heap_sizes();171ASSERT_LE(MinOldSize, param);172}173};174175class CheckOldInitial : public Executor {176public:177void execute() {178size_t heap_alignment = GCArguments::compute_heap_alignment();179180SerialArguments sa;181sa.initialize_heap_sizes();182183size_t expected_old_initial = align_up(InitialHeapSize, heap_alignment)184- MaxNewSize;185186ASSERT_EQ(expected_old_initial, OldSize);187}188};189190class CheckOldInitialMaxNewSize : public BinaryExecutor {191public:192CheckOldInitialMaxNewSize(size_t param1, size_t param2) : BinaryExecutor(param1, param2) { }193void execute() {194size_t heap_alignment = GCArguments::compute_heap_alignment();195size_t new_size_value = align_up(MaxHeapSize, heap_alignment)196- param1 + param2;197198SerialArguments sa;199sa.initialize_heap_sizes();200201size_t expected_old_initial = align_up(MaxHeapSize, heap_alignment)202- new_size_value;203204ASSERT_EQ(expected_old_initial, OldSize);205}206};207};208209210// Testing that the NewSize flag is handled correct is hard because it211// depends on so many other configurable variables. These tests only try to212// verify that there are some basic rules for NewSize honored by the policies.213214// If NewSize has been ergonomically set, the collector policy215// should use it for min216TEST_VM(CollectorPolicy, young_min_ergo) {217TestGenCollectorPolicy::SetNewSizeErgo setter(20 * M);218TestGenCollectorPolicy::CheckYoungMin checker(20 * M);219220TestGenCollectorPolicy::TestWrapper::test(&setter, &checker);221}222223// If NewSize has been ergonomically set, the collector policy224// should use it for min but calculate the initial young size225// using NewRatio.226TEST_VM(CollectorPolicy, young_scaled_initial_ergo) {227TestGenCollectorPolicy::SetNewSizeErgo setter(20 * M);228TestGenCollectorPolicy::CheckScaledYoungInitial checker;229230TestGenCollectorPolicy::TestWrapper::test(&setter, &checker);231}232233234// Since a flag has been set with FLAG_SET_CMDLINE it235// will be treated as it have been set on the command line for236// the rest of the VM lifetime. This is an irreversible change and237// could impact other tests so we use TEST_OTHER_VM238TEST_OTHER_VM(CollectorPolicy, young_cmd) {239// If NewSize is set on the command line, it should be used240// for both min and initial young size if less than min heap.241TestGenCollectorPolicy::SetNewSizeCmd setter(20 * M);242243TestGenCollectorPolicy::CheckYoungMin checker_min(20 * M);244TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_min);245246TestGenCollectorPolicy::CheckYoungInitial checker_initial(20 * M);247TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_initial);248249// If NewSize is set on command line, but is larger than the min250// heap size, it should only be used for initial young size.251TestGenCollectorPolicy::SetNewSizeCmd setter_large(80 * M);252TestGenCollectorPolicy::CheckYoungInitial checker_large(80 * M);253TestGenCollectorPolicy::TestWrapper::test(&setter_large, &checker_large);254}255256// Since a flag has been set with FLAG_SET_CMDLINE it257// will be treated as it have been set on the command line for258// the rest of the VM lifetime. This is an irreversible change and259// could impact other tests so we use TEST_OTHER_VM260TEST_OTHER_VM(CollectorPolicy, old_cmd) {261// If OldSize is set on the command line, it should be used262// for both min and initial old size if less than min heap.263TestGenCollectorPolicy::SetOldSizeCmd setter(20 * M);264265TestGenCollectorPolicy::CheckOldMin checker_min(20 * M);266TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_min);267268TestGenCollectorPolicy::CheckOldInitial checker_initial;269TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_initial);270271// If MaxNewSize is large, the maximum OldSize will be less than272// what's requested on the command line and it should be reset273// ergonomically.274// We intentionally set MaxNewSize + OldSize > MaxHeapSize275TestGenCollectorPolicy::SetOldSizeCmd setter_old_size(30 * M);276TestGenCollectorPolicy::SetMaxNewSizeCmd setter_max_new_size(30 * M, 20 * M);277TestGenCollectorPolicy::CheckOldInitialMaxNewSize checker_large(30 * M, 20 * M);278279TestGenCollectorPolicy::TestWrapper::test(&setter_old_size, &setter_max_new_size, &checker_large);280}281282283