Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/management/OperatingSystemMXBean/TestTotalSwap.java
38855 views
/*1* Copyright (c) 2003, 2015, 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*28* @library /lib/testlibrary29* @build TestTotalSwap jdk.testlibrary.*30* @run main TestTotalSwap31*32* @author Steve Bohne33* @author Jaroslav Bachorik34*/3536/*37* This test tests the actual swap size on linux and solaris.38* The correct value should be checked manually:39* Solaris:40* 1. In a shell, enter the command: "swap -l"41* 2. The value (reported in blocks) is in the "blocks" column.42* Linux:43* 1. In a shell, enter the command: "cat /proc/meminfo"44* 2. The value (reported in bytes) is in "Swap" entry, "total" column.45* Windows NT/XP/2000:46* 1. Run Start->Accessories->System Tools->System Information.47* 2. The value (reported in Kbytes) is in the "Page File Space" entry48* Windows 98/ME:49* Unknown.50*51* Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace]52*/5354import com.sun.management.OperatingSystemMXBean;55import java.lang.management.*;5657import jdk.testlibrary.OSInfo;58import jdk.testlibrary.ProcessTools;59import jdk.testlibrary.OutputAnalyzer;6061public class TestTotalSwap {6263private static final OperatingSystemMXBean mbean =64(com.sun.management.OperatingSystemMXBean)65ManagementFactory.getOperatingSystemMXBean();6667// Careful with these values.68// Min size for pass dynamically determined below.69// zero if no swap space is configured.70private static long min_size_for_pass = 0;71private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;7273public static void main(String args[]) throws Throwable {7475long expected_swap_size = getSwapSizeFromOs();7677long min_size = mbean.getFreeSwapSpaceSize();78if (min_size > 0) {79min_size_for_pass = min_size;80}8182long size = mbean.getTotalSwapSpaceSize();8384System.out.println("Total swap space size in bytes: " + size);8586if (expected_swap_size > -1) {87if (size != expected_swap_size) {88throw new RuntimeException("Expected total swap size : " +89expected_swap_size +90" but getTotalSwapSpaceSize returned: " +91size);92}93}9495// sanity check96if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {97throw new RuntimeException("Total swap space size " +98"illegal value: " + size + " bytes " +99"(MIN = " + min_size_for_pass + "; " +100"MAX = " + MAX_SIZE_FOR_PASS + ")");101}102103System.out.println("Test passed.");104}105106private static long getSwapSizeFromOs() throws Throwable {107OSInfo.OSType os = OSInfo.getOSType();108109switch (os) {110// total used free shared buffers cached111// Mem: 16533540864 13638467584 2895073280 534040576 1630248960 6236909568112// -/+ buffers/cache: 5771309056 10762231808113// Swap: 15999168512 0 15999168512114115case LINUX: {116String swapSizeStr = ProcessTools.executeCommand("free", "-b")117.firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1);118return Long.parseLong(swapSizeStr);119}120case SOLARIS: {121// swapfile dev swaplo blocks free122// /dev/dsk/c0t0d0s1 136,1 16 1638608 1600528123OutputAnalyzer out= ProcessTools.executeCommand(124"/usr/sbin/swap",125"-l"126);127128long swapSize = 0;129130for (String line : out.asLines()) {131if (line.contains("swapfile")) continue;132133String[] vals = line.split("\\s+");134if (vals.length == 5) {135swapSize += Long.parseLong(vals[3]) * 512; // size is reported in 512b blocks136}137}138139return swapSize;140}141case MACOSX: {142// total = 8192.00M used = 7471.11M free = 720.89M (encrypted)143String swapSizeStr = ProcessTools.executeCommand(144"/usr/sbin/sysctl",145"-n",146"vm.swapusage"147).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1);148if (swapSizeStr.toLowerCase().endsWith("m")) {149swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1);150return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB151}152return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024);153}154default: {155System.err.println("Unsupported operating system: " + os);156}157}158159return -1;160}161}162163164