1 /* 2 * Copyright (c) 2014, 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 import com.oracle.java.testlibrary.Asserts; 25 import java.lang.management.MemoryPoolMXBean; 26 import java.util.HashMap; 27 import java.util.Map; 28 import sun.hotspot.code.BlobType; 29 30 /* 31 * @test GetUsageTest 32 * @library /testlibrary /../../test/lib 33 * @build GetUsageTest 34 * @run main ClassFileInstaller sun.hotspot.WhiteBox 35 * sun.hotspot.WhiteBox$WhiteBoxPermission 36 * @run main/othervm -Xbootclasspath/a:. -XX:CompileCommand=compileonly,null::* 37 * -XX:-UseCodeCacheFlushing -XX:-MethodFlushing -XX:+SegmentedCodeCache 38 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI GetUsageTest 39 * @summary testing of getUsage() for segmented code cache 40 */ 41 public class GetUsageTest { 42 43 private final BlobType btype; 44 private final int allocateSize; 45 46 public GetUsageTest(BlobType btype, int allocSize) { 47 this.btype = btype; 48 this.allocateSize = allocSize; 49 } 50 51 public static void main(String[] args) throws Exception { 52 for (BlobType btype : BlobType.getAvailable()) { 53 if (CodeCacheUtils.isCodeHeapPredictable(btype)) { 54 for (int allocSize = 10; allocSize < 100000; allocSize *= 10) { 55 new GetUsageTest(btype, allocSize).runTest(); 56 } 57 } 58 } 59 } 60 61 protected final Map<MemoryPoolMXBean, Long> getBeanUsages() { 62 Map<MemoryPoolMXBean, Long> beanUsages = new HashMap<>(); 63 for (BlobType bt : BlobType.getAvailable()) { 64 beanUsages.put(bt.getMemoryPool(), 65 bt.getMemoryPool().getUsage().getUsed()); 66 } 67 return beanUsages; 68 } 69 70 protected void runTest() { 71 MemoryPoolMXBean[] predictableBeans = BlobType.getAvailable().stream() 72 .filter(CodeCacheUtils::isCodeHeapPredictable) 73 .map(BlobType::getMemoryPool) 74 .toArray(MemoryPoolMXBean[]::new); 75 Map<MemoryPoolMXBean, Long> initial = getBeanUsages(); 76 long addr = 0; 77 try { 78 addr = CodeCacheUtils.WB.allocateCodeBlob(allocateSize, btype.id); 79 Map<MemoryPoolMXBean, Long> current = getBeanUsages(); 80 long blockCount = Math.floorDiv(allocateSize 81 + CodeCacheUtils.getHeaderSize(btype) 82 + CodeCacheUtils.SEGMENT_SIZE - 1, CodeCacheUtils.SEGMENT_SIZE); 83 long usageUpperEstimate = Math.max(blockCount, 84 CodeCacheUtils.MIN_BLOCK_LENGTH) * CodeCacheUtils.SEGMENT_SIZE; 85 for (MemoryPoolMXBean entry : predictableBeans) { 86 long diff = current.get(entry) - initial.get(entry); 87 if (entry.equals(btype.getMemoryPool())) { 88 Asserts.assertFalse(diff <= 0L || diff > usageUpperEstimate, 89 String.format("Pool %s usage increase was reported " 90 + "unexpectedly as increased by %d using " 91 + "allocation size %d", entry.getName(), 92 diff, allocateSize)); 93 } else { 94 Asserts.assertEQ(diff, 0L, 95 String.format("Pool %s usage changed unexpectedly while" 96 + " trying to increase: %s using allocation " 97 + "size %d", entry.getName(), 98 btype.getMemoryPool().getName(), allocateSize)); 99 } 100 } 101 } finally { 102 if (addr != 0) { 103 CodeCacheUtils.WB.freeCodeBlob(addr); 104 } 105 } 106 System.out.printf("INFO: Scenario finished successfully for %s%n", 107 btype.getMemoryPool().getName()); 108 } 109 }