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