Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/management/OperatingSystemMXBean/TestTotalSwap.java
38855 views
1
/*
2
* Copyright (c) 2003, 2015, 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
*
29
* @library /lib/testlibrary
30
* @build TestTotalSwap jdk.testlibrary.*
31
* @run main TestTotalSwap
32
*
33
* @author Steve Bohne
34
* @author Jaroslav Bachorik
35
*/
36
37
/*
38
* This test tests the actual swap size on linux and solaris.
39
* The correct value should be checked manually:
40
* Solaris:
41
* 1. In a shell, enter the command: "swap -l"
42
* 2. The value (reported in blocks) is in the "blocks" column.
43
* Linux:
44
* 1. In a shell, enter the command: "cat /proc/meminfo"
45
* 2. The value (reported in bytes) is in "Swap" entry, "total" column.
46
* Windows NT/XP/2000:
47
* 1. Run Start->Accessories->System Tools->System Information.
48
* 2. The value (reported in Kbytes) is in the "Page File Space" entry
49
* Windows 98/ME:
50
* Unknown.
51
*
52
* Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace]
53
*/
54
55
import com.sun.management.OperatingSystemMXBean;
56
import java.lang.management.*;
57
58
import jdk.testlibrary.OSInfo;
59
import jdk.testlibrary.ProcessTools;
60
import jdk.testlibrary.OutputAnalyzer;
61
62
public class TestTotalSwap {
63
64
private static final OperatingSystemMXBean mbean =
65
(com.sun.management.OperatingSystemMXBean)
66
ManagementFactory.getOperatingSystemMXBean();
67
68
// Careful with these values.
69
// Min size for pass dynamically determined below.
70
// zero if no swap space is configured.
71
private static long min_size_for_pass = 0;
72
private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;
73
74
public static void main(String args[]) throws Throwable {
75
76
long expected_swap_size = getSwapSizeFromOs();
77
78
long min_size = mbean.getFreeSwapSpaceSize();
79
if (min_size > 0) {
80
min_size_for_pass = min_size;
81
}
82
83
long size = mbean.getTotalSwapSpaceSize();
84
85
System.out.println("Total swap space size in bytes: " + size);
86
87
if (expected_swap_size > -1) {
88
if (size != expected_swap_size) {
89
throw new RuntimeException("Expected total swap size : " +
90
expected_swap_size +
91
" but getTotalSwapSpaceSize returned: " +
92
size);
93
}
94
}
95
96
// sanity check
97
if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {
98
throw new RuntimeException("Total swap space size " +
99
"illegal value: " + size + " bytes " +
100
"(MIN = " + min_size_for_pass + "; " +
101
"MAX = " + MAX_SIZE_FOR_PASS + ")");
102
}
103
104
System.out.println("Test passed.");
105
}
106
107
private static long getSwapSizeFromOs() throws Throwable {
108
OSInfo.OSType os = OSInfo.getOSType();
109
110
switch (os) {
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
116
case LINUX: {
117
String swapSizeStr = ProcessTools.executeCommand("free", "-b")
118
.firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1);
119
return Long.parseLong(swapSizeStr);
120
}
121
case SOLARIS: {
122
// swapfile dev swaplo blocks free
123
// /dev/dsk/c0t0d0s1 136,1 16 1638608 1600528
124
OutputAnalyzer out= ProcessTools.executeCommand(
125
"/usr/sbin/swap",
126
"-l"
127
);
128
129
long swapSize = 0;
130
131
for (String line : out.asLines()) {
132
if (line.contains("swapfile")) continue;
133
134
String[] vals = line.split("\\s+");
135
if (vals.length == 5) {
136
swapSize += Long.parseLong(vals[3]) * 512; // size is reported in 512b blocks
137
}
138
}
139
140
return swapSize;
141
}
142
case MACOSX: {
143
// total = 8192.00M used = 7471.11M free = 720.89M (encrypted)
144
String swapSizeStr = ProcessTools.executeCommand(
145
"/usr/sbin/sysctl",
146
"-n",
147
"vm.swapusage"
148
).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1);
149
if (swapSizeStr.toLowerCase().endsWith("m")) {
150
swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1);
151
return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB
152
}
153
return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024);
154
}
155
default: {
156
System.err.println("Unsupported operating system: " + os);
157
}
158
}
159
160
return -1;
161
}
162
}
163
164