Path: blob/master/test/hotspot/gtest/metaspace/test_chunkheaderpool.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/chunkHeaderPool.hpp"27#include "memory/metaspace/counters.hpp"28#include "memory/metaspace/metachunk.hpp"29//#define LOG_PLEASE30#include "metaspaceGtestCommon.hpp"3132using metaspace::ChunkHeaderPool;33using metaspace::Metachunk;34using metaspace::SizeCounter;3536class ChunkHeaderPoolTest {3738static const size_t max_cap = 0x1000;3940ChunkHeaderPool _pool;4142// Array of the same size as the pool max capacity; holds the allocated elements.43Metachunk* _elems[max_cap];44SizeCounter _num_allocated;4546void attempt_free_at(size_t index) {4748LOG("attempt_free_at " SIZE_FORMAT ".", index);4950if (_elems[index] == NULL) {51return;52}5354_pool.return_chunk_header(_elems[index]);55_elems[index] = NULL;5657_num_allocated.decrement();58DEBUG_ONLY(_num_allocated.check(_pool.used());)5960DEBUG_ONLY(_pool.verify();)6162}6364void attempt_allocate_at(size_t index) {6566LOG("attempt_allocate_at " SIZE_FORMAT ".", index);6768if (_elems[index] != NULL) {69return;70}7172Metachunk* c = _pool.allocate_chunk_header();73EXPECT_NOT_NULL(c);74_elems[index] = c;75c->set_free();7677_num_allocated.increment();78DEBUG_ONLY(_num_allocated.check(_pool.used());)7980DEBUG_ONLY(_pool.verify();)81}8283void attempt_allocate_or_free_at(size_t index) {84if (_elems[index] == NULL) {85attempt_allocate_at(index);86} else {87attempt_free_at(index);88}89}9091// Randomly allocate from the pool and free. Slight preference for allocation.92void test_random_alloc_free(int num_iterations) {9394for (int iter = 0; iter < num_iterations; iter++) {95size_t index = (size_t)os::random() % max_cap;96attempt_allocate_or_free_at(index);97}9899DEBUG_ONLY(_pool.verify();)100101}102103static void test_once() {104ChunkHeaderPoolTest test;105test.test_random_alloc_free(100);106}107108public:109110ChunkHeaderPoolTest() : _pool() {111memset(_elems, 0, sizeof(_elems));112}113114static void run_tests() {115for (int i = 0; i < 1000; i++) {116test_once();117}118}119120};121122TEST_VM(metaspace, chunk_header_pool_basics) {123124ChunkHeaderPool pool;125EXPECT_EQ(pool.used(), (int)0);126EXPECT_EQ(pool.freelist_size(), (int)0);127128Metachunk* header = pool.allocate_chunk_header();129EXPECT_NOT_NULL(header);130EXPECT_EQ(pool.used(), 1);131EXPECT_EQ(pool.freelist_size(), (int)0);132133header->set_free();134pool.return_chunk_header(header);135EXPECT_EQ(pool.used(), (int)0);136EXPECT_EQ(pool.freelist_size(), 1);137138header = pool.allocate_chunk_header();139EXPECT_NOT_NULL(header);140EXPECT_EQ(pool.used(), 1);141EXPECT_EQ(pool.freelist_size(), (int)0);142143header->set_free();144pool.return_chunk_header(header);145EXPECT_EQ(pool.used(), (int)0);146EXPECT_EQ(pool.freelist_size(), 1);147148}149150TEST_VM(metaspace, chunk_header_pool) {151ChunkHeaderPoolTest::run_tests();152}153154155