hotspot/test/compiler/whitebox/AllocationCodeBlobTest.java
changeset 27642 8c9eff693145
child 27917 c5937f7b4e8b
equal deleted inserted replaced
27641:fca9ac607ebc 27642:8c9eff693145
       
     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.lang.management.MemoryPoolMXBean;
       
    26 import java.util.EnumSet;
       
    27 import java.util.ArrayList;
       
    28 
       
    29 import sun.hotspot.WhiteBox;
       
    30 import sun.hotspot.code.BlobType;
       
    31 import com.oracle.java.testlibrary.Asserts;
       
    32 
       
    33 /*
       
    34  * @test AllocationCodeBlobTest
       
    35  * @bug 8059624
       
    36  * @library /testlibrary /testlibrary/whitebox
       
    37  * @build AllocationCodeBlobTest
       
    38  * @run main ClassFileInstaller sun.hotspot.WhiteBox
       
    39  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
       
    40  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
       
    41  *                   -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
       
    42  *                   -XX:-SegmentedCodeCache AllocationCodeBlobTest
       
    43  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
       
    44  *                   -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
       
    45  *                   -XX:+SegmentedCodeCache AllocationCodeBlobTest
       
    46  * @summary testing of WB::allocate/freeCodeBlob()
       
    47  */
       
    48 public class AllocationCodeBlobTest {
       
    49     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
       
    50     private static final long CODE_CACHE_SIZE
       
    51             = WHITE_BOX.getUintxVMFlag("ReservedCodeCacheSize");
       
    52     private static final int SIZE = 1;
       
    53 
       
    54     public static void main(String[] args) {
       
    55         // check that Sweeper handels dummy blobs correctly
       
    56         new ForcedSweeper(500).start();
       
    57         EnumSet<BlobType> blobTypes = BlobType.getAvailable();
       
    58         for (BlobType type : blobTypes) {
       
    59             new AllocationCodeBlobTest(type).test();
       
    60         }
       
    61     }
       
    62 
       
    63     private final BlobType type;
       
    64     private final MemoryPoolMXBean bean;
       
    65     private AllocationCodeBlobTest(BlobType type) {
       
    66         this.type = type;
       
    67         bean = type.getMemoryPool();
       
    68     }
       
    69 
       
    70     private void test() {
       
    71         System.out.printf("type %s%n", type);
       
    72         long start = getUsage();
       
    73         long addr = WHITE_BOX.allocateCodeBlob(SIZE, type.id);
       
    74         Asserts.assertNE(0, addr, "allocation failed");
       
    75 
       
    76         long firstAllocation = getUsage();
       
    77         Asserts.assertLTE(start + SIZE, firstAllocation,
       
    78                 "allocation should increase memory usage: "
       
    79                 + start + " + " + SIZE + " <= " + firstAllocation);
       
    80 
       
    81         WHITE_BOX.freeCodeBlob(addr);
       
    82         long firstFree = getUsage();
       
    83         Asserts.assertLTE(firstFree, firstAllocation,
       
    84                 "free shouldn't increase memory usage: "
       
    85                 + firstFree + " <= " + firstAllocation);
       
    86 
       
    87         addr = WHITE_BOX.allocateCodeBlob(SIZE, type.id);
       
    88         Asserts.assertNE(0, addr, "allocation failed");
       
    89 
       
    90         long secondAllocation = getUsage();
       
    91         Asserts.assertEQ(firstAllocation, secondAllocation);
       
    92 
       
    93         WHITE_BOX.freeCodeBlob(addr);
       
    94         System.out.println("allocating till possible...");
       
    95         ArrayList<Long> blobs = new ArrayList<>();
       
    96         int size = (int) (CODE_CACHE_SIZE >> 7);
       
    97         while ((addr = WHITE_BOX.allocateCodeBlob(size, type.id)) != 0) {
       
    98             blobs.add(addr);
       
    99         }
       
   100         for (Long blob : blobs) {
       
   101             WHITE_BOX.freeCodeBlob(blob);
       
   102         }
       
   103     }
       
   104 
       
   105     private long getUsage() {
       
   106         return bean.getUsage().getUsed();
       
   107     }
       
   108 
       
   109     private static class ForcedSweeper extends Thread {
       
   110         private final int millis;
       
   111         public ForcedSweeper(int millis) {
       
   112             super("ForcedSweeper");
       
   113             setDaemon(true);
       
   114             this.millis = millis;
       
   115         }
       
   116         public void run() {
       
   117             try {
       
   118                 while (true) {
       
   119                     WHITE_BOX.forceNMethodSweep();
       
   120                     Thread.sleep(millis);
       
   121                 }
       
   122             } catch (InterruptedException e) {
       
   123                 Thread.currentThread().interrupt();
       
   124                 throw new Error(e);
       
   125             }
       
   126         }
       
   127     }
       
   128 }