Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/codecache/jmx/PoolsIndependenceTest.java
41153 views
1
/*
2
* Copyright (c) 2014, 2021, 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
* @modules java.base/jdk.internal.misc
28
* java.management
29
* @library /test/lib /
30
*
31
* @build sun.hotspot.WhiteBox
32
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
33
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
34
* -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing
35
* -XX:+SegmentedCodeCache
36
* compiler.codecache.jmx.PoolsIndependenceTest
37
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
38
* -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -XX:-MethodFlushing
39
* -XX:-SegmentedCodeCache
40
* compiler.codecache.jmx.PoolsIndependenceTest
41
*/
42
43
package compiler.codecache.jmx;
44
45
import jdk.test.lib.Asserts;
46
import jdk.test.lib.Utils;
47
import sun.hotspot.code.BlobType;
48
49
import javax.management.ListenerNotFoundException;
50
import javax.management.Notification;
51
import javax.management.NotificationEmitter;
52
import javax.management.NotificationListener;
53
import java.lang.management.ManagementFactory;
54
import java.lang.management.MemoryNotificationInfo;
55
import java.lang.management.MemoryPoolMXBean;
56
import java.util.HashMap;
57
import java.util.Map;
58
import java.util.concurrent.atomic.AtomicInteger;
59
60
import jtreg.SkippedException;
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
private volatile long maxUsageRegistered;
68
private final long TEST_TIMEOUT_LIMIT = System.currentTimeMillis() +
69
Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - 5_000; // 5 seconds allowance is arbitrary
70
71
public PoolsIndependenceTest(BlobType btype) {
72
counters = new HashMap<>();
73
for (BlobType bt : BlobType.getAvailable()) {
74
counters.put(bt.getMemoryPool().getName(), new AtomicInteger(0));
75
}
76
this.btype = btype;
77
lastEventTimestamp = 0;
78
maxUsageRegistered = 0;
79
CodeCacheUtils.disableCollectionUsageThresholds();
80
}
81
82
public static void main(String[] args) {
83
for (BlobType bt : BlobType.getAvailable()) {
84
new PoolsIndependenceTest(bt).runTest();
85
}
86
}
87
88
protected void runTest() {
89
MemoryPoolMXBean bean = btype.getMemoryPool();
90
System.out.printf("INFO: Starting scenario with %s%n", bean.getName());
91
((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
92
addNotificationListener(this, null, null);
93
final long usageThresholdLimit = bean.getUsage().getUsed() + 1;
94
bean.setUsageThreshold(usageThresholdLimit);
95
96
long beginTimestamp = System.currentTimeMillis();
97
final long phaseTimeout = Math.min(TEST_TIMEOUT_LIMIT,
98
beginTimestamp + 20_000); // 20 seconds is enought for everybody.
99
100
CodeCacheUtils.WB.allocateCodeBlob(
101
CodeCacheUtils.ALLOCATION_SIZE, btype.id);
102
CodeCacheUtils.WB.fullGC();
103
104
/* waiting for expected event to be received plus double the time took
105
to receive expected event(for possible unexpected) and
106
plus 1 second in case expected event received (almost)immediately */
107
Utils.waitForCondition(() -> {
108
maxUsageRegistered = Math.max(bean.getUsage().getUsed(), maxUsageRegistered);
109
long currentTimestamp = System.currentTimeMillis();
110
int eventsCount
111
= counters.get(btype.getMemoryPool().getName()).get();
112
if (eventsCount > 0) {
113
if (eventsCount > 1) {
114
return true;
115
}
116
long timeLastEventTook
117
= lastEventTimestamp - beginTimestamp;
118
119
long awaitForUnexpectedTimeout
120
= 1000L + beginTimestamp + 3L * timeLastEventTook;
121
122
return currentTimestamp > Math.min(phaseTimeout, awaitForUnexpectedTimeout);
123
};
124
125
if (currentTimestamp > phaseTimeout) {
126
if (maxUsageRegistered < usageThresholdLimit) {
127
throw new SkippedException("The code cache usage hasn't exceeded" +
128
" the limit of " + usageThresholdLimit +
129
" (max usage reached is " + maxUsageRegistered + ")" +
130
" within test timeouts, can't test notifications");
131
} else {
132
Asserts.fail("UsageThresholdLimit was set to " + usageThresholdLimit +
133
", max usage of " + maxUsageRegistered + " have been registered" +
134
", but no notifications issued");
135
}
136
}
137
return false;
138
});
139
for (BlobType bt : BlobType.getAvailable()) {
140
int expectedNotificationsAmount = bt.equals(btype) ? 1 : 0;
141
CodeCacheUtils.assertEQorGTE(btype, counters.get(bt.getMemoryPool().getName()).get(),
142
expectedNotificationsAmount, String.format("Unexpected "
143
+ "amount of notifications for pool: %s",
144
bt.getMemoryPool().getName()));
145
}
146
try {
147
((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
148
removeNotificationListener(this);
149
} catch (ListenerNotFoundException ex) {
150
throw new AssertionError("Can't remove notification listener", ex);
151
}
152
System.out.printf("INFO: Scenario with %s finished%n", bean.getName());
153
}
154
155
@Override
156
public void handleNotification(Notification notification, Object handback) {
157
String nType = notification.getType();
158
String poolName
159
= CodeCacheUtils.getPoolNameFromNotification(notification);
160
// consider code cache events only
161
if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) {
162
Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
163
nType, "Unexpected event received: " + nType);
164
// receiving events from available CodeCache-related beans only
165
if (counters.get(poolName) != null) {
166
counters.get(poolName).incrementAndGet();
167
lastEventTimestamp = System.currentTimeMillis();
168
}
169
}
170
}
171
}
172
173