Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/com/sun/management/OperatingSystemMXBean/TestTotalSwap.java
66645 views
1
/*
2
* Copyright (c) 2003, 2022, 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
* @bug 4858522
27
* @summary Basic unit test of OperatingSystemMXBean.getTotalSwapSpaceSize()
28
* @author Steve Bohne
29
* @author Jaroslav Bachorik
30
*
31
* @library /test/lib
32
*
33
* @run main TestTotalSwap
34
*/
35
36
/*
37
* This test tests the actual swap size on Linux and MacOS only.
38
*/
39
40
import com.sun.management.OperatingSystemMXBean;
41
import java.lang.management.*;
42
43
import jdk.test.lib.Platform;
44
import jdk.test.lib.process.ProcessTools;
45
import jdk.test.lib.process.OutputAnalyzer;
46
47
public class TestTotalSwap {
48
49
private static final OperatingSystemMXBean mbean =
50
(com.sun.management.OperatingSystemMXBean)
51
ManagementFactory.getOperatingSystemMXBean();
52
53
// Careful with these values.
54
// Min size for pass dynamically determined below.
55
// zero if no swap space is configured.
56
private static long min_size_for_pass = 0;
57
private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;
58
59
public static void main(String args[]) throws Throwable {
60
61
// yocto might ignore the request to report swap size in bytes
62
boolean swapInKB = mbean.getVersion().contains("yocto");
63
64
long min_size = mbean.getFreeSwapSpaceSize();
65
if (min_size > 0) {
66
min_size_for_pass = min_size;
67
}
68
69
long expected_swap_size = getSwapSizeFromOs();
70
long size = mbean.getTotalSwapSpaceSize();
71
72
System.out.println("Total swap space size from OS in bytes: " + expected_swap_size);
73
System.out.println("Total swap space size in MBean bytes: " + size);
74
75
// if swap data from OS chnaged re-read OS and MBean data
76
while (expected_swap_size != getSwapSizeFromOs()) {
77
System.out.println("Total swap space reported by OS changed form " + expected_swap_size
78
+ " current value = " + getSwapSizeFromOs());
79
expected_swap_size = getSwapSizeFromOs();
80
size = mbean.getTotalSwapSpaceSize();
81
82
System.out.println("Re-read total swap space size from OS in bytes: " + expected_swap_size);
83
System.out.println("Total swap space size in MBean bytes: " + size);
84
}
85
86
if (expected_swap_size > -1) {
87
if (size != expected_swap_size) {
88
// try the expected size in kiloBytes
89
if (!(swapInKB && expected_swap_size * 1024 == size)) {
90
throw new RuntimeException("Expected total swap size : " +
91
expected_swap_size +
92
" but getTotalSwapSpaceSize returned: " +
93
size);
94
}
95
}
96
}
97
98
// sanity check
99
if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {
100
throw new RuntimeException("Total swap space size " +
101
"illegal value: " + size + " bytes " +
102
"(MIN = " + min_size_for_pass + "; " +
103
"MAX = " + MAX_SIZE_FOR_PASS + ")");
104
}
105
106
System.out.println("Test passed.");
107
}
108
109
private static long getSwapSizeFromOs() throws Throwable {
110
if (Platform.isLinux()) {
111
// total used free shared buffers cached
112
// Mem: 16533540864 13638467584 2895073280 534040576 1630248960 6236909568
113
// -/+ buffers/cache: 5771309056 10762231808
114
// Swap: 15999168512 0 15999168512
115
String swapSizeStr = ProcessTools.executeCommand("free", "-b")
116
.firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1);
117
return Long.parseLong(swapSizeStr);
118
} else if (Platform.isOSX()) {
119
// total = 8192.00M used = 7471.11M free = 720.89M (encrypted)
120
String swapSizeStr = ProcessTools.executeCommand(
121
"/usr/sbin/sysctl",
122
"-n",
123
"vm.swapusage"
124
).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1);
125
if (swapSizeStr.toLowerCase().endsWith("m")) {
126
swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1);
127
return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB
128
}
129
return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024);
130
} else {
131
System.err.println("Unsupported operating system: " + Platform.getOsName());
132
}
133
134
return -1;
135
}
136
}
137
138