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