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