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 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  * @build ThresholdNotificationsTest
  39  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  40  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  41  * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing
  42  *     -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  43  *     -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::*
  44  *     ThresholdNotificationsTest
  45  * @summary testing of getUsageThreshold()
  46  */
  47 public class ThresholdNotificationsTest implements NotificationListener {
  48 
  49     private final static long WAIT_TIME = 10000L;
  50     private volatile long counter;
  51     private final BlobType btype;
  52 
  53     public static void main(String[] args) {
  54         for (BlobType bt : BlobType.getAvailable()) {
  55             if (CodeCacheUtils.isCodeHeapPredictable(bt)) {
  56                 new ThresholdNotificationsTest(bt).runTest();
  57             }
  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("com.oracle.java.testlibrary.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                         () -> counter == iterationsCount, WAIT_TIME),
  94                 "Couldn't receive expected notifications count");
  95         try {
  96             ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
  97                     removeNotificationListener(this);
  98         } catch (ListenerNotFoundException ex) {
  99             throw new AssertionError("Can't remove notification listener", ex);
 100         }
 101         System.out.printf("INFO: Scenario finished successfully for %s%n",
 102                 bean.getName());
 103     }
 104 }