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