Path: blob/master/test/hotspot/jtreg/compiler/codecache/jmx/InitialAndMaxUsageTest.java
41153 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test InitialAndMaxUsageTest25* @summary testing of initial and max usage26* @modules java.base/jdk.internal.misc27* java.management28* @library /test/lib /29*30* @build sun.hotspot.WhiteBox31* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox32* @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing33* -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI34* -XX:CompileCommand=compileonly,null::* -XX:-UseLargePages35* -XX:+SegmentedCodeCache36* compiler.codecache.jmx.InitialAndMaxUsageTest37* @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing38* -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI39* -XX:CompileCommand=compileonly,null::* -XX:-UseLargePages40* -XX:-SegmentedCodeCache41* compiler.codecache.jmx.InitialAndMaxUsageTest42*/4344package compiler.codecache.jmx;4546import jdk.test.lib.Asserts;47import sun.hotspot.code.BlobType;4849import java.lang.management.MemoryPoolMXBean;50import java.util.ArrayList;51import java.util.List;5253public class InitialAndMaxUsageTest {5455private static final double CACHE_USAGE_COEF = 0.95d;56private final BlobType btype;57private final boolean lowerBoundIsZero;58private final long maxSize;5960public InitialAndMaxUsageTest(BlobType btype) {61this.btype = btype;62this.maxSize = btype.getSize();63/* Only profiled code cache initial size should be 0, because of64-XX:CompileCommand=compileonly,null::* non-methods might be not empty,65as well as non-profiled methods, because it's used as fallback in66case non-methods is full */67lowerBoundIsZero = btype == BlobType.MethodProfiled;68}6970public static void main(String[] args) {71for (BlobType btype : BlobType.getAvailable()) {72new InitialAndMaxUsageTest(btype).runTest();73}74}7576private boolean canAllocate(double size, long maxSize, MemoryPoolMXBean bean) {77// Don't fill too much to have space for adapters. So, stop after crossing 95% and78// don't allocate in case we'll cross 97% on next allocation.79double used = bean.getUsage().getUsed();80return (used <= CACHE_USAGE_COEF * maxSize) &&81(used + size <= (CACHE_USAGE_COEF + 0.02d) * maxSize);82}8384protected void runTest() {85long headerSize = CodeCacheUtils.getHeaderSize(btype);86MemoryPoolMXBean bean = btype.getMemoryPool();87long initialUsage = btype.getMemoryPool().getUsage().getUsed();88System.out.printf("INFO: trying to test %s of max size %d and initial"89+ " usage %d%n", bean.getName(), maxSize, initialUsage);90Asserts.assertLT(initialUsage + headerSize + 1L, maxSize,91"Initial usage is close to total size for " + bean.getName());92if (lowerBoundIsZero) {93Asserts.assertEQ(initialUsage, 0L, "Unexpected initial usage");94}95ArrayList<Long> blobs = new ArrayList<>();96long minAllocationUnit = Math.max(1, CodeCacheUtils.MIN_ALLOCATION - headerSize);97/* now filling code cache with large-sized allocation first, since98lots of small allocations takes too much time, so, just a small99optimization */100try {101for (long size = 100_000 * minAllocationUnit; size > 0; size /= 10) {102long blob = 0;103while (canAllocate(size, maxSize, bean) &&104(blob = CodeCacheUtils.WB.allocateCodeBlob(size, btype.id)) != 0) {105blobs.add(blob);106}107}108Asserts.assertGT((double) bean.getUsage().getUsed(),109CACHE_USAGE_COEF * maxSize, String.format("Unable to fill "110+ "more than %f of %s. Reported usage is %d ",111CACHE_USAGE_COEF, bean.getName(),112bean.getUsage().getUsed()));113} finally {114for (long entry : blobs) {115CodeCacheUtils.WB.freeCodeBlob(entry);116}117}118System.out.printf("INFO: Scenario finished successfully for %s%n",119bean.getName());120}121122}123124125