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