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 /* 25 * @test PoolsIndependenceTest 26 * @summary testing of getUsageThreshold() 27 * @modules java.base/jdk.internal.misc 28 * java.management 29 * @library /test/lib / 30 * 31 * @build sun.hotspot.WhiteBox 32 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 33 * sun.hotspot.WhiteBox$WhiteBoxPermission 34 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 35 * -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing 36 * -XX:+SegmentedCodeCache 37 * compiler.codecache.jmx.PoolsIndependenceTest 38 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 39 * -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing 40 * -XX:-SegmentedCodeCache 41 * compiler.codecache.jmx.PoolsIndependenceTest 42 */ 43 44 package compiler.codecache.jmx; 45 46 import jdk.test.lib.Asserts; 47 import jdk.test.lib.Utils; 48 import sun.hotspot.code.BlobType; 49 50 import javax.management.ListenerNotFoundException; 51 import javax.management.Notification; 52 import javax.management.NotificationEmitter; 53 import javax.management.NotificationListener; 54 import java.lang.management.ManagementFactory; 55 import java.lang.management.MemoryNotificationInfo; 56 import java.lang.management.MemoryPoolMXBean; 57 import java.util.HashMap; 58 import java.util.Map; 59 import java.util.concurrent.atomic.AtomicInteger; 60 61 public class PoolsIndependenceTest implements NotificationListener { 62 63 private final Map<String, AtomicInteger> counters; 64 private final BlobType btype; 65 private volatile long lastEventTimestamp; 66 67 public PoolsIndependenceTest(BlobType btype) { 68 counters = new HashMap<>(); 69 for (BlobType bt : BlobType.getAvailable()) { 70 counters.put(bt.getMemoryPool().getName(), new AtomicInteger(0)); 71 } 72 this.btype = btype; 73 lastEventTimestamp = 0; 74 CodeCacheUtils.disableCollectionUsageThresholds(); 75 } 76 77 public static void main(String[] args) { 78 for (BlobType bt : BlobType.getAvailable()) { 79 new PoolsIndependenceTest(bt).runTest(); 80 } 81 } 82 83 protected void runTest() { 84 MemoryPoolMXBean bean = btype.getMemoryPool(); 85 ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). 86 addNotificationListener(this, null, null); 87 bean.setUsageThreshold(bean.getUsage().getUsed() + 1); 88 long beginTimestamp = System.currentTimeMillis(); 89 CodeCacheUtils.WB.allocateCodeBlob( 90 CodeCacheUtils.ALLOCATION_SIZE, btype.id); 91 CodeCacheUtils.WB.fullGC(); 92 /* waiting for expected event to be received plus double the time took 93 to receive expected event(for possible unexpected) and 94 plus 1 second in case expected event received (almost)immediately */ 95 Utils.waitForCondition(() -> { 96 long currentTimestamp = System.currentTimeMillis(); 97 int eventsCount 98 = counters.get(btype.getMemoryPool().getName()).get(); 99 if (eventsCount > 0) { 100 if (eventsCount > 1) { 101 return true; 102 } 103 long timeLastEventTook 104 = beginTimestamp - lastEventTimestamp; 105 long timeoutValue 106 = 1000L + beginTimestamp + 3L * timeLastEventTook; 107 return currentTimestamp > timeoutValue; 108 } 109 return false; 110 }); 111 for (BlobType bt : BlobType.getAvailable()) { 112 int expectedNotificationsAmount = bt.equals(btype) ? 1 : 0; 113 CodeCacheUtils.assertEQorGTE(btype, counters.get(bt.getMemoryPool().getName()).get(), 114 expectedNotificationsAmount, String.format("Unexpected " 115 + "amount of notifications for pool: %s", 116 bt.getMemoryPool().getName())); 117 } 118 try { 119 ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). 120 removeNotificationListener(this); 121 } catch (ListenerNotFoundException ex) { 122 throw new AssertionError("Can't remove notification listener", ex); 123 } 124 System.out.printf("INFO: Scenario with %s finished%n", bean.getName()); 125 } 126 127 @Override 128 public void handleNotification(Notification notification, Object handback) { 129 String nType = notification.getType(); 130 String poolName 131 = CodeCacheUtils.getPoolNameFromNotification(notification); 132 // consider code cache events only 133 if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) { 134 Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, 135 nType, "Unexpected event received: " + nType); 136 // receiving events from available CodeCache-related beans only 137 if (counters.get(poolName) != null) { 138 counters.get(poolName).incrementAndGet(); 139 lastEventTimestamp = System.currentTimeMillis(); 140 } 141 } 142 } 143 }