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 ThresholdNotificationsTest 26 * @summary testing of getUsageThreshold() 27 * @library /test/lib / 28 * @modules java.base/jdk.internal.misc 29 * java.management 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:-UseCodeCacheFlushing 35 * -XX:+WhiteBoxAPI -XX:-MethodFlushing -XX:CompileCommand=compileonly,null::* 36 * -XX:+SegmentedCodeCache 37 * compiler.codecache.jmx.ThresholdNotificationsTest 38 * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing 39 * -XX:+WhiteBoxAPI -XX:-MethodFlushing -XX:CompileCommand=compileonly,null::* 40 * -XX:-SegmentedCodeCache 41 * compiler.codecache.jmx.ThresholdNotificationsTest 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 58 public class ThresholdNotificationsTest implements NotificationListener { 59 60 private final static long WAIT_TIME = 10000L; 61 private volatile long counter; 62 private final BlobType btype; 63 64 public static void main(String[] args) { 65 for (BlobType bt : BlobType.getAvailable()) { 66 new ThresholdNotificationsTest(bt).runTest(); 67 } 68 } 69 70 public ThresholdNotificationsTest(BlobType btype) { 71 this.btype = btype; 72 counter = 0L; 73 CodeCacheUtils.disableCollectionUsageThresholds(); 74 } 75 76 @Override 77 public void handleNotification(Notification notification, Object handback) { 78 String nType = notification.getType(); 79 String poolName 80 = CodeCacheUtils.getPoolNameFromNotification(notification); 81 // consider code cache events only 82 if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) { 83 Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, 84 nType, "Unexpected event received: " + nType); 85 if (poolName.equals(btype.getMemoryPool().getName())) { 86 counter++; 87 } 88 } 89 } 90 91 protected void runTest() { 92 int iterationsCount 93 = Integer.getInteger("jdk.test.lib.iterations", 1); 94 MemoryPoolMXBean bean = btype.getMemoryPool(); 95 ((NotificationEmitter) ManagementFactory.getMemoryMXBean()). 96 addNotificationListener(this, null, null); 97 for (int i = 0; i < iterationsCount; i++) { 98 CodeCacheUtils.hitUsageThreshold(bean, btype); 99 } 100 Asserts.assertTrue( 101 Utils.waitForCondition( 102 () -> (CodeCacheUtils.isCodeHeapPredictable(btype) ? 103 (counter == iterationsCount) : (counter >= iterationsCount)), 104 WAIT_TIME), 105 "Couldn't receive expected notifications count"); 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 finished successfully for %s%n", 113 bean.getName()); 114 } 115 }