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 javax.management.ListenerNotFoundException; 33 import javax.management.Notification; 34 import javax.management.NotificationEmitter; 35 import javax.management.NotificationListener; 36 import sun.hotspot.code.BlobType; 37 38 /* 39 * @test PoolsIndependenceTest 40 * @ignore 8068385 41 * @library /testlibrary /../../test/lib 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 getUsageThreshold() 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.WB.allocateCodeBlob( 79 CodeCacheUtils.ALLOCATION_SIZE, btype.id); 80 CodeCacheUtils.WB.fullGC(); 81 /* waiting for expected event to be received plus double the time took 82 to receive expected event(for possible unexpected) and 83 plus 1 second in case expected event received (almost)immediately */ 84 Utils.waitForCondition(() -> { 85 long currentTimestamp = System.currentTimeMillis(); 86 int eventsCount 87 = counters.get(btype.getMemoryPool().getName()).get(); 88 if (eventsCount > 0) { 89 if (eventsCount > 1) { 90 return true; 91 } 92 long timeLastEventTook 93 = beginTimestamp - lastEventTimestamp; 94 long timeoutValue 95 = 1000L + beginTimestamp + 3L * timeLastEventTook; 96 return currentTimestamp > timeoutValue; 97 } 98 return false; 99 }); 100 for (BlobType bt : BlobType.getAvailable()) { 101 if (CodeCacheUtils.isCodeHeapPredictable(bt)) { 102 int expectedNotificationsAmount = bt.equals(btype) ? 1 : 0; 103 Asserts.assertEQ(counters.get(bt.getMemoryPool().getName()).get(), 104 expectedNotificationsAmount, String.format("Unexpected " 105 + "amount of notifications for pool: %s", 106 bt.getMemoryPool().getName())); 107 } 108 } 109 try { 110 ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). 111 removeNotificationListener(this); 112 } catch (ListenerNotFoundException ex) { 113 throw new AssertionError("Can't remove notification listener", ex); 114 } 115 System.out.printf("INFO: Scenario with %s finished%n", bean.getName()); 116 } 117 118 @Override 119 public void handleNotification(Notification notification, Object handback) { 120 String nType = notification.getType(); 121 String poolName 122 = CodeCacheUtils.getPoolNameFromNotification(notification); 123 // consider code cache events only 124 if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) { 125 Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, 126 nType, "Unexpected event received: " + nType); 127 // receiving events from available CodeCache-related beans only 128 if (counters.get(poolName) != null) { 129 counters.get(poolName).incrementAndGet(); 130 lastEventTimestamp = System.currentTimeMillis(); 131 } 132 } 133 } 134 }