Path: blob/master/test/jdk/com/sun/management/OperatingSystemMXBean/TestTotalSwap.java
66645 views
/*1* Copyright (c) 2003, 2022, 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 MacOS only.37*/3839import com.sun.management.OperatingSystemMXBean;40import java.lang.management.*;4142import jdk.test.lib.Platform;43import jdk.test.lib.process.ProcessTools;44import jdk.test.lib.process.OutputAnalyzer;4546public class TestTotalSwap {4748private static final OperatingSystemMXBean mbean =49(com.sun.management.OperatingSystemMXBean)50ManagementFactory.getOperatingSystemMXBean();5152// Careful with these values.53// Min size for pass dynamically determined below.54// zero if no swap space is configured.55private static long min_size_for_pass = 0;56private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;5758public static void main(String args[]) throws Throwable {5960// yocto might ignore the request to report swap size in bytes61boolean swapInKB = mbean.getVersion().contains("yocto");6263long min_size = mbean.getFreeSwapSpaceSize();64if (min_size > 0) {65min_size_for_pass = min_size;66}6768long expected_swap_size = getSwapSizeFromOs();69long size = mbean.getTotalSwapSpaceSize();7071System.out.println("Total swap space size from OS in bytes: " + expected_swap_size);72System.out.println("Total swap space size in MBean bytes: " + size);7374// if swap data from OS chnaged re-read OS and MBean data75while (expected_swap_size != getSwapSizeFromOs()) {76System.out.println("Total swap space reported by OS changed form " + expected_swap_size77+ " current value = " + getSwapSizeFromOs());78expected_swap_size = getSwapSizeFromOs();79size = mbean.getTotalSwapSpaceSize();8081System.out.println("Re-read total swap space size from OS in bytes: " + expected_swap_size);82System.out.println("Total swap space size in MBean bytes: " + size);83}8485if (expected_swap_size > -1) {86if (size != expected_swap_size) {87// try the expected size in kiloBytes88if (!(swapInKB && expected_swap_size * 1024 == size)) {89throw new RuntimeException("Expected total swap size : " +90expected_swap_size +91" but getTotalSwapSpaceSize returned: " +92size);93}94}95}9697// sanity check98if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {99throw new RuntimeException("Total swap space size " +100"illegal value: " + size + " bytes " +101"(MIN = " + min_size_for_pass + "; " +102"MAX = " + MAX_SIZE_FOR_PASS + ")");103}104105System.out.println("Test passed.");106}107108private static long getSwapSizeFromOs() throws Throwable {109if (Platform.isLinux()) {110// total used free shared buffers cached111// Mem: 16533540864 13638467584 2895073280 534040576 1630248960 6236909568112// -/+ buffers/cache: 5771309056 10762231808113// Swap: 15999168512 0 15999168512114String swapSizeStr = ProcessTools.executeCommand("free", "-b")115.firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1);116return Long.parseLong(swapSizeStr);117} else if (Platform.isOSX()) {118// total = 8192.00M used = 7471.11M free = 720.89M (encrypted)119String swapSizeStr = ProcessTools.executeCommand(120"/usr/sbin/sysctl",121"-n",122"vm.swapusage"123).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1);124if (swapSizeStr.toLowerCase().endsWith("m")) {125swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1);126return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB127}128return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024);129} else {130System.err.println("Unsupported operating system: " + Platform.getOsName());131}132133return -1;134}135}136137138