Path: blob/master/test/hotspot/jtreg/compiler/codecache/jmx/PoolsIndependenceTest.java
41153 views
/*1* Copyright (c) 2014, 2021, 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*/2223/*24* @test PoolsIndependenceTest25* @summary testing of getUsageThreshold()26* @modules java.base/jdk.internal.misc27* java.management28* @library /test/lib /29*30* @build sun.hotspot.WhiteBox31* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox32* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions33* -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing34* -XX:+SegmentedCodeCache35* compiler.codecache.jmx.PoolsIndependenceTest36* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions37* -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing38* -XX:-SegmentedCodeCache39* compiler.codecache.jmx.PoolsIndependenceTest40*/4142package compiler.codecache.jmx;4344import jdk.test.lib.Asserts;45import jdk.test.lib.Utils;46import sun.hotspot.code.BlobType;4748import javax.management.ListenerNotFoundException;49import javax.management.Notification;50import javax.management.NotificationEmitter;51import javax.management.NotificationListener;52import java.lang.management.ManagementFactory;53import java.lang.management.MemoryNotificationInfo;54import java.lang.management.MemoryPoolMXBean;55import java.util.HashMap;56import java.util.Map;57import java.util.concurrent.atomic.AtomicInteger;5859import jtreg.SkippedException;6061public class PoolsIndependenceTest implements NotificationListener {6263private final Map<String, AtomicInteger> counters;64private final BlobType btype;65private volatile long lastEventTimestamp;66private volatile long maxUsageRegistered;67private final long TEST_TIMEOUT_LIMIT = System.currentTimeMillis() +68Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - 5_000; // 5 seconds allowance is arbitrary6970public PoolsIndependenceTest(BlobType btype) {71counters = new HashMap<>();72for (BlobType bt : BlobType.getAvailable()) {73counters.put(bt.getMemoryPool().getName(), new AtomicInteger(0));74}75this.btype = btype;76lastEventTimestamp = 0;77maxUsageRegistered = 0;78CodeCacheUtils.disableCollectionUsageThresholds();79}8081public static void main(String[] args) {82for (BlobType bt : BlobType.getAvailable()) {83new PoolsIndependenceTest(bt).runTest();84}85}8687protected void runTest() {88MemoryPoolMXBean bean = btype.getMemoryPool();89System.out.printf("INFO: Starting scenario with %s%n", bean.getName());90((NotificationEmitter) ManagementFactory.getMemoryMXBean()).91addNotificationListener(this, null, null);92final long usageThresholdLimit = bean.getUsage().getUsed() + 1;93bean.setUsageThreshold(usageThresholdLimit);9495long beginTimestamp = System.currentTimeMillis();96final long phaseTimeout = Math.min(TEST_TIMEOUT_LIMIT,97beginTimestamp + 20_000); // 20 seconds is enought for everybody.9899CodeCacheUtils.WB.allocateCodeBlob(100CodeCacheUtils.ALLOCATION_SIZE, btype.id);101CodeCacheUtils.WB.fullGC();102103/* waiting for expected event to be received plus double the time took104to receive expected event(for possible unexpected) and105plus 1 second in case expected event received (almost)immediately */106Utils.waitForCondition(() -> {107maxUsageRegistered = Math.max(bean.getUsage().getUsed(), maxUsageRegistered);108long currentTimestamp = System.currentTimeMillis();109int eventsCount110= counters.get(btype.getMemoryPool().getName()).get();111if (eventsCount > 0) {112if (eventsCount > 1) {113return true;114}115long timeLastEventTook116= lastEventTimestamp - beginTimestamp;117118long awaitForUnexpectedTimeout119= 1000L + beginTimestamp + 3L * timeLastEventTook;120121return currentTimestamp > Math.min(phaseTimeout, awaitForUnexpectedTimeout);122};123124if (currentTimestamp > phaseTimeout) {125if (maxUsageRegistered < usageThresholdLimit) {126throw new SkippedException("The code cache usage hasn't exceeded" +127" the limit of " + usageThresholdLimit +128" (max usage reached is " + maxUsageRegistered + ")" +129" within test timeouts, can't test notifications");130} else {131Asserts.fail("UsageThresholdLimit was set to " + usageThresholdLimit +132", max usage of " + maxUsageRegistered + " have been registered" +133", but no notifications issued");134}135}136return false;137});138for (BlobType bt : BlobType.getAvailable()) {139int expectedNotificationsAmount = bt.equals(btype) ? 1 : 0;140CodeCacheUtils.assertEQorGTE(btype, counters.get(bt.getMemoryPool().getName()).get(),141expectedNotificationsAmount, String.format("Unexpected "142+ "amount of notifications for pool: %s",143bt.getMemoryPool().getName()));144}145try {146((NotificationEmitter) ManagementFactory.getMemoryMXBean()).147removeNotificationListener(this);148} catch (ListenerNotFoundException ex) {149throw new AssertionError("Can't remove notification listener", ex);150}151System.out.printf("INFO: Scenario with %s finished%n", bean.getName());152}153154@Override155public void handleNotification(Notification notification, Object handback) {156String nType = notification.getType();157String poolName158= CodeCacheUtils.getPoolNameFromNotification(notification);159// consider code cache events only160if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) {161Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,162nType, "Unexpected event received: " + nType);163// receiving events from available CodeCache-related beans only164if (counters.get(poolName) != null) {165counters.get(poolName).incrementAndGet();166lastEventTimestamp = System.currentTimeMillis();167}168}169}170}171172173