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 import com.oracle.java.testlibrary.Utils;
  25 import java.lang.management.MemoryPoolMXBean;
  26 import javax.management.Notification;
  27 import sun.hotspot.WhiteBox;
  28 import sun.hotspot.code.BlobType;
  29 import sun.hotspot.code.CodeBlob;
  30 
  31 public final class CodeCacheUtils {
  32 
  33     /**
  34     * Returns the value to be used for code heap allocation
  35     */
  36     public static final int ALLOCATION_SIZE
  37             = Integer.getInteger("codecache.allocation.size", 100);
  38     public static final WhiteBox WB = WhiteBox.getWhiteBox();
  39     public static final long SEGMENT_SIZE
  40             = WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheSegmentSize");
  41     public static final long MIN_BLOCK_LENGTH
  42             = WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheMinBlockLength");
  43     public static final long MIN_ALLOCATION = SEGMENT_SIZE * MIN_BLOCK_LENGTH;
  44 
  45     private CodeCacheUtils() {
  46         // To prevent from instantiation
  47     }
  48 
  49     public static final void hitUsageThreshold(MemoryPoolMXBean bean,
  50             BlobType btype) {
  51         long initialSize = bean.getUsage().getUsed();
  52         bean.setUsageThreshold(initialSize + 1);
  53         long usageThresholdCount = bean.getUsageThresholdCount();
  54         long addr = WB.allocateCodeBlob(1, btype.id);
  55         WB.fullGC();
  56         Utils.waitForCondition(()
  57                 -> bean.getUsageThresholdCount() == usageThresholdCount + 1);
  58         WB.freeCodeBlob(addr);
  59     }
  60 
  61     public static long allocateDefaultSize(BlobType btype) {
  62         return WB.allocateCodeBlob(ALLOCATION_SIZE, btype.id);
  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      * A "non-nmethods" code heap is used by interpreter during bytecode
  88      * execution, thus, it can't be predicted if this code heap usage will be
  89      * increased or not. Same goes for 'All'.
  90      *
  91      * @param btype BlobType to be checked
  92      * @return boolean value, true if respective code heap is predictable
  93      */
  94     public static boolean isCodeHeapPredictable(BlobType btype) {
  95         return btype == BlobType.MethodNonProfiled
  96                 || btype == BlobType.MethodProfiled;
  97     }
  98 
  99     public static void disableCollectionUsageThresholds(){
 100         BlobType.getAvailable().stream()
 101                 .map(BlobType::getMemoryPool)
 102                 .filter(MemoryPoolMXBean::isCollectionUsageThresholdSupported)
 103                 .forEach(b -> b.setCollectionUsageThreshold(0L));
 104     }
 105 }