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 PoolsIndependenceTest
  26  * @summary testing of getUsageThreshold()
  27  * @requires vm.flavor != "minimal"
  28  * @modules java.base/jdk.internal.misc
  29  *          java.management
  30  * @library /test/lib /
  31  *
  32  * @build sun.hotspot.WhiteBox
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  34  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  36  *     -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing
  37  *     -XX:+SegmentedCodeCache
  38  *     compiler.codecache.jmx.PoolsIndependenceTest
  39  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  40  *     -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing
  41  *     -XX:-SegmentedCodeCache
  42  *     compiler.codecache.jmx.PoolsIndependenceTest
  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 import java.util.HashMap;
  59 import java.util.Map;
  60 import java.util.concurrent.atomic.AtomicInteger;
  61 
  62 public class PoolsIndependenceTest implements NotificationListener {
  63 
  64     private final Map<String, AtomicInteger> counters;
  65     private final BlobType btype;
  66     private volatile long lastEventTimestamp;
  67 
  68     public PoolsIndependenceTest(BlobType btype) {
  69         counters = new HashMap<>();
  70         for (BlobType bt : BlobType.getAvailable()) {
  71             counters.put(bt.getMemoryPool().getName(), new AtomicInteger(0));
  72         }
  73         this.btype = btype;
  74         lastEventTimestamp = 0;
  75         CodeCacheUtils.disableCollectionUsageThresholds();
  76     }
  77 
  78     public static void main(String[] args) {
  79         for (BlobType bt : BlobType.getAvailable()) {
  80             new PoolsIndependenceTest(bt).runTest();
  81         }
  82     }
  83 
  84     protected void runTest() {
  85         MemoryPoolMXBean bean = btype.getMemoryPool();
  86         ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
  87                 addNotificationListener(this, null, null);
  88         bean.setUsageThreshold(bean.getUsage().getUsed() + 1);
  89         long beginTimestamp = System.currentTimeMillis();
  90         CodeCacheUtils.WB.allocateCodeBlob(
  91                 CodeCacheUtils.ALLOCATION_SIZE, btype.id);
  92         CodeCacheUtils.WB.fullGC();
  93         /* waiting for expected event to be received plus double the time took
  94          to receive expected event(for possible unexpected) and
  95          plus 1 second in case expected event received (almost)immediately */
  96         Utils.waitForCondition(() -> {
  97             long currentTimestamp = System.currentTimeMillis();
  98             int eventsCount
  99                     = counters.get(btype.getMemoryPool().getName()).get();
 100             if (eventsCount > 0) {
 101                 if (eventsCount > 1) {
 102                     return true;
 103                 }
 104                 long timeLastEventTook
 105                         = beginTimestamp - lastEventTimestamp;
 106                 long timeoutValue
 107                         = 1000L + beginTimestamp + 3L * timeLastEventTook;
 108                 return currentTimestamp > timeoutValue;
 109             }
 110             return false;
 111         });
 112         for (BlobType bt : BlobType.getAvailable()) {
 113             int expectedNotificationsAmount = bt.equals(btype) ? 1 : 0;
 114             CodeCacheUtils.assertEQorGTE(btype, counters.get(bt.getMemoryPool().getName()).get(),
 115                     expectedNotificationsAmount, String.format("Unexpected "
 116                             + "amount of notifications for pool: %s",
 117                             bt.getMemoryPool().getName()));
 118         }
 119         try {
 120             ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
 121                     removeNotificationListener(this);
 122         } catch (ListenerNotFoundException ex) {
 123             throw new AssertionError("Can't remove notification listener", ex);
 124         }
 125         System.out.printf("INFO: Scenario with %s finished%n", bean.getName());
 126     }
 127 
 128     @Override
 129     public void handleNotification(Notification notification, Object handback) {
 130         String nType = notification.getType();
 131         String poolName
 132                 = CodeCacheUtils.getPoolNameFromNotification(notification);
 133         // consider code cache events only
 134         if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) {
 135             Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
 136                     nType, "Unexpected event received: " + nType);
 137             // receiving events from available CodeCache-related beans only
 138             if (counters.get(poolName) != null) {
 139                 counters.get(poolName).incrementAndGet();
 140                 lastEventTimestamp = System.currentTimeMillis();
 141             }
 142         }
 143     }
 144 }