GetUsageTest.java revision 11833:1cbffa2beba6
1193323Sed/*
2193323Sed * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193323Sed *
5193323Sed * This code is free software; you can redistribute it and/or modify it
6193323Sed * under the terms of the GNU General Public License version 2 only, as
7193323Sed * published by the Free Software Foundation.
8193323Sed *
9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11193323Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12193323Sed * version 2 for more details (a copy is included in the LICENSE file that
13193323Sed * accompanied this code).
14193323Sed *
15193323Sed * You should have received a copy of the GNU General Public License version
16249423Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17249423Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18249423Sdim *
19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20201360Srdivacky * or visit www.oracle.com if you need additional information or have any
21198090Srdivacky * questions.
22198090Srdivacky */
23193323Sed
24193323Sed/*
25193323Sed * @test GetUsageTest
26193323Sed * @summary testing of getUsage() for segmented code cache
27193323Sed * @modules java.base/jdk.internal.misc
28193323Sed *          java.management
29193323Sed * @library /test/lib /
30193323Sed *
31193323Sed * @build sun.hotspot.WhiteBox
32193323Sed * @run driver ClassFileInstaller sun.hotspot.WhiteBox
33249423Sdim *                                sun.hotspot.WhiteBox$WhiteBoxPermission
34193323Sed * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
35193323Sed *     -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
36193323Sed *     -XX:-UseCodeCacheFlushing -XX:-MethodFlushing
37198892Srdivacky *     -XX:+SegmentedCodeCache
38193323Sed *     compiler.codecache.jmx.GetUsageTest
39193323Sed * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
40193323Sed *     -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
41193323Sed *     -XX:-UseCodeCacheFlushing -XX:-MethodFlushing
42193323Sed *     -XX:-SegmentedCodeCache
43193323Sed *     compiler.codecache.jmx.GetUsageTest
44193323Sed */
45193323Sed
46249423Sdimpackage compiler.codecache.jmx;
47193323Sed
48193323Sedimport jdk.test.lib.Asserts;
49198090Srdivackyimport sun.hotspot.code.BlobType;
50198090Srdivacky
51193323Sedimport java.lang.management.MemoryPoolMXBean;
52193323Sedimport java.util.HashMap;
53193323Sedimport java.util.Map;
54218893Sdim
55218893Sdimpublic class GetUsageTest {
56218893Sdim
57193323Sed    private final BlobType btype;
58193323Sed    private final int allocateSize;
59193323Sed
60193323Sed    public GetUsageTest(BlobType btype, int allocSize) {
61193323Sed        this.btype = btype;
62193323Sed        this.allocateSize = allocSize;
63198090Srdivacky    }
64193323Sed
65193323Sed    public static void main(String[] args) throws Exception {
66193323Sed        for (BlobType btype : BlobType.getAvailable()) {
67193323Sed            for (int allocSize = 10; allocSize < 100000; allocSize *= 10) {
68193323Sed                new GetUsageTest(btype, allocSize).runTest();
69212904Sdim            }
70218893Sdim        }
71193323Sed    }
72193323Sed
73193323Sed    protected final Map<MemoryPoolMXBean, Long> getBeanUsages() {
74193323Sed        Map<MemoryPoolMXBean, Long> beanUsages = new HashMap<>();
75193323Sed        for (BlobType bt : BlobType.getAvailable()) {
76193323Sed            beanUsages.put(bt.getMemoryPool(),
77193323Sed                    bt.getMemoryPool().getUsage().getUsed());
78193323Sed        }
79193323Sed        return beanUsages;
80198892Srdivacky    }
81193323Sed
82193323Sed    protected void runTest() {
83193323Sed        MemoryPoolMXBean[] predictableBeans = BlobType.getAvailable().stream()
84198892Srdivacky                .filter(CodeCacheUtils::isCodeHeapPredictable)
85193323Sed                .map(BlobType::getMemoryPool)
86193323Sed                .toArray(MemoryPoolMXBean[]::new);
87193323Sed        Map<MemoryPoolMXBean, Long> initial = getBeanUsages();
88        long addr = 0;
89        try {
90            addr = CodeCacheUtils.WB.allocateCodeBlob(allocateSize, btype.id);
91            Map<MemoryPoolMXBean, Long> current = getBeanUsages();
92            long blockCount = Math.floorDiv(allocateSize
93                    + CodeCacheUtils.getHeaderSize(btype)
94                    + CodeCacheUtils.SEGMENT_SIZE - 1, CodeCacheUtils.SEGMENT_SIZE);
95            long usageUpperEstimate = Math.max(blockCount,
96                    CodeCacheUtils.MIN_BLOCK_LENGTH) * CodeCacheUtils.SEGMENT_SIZE;
97            for (MemoryPoolMXBean entry : predictableBeans) {
98                long diff = current.get(entry) - initial.get(entry);
99                if (entry.equals(btype.getMemoryPool())) {
100                    if (CodeCacheUtils.isCodeHeapPredictable(btype)) {
101                        Asserts.assertFalse(diff <= 0L || diff > usageUpperEstimate,
102                                String.format("Pool %s usage increase was reported "
103                                        + "unexpectedly as increased by %d using "
104                                        + "allocation size %d", entry.getName(),
105                                        diff, allocateSize));
106                    }
107                } else {
108                    CodeCacheUtils.assertEQorGTE(btype, diff, 0L,
109                            String.format("Pool %s usage changed unexpectedly while"
110                                    + " trying to increase: %s using allocation "
111                                    + "size %d", entry.getName(),
112                                    btype.getMemoryPool().getName(), allocateSize));
113                }
114            }
115        } finally {
116            if (addr != 0) {
117                CodeCacheUtils.WB.freeCodeBlob(addr);
118            }
119        }
120        System.out.printf("INFO: Scenario finished successfully for %s%n",
121                btype.getMemoryPool().getName());
122    }
123}
124