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