Path: blob/master/test/hotspot/gtest/metaspace/test_binlist.cpp
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "memory/metaspace/binList.hpp"27#include "memory/metaspace/counters.hpp"28//#define LOG_PLEASE29#include "metaspaceGtestCommon.hpp"3031using metaspace::BinList32;32using metaspace::BinListImpl;33using metaspace::MemRangeCounter;3435#define CHECK_BL_CONTENT(bl, expected_num, expected_size) { \36EXPECT_EQ(bl.count(), (unsigned)expected_num); \37EXPECT_EQ(bl.total_size(), (size_t)expected_size); \38if (expected_num == 0) { \39EXPECT_TRUE(bl.is_empty()); \40} else { \41EXPECT_FALSE(bl.is_empty()); \42} \43}4445template <class BINLISTTYPE>46struct BinListBasicTest {4748static const size_t minws;49static const size_t maxws;5051static void basic_test() {5253BINLISTTYPE bl;5455CHECK_BL_CONTENT(bl, 0, 0);5657MetaWord arr[1000];5859size_t innocous_size = minws + ((maxws - minws) / 2);6061// Try to get a block from an empty list.62size_t real_size = 4711;63MetaWord* p = bl.remove_block(innocous_size, &real_size);64EXPECT_EQ(p, (MetaWord*)NULL);65EXPECT_EQ((size_t)0, real_size);6667// Add a block...68bl.add_block(arr, innocous_size);69CHECK_BL_CONTENT(bl, 1, innocous_size);70DEBUG_ONLY(bl.verify();)7172// And retrieve it.73real_size = 4711;74p = bl.remove_block(innocous_size, &real_size);75EXPECT_EQ(p, arr);76EXPECT_EQ((size_t)innocous_size, real_size);77CHECK_BL_CONTENT(bl, 0, 0);78DEBUG_ONLY(bl.verify();)7980}8182static void basic_test_2() {8384BINLISTTYPE bl;8586CHECK_BL_CONTENT(bl, 0, 0);8788MetaWord arr[1000];8990for (size_t s1 = minws; s1 <= maxws; s1++) {91for (size_t s2 = minws; s2 <= maxws; s2++) {9293bl.add_block(arr, s1);94CHECK_BL_CONTENT(bl, 1, s1);95DEBUG_ONLY(bl.verify();)9697size_t real_size = 4711;98MetaWord* p = bl.remove_block(s2, &real_size);99if (s1 >= s2) {100EXPECT_EQ(p, arr);101EXPECT_EQ((size_t)s1, real_size);102CHECK_BL_CONTENT(bl, 0, 0);103DEBUG_ONLY(bl.verify();)104} else {105EXPECT_EQ(p, (MetaWord*)NULL);106EXPECT_EQ((size_t)0, real_size);107CHECK_BL_CONTENT(bl, 1, s1);108DEBUG_ONLY(bl.verify();)109// drain bl110p = bl.remove_block(minws, &real_size);111EXPECT_EQ(p, arr);112EXPECT_EQ((size_t)s1, real_size);113CHECK_BL_CONTENT(bl, 0, 0);114}115}116}117}118119static void random_test() {120121BINLISTTYPE bl[2];122MemRangeCounter cnt[2];123124#define CHECK_COUNTERS \125ASSERT_EQ(cnt[0].count(), bl[0].count()); \126ASSERT_EQ(cnt[1].count(), bl[1].count()); \127ASSERT_EQ(cnt[0].total_size(), bl[0].total_size()); \128ASSERT_EQ(cnt[1].total_size(), bl[1].total_size());129130FeederBuffer fb(1024);131RandSizeGenerator rgen(minws, maxws + 1);132133// feed all134int which = 0;135for (;;) {136size_t s = rgen.get();137MetaWord* p = fb.get(s);138if (p != NULL) {139bl[which].add_block(p, s);140cnt[which].add(s);141which = which == 0 ? 1 : 0;142} else {143break;144}145}146147CHECK_COUNTERS;148DEBUG_ONLY(bl[0].verify();)149DEBUG_ONLY(bl[1].verify();)150151// play pingpong152for (int iter = 0; iter < 1000; iter++) {153size_t s = rgen.get();154int taker = iter % 2;155int giver = taker == 0 ? 1 : 0;156157size_t real_size = 4711;158MetaWord* p = bl[giver].remove_block(s, &real_size);159if (p != NULL) {160161ASSERT_TRUE(fb.is_valid_range(p, real_size));162ASSERT_GE(real_size, s);163cnt[giver].sub(real_size);164165bl[taker].add_block(p, real_size);166cnt[taker].add(real_size);167168} else {169ASSERT_EQ(real_size, (size_t)NULL);170}171172CHECK_COUNTERS;173174}175176CHECK_COUNTERS;177DEBUG_ONLY(bl[0].verify();)178DEBUG_ONLY(bl[1].verify();)179180// drain both lists.181for (int which = 0; which < 2; which++) {182size_t last_size = 0;183while (bl[which].is_empty() == false) {184185size_t real_size = 4711;186MetaWord* p = bl[which].remove_block(minws, &real_size);187188ASSERT_NE(p, (MetaWord*) NULL);189ASSERT_GE(real_size, minws);190ASSERT_TRUE(fb.is_valid_range(p, real_size));191192// This must hold true since we always return the smallest fit.193ASSERT_GE(real_size, last_size);194if (real_size > last_size) {195last_size = real_size;196}197198cnt[which].sub(real_size);199200CHECK_COUNTERS;201}202}203204}205};206207template <typename BINLISTTYPE> const size_t BinListBasicTest<BINLISTTYPE>::minws = BINLISTTYPE::MinWordSize;208template <typename BINLISTTYPE> const size_t BinListBasicTest<BINLISTTYPE>::maxws = BINLISTTYPE::MaxWordSize;209210TEST_VM(metaspace, BinList_basic_8) { BinListBasicTest< BinListImpl<2, 8> >::basic_test(); }211TEST_VM(metaspace, BinList_basic_16) { BinListBasicTest< BinListImpl<2, 16> >::basic_test(); }212TEST_VM(metaspace, BinList_basic_32) { BinListBasicTest<BinList32>::basic_test(); }213TEST_VM(metaspace, BinList_basic_1331) { BinListBasicTest< BinListImpl<13, 31> >::basic_test(); }214TEST_VM(metaspace, BinList_basic_131) { BinListBasicTest< BinListImpl<13, 1> >::basic_test(); }215216TEST_VM(metaspace, BinList_basic2_8) { BinListBasicTest< BinListImpl<2, 8> >::basic_test_2(); }217TEST_VM(metaspace, BinList_basic2_16) { BinListBasicTest< BinListImpl<2, 16> >::basic_test_2(); }218TEST_VM(metaspace, BinList_basic2_32) { BinListBasicTest<BinList32 >::basic_test_2(); }219TEST_VM(metaspace, BinList_basic2_1331) { BinListBasicTest< BinListImpl<13, 31> >::basic_test_2(); }220TEST_VM(metaspace, BinList_basic2_131) { BinListBasicTest< BinListImpl<13, 1> >::basic_test_2(); }221222TEST_VM(metaspace, BinList_random_test_8) { BinListBasicTest< BinListImpl<2, 8> >::random_test(); }223TEST_VM(metaspace, BinList_random_test_16) { BinListBasicTest< BinListImpl<2, 16> >::random_test(); }224TEST_VM(metaspace, BinList_random_test_32) { BinListBasicTest<BinList32>::random_test(); }225TEST_VM(metaspace, BinList_random_test_1331) { BinListBasicTest< BinListImpl<13, 31> >::random_test(); }226TEST_VM(metaspace, BinList_random_test_131) { BinListBasicTest< BinListImpl<13, 1> >::random_test(); }227228229230