Path: blob/master/test/hotspot/jtreg/compiler/codecache/jmx/CodeCacheUtils.java
41153 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.codecache.jmx;2425import jdk.test.lib.Asserts;26import jdk.test.lib.Utils;27import sun.hotspot.WhiteBox;28import sun.hotspot.code.BlobType;29import sun.hotspot.code.CodeBlob;3031import javax.management.Notification;32import java.lang.management.MemoryPoolMXBean;3334public final class CodeCacheUtils {3536/**37* Returns the value to be used for code heap allocation38*/39public static final int ALLOCATION_SIZE40= Integer.getInteger("codecache.allocation.size", 100);41public static final WhiteBox WB = WhiteBox.getWhiteBox();42public static final long SEGMENT_SIZE43= WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheSegmentSize");44public static final long MIN_BLOCK_LENGTH45= WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheMinBlockLength");46public static final long MIN_ALLOCATION = SEGMENT_SIZE * MIN_BLOCK_LENGTH;4748private CodeCacheUtils() {49// To prevent from instantiation50}5152public static final void hitUsageThreshold(MemoryPoolMXBean bean,53BlobType btype) {54long initialSize = bean.getUsage().getUsed();55bean.setUsageThreshold(initialSize + 1);56long usageThresholdCount = bean.getUsageThresholdCount();57long addr = WB.allocateCodeBlob(1, btype.id);58WB.fullGC();59Utils.waitForCondition(()60-> bean.getUsageThresholdCount() == usageThresholdCount + 1);61WB.freeCodeBlob(addr);62}6364public static final long getHeaderSize(BlobType btype) {65long addr = WB.allocateCodeBlob(0, btype.id);66int size = CodeBlob.getCodeBlob(addr).size;67WB.freeCodeBlob(addr);68return size;69}7071public static String getPoolNameFromNotification(72Notification notification) {73return ((javax.management.openmbean.CompositeDataSupport)74notification.getUserData()).get("poolName").toString();75}7677public static boolean isAvailableCodeHeapPoolName(String name) {78return BlobType.getAvailable().stream()79.map(BlobType::getMemoryPool)80.map(MemoryPoolMXBean::getName)81.filter(name::equals)82.findAny().isPresent();83}8485/**86* Checks if the usage of the code heap corresponding to 'btype' can be87* predicted at runtime if we disable compilation. The usage of the88* 'NonNMethod' code heap can not be predicted because we generate adapters89* and buffers at runtime. The 'MethodNonProfiled' code heap is also not90* predictable because we may generate compiled versions of method handle91* intrinsics while resolving methods at runtime. Same applies to 'All'.92*93* @param btype BlobType to be checked94* @return boolean value, true if respective code heap is predictable95*/96public static boolean isCodeHeapPredictable(BlobType btype) {97return btype == BlobType.MethodProfiled;98}99100/**101* Verifies that 'newValue' is equal to 'oldValue' if usage of the102* corresponding code heap is predictable. Checks the weaker condition103* 'newValue >= oldValue' if usage is not predictable because intermediate104* allocations may happen.105*106* @param btype BlobType of the code heap to be checked107* @param newValue New value to be verified108* @param oldValue Old value to be verified109* @param msg Error message if verification fails110*/111public static void assertEQorGTE(BlobType btype, long newValue, long oldValue, String msg) {112if (CodeCacheUtils.isCodeHeapPredictable(btype)) {113// Usage is predictable, check strong == condition114Asserts.assertEQ(newValue, oldValue, msg);115} else {116// Usage is not predictable, check weaker >= condition117Asserts.assertGTE(newValue, oldValue, msg);118}119}120121/**122* Verifies that 'newValue' is equal to 'oldValue' if usage of the123* corresponding code heap is predictable. Checks the weaker condition124* 'newValue <= oldValue' if usage is not predictable because intermediate125* allocations may happen.126*127* @param btype BlobType of the code heap to be checked128* @param newValue New value to be verified129* @param oldValue Old value to be verified130* @param msg Error message if verification fails131*/132public static void assertEQorLTE(BlobType btype, long newValue, long oldValue, String msg) {133if (CodeCacheUtils.isCodeHeapPredictable(btype)) {134// Usage is predictable, check strong == condition135Asserts.assertEQ(newValue, oldValue, msg);136} else {137// Usage is not predictable, check weaker <= condition138Asserts.assertLTE(newValue, oldValue, msg);139}140}141142143public static void disableCollectionUsageThresholds() {144BlobType.getAvailable().stream()145.map(BlobType::getMemoryPool)146.filter(MemoryPoolMXBean::isCollectionUsageThresholdSupported)147.forEach(b -> b.setCollectionUsageThreshold(0L));148}149}150151152