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             new ThresholdNotificationsTest(bt).runTest();
  58         }
  59     }
  60 
  61     public ThresholdNotificationsTest(BlobType btype) {
  62         this.btype = btype;
  63         counter = 0L;
  64         CodeCacheUtils.disableCollectionUsageThresholds();
  65     }
  66 
  67     @Override
  68     public void handleNotification(Notification notification, Object handback) {
  69         String nType = notification.getType();
  70         String poolName
  71                 = CodeCacheUtils.getPoolNameFromNotification(notification);
  72         // consider code cache events only
  73         if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) {
  74             Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
  75                     nType, "Unexpected event received: " + nType);
  76             if (poolName.equals(btype.getMemoryPool().getName())) {
  77                 counter++;
  78             }
  79         }
  80     }
  81 
  82     protected void runTest() {
  83         int iterationsCount =
  84             Integer.getInteger("jdk.test.lib.iterations", 1);
  85         MemoryPoolMXBean bean = btype.getMemoryPool();
  86         ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
  87                 addNotificationListener(this, null, null);
  88         for (int i = 0; i < iterationsCount; i++) {
  89             CodeCacheUtils.hitUsageThreshold(bean, btype);
  90         }
  91         Asserts.assertTrue(
  92                 Utils.waitForCondition(
  93                         () -> (CodeCacheUtils.isCodeHeapPredictable(btype) ?
  94                                 (counter == iterationsCount) : (counter >= iterationsCount)),
  95                         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 }