From 4b8ab368ca52dbc226a9584f06395c8aef46ed8a Mon Sep 17 00:00:00 2001 From: iignatyev Date: Sat, 13 Dec 2014 00:13:05 +0300 Subject: [PATCH] 8059613: JEP-JDK-8043304: Test task: JMX- tests Reviewed-by: thartmann, twisti Contributed-by: dmitrij.pochepko@oracle.com --- hotspot/test/TEST.groups | 1 + .../compiler/codecache/jmx/BeanTypeTest.java | 47 +++++++ .../codecache/jmx/CodeCacheUtils.java | 101 ++++++++++++++ .../jmx/CodeHeapBeanPresenceTest.java | 55 ++++++++ .../compiler/codecache/jmx/GetUsageTest.java | 109 +++++++++++++++ .../codecache/jmx/InitialAndMaxUsageTest.java | 107 ++++++++++++++ .../codecache/jmx/ManagerNamesTest.java | 64 +++++++++ .../jmx/MemoryPoolsPresenceTest.java | 77 ++++++++++ .../compiler/codecache/jmx/PeakUsageTest.java | 97 +++++++++++++ .../codecache/jmx/PoolsIndependenceTest.java | 131 ++++++++++++++++++ .../jmx/ThresholdNotificationsTest.java | 102 ++++++++++++++ ...sageThresholdExceededSeveralTimesTest.java | 36 +++++ .../jmx/UsageThresholdExceededTest.java | 73 ++++++++++ .../jmx/UsageThresholdIncreasedTest.java | 88 ++++++++++++ .../jmx/UsageThresholdNotExceededTest.java | 75 ++++++++++ 15 files changed, 1163 insertions(+) create mode 100644 hotspot/test/compiler/codecache/jmx/BeanTypeTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/CodeCacheUtils.java create mode 100644 hotspot/test/compiler/codecache/jmx/CodeHeapBeanPresenceTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/GetUsageTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/InitialAndMaxUsageTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/ManagerNamesTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/MemoryPoolsPresenceTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/PeakUsageTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/PoolsIndependenceTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/ThresholdNotificationsTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/UsageThresholdExceededSeveralTimesTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/UsageThresholdExceededTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/UsageThresholdIncreasedTest.java create mode 100644 hotspot/test/compiler/codecache/jmx/UsageThresholdNotExceededTest.java diff --git a/hotspot/test/TEST.groups b/hotspot/test/TEST.groups index 261e0cf073..5baf020e4b 100644 --- a/hotspot/test/TEST.groups +++ b/hotspot/test/TEST.groups @@ -145,6 +145,7 @@ needs_compact3 = \ gc/survivorAlignment \ runtime/InternalApi/ThreadCpuTimesDeadlock.java \ serviceability/threads/TestFalseDeadLock.java \ + compiler/codecache/jmx # Compact 2 adds full VM tests compact2 = \ diff --git a/hotspot/test/compiler/codecache/jmx/BeanTypeTest.java b/hotspot/test/compiler/codecache/jmx/BeanTypeTest.java new file mode 100644 index 0000000000..9ce162d2c2 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/BeanTypeTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryType; +import sun.hotspot.code.BlobType; + +/** + * @test BeanTypeTest + * @library /testlibrary /../../test/lib + * @build BeanTypeTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache BeanTypeTest + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache BeanTypeTest + * @summary verify types of code cache memory pool bean + */ +public class BeanTypeTest { + + public static void main(String args[]) { + for (BlobType bt : BlobType.getAvailable()) { + Asserts.assertEQ(MemoryType.NON_HEAP, bt.getMemoryPool().getType()); + } + } +} diff --git a/hotspot/test/compiler/codecache/jmx/CodeCacheUtils.java b/hotspot/test/compiler/codecache/jmx/CodeCacheUtils.java new file mode 100644 index 0000000000..472a88117b --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/CodeCacheUtils.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Utils; +import java.lang.management.MemoryPoolMXBean; +import javax.management.Notification; +import sun.hotspot.WhiteBox; +import sun.hotspot.code.BlobType; +import sun.hotspot.code.CodeBlob; + +public final class CodeCacheUtils { + + /** + * Returns the value to be used for code heap allocation + */ + public static final int ALLOCATION_SIZE + = Integer.getInteger("codecache.allocation.size", 100); + public static final WhiteBox WB = WhiteBox.getWhiteBox(); + public static final long SEGMENT_SIZE + = WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheSegmentSize"); + public static final long MIN_BLOCK_LENGTH + = WhiteBox.getWhiteBox().getUintxVMFlag("CodeCacheMinBlockLength"); + public static final long MIN_ALLOCATION = SEGMENT_SIZE * MIN_BLOCK_LENGTH; + + private CodeCacheUtils() { + // To prevent from instantiation + } + + public static final void hitUsageThreshold(MemoryPoolMXBean bean, + BlobType btype) { + long initialSize = bean.getUsage().getUsed(); + bean.setUsageThreshold(initialSize + 1); + long usageThresholdCount = bean.getUsageThresholdCount(); + long addr = WB.allocateCodeBlob(1, btype.id); + WB.fullGC(); + Utils.waitForCondition(() + -> bean.getUsageThresholdCount() == usageThresholdCount + 1); + WB.freeCodeBlob(addr); + } + + public static final long getHeaderSize(BlobType btype) { + long addr = WB.allocateCodeBlob(0, btype.id); + int size = CodeBlob.getCodeBlob(addr).size; + WB.freeCodeBlob(addr); + return size; + } + + public static String getPoolNameFromNotification( + Notification notification) { + return ((javax.management.openmbean.CompositeDataSupport) + notification.getUserData()).get("poolName").toString(); + } + + public static boolean isAvailableCodeHeapPoolName(String name) { + return BlobType.getAvailable().stream() + .map(BlobType::getMemoryPool) + .map(MemoryPoolMXBean::getName) + .filter(name::equals) + .findAny().isPresent(); + } + + /** + * A "non-nmethods" code heap is used by interpreter during bytecode + * execution, thus, it can't be predicted if this code heap usage will be + * increased or not. Same goes for 'All'. + * + * @param btype BlobType to be checked + * @return boolean value, true if respective code heap is predictable + */ + public static boolean isCodeHeapPredictable(BlobType btype) { + return btype == BlobType.MethodNonProfiled + || btype == BlobType.MethodProfiled; + } + + public static void disableCollectionUsageThresholds(){ + BlobType.getAvailable().stream() + .map(BlobType::getMemoryPool) + .filter(MemoryPoolMXBean::isCollectionUsageThresholdSupported) + .forEach(b -> b.setCollectionUsageThreshold(0L)); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/CodeHeapBeanPresenceTest.java b/hotspot/test/compiler/codecache/jmx/CodeHeapBeanPresenceTest.java new file mode 100644 index 0000000000..1ebacf9597 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/CodeHeapBeanPresenceTest.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.util.EnumSet; +import sun.hotspot.code.BlobType; + +/** + * @test CodeHeapBeanPresenceTest + * @library /testlibrary /../../test/lib + * @build CodeHeapBeanPresenceTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache CodeHeapBeanPresenceTest + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache CodeHeapBeanPresenceTest + * @summary verify CodeHeap bean presence + */ +public class CodeHeapBeanPresenceTest { + + public static void main(String args[]) { + EnumSet shouldBeAvailable = BlobType.getAvailable(); + EnumSet shouldNotBeAvailable + = EnumSet.complementOf(shouldBeAvailable); + for (BlobType btype : shouldBeAvailable) { + Asserts.assertNotNull(btype.getMemoryPool(), + "Can't find memory pool for " + btype.name()); + } + for (BlobType btype : shouldNotBeAvailable) { + Asserts.assertNull(btype.getMemoryPool(), + "Memory pool unexpected for " + btype.name()); + } + } +} diff --git a/hotspot/test/compiler/codecache/jmx/GetUsageTest.java b/hotspot/test/compiler/codecache/jmx/GetUsageTest.java new file mode 100644 index 0000000000..87139abd65 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/GetUsageTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryPoolMXBean; +import java.util.HashMap; +import java.util.Map; +import sun.hotspot.code.BlobType; + +/* + * @test GetUsageTest + * @library /testlibrary /../../test/lib + * @build GetUsageTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:CompileCommand=compileonly,null::* + * -XX:-UseCodeCacheFlushing -XX:-MethodFlushing -XX:+SegmentedCodeCache + * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI GetUsageTest + * @summary testing of getUsage() for segmented code cache + */ +public class GetUsageTest { + + private final BlobType btype; + private final int allocateSize; + + public GetUsageTest(BlobType btype, int allocSize) { + this.btype = btype; + this.allocateSize = allocSize; + } + + public static void main(String[] args) throws Exception { + for (BlobType btype : BlobType.getAvailable()) { + if (CodeCacheUtils.isCodeHeapPredictable(btype)) { + for (int allocSize = 10; allocSize < 100000; allocSize *= 10) { + new GetUsageTest(btype, allocSize).runTest(); + } + } + } + } + + protected final Map getBeanUsages() { + Map beanUsages = new HashMap<>(); + for (BlobType bt : BlobType.getAvailable()) { + beanUsages.put(bt.getMemoryPool(), + bt.getMemoryPool().getUsage().getUsed()); + } + return beanUsages; + } + + protected void runTest() { + MemoryPoolMXBean[] predictableBeans = BlobType.getAvailable().stream() + .filter(CodeCacheUtils::isCodeHeapPredictable) + .map(BlobType::getMemoryPool) + .toArray(MemoryPoolMXBean[]::new); + Map initial = getBeanUsages(); + long addr = 0; + try { + addr = CodeCacheUtils.WB.allocateCodeBlob(allocateSize, btype.id); + Map current = getBeanUsages(); + long blockCount = Math.floorDiv(allocateSize + + CodeCacheUtils.getHeaderSize(btype) + + CodeCacheUtils.SEGMENT_SIZE - 1, CodeCacheUtils.SEGMENT_SIZE); + long usageUpperEstimate = Math.max(blockCount, + CodeCacheUtils.MIN_BLOCK_LENGTH) * CodeCacheUtils.SEGMENT_SIZE; + for (MemoryPoolMXBean entry : predictableBeans) { + long diff = current.get(entry) - initial.get(entry); + if (entry.equals(btype.getMemoryPool())) { + Asserts.assertFalse(diff <= 0L || diff > usageUpperEstimate, + String.format("Pool %s usage increase was reported " + + "unexpectedly as increased by %d using " + + "allocation size %d", entry.getName(), + diff, allocateSize)); + } else { + Asserts.assertEQ(diff, 0L, + String.format("Pool %s usage changed unexpectedly while" + + " trying to increase: %s using allocation " + + "size %d", entry.getName(), + btype.getMemoryPool().getName(), allocateSize)); + } + } + } finally { + if (addr != 0) { + CodeCacheUtils.WB.freeCodeBlob(addr); + } + } + System.out.printf("INFO: Scenario finished successfully for %s%n", + btype.getMemoryPool().getName()); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/InitialAndMaxUsageTest.java b/hotspot/test/compiler/codecache/jmx/InitialAndMaxUsageTest.java new file mode 100644 index 0000000000..e18be5cab9 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/InitialAndMaxUsageTest.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryPoolMXBean; +import java.util.ArrayList; +import java.util.List; +import sun.hotspot.code.BlobType; + +/* + * @test InitialAndMaxUsageTest + * @library /testlibrary /../../test/lib + * @build InitialAndMaxUsageTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing + * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI + * -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::* + * InitialAndMaxUsageTest + * @summary testing of initial and max usage + */ +public class InitialAndMaxUsageTest { + + private static final double CACHE_USAGE_COEF = 0.95d; + private final BlobType btype; + private final boolean lowerBoundIsZero; + private final long maxSize; + + public InitialAndMaxUsageTest(BlobType btype) { + this.btype = btype; + this.maxSize = btype.getSize(); + /* Only profiled code cache initial size should be 0, because of + -XX:CompileCommand=compileonly,null::* non-methods might be not empty, + as well as non-profiled methods, because it's used as fallback in + case non-methods is full */ + lowerBoundIsZero = btype == BlobType.MethodProfiled; + } + + public static void main(String[] args) { + for (BlobType btype : BlobType.getAvailable()) { + new InitialAndMaxUsageTest(btype).runTest(); + } + } + + private void fillWithSize(long size, List blobs) { + long blob; + while ((blob = CodeCacheUtils.WB.allocateCodeBlob(size, btype.id)) + != 0L) { + blobs.add(blob); + } + } + + protected void runTest() { + long headerSize = CodeCacheUtils.getHeaderSize(btype); + MemoryPoolMXBean bean = btype.getMemoryPool(); + long initialUsage = btype.getMemoryPool().getUsage().getUsed(); + System.out.printf("INFO: trying to test %s of max size %d and initial" + + " usage %d%n", bean.getName(), maxSize, initialUsage); + Asserts.assertLT(initialUsage + headerSize + 1L, maxSize, + "Initial usage is close to total size for " + bean.getName()); + if (lowerBoundIsZero) { + Asserts.assertEQ(initialUsage, 0L, "Unexpected initial usage"); + } + ArrayList blobs = new ArrayList<>(); + long minAllocationUnit = CodeCacheUtils.MIN_ALLOCATION - headerSize; + /* now filling code cache with large-sized allocation first, since + lots of small allocations takes too much time, so, just a small + optimization */ + try { + for (int coef = 1000000; coef > 0; coef /= 10) { + fillWithSize(coef * minAllocationUnit, blobs); + } + Asserts.assertGT((double) bean.getUsage().getUsed(), + CACHE_USAGE_COEF * maxSize, String.format("Unable to fill " + + "more than %f of %s. Reported usage is %d ", + CACHE_USAGE_COEF, bean.getName(), + bean.getUsage().getUsed())); + } finally { + for (long entry : blobs) { + CodeCacheUtils.WB.freeCodeBlob(entry); + } + } + System.out.printf("INFO: Scenario finished successfully for %s%n", + bean.getName()); + } + +} diff --git a/hotspot/test/compiler/codecache/jmx/ManagerNamesTest.java b/hotspot/test/compiler/codecache/jmx/ManagerNamesTest.java new file mode 100644 index 0000000000..57be84e2b4 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/ManagerNamesTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryPoolMXBean; +import sun.hotspot.code.BlobType; + +/** + * @test ManagerNamesTest + * @library /testlibrary /../../test/lib + * @build ManagerNamesTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache ManagerNamesTest + * * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache ManagerNamesTest + * @summary verify getMemoryManageNames calls in case of segmented code cache + */ +public class ManagerNamesTest { + + private final MemoryPoolMXBean bean; + private final static String POOL_NAME = "CodeCacheManager"; + + public static void main(String args[]) { + for (BlobType btype : BlobType.getAvailable()) { + new ManagerNamesTest(btype).runTest(); + } + } + + public ManagerNamesTest(BlobType btype) { + bean = btype.getMemoryPool(); + } + + protected void runTest() { + String[] names = bean.getMemoryManagerNames(); + Asserts.assertEQ(names.length, 1, + "Unexpected length of MemoryManagerNames"); + Asserts.assertEQ(POOL_NAME, names[0], + "Unexpected value of MemoryManagerName"); + System.out.printf("INFO: Scenario finished successfully for %s%n", + bean.getName()); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/MemoryPoolsPresenceTest.java b/hotspot/test/compiler/codecache/jmx/MemoryPoolsPresenceTest.java new file mode 100644 index 0000000000..d850e926eb --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/MemoryPoolsPresenceTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryManagerMXBean; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import sun.hotspot.code.BlobType; + +/** + * @test MemoryPoolsPresenceTest + * @library /testlibrary /../../test/lib + * @build MemoryPoolsPresenceTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache MemoryPoolsPresenceTest + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache MemoryPoolsPresenceTest + * @summary verify that MemoryManagerMXBean exists for every code cache segment + */ +public class MemoryPoolsPresenceTest { + + private static final String CC_MANAGER = "CodeCacheManager"; + private final Map counters = new HashMap<>(); + + public static void main(String args[]) { + new MemoryPoolsPresenceTest().runTest(); + } + + protected void runTest() { + List beans + = ManagementFactory.getMemoryManagerMXBeans(); + Optional any = beans + .stream() + .filter(bean -> CC_MANAGER.equals(bean.getName())) + .findAny(); + Asserts.assertTrue(any.isPresent(), "Bean not found: " + CC_MANAGER); + MemoryManagerMXBean ccManager = any.get(); + Asserts.assertNotNull(ccManager, "Found null for " + CC_MANAGER); + String names[] = ccManager.getMemoryPoolNames(); + for (String name : names) { + counters.put(name, counters.containsKey(name) + ? counters.get(name) + 1 : 1); + } + for (BlobType btype : BlobType.getAvailable()) { + Asserts.assertEQ(counters.get(btype.getMemoryPool().getName()), 1, + "Found unexpected amount of beans for pool " + + btype.getMemoryPool().getName()); + } + Asserts.assertEQ(BlobType.getAvailable().size(), + counters.keySet().size(), "Unexpected amount of bean names"); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/PeakUsageTest.java b/hotspot/test/compiler/codecache/jmx/PeakUsageTest.java new file mode 100644 index 0000000000..b77e054c37 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/PeakUsageTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryPoolMXBean; +import sun.hotspot.code.BlobType; + +/* + * @test PeakUsageTest + * @library /testlibrary /../../test/lib + * @build PeakUsageTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache + * -XX:CompileCommand=compileonly,null::* PeakUsageTest + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache + * -XX:CompileCommand=compileonly,null::* PeakUsageTest + * @summary testing of getPeakUsage() and resetPeakUsage for + * segmented code cache + */ +public class PeakUsageTest { + + private final BlobType btype; + + public PeakUsageTest(BlobType btype) { + this.btype = btype; + } + + public static void main(String[] args) { + for (BlobType btype : BlobType.getAvailable()) { + if (CodeCacheUtils.isCodeHeapPredictable(btype)) { + new PeakUsageTest(btype).runTest(); + } + } + } + + protected void runTest() { + MemoryPoolMXBean bean = btype.getMemoryPool(); + bean.resetPeakUsage(); + long addr = CodeCacheUtils.WB.allocateCodeBlob( + CodeCacheUtils.ALLOCATION_SIZE, btype.id); + long newPeakUsage = bean.getPeakUsage().getUsed(); + try { + Asserts.assertEQ(newPeakUsage, bean.getUsage().getUsed(), + "Peak usage does not match usage after allocation for " + + bean.getName()); + } finally { + if (addr != 0) { + CodeCacheUtils.WB.freeCodeBlob(addr); + } + } + Asserts.assertEQ(newPeakUsage, bean.getPeakUsage().getUsed(), + "Code cache peak usage has changed after usage decreased for " + + bean.getName()); + bean.resetPeakUsage(); + Asserts.assertEQ(bean.getPeakUsage().getUsed(), + bean.getUsage().getUsed(), + "Code cache peak usage is not equal to usage after reset for " + + bean.getName()); + long addr2 = CodeCacheUtils.WB.allocateCodeBlob( + CodeCacheUtils.ALLOCATION_SIZE, btype.id); + try { + Asserts.assertEQ(bean.getPeakUsage().getUsed(), + bean.getUsage().getUsed(), + "Code cache peak usage is not equal to usage after fresh " + + "allocation for " + bean.getName()); + } finally { + if (addr2 != 0) { + CodeCacheUtils.WB.freeCodeBlob(addr2); + } + } + System.out.printf("INFO: Scenario finished successfully for %s%n", + bean.getName()); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/PoolsIndependenceTest.java b/hotspot/test/compiler/codecache/jmx/PoolsIndependenceTest.java new file mode 100644 index 0000000000..a2ea70a1fe --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/PoolsIndependenceTest.java @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import com.oracle.java.testlibrary.Utils; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryNotificationInfo; +import java.lang.management.MemoryPoolMXBean; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import javax.management.ListenerNotFoundException; +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; +import sun.hotspot.code.BlobType; + +/* + * @test PoolsIndependenceTest + * @library /testlibrary /../../test/lib + * @build PoolsIndependenceTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing + * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI + * -XX:+SegmentedCodeCache PoolsIndependenceTest + * @summary testing of getUsageThreshold() + */ +public class PoolsIndependenceTest implements NotificationListener { + + private final Map counters; + private final BlobType btype; + private volatile long lastEventTimestamp; + + public PoolsIndependenceTest(BlobType btype) { + counters = new HashMap<>(); + for (BlobType bt : BlobType.getAvailable()) { + counters.put(bt.getMemoryPool().getName(), new AtomicInteger(0)); + } + this.btype = btype; + lastEventTimestamp = 0; + CodeCacheUtils.disableCollectionUsageThresholds(); + } + + public static void main(String[] args) { + for (BlobType bt : BlobType.getAvailable()) { + new PoolsIndependenceTest(bt).runTest(); + } + } + + protected void runTest() { + MemoryPoolMXBean bean = btype.getMemoryPool(); + ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). + addNotificationListener(this, null, null); + bean.setUsageThreshold(bean.getUsage().getUsed() + 1); + long beginTimestamp = System.currentTimeMillis(); + CodeCacheUtils.WB.allocateCodeBlob( + CodeCacheUtils.ALLOCATION_SIZE, btype.id); + CodeCacheUtils.WB.fullGC(); + /* waiting for expected event to be received plus double the time took + to receive expected event(for possible unexpected) and + plus 1 second in case expected event received (almost)immediately */ + Utils.waitForCondition(() -> { + long currentTimestamp = System.currentTimeMillis(); + int eventsCount + = counters.get(btype.getMemoryPool().getName()).get(); + if (eventsCount > 0) { + if (eventsCount > 1) { + return true; + } + long timeLastEventTook + = beginTimestamp - lastEventTimestamp; + long timeoutValue + = 1000L + beginTimestamp + 3L * timeLastEventTook; + return currentTimestamp > timeoutValue; + } + return false; + }); + for (BlobType bt : BlobType.getAvailable()) { + int expectedNotificationsAmount = bt.equals(btype) ? 1 : 0; + Asserts.assertEQ(counters.get(bt.getMemoryPool().getName()).get(), + expectedNotificationsAmount, String.format("Unexpected " + + "amount of notifications for pool: %s", + bt.getMemoryPool().getName())); + } + try { + ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). + removeNotificationListener(this); + } catch (ListenerNotFoundException ex) { + throw new AssertionError("Can't remove notification listener", ex); + } + System.out.printf("INFO: Scenario with %s finished%n", bean.getName()); + } + + @Override + public void handleNotification(Notification notification, Object handback) { + String nType = notification.getType(); + String poolName + = CodeCacheUtils.getPoolNameFromNotification(notification); + // consider code cache events only + if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) { + Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, + nType, "Unexpected event received: " + nType); + // receiving events from available CodeCache-related beans only + if (counters.get(poolName) != null) { + counters.get(poolName).incrementAndGet(); + lastEventTimestamp = System.currentTimeMillis(); + } + } + } +} diff --git a/hotspot/test/compiler/codecache/jmx/ThresholdNotificationsTest.java b/hotspot/test/compiler/codecache/jmx/ThresholdNotificationsTest.java new file mode 100644 index 0000000000..b03fbc2f4a --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/ThresholdNotificationsTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import com.oracle.java.testlibrary.Utils; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryNotificationInfo; +import java.lang.management.MemoryPoolMXBean; +import javax.management.ListenerNotFoundException; +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; +import sun.hotspot.code.BlobType; + +/* + * @test ThresholdNotificationsTest + * @library /testlibrary /../../test/lib + * @build ThresholdNotificationsTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing + * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI + * -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::* + * ThresholdNotificationsTest + * @summary testing of getUsageThreshold() + */ +public class ThresholdNotificationsTest implements NotificationListener { + + private final static long WAIT_TIME = 10000L; + private volatile long counter; + private final BlobType btype; + + public static void main(String[] args) { + for (BlobType bt : BlobType.getAvailable()) { + new ThresholdNotificationsTest(bt).runTest(); + } + } + + public ThresholdNotificationsTest(BlobType btype) { + this.btype = btype; + counter = 0L; + CodeCacheUtils.disableCollectionUsageThresholds(); + } + + @Override + public void handleNotification(Notification notification, Object handback) { + String nType = notification.getType(); + String poolName + = CodeCacheUtils.getPoolNameFromNotification(notification); + // consider code cache events only + if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) { + Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, + nType, "Unexpected event received: " + nType); + if (poolName.equals(btype.getMemoryPool().getName())) { + counter++; + } + } + } + + protected void runTest() { + int iterationsCount = + Integer.getInteger("com.oracle.java.testlibrary.iterations", 1); + MemoryPoolMXBean bean = btype.getMemoryPool(); + ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). + addNotificationListener(this, null, null); + for (int i = 0; i < iterationsCount; i++) { + CodeCacheUtils.hitUsageThreshold(bean, btype); + } + Asserts.assertTrue( + Utils.waitForCondition( + () -> counter == iterationsCount, WAIT_TIME), + "Couldn't receive expected notifications count"); + try { + ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). + removeNotificationListener(this); + } catch (ListenerNotFoundException ex) { + throw new AssertionError("Can't remove notification listener", ex); + } + System.out.printf("INFO: Scenario finished successfully for %s%n", + bean.getName()); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/UsageThresholdExceededSeveralTimesTest.java b/hotspot/test/compiler/codecache/jmx/UsageThresholdExceededSeveralTimesTest.java new file mode 100644 index 0000000000..ad2ddc8603 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/UsageThresholdExceededSeveralTimesTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test UsageThresholdExceededSeveralTimesTest + * @library /testlibrary /../../test/lib + * @build UsageThresholdExceededTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing + * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI + * -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::* + * -Dcom.oracle.java.testlibrary.iterations=10 UsageThresholdExceededTest + * @summary verifying that getUsageThresholdCount() returns correct value + * after threshold has been hit several times + */ diff --git a/hotspot/test/compiler/codecache/jmx/UsageThresholdExceededTest.java b/hotspot/test/compiler/codecache/jmx/UsageThresholdExceededTest.java new file mode 100644 index 0000000000..50e455627d --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/UsageThresholdExceededTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryPoolMXBean; +import sun.hotspot.code.BlobType; + +/* + * @test UsageThresholdExceededTest + * @library /testlibrary /../../test/lib + * @build UsageThresholdExceededTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache -XX:-UseCodeCacheFlushing + * -XX:-MethodFlushing -XX:CompileCommand=compileonly,null::* + * UsageThresholdExceededTest + * @summary verifying that getUsageThresholdCount() returns correct value + * after threshold has been hit + */ +public class UsageThresholdExceededTest { + + protected final int iterations; + private final BlobType btype; + + public UsageThresholdExceededTest(BlobType btype, int iterations) { + this.btype = btype; + this.iterations = iterations; + } + + public static void main(String[] args) { + int iterationsCount = + Integer.getInteger("com.oracle.java.testlibrary.iterations", 1); + for (BlobType btype : BlobType.getAvailable()) { + if (CodeCacheUtils.isCodeHeapPredictable(btype)) { + new UsageThresholdExceededTest(btype, iterationsCount) + .runTest(); + } + } + } + + protected void runTest() { + MemoryPoolMXBean bean = btype.getMemoryPool(); + long oldValue = bean.getUsageThresholdCount(); + for (int i = 0; i < iterations; i++) { + CodeCacheUtils.hitUsageThreshold(bean, btype); + } + Asserts.assertEQ(bean.getUsageThresholdCount(), oldValue + iterations, + "Unexpected threshold usage count"); + System.out.printf("INFO: Scenario finished successfully for %s%n", + bean.getName()); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/UsageThresholdIncreasedTest.java b/hotspot/test/compiler/codecache/jmx/UsageThresholdIncreasedTest.java new file mode 100644 index 0000000000..e70f62aa4d --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/UsageThresholdIncreasedTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryPoolMXBean; +import sun.hotspot.code.BlobType; + +/* + * @test UsageThresholdIncreasedTest + * @library /testlibrary /../../test/lib + * @build UsageThresholdIncreasedTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache -XX:-UseCodeCacheFlushing + * -XX:-MethodFlushing -XX:CompileCommand=compileonly,null::* + * UsageThresholdIncreasedTest + * @summary verifying that threshold hasn't been hit after allocation smaller + * than threshold value and that threshold value can be changed + */ +public class UsageThresholdIncreasedTest { + + private static final int ALLOCATION_STEP = 5; + private static final long THRESHOLD_STEP = ALLOCATION_STEP + * CodeCacheUtils.MIN_ALLOCATION; + private final BlobType btype; + + public UsageThresholdIncreasedTest(BlobType btype) { + this.btype = btype; + } + + public static void main(String[] args) { + for (BlobType btype : BlobType.getAvailable()) { + new UsageThresholdIncreasedTest(btype).runTest(); + } + } + + private void checkUsageThresholdCount(MemoryPoolMXBean bean, long count){ + Asserts.assertEQ(bean.getUsageThresholdCount(), count, + String.format("Usage threshold was hit: %d times for %s " + + "Threshold value: %d with current usage: %d", + bean.getUsageThresholdCount(), bean.getName(), + bean.getUsageThreshold(), bean.getUsage().getUsed())); + } + + protected void runTest() { + long headerSize = CodeCacheUtils.getHeaderSize(btype); + long allocationUnit = CodeCacheUtils.MIN_ALLOCATION - headerSize; + MemoryPoolMXBean bean = btype.getMemoryPool(); + long initialCount = bean.getUsageThresholdCount(); + long initialSize = bean.getUsage().getUsed(); + bean.setUsageThreshold(initialSize + THRESHOLD_STEP); + for (int i = 0; i < ALLOCATION_STEP - 1; i++) { + CodeCacheUtils.WB.allocateCodeBlob(allocationUnit, btype.id); + } + // Usage threshold check is triggered by GC cycle, so, call it + CodeCacheUtils.WB.fullGC(); + checkUsageThresholdCount(bean, initialCount); + long filledSize = bean.getUsage().getUsed(); + bean.setUsageThreshold(filledSize + THRESHOLD_STEP); + for (int i = 0; i < ALLOCATION_STEP - 1; i++) { + CodeCacheUtils.WB.allocateCodeBlob(allocationUnit, btype.id); + } + CodeCacheUtils.WB.fullGC(); + checkUsageThresholdCount(bean, initialCount); + System.out.println("INFO: Case finished successfully for " + bean.getName()); + } +} diff --git a/hotspot/test/compiler/codecache/jmx/UsageThresholdNotExceededTest.java b/hotspot/test/compiler/codecache/jmx/UsageThresholdNotExceededTest.java new file mode 100644 index 0000000000..23fdf9c7b8 --- /dev/null +++ b/hotspot/test/compiler/codecache/jmx/UsageThresholdNotExceededTest.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.oracle.java.testlibrary.Asserts; +import java.lang.management.MemoryPoolMXBean; +import sun.hotspot.code.BlobType; + +/* + * @test UsageThresholdNotExceededTest + * @library /testlibrary /../../test/lib + * @build UsageThresholdNotExceededTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing + * -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI + * -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::* + * UsageThresholdNotExceededTest + * @summary verifying that usage threshold not exceeded while allocating less + * than usage threshold + */ +public class UsageThresholdNotExceededTest { + + private final BlobType btype; + + public UsageThresholdNotExceededTest(BlobType btype) { + this.btype = btype; + } + + public static void main(String[] args) { + for (BlobType btype : BlobType.getAvailable()) { + if (CodeCacheUtils.isCodeHeapPredictable(btype)) { + new UsageThresholdNotExceededTest(btype).runTest(); + } + } + } + + protected void runTest() { + MemoryPoolMXBean bean = btype.getMemoryPool(); + long initialThresholdCount = bean.getUsageThresholdCount(); + long initialUsage = bean.getUsage().getUsed(); + bean.setUsageThreshold(initialUsage + 1 + CodeCacheUtils.MIN_ALLOCATION); + CodeCacheUtils.WB.allocateCodeBlob(CodeCacheUtils.MIN_ALLOCATION + - CodeCacheUtils.getHeaderSize(btype), btype.id); + // a gc cycle triggers usage threshold recalculation + CodeCacheUtils.WB.fullGC(); + Asserts.assertEQ(bean.getUsageThresholdCount(), initialThresholdCount, + String.format("Usage threshold was hit: %d times for %s. " + + "Threshold value: %d with current usage: %d", + bean.getUsageThresholdCount(), bean.getName(), + bean.getUsageThreshold(), bean.getUsage().getUsed())); + + System.out.println("INFO: Case finished successfully for " + + bean.getName()); + } +} -- GitLab