Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jtreg-ext/requires/VMProps.java
40948 views
1
/*
2
* Copyright (c) 2016, 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
package requires;
25
26
import java.io.BufferedInputStream;
27
import java.io.FileInputStream;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.nio.file.Files;
31
import java.nio.file.Path;
32
import java.nio.file.Paths;
33
import java.nio.file.StandardOpenOption;
34
import java.util.ArrayList;
35
import java.util.Collections;
36
import java.util.HashMap;
37
import java.util.List;
38
import java.util.Map;
39
import java.util.Properties;
40
import java.util.Set;
41
import java.util.concurrent.Callable;
42
import java.util.concurrent.TimeUnit;
43
import java.util.function.Supplier;
44
import java.util.regex.Matcher;
45
import java.util.regex.Pattern;
46
47
import sun.hotspot.code.Compiler;
48
import sun.hotspot.cpuinfo.CPUInfo;
49
import sun.hotspot.gc.GC;
50
import sun.hotspot.WhiteBox;
51
import jdk.test.lib.Platform;
52
import jdk.test.lib.Container;
53
54
/**
55
* The Class to be invoked by jtreg prior Test Suite execution to
56
* collect information about VM.
57
* Do not use any APIs that may not be available in all target VMs.
58
* Properties set by this Class will be available in the @requires expressions.
59
*/
60
public class VMProps implements Callable<Map<String, String>> {
61
// value known to jtreg as an indicator of error state
62
private static final String ERROR_STATE = "__ERROR__";
63
64
private static final WhiteBox WB = WhiteBox.getWhiteBox();
65
66
private static class SafeMap {
67
private final Map<String, String> map = new HashMap<>();
68
69
public void put(String key, Supplier<String> s) {
70
String value;
71
try {
72
value = s.get();
73
} catch (Throwable t) {
74
System.err.println("failed to get value for " + key);
75
t.printStackTrace(System.err);
76
value = ERROR_STATE + t;
77
}
78
map.put(key, value);
79
}
80
}
81
82
/**
83
* Collects information about VM properties.
84
* This method will be invoked by jtreg.
85
*
86
* @return Map of property-value pairs.
87
*/
88
@Override
89
public Map<String, String> call() {
90
SafeMap map = new SafeMap();
91
map.put("vm.flavor", this::vmFlavor);
92
map.put("vm.compMode", this::vmCompMode);
93
map.put("vm.bits", this::vmBits);
94
map.put("vm.flightRecorder", this::vmFlightRecorder);
95
map.put("vm.simpleArch", this::vmArch);
96
map.put("vm.debug", this::vmDebug);
97
map.put("vm.jvmci", this::vmJvmci);
98
map.put("vm.emulatedClient", this::vmEmulatedClient);
99
// vm.hasSA is "true" if the VM contains the serviceability agent
100
// and jhsdb.
101
map.put("vm.hasSA", this::vmHasSA);
102
// vm.hasJFR is "true" if JFR is included in the build of the VM and
103
// so tests can be executed.
104
map.put("vm.hasJFR", this::vmHasJFR);
105
map.put("vm.jvmti", this::vmHasJVMTI);
106
map.put("vm.cpu.features", this::cpuFeatures);
107
map.put("vm.pageSize", this::vmPageSize);
108
map.put("vm.rtm.cpu", this::vmRTMCPU);
109
map.put("vm.rtm.compiler", this::vmRTMCompiler);
110
// vm.cds is true if the VM is compiled with cds support.
111
map.put("vm.cds", this::vmCDS);
112
map.put("vm.cds.custom.loaders", this::vmCDSForCustomLoaders);
113
map.put("vm.cds.archived.java.heap", this::vmCDSForArchivedJavaHeap);
114
// vm.graal.enabled is true if Graal is used as JIT
115
map.put("vm.graal.enabled", this::isGraalEnabled);
116
map.put("vm.compiler1.enabled", this::isCompiler1Enabled);
117
map.put("vm.compiler2.enabled", this::isCompiler2Enabled);
118
map.put("docker.support", this::dockerSupport);
119
map.put("vm.musl", this::isMusl);
120
map.put("release.implementor", this::implementor);
121
map.put("jdk.containerized", this::jdkContainerized);
122
map.put("vm.flagless", this::isFlagless);
123
vmGC(map); // vm.gc.X = true/false
124
vmOptFinalFlags(map);
125
126
dump(map.map);
127
return map.map;
128
}
129
130
/**
131
* Print a stack trace before returning error state;
132
* Used by the various helper functions which parse information from
133
* VM properties in the case where they don't find an expected property
134
* or a property doesn't conform to an expected format.
135
*
136
* @return {@link #ERROR_STATE}
137
*/
138
private String errorWithMessage(String message) {
139
new Exception(message).printStackTrace();
140
return ERROR_STATE + message;
141
}
142
143
/**
144
* @return vm.simpleArch value of "os.simpleArch" property of tested JDK.
145
*/
146
protected String vmArch() {
147
String arch = System.getProperty("os.arch");
148
if (arch.equals("x86_64") || arch.equals("amd64")) {
149
return "x64";
150
} else if (arch.contains("86")) {
151
return "x86";
152
} else {
153
return arch;
154
}
155
}
156
157
/**
158
* @return VM type value extracted from the "java.vm.name" property.
159
*/
160
protected String vmFlavor() {
161
// E.g. "Java HotSpot(TM) 64-Bit Server VM"
162
String vmName = System.getProperty("java.vm.name");
163
if (vmName == null) {
164
return errorWithMessage("Can't get 'java.vm.name' property");
165
}
166
167
Pattern startP = Pattern.compile(".* (\\S+) VM");
168
Matcher m = startP.matcher(vmName);
169
if (m.matches()) {
170
return m.group(1).toLowerCase();
171
}
172
return errorWithMessage("Can't get VM flavor from 'java.vm.name'");
173
}
174
175
/**
176
* @return VM compilation mode extracted from the "java.vm.info" property.
177
*/
178
protected String vmCompMode() {
179
// E.g. "mixed mode"
180
String vmInfo = System.getProperty("java.vm.info");
181
if (vmInfo == null) {
182
return errorWithMessage("Can't get 'java.vm.info' property");
183
}
184
vmInfo = vmInfo.toLowerCase();
185
if (vmInfo.contains("mixed mode")) {
186
return "Xmixed";
187
} else if (vmInfo.contains("compiled mode")) {
188
return "Xcomp";
189
} else if (vmInfo.contains("interpreted mode")) {
190
return "Xint";
191
} else {
192
return errorWithMessage("Can't get compilation mode from 'java.vm.info'");
193
}
194
}
195
196
/**
197
* @return VM bitness, the value of the "sun.arch.data.model" property.
198
*/
199
protected String vmBits() {
200
String dataModel = System.getProperty("sun.arch.data.model");
201
if (dataModel != null) {
202
return dataModel;
203
} else {
204
return errorWithMessage("Can't get 'sun.arch.data.model' property");
205
}
206
}
207
208
/**
209
* @return "true" if Flight Recorder is enabled, "false" if is disabled.
210
*/
211
protected String vmFlightRecorder() {
212
Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder");
213
String startFROptions = WB.getStringVMFlag("StartFlightRecording");
214
if (isFlightRecorder != null && isFlightRecorder) {
215
return "true";
216
}
217
if (startFROptions != null && !startFROptions.isEmpty()) {
218
return "true";
219
}
220
return "false";
221
}
222
223
/**
224
* @return debug level value extracted from the "jdk.debug" property.
225
*/
226
protected String vmDebug() {
227
String debug = System.getProperty("jdk.debug");
228
if (debug != null) {
229
return "" + debug.contains("debug");
230
} else {
231
return errorWithMessage("Can't get 'jdk.debug' property");
232
}
233
}
234
235
/**
236
* @return true if VM supports JVMCI and false otherwise
237
*/
238
protected String vmJvmci() {
239
// builds with jvmci have this flag
240
if (WB.getBooleanVMFlag("EnableJVMCI") == null) {
241
return "false";
242
}
243
244
// Not all GCs have full JVMCI support
245
if (!WB.isJVMCISupportedByGC()) {
246
return "false";
247
}
248
249
// Interpreted mode cannot enable JVMCI
250
if (vmCompMode().equals("Xint")) {
251
return "false";
252
}
253
254
return "true";
255
}
256
257
/**
258
* @return true if VM runs in emulated-client mode and false otherwise.
259
*/
260
protected String vmEmulatedClient() {
261
String vmInfo = System.getProperty("java.vm.info");
262
if (vmInfo == null) {
263
return errorWithMessage("Can't get 'java.vm.info' property");
264
}
265
return "" + vmInfo.contains(" emulated-client");
266
}
267
268
/**
269
* @return supported CPU features
270
*/
271
protected String cpuFeatures() {
272
return CPUInfo.getFeatures().toString();
273
}
274
275
/**
276
* For all existing GC sets vm.gc.X property.
277
* Example vm.gc.G1=true means:
278
* VM supports G1
279
* User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC
280
* G1 can be selected, i.e. it doesn't conflict with other VM flags
281
*
282
* @param map - property-value pairs
283
*/
284
protected void vmGC(SafeMap map) {
285
var isJVMCIEnabled = Compiler.isJVMCIEnabled();
286
for (GC gc: GC.values()) {
287
map.put("vm.gc." + gc.name(),
288
() -> "" + (gc.isSupported()
289
&& (!isJVMCIEnabled || gc.isSupportedByJVMCICompiler())
290
&& (gc.isSelected() || GC.isSelectedErgonomically())));
291
}
292
}
293
294
/**
295
* Selected final flag.
296
*
297
* @param map - property-value pairs
298
* @param flagName - flag name
299
*/
300
private void vmOptFinalFlag(SafeMap map, String flagName) {
301
map.put("vm.opt.final." + flagName,
302
() -> String.valueOf(WB.getBooleanVMFlag(flagName)));
303
}
304
305
/**
306
* Selected sets of final flags.
307
*
308
* @param map - property-value pairs
309
*/
310
protected void vmOptFinalFlags(SafeMap map) {
311
vmOptFinalFlag(map, "ClassUnloading");
312
vmOptFinalFlag(map, "ClassUnloadingWithConcurrentMark");
313
vmOptFinalFlag(map, "UseCompressedOops");
314
vmOptFinalFlag(map, "EnableJVMCI");
315
vmOptFinalFlag(map, "EliminateAllocations");
316
vmOptFinalFlag(map, "UseVtableBasedCHA");
317
}
318
319
/**
320
* @return "true" if VM has a serviceability agent.
321
*/
322
protected String vmHasSA() {
323
return "" + Platform.hasSA();
324
}
325
326
/**
327
* @return "true" if the VM is compiled with Java Flight Recorder (JFR)
328
* support.
329
*/
330
protected String vmHasJFR() {
331
return "" + WB.isJFRIncluded();
332
}
333
334
/**
335
* @return "true" if the VM is compiled with JVMTI
336
*/
337
protected String vmHasJVMTI() {
338
return "" + WB.isJVMTIIncluded();
339
}
340
341
/**
342
* @return true if compiler in use supports RTM and false otherwise.
343
*/
344
protected String vmRTMCompiler() {
345
boolean isRTMCompiler = false;
346
347
if (Compiler.isC2Enabled() &&
348
(Platform.isX86() || Platform.isX64() || Platform.isPPC())) {
349
isRTMCompiler = true;
350
}
351
return "" + isRTMCompiler;
352
}
353
354
/**
355
* @return true if VM runs RTM supported CPU and false otherwise.
356
*/
357
protected String vmRTMCPU() {
358
return "" + CPUInfo.hasFeature("rtm");
359
}
360
361
/**
362
* Check for CDS support.
363
*
364
* @return true if CDS is supported by the VM to be tested.
365
*/
366
protected String vmCDS() {
367
return "" + WB.isCDSIncluded();
368
}
369
370
/**
371
* Check for CDS support for custom loaders.
372
*
373
* @return true if CDS provides support for customer loader in the VM to be tested.
374
*/
375
protected String vmCDSForCustomLoaders() {
376
return "" + ("true".equals(vmCDS()) && Platform.areCustomLoadersSupportedForCDS());
377
}
378
379
/**
380
* Check for CDS support for archived Java heap regions.
381
*
382
* @return true if CDS provides support for archive Java heap regions in the VM to be tested.
383
*/
384
protected String vmCDSForArchivedJavaHeap() {
385
return "" + ("true".equals(vmCDS()) && WB.isJavaHeapArchiveSupported());
386
}
387
388
/**
389
* @return System page size in bytes.
390
*/
391
protected String vmPageSize() {
392
return "" + WB.getVMPageSize();
393
}
394
395
/**
396
* Check if Graal is used as JIT compiler.
397
*
398
* @return true if Graal is used as JIT compiler.
399
*/
400
protected String isGraalEnabled() {
401
return "" + Compiler.isGraalEnabled();
402
}
403
404
/**
405
* Check if Compiler1 is present.
406
*
407
* @return true if Compiler1 is used as JIT compiler, either alone or as part of the tiered system.
408
*/
409
protected String isCompiler1Enabled() {
410
return "" + Compiler.isC1Enabled();
411
}
412
413
/**
414
* Check if Compiler2 is present.
415
*
416
* @return true if Compiler2 is used as JIT compiler, either alone or as part of the tiered system.
417
*/
418
protected String isCompiler2Enabled() {
419
return "" + Compiler.isC2Enabled();
420
}
421
422
/**
423
* A simple check for docker support
424
*
425
* @return true if docker is supported in a given environment
426
*/
427
protected String dockerSupport() {
428
boolean isSupported = false;
429
if (Platform.isLinux()) {
430
// currently docker testing is only supported for Linux,
431
// on certain platforms
432
433
String arch = System.getProperty("os.arch");
434
435
if (Platform.isX64()) {
436
isSupported = true;
437
} else if (Platform.isAArch64()) {
438
isSupported = true;
439
} else if (Platform.isS390x()) {
440
isSupported = true;
441
} else if (arch.equals("ppc64le")) {
442
isSupported = true;
443
}
444
}
445
446
if (isSupported) {
447
try {
448
isSupported = checkDockerSupport();
449
} catch (Exception e) {
450
isSupported = false;
451
}
452
}
453
454
return "" + isSupported;
455
}
456
457
private boolean checkDockerSupport() throws IOException, InterruptedException {
458
ProcessBuilder pb = new ProcessBuilder(Container.ENGINE_COMMAND, "ps");
459
Process p = pb.start();
460
p.waitFor(10, TimeUnit.SECONDS);
461
462
return (p.exitValue() == 0);
463
}
464
465
/**
466
* Checks musl libc.
467
*
468
* @return true if musl libc is used.
469
*/
470
protected String isMusl() {
471
return Boolean.toString(WB.getLibcName().contains("musl"));
472
}
473
474
private String implementor() {
475
try (InputStream in = new BufferedInputStream(new FileInputStream(
476
System.getProperty("java.home") + "/release"))) {
477
Properties properties = new Properties();
478
properties.load(in);
479
String implementorProperty = properties.getProperty("IMPLEMENTOR");
480
if (implementorProperty != null) {
481
return implementorProperty.replace("\"", "");
482
}
483
return errorWithMessage("Can't get 'IMPLEMENTOR' property from 'release' file");
484
} catch (IOException e) {
485
e.printStackTrace();
486
return errorWithMessage("Failed to read 'release' file " + e);
487
}
488
}
489
490
private String jdkContainerized() {
491
String isEnabled = System.getenv("TEST_JDK_CONTAINERIZED");
492
return "" + "true".equalsIgnoreCase(isEnabled);
493
}
494
495
/**
496
* Checks if we are in <i>almost</i> out-of-box configuration, i.e. the flags
497
* which JVM is started with don't affect its behavior "significantly".
498
* {@code TEST_VM_FLAGLESS} enviroment variable can be used to force this
499
* method to return true and allow any flags.
500
*
501
* @return true if there are no JVM flags
502
*/
503
private String isFlagless() {
504
boolean result = true;
505
if (System.getenv("TEST_VM_FLAGLESS") != null) {
506
return "" + result;
507
}
508
509
List<String> allFlags = new ArrayList<String>();
510
Collections.addAll(allFlags, System.getProperty("test.vm.opts", "").trim().split("\\s+"));
511
Collections.addAll(allFlags, System.getProperty("test.java.opts", "").trim().split("\\s+"));
512
513
// check -XX flags
514
var ignoredXXFlags = Set.of(
515
// added by run-test framework
516
"MaxRAMPercentage",
517
// added by test environment
518
"CreateCoredumpOnCrash"
519
);
520
result &= allFlags.stream()
521
.filter(s -> s.startsWith("-XX:"))
522
// map to names:
523
// remove -XX:
524
.map(s -> s.substring(4))
525
// remove +/- from bool flags
526
.map(s -> s.charAt(0) == '+' || s.charAt(0) == '-' ? s.substring(1) : s)
527
// remove =.* from others
528
.map(s -> s.contains("=") ? s.substring(0, s.indexOf('=')) : s)
529
// skip known-to-be-there flags
530
.filter(s -> !ignoredXXFlags.contains(s))
531
.findAny()
532
.isEmpty();
533
534
// check -X flags
535
var ignoredXFlags = Set.of(
536
// default, yet still seen to be explicitly set
537
"mixed"
538
);
539
result &= allFlags.stream()
540
.filter(s -> s.startsWith("-X") && !s.startsWith("-XX:"))
541
// map to names:
542
// remove -X
543
.map(s -> s.substring(2))
544
// remove :.* from flags with values
545
.map(s -> s.contains(":") ? s.substring(0, s.indexOf(':')) : s)
546
// skip known-to-be-there flags
547
.filter(s -> !ignoredXFlags.contains(s))
548
.findAny()
549
.isEmpty();
550
551
return "" + result;
552
}
553
554
/**
555
* Dumps the map to the file if the file name is given as the property.
556
* This functionality could be helpful to know context in the real
557
* execution.
558
*
559
* @param map
560
*/
561
protected static void dump(Map<String, String> map) {
562
String dumpFileName = System.getProperty("vmprops.dump");
563
if (dumpFileName == null) {
564
return;
565
}
566
List<String> lines = new ArrayList<>();
567
map.forEach((k, v) -> lines.add(k + ":" + v));
568
try {
569
Files.write(Paths.get(dumpFileName), lines,
570
StandardOpenOption.APPEND, StandardOpenOption.CREATE);
571
} catch (IOException e) {
572
throw new RuntimeException("Failed to dump properties into '"
573
+ dumpFileName + "'", e);
574
}
575
}
576
577
/**
578
* This method is for the testing purpose only.
579
*
580
* @param args
581
*/
582
public static void main(String args[]) {
583
Map<String, String> map = new VMProps().call();
584
map.forEach((k, v) -> System.out.println(k + ": '" + v + "'"));
585
}
586
}
587
588