1 /* 2 * Copyright (c) 2003, 2018, 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 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" entry 48 * Windows 98/ME: 49 * Unknown. 50 * 51 * Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace] 52 */ 53 54 import com.sun.management.OperatingSystemMXBean; 55 import java.lang.management.*; 56 57 import jdk.test.lib.Platform; 58 import jdk.test.lib.process.ProcessTools; 59 import jdk.test.lib.process.OutputAnalyzer; 60 61 public class TestTotalSwap { 62 63 private static final OperatingSystemMXBean mbean = 64 (com.sun.management.OperatingSystemMXBean) 65 ManagementFactory.getOperatingSystemMXBean(); 66 67 // Careful with these values. 68 // Min size for pass dynamically determined below. 69 // zero if no swap space is configured. 70 private static long min_size_for_pass = 0; 71 private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE; 72 73 public static void main(String args[]) throws Throwable { 74 // yocto might ignore the request to report swap size in bytes 75 boolean swapInKB = mbean.getVersion().contains("yocto"); 76 77 long expected_swap_size = getSwapSizeFromOs(); 78 79 long min_size = mbean.getFreeSwapSpaceSize(); 80 if (min_size > 0) { 81 min_size_for_pass = min_size; 82 } 83 84 long size = mbean.getTotalSwapSpaceSize(); 85 86 System.out.println("Total swap space size in bytes: " + size); 87 88 if (expected_swap_size > -1) { 89 if (size != expected_swap_size) { 90 // try the expected size in kiloBytes 91 if (!(swapInKB && expected_swap_size * 1024 == size)) { 92 throw new RuntimeException("Expected total swap size : " + 93 expected_swap_size + 94 " but getTotalSwapSpaceSize returned: " + 95 size); 96 } 97 } 98 } 99 100 // sanity check 101 if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) { 102 throw new RuntimeException("Total swap space size " + 103 "illegal value: " + size + " bytes " + 104 "(MIN = " + min_size_for_pass + "; " + 105 "MAX = " + MAX_SIZE_FOR_PASS + ")"); 106 } 107 108 System.out.println("Test passed."); 109 } 110 111 private static long getSwapSizeFromOs() throws Throwable { 112 if (Platform.isLinux()) { 113 // total used free shared buffers cached 114 // Mem: 16533540864 13638467584 2895073280 534040576 1630248960 6236909568 115 // -/+ buffers/cache: 5771309056 10762231808 116 // Swap: 15999168512 0 15999168512 117 String swapSizeStr = ProcessTools.executeCommand("free", "-b") 118 .firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1); 119 return Long.parseLong(swapSizeStr); 120 } else if (Platform.isOSX()) { 121 // total = 8192.00M used = 7471.11M free = 720.89M (encrypted) 122 String swapSizeStr = ProcessTools.executeCommand( 123 "/usr/sbin/sysctl", 124 "-n", 125 "vm.swapusage" 126 ).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1); 127 if (swapSizeStr.toLowerCase().endsWith("m")) { 128 swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1); 129 return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB 130 } 131 return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); 132 } else { 133 System.err.println("Unsupported operating system: " + Platform.getOsName()); 134 } 135 136 return -1; 137 } 138 }