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 java.util.concurrent.atomic.AtomicInteger;
  30 import javax.management.ListenerNotFoundException;
  31 import javax.management.Notification;
  32 import javax.management.NotificationEmitter;
  33 import javax.management.NotificationListener;
  34 import sun.hotspot.code.BlobType;
  35 
  36 /*
  37  * @test ThresholdNotificationsTest
  38  * @library /testlibrary /testlibrary/whitebox
  39  * @build ThresholdNotificationsTest
  40  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  41  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  42  * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing
  43  *     -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  44  *     -XX:+SegmentedCodeCache -XX:CompileCommand=compileonly,null::*
  45  *     ThresholdNotificationsTest
  46  * @summary testing of getUsageThreashold()
  47  */
  48 public class ThresholdNotificationsTest implements NotificationListener {
  49 
  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             new ThresholdNotificationsTest(bt).runTest();
  56         }
  57     }
  58 
  59     public ThresholdNotificationsTest(BlobType btype) {
  60         this.btype = btype;
  61         counter = 0L;
  62         CodeCacheUtils.disableCollectionUsageThresholds();
  63     }
  64 
  65     @Override
  66     public void handleNotification(Notification notification, Object handback) {
  67         String nType = notification.getType();
  68         String poolName
  69                 = CodeCacheUtils.getPoolNameFromNotification(notification);
  70         // consider code cache events only
  71         if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) {
  72             Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
  73                     nType, "Unexpected event received: " + nType);
  74             if (poolName.equals(btype.getMemoryPool().getName())) {
  75                 counter++;
  76             }
  77         }
  78     }
  79 
  80     protected void runTest() {
  81         MemoryPoolMXBean bean = btype.getMemoryPool();
  82         ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
  83                 addNotificationListener(this, null, null);
  84         for (int i = 0; i < Utils.ITERATIONS_COUNT; i++) {
  85             CodeCacheUtils.hitUsageThreshold(bean, btype);
  86         }
  87         Utils.waitForCondition(() -> counter == Utils.ITERATIONS_COUNT);
  88         try {
  89             ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
  90                     removeNotificationListener(this);
  91         } catch (ListenerNotFoundException ex) {
  92             throw new AssertionError("Can't remove notification listener", ex);
  93         }
  94         System.out.printf("INFO: Scenario finished successfully for %s%n",
  95                 bean.getName());
  96     }
  97 }