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