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