Path: blob/master/test/hotspot/jtreg/runtime/Metaspace/elastic/MetaspaceTestArena.java
41155 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*/2425import sun.hotspot.WhiteBox;2627import java.util.concurrent.atomic.AtomicLong;2829public class MetaspaceTestArena {3031long arena;3233final long allocationCeiling;3435// Number and word size of allocations36long allocatedWords = 0;37long numAllocated = 0;38long deallocatedWords = 0;39long numDeallocated = 0;40long numAllocationFailures = 0;4142private synchronized boolean reachedCeiling() {43return (allocatedWords - deallocatedWords) > allocationCeiling;44}4546private synchronized void accountAllocation(long words) {47numAllocated ++;48allocatedWords += words;49}5051private synchronized void accountDeallocation(long words) {52numDeallocated ++;53deallocatedWords += words;54}5556MetaspaceTestArena(long arena0, long allocationCeiling) {57this.allocationCeiling = allocationCeiling;58this.arena = arena0;59}6061public Allocation allocate(long words) {62if (reachedCeiling()) {63numAllocationFailures ++;64return null;65}66WhiteBox wb = WhiteBox.getWhiteBox();67long p = wb.allocateFromMetaspaceTestArena(arena, words);68if (p == 0) {69numAllocationFailures ++;70return null;71} else {72accountAllocation(words);73}74return new Allocation(p, words);75}7677public void deallocate(Allocation a) {78WhiteBox wb = WhiteBox.getWhiteBox();79wb.deallocateToMetaspaceTestArena(arena, a.p, a.word_size);80accountDeallocation(a.word_size);81}8283//// Convenience functions ////8485public Allocation allocate_expect_success(long words) {86Allocation a = allocate(words);87if (a.isNull()) {88throw new RuntimeException("Allocation failed (" + words + ")");89}90return a;91}9293public void allocate_expect_failure(long words) {94Allocation a = allocate(words);95if (!a.isNull()) {96throw new RuntimeException("Allocation failed (" + words + ")");97}98}99100boolean isLive() {101return arena != 0;102}103104@Override105public String toString() {106return "arena=" + arena +107", ceiling=" + allocationCeiling +108", allocatedWords=" + allocatedWords +109", numAllocated=" + numAllocated +110", deallocatedWords=" + deallocatedWords +111", numDeallocated=" + numDeallocated +112", numAllocationFailures=" + numAllocationFailures +113'}';114}115116}117118119