1 /* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. 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 it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 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 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test GetUsageTest 26 * @summary testing of getUsage() for segmented code cache 27 * @requires vm.flavor != "minimal" 28 * @modules java.base/jdk.internal.misc 29 * java.management 30 * @library /test/lib / 31 * 32 * @build sun.hotspot.WhiteBox 33 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 34 * sun.hotspot.WhiteBox$WhiteBoxPermission 35 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 36 * -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::* 37 * -XX:-UseCodeCacheFlushing -XX:-MethodFlushing 38 * -XX:+SegmentedCodeCache 39 * compiler.codecache.jmx.GetUsageTest 40 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 41 * -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::* 42 * -XX:-UseCodeCacheFlushing -XX:-MethodFlushing 43 * -XX:-SegmentedCodeCache 44 * compiler.codecache.jmx.GetUsageTest 45 */ 46 47 package compiler.codecache.jmx; 48 49 import jdk.test.lib.Asserts; 50 import sun.hotspot.code.BlobType; 51 52 import java.lang.management.MemoryPoolMXBean; 53 import java.util.HashMap; 54 import java.util.Map; 55 56 public class GetUsageTest { 57 58 private final BlobType btype; 59 private final int allocateSize; 60 61 public GetUsageTest(BlobType btype, int allocSize) { 62 this.btype = btype; 63 this.allocateSize = allocSize; 64 } 65 66 public static void main(String[] args) throws Exception { 67 for (BlobType btype : BlobType.getAvailable()) { 68 for (int allocSize = 10; allocSize < 100000; allocSize *= 10) { 69 new GetUsageTest(btype, allocSize).runTest(); 70 } 71 } 72 } 73 74 protected final Map<MemoryPoolMXBean, Long> getBeanUsages() { 75 Map<MemoryPoolMXBean, Long> beanUsages = new HashMap<>(); 76 for (BlobType bt : BlobType.getAvailable()) { 77 beanUsages.put(bt.getMemoryPool(), 78 bt.getMemoryPool().getUsage().getUsed()); 79 } 80 return beanUsages; 81 } 82 83 protected void runTest() { 84 MemoryPoolMXBean[] predictableBeans = BlobType.getAvailable().stream() 85 .filter(CodeCacheUtils::isCodeHeapPredictable) 86 .map(BlobType::getMemoryPool) 87 .toArray(MemoryPoolMXBean[]::new); 88 Map<MemoryPoolMXBean, Long> initial = getBeanUsages(); 89 long addr = 0; 90 try { 91 addr = CodeCacheUtils.WB.allocateCodeBlob(allocateSize, btype.id); 92 Map<MemoryPoolMXBean, Long> current = getBeanUsages(); 93 long blockCount = Math.floorDiv(allocateSize 94 + CodeCacheUtils.getHeaderSize(btype) 95 + CodeCacheUtils.SEGMENT_SIZE - 1, CodeCacheUtils.SEGMENT_SIZE); 96 long usageUpperEstimate = Math.max(blockCount, 97 CodeCacheUtils.MIN_BLOCK_LENGTH) * CodeCacheUtils.SEGMENT_SIZE; 98 for (MemoryPoolMXBean entry : predictableBeans) { 99 long diff = current.get(entry) - initial.get(entry); 100 if (entry.equals(btype.getMemoryPool())) { 101 if (CodeCacheUtils.isCodeHeapPredictable(btype)) { 102 Asserts.assertFalse(diff <= 0L || diff > usageUpperEstimate, 103 String.format("Pool %s usage increase was reported " 104 + "unexpectedly as increased by %d using " 105 + "allocation size %d", entry.getName(), 106 diff, allocateSize)); 107 } 108 } else { 109 CodeCacheUtils.assertEQorGTE(btype, diff, 0L, 110 String.format("Pool %s usage changed unexpectedly while" 111 + " trying to increase: %s using allocation " 112 + "size %d", entry.getName(), 113 btype.getMemoryPool().getName(), allocateSize)); 114 } 115 } 116 } finally { 117 if (addr != 0) { 118 CodeCacheUtils.WB.freeCodeBlob(addr); 119 } 120 } 121 System.out.printf("INFO: Scenario finished successfully for %s%n", 122 btype.getMemoryPool().getName()); 123 } 124 }