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.ArrayList; 27 import java.util.List; 28 import sun.hotspot.code.BlobType; 29 30 /* 31 * @test InitialAndMaxUsageTest 32 * @library /testlibrary /testlibrary/whitebox 33 * @build InitialAndMaxUsageTest 34 * @run main ClassFileInstaller sun.hotspot.WhiteBox 35 * sun.hotspot.WhiteBox$WhiteBoxPermission 36 * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing 37 * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 38 * -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::* 39 * InitialAndMaxUsageTest 40 * @summary testing of initial and max usage 41 */ 42 public class InitialAndMaxUsageTest { 43 44 private static final double CACHE_USAGE_COEF = 0.95d; 45 private final BlobType btype; 46 private final boolean lowerBoundIsZero; 47 private final long maxSize; 48 49 public InitialAndMaxUsageTest(BlobType btype) { 50 this.btype = btype; 51 this.maxSize = btype.getSize(); 52 /* Only profiled code cache initial size should be 0, because of 53 -XX:CompileCommand=compileonly,null::* non-methods might be not empty, 54 as well as non-profiled methods, because it's used as fallback in 55 case non-methods is full */ 56 lowerBoundIsZero = btype == BlobType.MethodProfiled; 57 } 58 59 public static void main(String[] args) { 60 for (BlobType btype : BlobType.getAvailable()) { 61 new InitialAndMaxUsageTest(btype).runTest(); 62 } 63 } 64 65 private void fillWithSize(long size, List<Long> blobs) { 66 long blob; 67 while ((blob = CodeCacheUtils.WB.allocateCodeBlob(size, btype.id)) 68 != 0L) { 69 blobs.add(blob); 70 } 71 } 72 73 protected void runTest() { 74 long headerSize = CodeCacheUtils.getHeaderSize(btype); 75 MemoryPoolMXBean bean = btype.getMemoryPool(); 76 long initialUsage = btype.getMemoryPool().getUsage().getUsed(); 77 System.out.printf("INFO: trying to test %s of max size %d and initial" 78 + " usage %d%n", bean.getName(), maxSize, initialUsage); 79 Asserts.assertLT(initialUsage + headerSize + 1L, maxSize, 80 "Initial usage is close to total size for " + bean.getName()); 81 if (lowerBoundIsZero) { 82 Asserts.assertEQ(initialUsage, 0L, "Unexpected initial usage"); 83 } 84 ArrayList<Long> blobs = new ArrayList<>(); 85 long minAllocationUnit = CodeCacheUtils.MIN_ALLOCATION - headerSize; 86 /* now filling code cache with large-sized allocation first, since 87 lots of small allocations takes too much time, so, just a small 88 optimization */ 89 try { 90 for (int coef = 1000000; coef > 0; coef /= 10) { 91 fillWithSize(coef * minAllocationUnit, blobs); 92 } 93 Asserts.assertGT((double) bean.getUsage().getUsed(), 94 CACHE_USAGE_COEF * maxSize, String.format("Unable to fill " 95 + "more than %f of %s. Reported usage is %d ", 96 CACHE_USAGE_COEF, bean.getName(), 97 bean.getUsage().getUsed())); 98 } finally { 99 for (long entry : blobs) { 100 CodeCacheUtils.WB.freeCodeBlob(entry); 101 } 102 } 103 System.out.printf("INFO: Scenario finished successfully for %s%n", 104 bean.getName()); 105 } 106 107 }