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 import jdk.test.lib.Asserts;
  25 import jdk.test.lib.Utils;
  26 import java.lang.management.ManagementFactory;
  27 import java.lang.management.MemoryNotificationInfo;
  28 import java.lang.management.MemoryPoolMXBean;
  29 import javax.management.ListenerNotFoundException;
  30 import javax.management.Notification;
  31 import javax.management.NotificationEmitter;
  32 import javax.management.NotificationListener;
  33 import sun.hotspot.code.BlobType;
  34 
  35 /*
  36  * @test ThresholdNotificationsTest
  37  * @library /testlibrary /../../test/lib
  38  * @modules java.base/sun.misc
  39  *          java.management
  40  * @build ThresholdNotificationsTest
  41  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  42  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  43  * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing
  44  *     -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  45  *     -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::*
  46  *     ThresholdNotificationsTest
  47  * @summary testing of getUsageThreshold()
  48  */
  49 public class ThresholdNotificationsTest implements NotificationListener {
  50 
  51     private final static long WAIT_TIME = 10000L;
  52     private volatile long counter;
  53     private final BlobType btype;
  54 
  55     public static void main(String[] args) {
  56         for (BlobType bt : BlobType.getAvailable()) {
  57             if (CodeCacheUtils.isCodeHeapPredictable(bt)) {
  58                 new ThresholdNotificationsTest(bt).runTest();
  59             }
  60         }
  61     }
  62 
  63     public ThresholdNotificationsTest(BlobType btype) {
  64         this.btype = btype;
  65         counter = 0L;
  66         CodeCacheUtils.disableCollectionUsageThresholds();
  67     }
  68 
  69     @Override
  70     public void handleNotification(Notification notification, Object handback) {
  71         String nType = notification.getType();
  72         String poolName
  73                 = CodeCacheUtils.getPoolNameFromNotification(notification);
  74         // consider code cache events only
  75         if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) {
  76             Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
  77                     nType, "Unexpected event received: " + nType);
  78             if (poolName.equals(btype.getMemoryPool().getName())) {
  79                 counter++;
  80             }
  81         }
  82     }
  83 
  84     protected void runTest() {
  85         int iterationsCount =
  86             Integer.getInteger("jdk.test.lib.iterations", 1);
  87         MemoryPoolMXBean bean = btype.getMemoryPool();
  88         ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
  89                 addNotificationListener(this, null, null);
  90         for (int i = 0; i < iterationsCount; i++) {
  91             CodeCacheUtils.hitUsageThreshold(bean, btype);
  92         }
  93         Asserts.assertTrue(
  94                 Utils.waitForCondition(
  95                         () -> counter == iterationsCount, WAIT_TIME),
  96                 "Couldn't receive expected notifications count");
  97         try {
  98             ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
  99                     removeNotificationListener(this);
 100         } catch (ListenerNotFoundException ex) {
 101             throw new AssertionError("Can't remove notification listener", ex);
 102         }
 103         System.out.printf("INFO: Scenario finished successfully for %s%n",
 104                 bean.getName());
 105     }
 106 }