Path: blob/master/test/jdk/com/sun/management/OperatingSystemMXBean/TestTotalSwap.java
41153 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 485852226* @summary Basic unit test of OperatingSystemMXBean.getTotalSwapSpaceSize()27* @author Steve Bohne28* @author Jaroslav Bachorik29*30* @library /test/lib31*32* @run main TestTotalSwap33*/3435/*36* This test tests the actual swap size on linux and solaris.37* The correct value should be checked manually:38* Solaris:39* 1. In a shell, enter the command: "swap -l"40* 2. The value (reported in blocks) is in the "blocks" column.41* Linux:42* 1. In a shell, enter the command: "cat /proc/meminfo"43* 2. The value (reported in bytes) is in "Swap" entry, "total" column.44* Windows NT/XP/2000:45* 1. Run Start->Accessories->System Tools->System Information.46* 2. The value (reported in Kbytes) is in the "Page File Space" entry47* Windows 98/ME:48* Unknown.49*50* Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace]51*/5253import com.sun.management.OperatingSystemMXBean;54import java.lang.management.*;5556import jdk.test.lib.Platform;57import jdk.test.lib.process.ProcessTools;58import jdk.test.lib.process.OutputAnalyzer;5960public class TestTotalSwap {6162private static final OperatingSystemMXBean mbean =63(com.sun.management.OperatingSystemMXBean)64ManagementFactory.getOperatingSystemMXBean();6566// Careful with these values.67// Min size for pass dynamically determined below.68// zero if no swap space is configured.69private static long min_size_for_pass = 0;70private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;7172public static void main(String args[]) throws Throwable {73// yocto might ignore the request to report swap size in bytes74boolean swapInKB = mbean.getVersion().contains("yocto");7576long expected_swap_size = getSwapSizeFromOs();7778long min_size = mbean.getFreeSwapSpaceSize();79if (min_size > 0) {80min_size_for_pass = min_size;81}8283long size = mbean.getTotalSwapSpaceSize();8485System.out.println("Total swap space size in bytes: " + size);8687if (expected_swap_size > -1) {88if (size != expected_swap_size) {89// try the expected size in kiloBytes90if (!(swapInKB && expected_swap_size * 1024 == size)) {91throw new RuntimeException("Expected total swap size : " +92expected_swap_size +93" but getTotalSwapSpaceSize returned: " +94size);95}96}97}9899// sanity check100if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {101throw new RuntimeException("Total swap space size " +102"illegal value: " + size + " bytes " +103"(MIN = " + min_size_for_pass + "; " +104"MAX = " + MAX_SIZE_FOR_PASS + ")");105}106107System.out.println("Test passed.");108}109110private static long getSwapSizeFromOs() throws Throwable {111if (Platform.isLinux()) {112// total used free shared buffers cached113// Mem: 16533540864 13638467584 2895073280 534040576 1630248960 6236909568114// -/+ buffers/cache: 5771309056 10762231808115// Swap: 15999168512 0 15999168512116String swapSizeStr = ProcessTools.executeCommand("free", "-b")117.firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1);118return Long.parseLong(swapSizeStr);119} else if (Platform.isOSX()) {120// total = 8192.00M used = 7471.11M free = 720.89M (encrypted)121String swapSizeStr = ProcessTools.executeCommand(122"/usr/sbin/sysctl",123"-n",124"vm.swapusage"125).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1);126if (swapSizeStr.toLowerCase().endsWith("m")) {127swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1);128return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB129}130return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024);131} else {132System.err.println("Unsupported operating system: " + Platform.getOsName());133}134135return -1;136}137}138139140