1 /*
2  * Copyright (c) 2014, 2017, 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 
InitialAndMaxUsageTest(BlobType btype)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 
main(String[] args)72     public static void main(String[] args) {
73         for (BlobType btype : BlobType.getAvailable()) {
74             new InitialAndMaxUsageTest(btype).runTest();
75         }
76     }
77 
canAllocate(double size, long maxSize, MemoryPoolMXBean bean)78     private boolean canAllocate(double size, long maxSize, MemoryPoolMXBean bean) {
79         // Don't fill too much to have space for adapters. So, stop after crossing 95% and
80         // don't allocate in case we'll cross 97% on next allocation.
81         double used = bean.getUsage().getUsed();
82         return (used <= CACHE_USAGE_COEF * maxSize) &&
83                (used + size <= (CACHE_USAGE_COEF + 0.02d)  * maxSize);
84     }
85 
runTest()86     protected void runTest() {
87         long headerSize = CodeCacheUtils.getHeaderSize(btype);
88         MemoryPoolMXBean bean = btype.getMemoryPool();
89         long initialUsage = btype.getMemoryPool().getUsage().getUsed();
90         System.out.printf("INFO: trying to test %s of max size %d and initial"
91                 + " usage %d%n", bean.getName(), maxSize, initialUsage);
92         Asserts.assertLT(initialUsage + headerSize + 1L, maxSize,
93                 "Initial usage is close to total size for " + bean.getName());
94         if (lowerBoundIsZero) {
95             Asserts.assertEQ(initialUsage, 0L, "Unexpected initial usage");
96         }
97         ArrayList<Long> blobs = new ArrayList<>();
98         long minAllocationUnit = Math.max(1, CodeCacheUtils.MIN_ALLOCATION - headerSize);
99         /* now filling code cache with large-sized allocation first, since
100          lots of small allocations takes too much time, so, just a small
101          optimization */
102         try {
103             for (long size = 100_000 * minAllocationUnit; size > 0; size /= 10) {
104                 long blob = 0;
105                 while (canAllocate(size, maxSize, bean) &&
106                        (blob = CodeCacheUtils.WB.allocateCodeBlob(size, btype.id)) != 0) {
107                     blobs.add(blob);
108                 }
109             }
110             Asserts.assertGT((double) bean.getUsage().getUsed(),
111                     CACHE_USAGE_COEF * maxSize, String.format("Unable to fill "
112                             + "more than %f of %s. Reported usage is %d ",
113                             CACHE_USAGE_COEF, bean.getName(),
114                             bean.getUsage().getUsed()));
115         } finally {
116             for (long entry : blobs) {
117                 CodeCacheUtils.WB.freeCodeBlob(entry);
118             }
119         }
120         System.out.printf("INFO: Scenario finished successfully for %s%n",
121                 bean.getName());
122     }
123 
124 }
125