Path: blob/master/test/hotspot/jtreg/compiler/codecache/jmx/GetUsageTest.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 GetUsageTest25* @summary testing of getUsage() for segmented code cache26* @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:+UnlockDiagnosticVMOptions33* -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*34* -XX:-UseCodeCacheFlushing -XX:-MethodFlushing35* -XX:+SegmentedCodeCache36* compiler.codecache.jmx.GetUsageTest37* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions38* -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*39* -XX:-UseCodeCacheFlushing -XX:-MethodFlushing40* -XX:-SegmentedCodeCache41* compiler.codecache.jmx.GetUsageTest42*/4344package compiler.codecache.jmx;4546import jdk.test.lib.Asserts;47import sun.hotspot.code.BlobType;4849import java.lang.management.MemoryPoolMXBean;50import java.util.HashMap;51import java.util.Map;5253public class GetUsageTest {5455private final BlobType btype;56private final int allocateSize;5758public GetUsageTest(BlobType btype, int allocSize) {59this.btype = btype;60this.allocateSize = allocSize;61}6263public static void main(String[] args) throws Exception {64for (BlobType btype : BlobType.getAvailable()) {65for (int allocSize = 10; allocSize < 100000; allocSize *= 10) {66new GetUsageTest(btype, allocSize).runTest();67}68}69}7071protected final Map<MemoryPoolMXBean, Long> getBeanUsages() {72Map<MemoryPoolMXBean, Long> beanUsages = new HashMap<>();73for (BlobType bt : BlobType.getAvailable()) {74beanUsages.put(bt.getMemoryPool(),75bt.getMemoryPool().getUsage().getUsed());76}77return beanUsages;78}7980protected void runTest() {81MemoryPoolMXBean[] predictableBeans = BlobType.getAvailable().stream()82.filter(CodeCacheUtils::isCodeHeapPredictable)83.map(BlobType::getMemoryPool)84.toArray(MemoryPoolMXBean[]::new);85Map<MemoryPoolMXBean, Long> initial = getBeanUsages();86long addr = 0;87try {88addr = CodeCacheUtils.WB.allocateCodeBlob(allocateSize, btype.id);89Map<MemoryPoolMXBean, Long> current = getBeanUsages();90long blockCount = Math.floorDiv(allocateSize91+ CodeCacheUtils.getHeaderSize(btype)92+ CodeCacheUtils.SEGMENT_SIZE - 1, CodeCacheUtils.SEGMENT_SIZE);93long usageUpperEstimate = Math.max(blockCount,94CodeCacheUtils.MIN_BLOCK_LENGTH) * CodeCacheUtils.SEGMENT_SIZE;95for (MemoryPoolMXBean entry : predictableBeans) {96long diff = current.get(entry) - initial.get(entry);97if (entry.equals(btype.getMemoryPool())) {98if (CodeCacheUtils.isCodeHeapPredictable(btype)) {99Asserts.assertFalse(diff <= 0L || diff > usageUpperEstimate,100String.format("Pool %s usage increase was reported "101+ "unexpectedly as increased by %d using "102+ "allocation size %d", entry.getName(),103diff, allocateSize));104}105} else {106CodeCacheUtils.assertEQorGTE(btype, diff, 0L,107String.format("Pool %s usage changed unexpectedly while"108+ " trying to increase: %s using allocation "109+ "size %d", entry.getName(),110btype.getMemoryPool().getName(), allocateSize));111}112}113} finally {114if (addr != 0) {115CodeCacheUtils.WB.freeCodeBlob(addr);116}117}118System.out.printf("INFO: Scenario finished successfully for %s%n",119btype.getMemoryPool().getName());120}121}122123124