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