Path: blob/master/test/hotspot/jtreg/runtime/Metaspace/elastic/MetaspaceTestArena.java
64507 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;2627public class MetaspaceTestArena {2829long arena;3031final long allocationCeiling;3233// Number and word size of allocations34long allocatedWords = 0;35long numAllocated = 0;36long deallocatedWords = 0;37long numDeallocated = 0;38volatile long numAllocationFailures = 0;3940private synchronized boolean reachedCeiling() {41return (allocatedWords - deallocatedWords) > allocationCeiling;42}4344private synchronized void accountAllocation(long words) {45numAllocated ++;46allocatedWords += words;47}4849private synchronized void accountDeallocation(long words) {50numDeallocated ++;51deallocatedWords += words;52}5354MetaspaceTestArena(long arena0, long allocationCeiling) {55this.allocationCeiling = allocationCeiling;56this.arena = arena0;57}5859public Allocation allocate(long words) {60if (reachedCeiling()) {61numAllocationFailures ++;62return null;63}64WhiteBox wb = WhiteBox.getWhiteBox();65long p = wb.allocateFromMetaspaceTestArena(arena, words);66if (p == 0) {67numAllocationFailures ++;68return null;69} else {70accountAllocation(words);71}72return new Allocation(p, words);73}7475public void deallocate(Allocation a) {76WhiteBox wb = WhiteBox.getWhiteBox();77wb.deallocateToMetaspaceTestArena(arena, a.p, a.word_size);78accountDeallocation(a.word_size);79}8081//// Convenience functions ////8283public Allocation allocate_expect_success(long words) {84Allocation a = allocate(words);85if (a.isNull()) {86throw new RuntimeException("Allocation failed (" + words + ")");87}88return a;89}9091public void allocate_expect_failure(long words) {92Allocation a = allocate(words);93if (!a.isNull()) {94throw new RuntimeException("Allocation failed (" + words + ")");95}96}9798boolean isLive() {99return arena != 0;100}101102@Override103public String toString() {104return "arena=" + arena +105", ceiling=" + allocationCeiling +106", allocatedWords=" + allocatedWords +107", numAllocated=" + numAllocated +108", deallocatedWords=" + deallocatedWords +109", numDeallocated=" + numDeallocated +110", numAllocationFailures=" + numAllocationFailures +111'}';112}113114}115116117