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 InitialAndMaxUsageTest 26 * @summary testing of initial and max usage 27 * @modules java.base/jdk.internal.misc 28 * java.management 29 * @library /test/lib / 30 * 31 * @build sun.hotspot.WhiteBox 32 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 33 * sun.hotspot.WhiteBox$WhiteBoxPermission 34 * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing 35 * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 36 * -XX:CompileCommand=compileonly,null::* -XX:-UseLargePages 37 * -XX:+SegmentedCodeCache 38 * compiler.codecache.jmx.InitialAndMaxUsageTest 39 * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing 40 * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 41 * -XX:CompileCommand=compileonly,null::* -XX:-UseLargePages 42 * -XX:-SegmentedCodeCache 43 * compiler.codecache.jmx.InitialAndMaxUsageTest 44 */ 45 46 package compiler.codecache.jmx; 47 48 import jdk.test.lib.Asserts; 49 import sun.hotspot.code.BlobType; 50 51 import java.lang.management.MemoryPoolMXBean; 52 import java.util.ArrayList; 53 import java.util.List; 54 55 public class InitialAndMaxUsageTest { 56 57 private static final double CACHE_USAGE_COEF = 0.95d; 58 private final BlobType btype; 59 private final boolean lowerBoundIsZero; 60 private final long maxSize; 61 62 public InitialAndMaxUsageTest(BlobType btype) { 63 this.btype = btype; 64 this.maxSize = btype.getSize(); 65 /* Only profiled code cache initial size should be 0, because of 66 -XX:CompileCommand=compileonly,null::* non-methods might be not empty, 67 as well as non-profiled methods, because it's used as fallback in 68 case non-methods is full */ 69 lowerBoundIsZero = btype == BlobType.MethodProfiled; 70 } 71 72 public static void main(String[] args) { 73 for (BlobType btype : BlobType.getAvailable()) { 74 new InitialAndMaxUsageTest(btype).runTest(); 75 } 76 } 77 78 private void fillWithSize(long size, List<Long> blobs, MemoryPoolMXBean bean) { 79 long blob; 80 /* Don't fill too much to have space for adapters. So, stop after crossing 95% and 81 don't allocate in case we'll cross 97% on next allocation. We can hit situation 82 like 94% -> (1 allocation) -> 100% otherwise. So, check if 83 <Usage + allocatedSize> is less than 97%, then allocate in case it is, then, stop 84 further allocations with given size in case <Usage> more than 95% */ 85 while (((double) bean.getUsage().getUsed() + size <= (CACHE_USAGE_COEF + 0.02d) * maxSize) 86 && (blob = CodeCacheUtils.WB.allocateCodeBlob(size, btype.id)) != 0L 87 && ((double) bean.getUsage().getUsed() <= CACHE_USAGE_COEF * maxSize)) { 88 blobs.add(blob); 89 } 90 } 91 92 protected void runTest() { 93 long headerSize = CodeCacheUtils.getHeaderSize(btype); 94 MemoryPoolMXBean bean = btype.getMemoryPool(); 95 long initialUsage = btype.getMemoryPool().getUsage().getUsed(); 96 System.out.printf("INFO: trying to test %s of max size %d and initial" 97 + " usage %d%n", bean.getName(), maxSize, initialUsage); 98 Asserts.assertLT(initialUsage + headerSize + 1L, maxSize, 99 "Initial usage is close to total size for " + bean.getName()); 100 if (lowerBoundIsZero) { 101 Asserts.assertEQ(initialUsage, 0L, "Unexpected initial usage"); 102 } 103 ArrayList<Long> blobs = new ArrayList<>(); 104 long minAllocationUnit = Math.max(1, CodeCacheUtils.MIN_ALLOCATION - headerSize); 105 /* now filling code cache with large-sized allocation first, since 106 lots of small allocations takes too much time, so, just a small 107 optimization */ 108 try { 109 for (int coef = 1000000; coef > 0; coef /= 10) { 110 fillWithSize(coef * minAllocationUnit, blobs, bean); 111 } 112 Asserts.assertGT((double) bean.getUsage().getUsed(), 113 CACHE_USAGE_COEF * maxSize, String.format("Unable to fill " 114 + "more than %f of %s. Reported usage is %d ", 115 CACHE_USAGE_COEF, bean.getName(), 116 bean.getUsage().getUsed())); 117 } finally { 118 for (long entry : blobs) { 119 CodeCacheUtils.WB.freeCodeBlob(entry); 120 } 121 } 122 System.out.printf("INFO: Scenario finished successfully for %s%n", 123 bean.getName()); 124 } 125 126 }