Path: blob/master/test/hotspot/gtest/gc/g1/test_g1RegionMap.cpp
41152 views
/*1* Copyright (c) 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*22*/2324#include "precompiled.hpp"25#include "gc/g1/g1CommittedRegionMap.inline.hpp"26#include "runtime/os.hpp"27#include "unittest.hpp"2829class G1CommittedRegionMapSerial : public G1CommittedRegionMap {30public:31static const uint TestRegions = 512;3233void verify_counts() {34verify_active_count(0, TestRegions, num_active());35verify_inactive_count(0, TestRegions, num_inactive());36}3738protected:39void guarantee_mt_safety_active() const { }40void guarantee_mt_safety_inactive() const { }41};4243static bool mutate() {44return os::random() % 2 == 0;45}4647static void generate_random_map(G1CommittedRegionMap* map) {48for (uint i = 0; i < G1CommittedRegionMapSerial::TestRegions; i++) {49if (mutate()) {50map->activate(i, i+1);51}52}5354if (map->num_active() == 0) {55// If we randomly activated 0 regions, activate the first half56// to have some regions to test.57map->activate(0, G1CommittedRegionMapSerial::TestRegions / 2);58}59}6061static void random_deactivate(G1CommittedRegionMap* map) {62uint current_offset = 0;63do {64HeapRegionRange current = map->next_active_range(current_offset);65if (mutate()) {66if (current.length() < 5) {67// For short ranges, deactivate whole.68map->deactivate(current.start(), current.end());69} else {70// For larger ranges, deactivate half.71map->deactivate(current.start(), current.end() - (current.length() / 2));72}73}74current_offset = current.end();75} while (current_offset != G1CommittedRegionMapSerial::TestRegions);76}7778static void random_uncommit_or_reactive(G1CommittedRegionMap* map) {79uint current_offset = 0;80do {81HeapRegionRange current = map->next_inactive_range(current_offset);82// Randomly either reactivate or uncommit83if (mutate()) {84map->reactivate(current.start(), current.end());85} else {86map->uncommit(current.start(), current.end());87}8889current_offset = current.end();90} while (current_offset != G1CommittedRegionMapSerial::TestRegions);91}9293static void random_activate_free(G1CommittedRegionMap* map) {94uint current_offset = 0;95do {96HeapRegionRange current = map->next_committable_range(current_offset);97// Randomly either reactivate or uncommit98if (mutate()) {99if (current.length() < 5) {100// For short ranges, deactivate whole.101map->activate(current.start(), current.end());102} else {103// For larger ranges, deactivate half.104map->activate(current.start(), current.end() - (current.length() / 2));105}106}107108current_offset = current.end();109} while (current_offset != G1CommittedRegionMapSerial::TestRegions);110}111112TEST(G1CommittedRegionMapTest, serial) {113G1CommittedRegionMapSerial serial_map;114serial_map.initialize(G1CommittedRegionMapSerial::TestRegions);115116// Activate some regions117generate_random_map(&serial_map);118119// Work through the map and mutate it120for (int i = 0; i < 500; i++) {121random_deactivate(&serial_map);122serial_map.verify_counts();123random_uncommit_or_reactive(&serial_map);124serial_map.verify_counts();125random_activate_free(&serial_map);126serial_map.verify_counts();127ASSERT_EQ(serial_map.num_inactive(), 0u);128}129}130131