1 /*
2  * Copyright (c) 2014, 2020, 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 AllocationCodeBlobTest
26  * @summary testing of WB::allocate/freeCodeBlob()
27  * @bug 8059624 8064669
28  * @library /test/lib /
29  * @modules java.base/jdk.internal.misc
30  *          java.management
31  * @build sun.hotspot.WhiteBox
32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
34  *                   -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
35  *                   -XX:-SegmentedCodeCache
36  *                   compiler.whitebox.AllocationCodeBlobTest
37  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
38  *                   -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
39  *                   -XX:+SegmentedCodeCache
40  *                   compiler.whitebox.AllocationCodeBlobTest
41  */
42 
43 package compiler.whitebox;
44 
45 import jdk.test.lib.Asserts;
46 import jdk.test.lib.InfiniteLoop;
47 import sun.hotspot.WhiteBox;
48 import sun.hotspot.code.BlobType;
49 
50 import java.lang.management.MemoryPoolMXBean;
51 import java.util.ArrayList;
52 import java.util.EnumSet;
53 
54 public class AllocationCodeBlobTest {
55     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
56     private static final long CODE_CACHE_SIZE
57             = WHITE_BOX.getUintxVMFlag("ReservedCodeCacheSize");
58     private static final int SIZE = 1;
59 
main(String[] args)60     public static void main(String[] args) {
61         // check that Sweeper handels dummy blobs correctly
62         Thread t = new Thread(
63                 new InfiniteLoop(WHITE_BOX::forceNMethodSweep, 1L),
64                 "ForcedSweeper");
65         t.setDaemon(true);
66         System.out.println("Starting " + t.getName());
67         t.start();
68 
69         EnumSet<BlobType> blobTypes = BlobType.getAvailable();
70         for (BlobType type : blobTypes) {
71             new AllocationCodeBlobTest(type).test();
72         }
73 
74         // check that deoptimization works well w/ dummy blobs
75         t = new Thread(
76                 new InfiniteLoop(WHITE_BOX::deoptimizeAll, 1L),
77                 "Deoptimize Thread");
78         t.setDaemon(true);
79         System.out.println("Starting " + t.getName());
80         t.start();
81 
82         for (int i = 0; i < 10_000; ++i) {
83             for (BlobType type : blobTypes) {
84                 long addr = WHITE_BOX.allocateCodeBlob(SIZE, type.id);
85             }
86         }
87 
88     }
89 
90     private final BlobType type;
91     private final MemoryPoolMXBean bean;
AllocationCodeBlobTest(BlobType type)92     private AllocationCodeBlobTest(BlobType type) {
93         this.type = type;
94         bean = type.getMemoryPool();
95     }
96 
test()97     private void test() {
98         System.out.printf("type %s%n", type);
99 
100         // Measure the code cache usage after allocate/free.
101         long start = getUsage();
102         long addr1 = WHITE_BOX.allocateCodeBlob(SIZE, type.id);
103         long firstAllocation = getUsage();
104         WHITE_BOX.freeCodeBlob(addr1);
105         long firstFree = getUsage();
106         long addr2 = WHITE_BOX.allocateCodeBlob(SIZE, type.id);
107         long secondAllocation = getUsage();
108         WHITE_BOX.freeCodeBlob(addr2);
109 
110         // The following code may trigger resolving of invokedynamic
111         // instructions and therefore method handle intrinsic creation
112         // in the code cache. Make sure this is executed after measuring
113         // the code cache usage.
114         Asserts.assertNE(0, addr1, "first allocation failed");
115         Asserts.assertNE(0, addr2, "second allocation failed");
116         Asserts.assertLTE(start + SIZE, firstAllocation,
117                 "allocation should increase memory usage: "
118                 + start + " + " + SIZE + " <= " + firstAllocation);
119         Asserts.assertLTE(firstFree, firstAllocation,
120                 "free shouldn't increase memory usage: "
121                 + firstFree + " <= " + firstAllocation);
122         Asserts.assertEQ(firstAllocation, secondAllocation);
123 
124         System.out.println("allocating till possible...");
125         ArrayList<Long> blobs = new ArrayList<>();
126         int size = (int) (CODE_CACHE_SIZE >> 7);
127         while ((addr1 = WHITE_BOX.allocateCodeBlob(size, type.id)) != 0) {
128             blobs.add(addr1);
129         }
130         for (Long blob : blobs) {
131             WHITE_BOX.freeCodeBlob(blob);
132         }
133     }
134 
getUsage()135     private long getUsage() {
136         return bean.getUsage().getUsed();
137     }
138 }
139