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 GetCodeHeapEntriesTest
26 * @bug 8059624
27 * @summary testing of WB::getCodeHeapEntries()
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 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
34 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
35 *                   -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache
36 *                   compiler.whitebox.GetCodeHeapEntriesTest
37 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
38 *                   -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache
39 *                   compiler.whitebox.GetCodeHeapEntriesTest
40 */
41
42package compiler.whitebox;
43
44import jdk.test.lib.Asserts;
45import sun.hotspot.WhiteBox;
46import sun.hotspot.code.BlobType;
47import sun.hotspot.code.CodeBlob;
48
49import java.util.Arrays;
50import java.util.EnumSet;
51
52public class GetCodeHeapEntriesTest {
53    private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
54    private static final int SIZE = 1024;
55    private static final String DUMMY_NAME = "WB::DummyBlob";
56    private static EnumSet<BlobType> SEGMENTED_TYPES
57            = EnumSet.complementOf(EnumSet.of(BlobType.All));
58
59    public static void main(String[] args) {
60        EnumSet<BlobType> blobTypes = BlobType.getAvailable();
61        for (BlobType type : blobTypes) {
62            new GetCodeHeapEntriesTest(type).test();
63        }
64    }
65
66    private final BlobType type;
67    private GetCodeHeapEntriesTest(BlobType type) {
68        this.type = type;
69    }
70
71    private void test() {
72        System.out.printf("type %s%n", type);
73        long addr = WHITE_BOX.allocateCodeBlob(SIZE, type.id);
74        Asserts.assertNE(0, addr, "allocation failed");
75        CodeBlob[] blobs = CodeBlob.getCodeBlobs(type);
76        Asserts.assertNotNull(blobs);
77        CodeBlob blob = Arrays.stream(blobs)
78                              .filter(GetCodeHeapEntriesTest::filter)
79                              .findAny()
80                              .get();
81        Asserts.assertNotNull(blob);
82        Asserts.assertEQ(blob.code_blob_type, type);
83        Asserts.assertGTE(blob.size, SIZE);
84
85        WHITE_BOX.freeCodeBlob(addr);
86        blobs = CodeBlob.getCodeBlobs(type);
87        long count = Arrays.stream(blobs)
88                           .filter(GetCodeHeapEntriesTest::filter)
89                           .count();
90        Asserts.assertEQ(0L, count);
91    }
92
93    private static boolean filter(CodeBlob blob) {
94        if (blob == null) {
95            return false;
96        }
97        return DUMMY_NAME.equals(blob.name);
98    }
99}
100