Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/NMT/MallocStressTest.java
32284 views
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
/*
25
* @test
26
* @summary Stress test for malloc tracking
27
* @key nmt jcmd stress
28
* @library /testlibrary /testlibrary/whitebox
29
* @build MallocStressTest
30
* @run main ClassFileInstaller sun.hotspot.WhiteBox
31
* @run main/othervm/timeout=600 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail MallocStressTest
32
*/
33
34
import java.util.concurrent.atomic.AtomicInteger;
35
import java.util.ArrayList;
36
import java.util.List;
37
import java.util.Random;
38
import com.oracle.java.testlibrary.*;
39
import sun.hotspot.WhiteBox;
40
41
public class MallocStressTest {
42
private static int K = 1024;
43
44
// The stress test runs in three phases:
45
// 1. alloc: A lot of malloc with fewer free, which simulates a burst memory allocation
46
// that is usually seen during startup or class loading.
47
// 2. pause: Pause the test to check accuracy of native memory tracking
48
// 3. release: Release all malloc'd memory and check native memory tracking result.
49
public enum TestPhase {
50
alloc,
51
pause,
52
release
53
};
54
55
static TestPhase phase = TestPhase.alloc;
56
57
// malloc'd memory
58
static ArrayList<MallocMemory> mallocd_memory = new ArrayList<MallocMemory>();
59
static long mallocd_total = 0;
60
static WhiteBox whiteBox;
61
static AtomicInteger pause_count = new AtomicInteger();
62
63
static boolean is_64_bit_system;
64
65
private static boolean is_64_bit_system() { return is_64_bit_system; }
66
67
public static void main(String args[]) throws Exception {
68
is_64_bit_system = (Platform.is64bit());
69
70
OutputAnalyzer output;
71
whiteBox = WhiteBox.getWhiteBox();
72
73
// Grab my own PID
74
String pid = Integer.toString(ProcessTools.getProcessId());
75
ProcessBuilder pb = new ProcessBuilder();
76
77
AllocThread[] alloc_threads = new AllocThread[256];
78
ReleaseThread[] release_threads = new ReleaseThread[64];
79
80
int index;
81
// Create many allocation threads
82
for (index = 0; index < alloc_threads.length; index ++) {
83
alloc_threads[index] = new AllocThread();
84
}
85
86
// Fewer release threads
87
for (index = 0; index < release_threads.length; index ++) {
88
release_threads[index] = new ReleaseThread();
89
}
90
91
if (is_64_bit_system()) {
92
sleep_wait(2*60*1000);
93
} else {
94
sleep_wait(60*1000);
95
}
96
// pause the stress test
97
phase = TestPhase.pause;
98
while (pause_count.intValue() < alloc_threads.length + release_threads.length) {
99
sleep_wait(10);
100
}
101
102
long mallocd_total_in_KB = (mallocd_total + K / 2) / K;
103
104
// Now check if the result from NMT matches the total memory allocated.
105
String expected_test_summary = "Test (reserved=" + mallocd_total_in_KB +"KB, committed=" + mallocd_total_in_KB + "KB)";
106
// Run 'jcmd <pid> VM.native_memory summary'
107
pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary"});
108
output = new OutputAnalyzer(pb.start());
109
output.shouldContain(expected_test_summary);
110
111
// Release all allocated memory
112
phase = TestPhase.release;
113
synchronized(mallocd_memory) {
114
mallocd_memory.notifyAll();
115
}
116
117
// Join all threads
118
for (index = 0; index < alloc_threads.length; index ++) {
119
try {
120
alloc_threads[index].join();
121
} catch (InterruptedException e) {
122
}
123
}
124
125
for (index = 0; index < release_threads.length; index ++) {
126
try {
127
release_threads[index].join();
128
} catch (InterruptedException e) {
129
}
130
}
131
132
// All test memory allocated should be released
133
output = new OutputAnalyzer(pb.start());
134
output.shouldNotContain("Test (reserved=");
135
136
// Verify that tracking level has not been downgraded
137
pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "statistics"});
138
output = new OutputAnalyzer(pb.start());
139
output.shouldNotContain("Tracking level has been downgraded due to lack of resources");
140
}
141
142
private static void sleep_wait(int n) {
143
try {
144
Thread.sleep(n);
145
} catch (InterruptedException e) {
146
}
147
}
148
149
150
static class MallocMemory {
151
private long addr;
152
private int size;
153
154
MallocMemory(long addr, int size) {
155
this.addr = addr;
156
this.size = size;
157
}
158
159
long addr() { return this.addr; }
160
int size() { return this.size; }
161
}
162
163
static class AllocThread extends Thread {
164
AllocThread() {
165
this.setName("MallocThread");
166
this.start();
167
}
168
169
// AllocThread only runs "Alloc" phase
170
public void run() {
171
Random random = new Random();
172
while (MallocStressTest.phase == TestPhase.alloc) {
173
int r = Math.abs(random.nextInt());
174
// Only malloc small amount to avoid OOM
175
int size = r % 32;
176
if (is_64_bit_system()) {
177
r = r % 32 * K;
178
} else {
179
r = r % 64;
180
}
181
if (size == 0) size = 1;
182
long addr = MallocStressTest.whiteBox.NMTMallocWithPseudoStack(size, r);
183
if (addr != 0) {
184
MallocMemory mem = new MallocMemory(addr, size);
185
synchronized(MallocStressTest.mallocd_memory) {
186
MallocStressTest.mallocd_memory.add(mem);
187
MallocStressTest.mallocd_total += size;
188
}
189
} else {
190
System.out.println("Out of malloc memory");
191
break;
192
}
193
}
194
MallocStressTest.pause_count.incrementAndGet();
195
}
196
}
197
198
static class ReleaseThread extends Thread {
199
private Random random = new Random();
200
ReleaseThread() {
201
this.setName("ReleaseThread");
202
this.start();
203
}
204
205
public void run() {
206
while(true) {
207
switch(MallocStressTest.phase) {
208
case alloc:
209
slow_release();
210
break;
211
case pause:
212
enter_pause();
213
break;
214
case release:
215
quick_release();
216
return;
217
}
218
}
219
}
220
221
private void enter_pause() {
222
MallocStressTest.pause_count.incrementAndGet();
223
while (MallocStressTest.phase != MallocStressTest.TestPhase.release) {
224
try {
225
synchronized(MallocStressTest.mallocd_memory) {
226
MallocStressTest.mallocd_memory.wait(10);
227
}
228
} catch (InterruptedException e) {
229
}
230
}
231
}
232
233
private void quick_release() {
234
List<MallocMemory> free_list;
235
while (true) {
236
synchronized(MallocStressTest.mallocd_memory) {
237
if (MallocStressTest.mallocd_memory.isEmpty()) return;
238
int size = Math.min(MallocStressTest.mallocd_memory.size(), 5000);
239
List<MallocMemory> subList = MallocStressTest.mallocd_memory.subList(0, size);
240
free_list = new ArrayList<MallocMemory>(subList);
241
subList.clear();
242
}
243
for (int index = 0; index < free_list.size(); index ++) {
244
MallocMemory mem = free_list.get(index);
245
MallocStressTest.whiteBox.NMTFree(mem.addr());
246
}
247
}
248
}
249
250
private void slow_release() {
251
try {
252
Thread.sleep(10);
253
} catch (InterruptedException e) {
254
}
255
synchronized(MallocStressTest.mallocd_memory) {
256
if (MallocStressTest.mallocd_memory.isEmpty()) return;
257
int n = Math.abs(random.nextInt()) % MallocStressTest.mallocd_memory.size();
258
MallocMemory mem = mallocd_memory.remove(n);
259
MallocStressTest.whiteBox.NMTFree(mem.addr());
260
MallocStressTest.mallocd_total -= mem.size();
261
}
262
}
263
}
264
}
265
266