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