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 package compiler.codecache.jmx;
  25 
  26 import jdk.test.lib.Asserts;
  27 import jdk.test.lib.Utils;
  28 import sun.hotspot.WhiteBox;
  29 import sun.hotspot.code.BlobType;
  30 import sun.hotspot.code.CodeBlob;
  31 
  32 import javax.management.Notification;
  33 import java.lang.management.MemoryPoolMXBean;
  34 
  35 public final class CodeCacheUtils {
  36 
  37     /**
  38     * Returns the value to be used for code heap allocation
  39     */
  40     public static final int ALLOCATION_SIZE
  41             = Integer.getInteger("codecache.allocation.size", 100);
  42     public static final WhiteBox WB = WhiteBox.getWhiteBox();
  43     public static final long SEGMENT_SIZE
  44             = WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheSegmentSize");
  45     public static final long MIN_BLOCK_LENGTH
  46             = WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheMinBlockLength");
  47     public static final long MIN_ALLOCATION = SEGMENT_SIZE * MIN_BLOCK_LENGTH;
  48 
  49     private CodeCacheUtils() {
  50         // To prevent from instantiation
  51     }
  52 
  53     public static final void hitUsageThreshold(MemoryPoolMXBean bean,
  54             BlobType btype) {
  55         long initialSize = bean.getUsage().getUsed();
  56         bean.setUsageThreshold(initialSize + 1);
  57         long usageThresholdCount = bean.getUsageThresholdCount();
  58         long addr = WB.allocateCodeBlob(1, btype.id);
  59         WB.fullGC();
  60         Utils.waitForCondition(()
  61                 -> bean.getUsageThresholdCount() == usageThresholdCount + 1);
  62         WB.freeCodeBlob(addr);
  63     }
  64 
  65     public static final long getHeaderSize(BlobType btype) {
  66         long addr = WB.allocateCodeBlob(0, btype.id);
  67         int size = CodeBlob.getCodeBlob(addr).size;
  68         WB.freeCodeBlob(addr);
  69         return size;
  70     }
  71 
  72     public static String getPoolNameFromNotification(
  73             Notification notification) {
  74         return ((javax.management.openmbean.CompositeDataSupport)
  75                 notification.getUserData()).get("poolName").toString();
  76     }
  77 
  78     public static boolean isAvailableCodeHeapPoolName(String name) {
  79         return BlobType.getAvailable().stream()
  80                 .map(BlobType::getMemoryPool)
  81                 .map(MemoryPoolMXBean::getName)
  82                 .filter(name::equals)
  83                 .findAny().isPresent();
  84     }
  85 
  86     /**
  87      * Checks if the usage of the code heap corresponding to 'btype' can be
  88      * predicted at runtime if we disable compilation. The usage of the
  89      * 'NonNMethod' code heap can not be predicted because we generate adapters
  90      * and buffers at runtime. The 'MethodNonProfiled' code heap is also not
  91      * predictable because we may generate compiled versions of method handle
  92      * intrinsics while resolving methods at runtime. Same applies to 'All'.
  93      *
  94      * @param btype BlobType to be checked
  95      * @return boolean value, true if respective code heap is predictable
  96      */
  97     public static boolean isCodeHeapPredictable(BlobType btype) {
  98         return btype == BlobType.MethodProfiled;
  99     }
 100 
 101     /**
 102      * Verifies that 'newValue' is equal to 'oldValue' if usage of the
 103      * corresponding code heap is predictable. Checks the weaker condition
 104      * 'newValue >= oldValue' if usage is not predictable because intermediate
 105      * allocations may happen.
 106      *
 107      * @param btype BlobType of the code heap to be checked
 108      * @param newValue New value to be verified
 109      * @param oldValue Old value to be verified
 110      * @param msg Error message if verification fails
 111      */
 112     public static void assertEQorGTE(BlobType btype, long newValue, long oldValue, String msg) {
 113         if (CodeCacheUtils.isCodeHeapPredictable(btype)) {
 114             // Usage is predictable, check strong == condition
 115             Asserts.assertEQ(newValue, oldValue, msg);
 116         } else {
 117             // Usage is not predictable, check weaker >= condition
 118             Asserts.assertGTE(newValue, oldValue, msg);
 119         }
 120     }
 121 
 122     public static void disableCollectionUsageThresholds() {
 123         BlobType.getAvailable().stream()
 124                 .map(BlobType::getMemoryPool)
 125                 .filter(MemoryPoolMXBean::isCollectionUsageThresholdSupported)
 126                 .forEach(b -> b.setCollectionUsageThreshold(0L));
 127     }
 128 }