Path: blob/master/src/hotspot/share/memory/metaspace/testHelpers.cpp
40957 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020, 2021 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/metaspaceArena.hpp"27#include "memory/metaspace/metaspaceArenaGrowthPolicy.hpp"28#include "memory/metaspace/metaspaceContext.hpp"29#include "memory/metaspace/testHelpers.hpp"30#include "runtime/mutexLocker.hpp"31#include "utilities/debug.hpp"32#include "utilities/globalDefinitions.hpp"33#include "utilities/ostream.hpp"3435namespace metaspace {3637///// MetaspaceTestArena //////3839MetaspaceTestArena::MetaspaceTestArena(Mutex* lock, MetaspaceArena* arena) :40_lock(lock),41_arena(arena)42{}4344MetaspaceTestArena::~MetaspaceTestArena() {45delete _arena;46delete _lock;47}4849MetaWord* MetaspaceTestArena::allocate(size_t word_size) {50return _arena->allocate(word_size);51}5253void MetaspaceTestArena::deallocate(MetaWord* p, size_t word_size) {54return _arena->deallocate(p, word_size);55}5657///// MetaspaceTestArea //////5859MetaspaceTestContext::MetaspaceTestContext(const char* name, size_t commit_limit, size_t reserve_limit) :60_name(name),61_reserve_limit(reserve_limit),62_commit_limit(commit_limit),63_context(NULL),64_commit_limiter(commit_limit == 0 ? max_uintx : commit_limit), // commit_limit == 0 -> no limit65_used_words_counter(),66_rs()67{68assert(is_aligned(reserve_limit, Metaspace::reserve_alignment_words()), "reserve_limit (" SIZE_FORMAT ") "69"not aligned to metaspace reserve alignment (" SIZE_FORMAT ")",70reserve_limit, Metaspace::reserve_alignment_words());71if (reserve_limit > 0) {72// have reserve limit -> non-expandable context73_rs = ReservedSpace(reserve_limit * BytesPerWord, Metaspace::reserve_alignment(), os::vm_page_size());74_context = MetaspaceContext::create_nonexpandable_context(name, _rs, &_commit_limiter);75} else {76// no reserve limit -> expandable vslist77_context = MetaspaceContext::create_expandable_context(name, &_commit_limiter);78}7980}8182MetaspaceTestContext::~MetaspaceTestContext() {83DEBUG_ONLY(verify();)84MutexLocker fcl(Metaspace_lock, Mutex::_no_safepoint_check_flag);85delete _context;86if (_rs.is_reserved()) {87_rs.release();88}89}9091// Create an arena, feeding off this area.92MetaspaceTestArena* MetaspaceTestContext::create_arena(Metaspace::MetaspaceType type) {93const ArenaGrowthPolicy* growth_policy = ArenaGrowthPolicy::policy_for_space_type(type, false);94Mutex* lock = new Mutex(Monitor::native, "MetaspaceTestArea-lock", false, Monitor::_safepoint_check_never);95MetaspaceArena* arena = NULL;96{97MutexLocker ml(lock, Mutex::_no_safepoint_check_flag);98arena = new MetaspaceArena(_context->cm(), growth_policy, lock, &_used_words_counter, _name);99}100return new MetaspaceTestArena(lock, arena);101}102103void MetaspaceTestContext::purge_area() {104_context->cm()->purge();105}106107#ifdef ASSERT108void MetaspaceTestContext::verify() const {109if (_context != NULL) {110_context->verify();111}112}113#endif114115void MetaspaceTestContext::print_on(outputStream* st) const {116_context->print_on(st);117}118119} // namespace metaspace120121122123