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.getFreeSwapSpaceSize() 28 * @author Steve Bohne 29 */ 30 31 /* 32 * This test is just a sanity check and does not check for the correct 33 * value. The correct value should be checked manually: 34 * Solaris: 35 * 1. In a shell, enter the command: "swap -l" 36 * 2. The value (reported in blocks) is in the "free" column. 37 * Linux: 38 * 1. In a shell, enter the command: "cat /proc/meminfo" 39 * 2. The value (reported in bytes) is in "Swap" entry, "free" column. 40 * Windows NT/XP/2000: 41 * Unknown. 42 * Windows 98/ME: 43 * Unknown. 44 */ 45 46 import com.sun.management.OperatingSystemMXBean; 47 import java.lang.management.*; 48 49 public class GetFreeSwapSpaceSize { 50 51 private static OperatingSystemMXBean mbean = 52 (com.sun.management.OperatingSystemMXBean) 53 ManagementFactory.getOperatingSystemMXBean(); 54 55 // Careful with these values. 56 // Min size is zero since the system can run of swap spaces 57 private static final long MIN_SIZE_FOR_PASS = 0; 58 // Max size for pass dynamically determined below. 59 private static long max_size_for_pass = Long.MAX_VALUE; 60 61 private static boolean trace = false; 62 63 public static void main(String args[]) throws Exception { 64 if (args.length > 0 && args[0].equals("trace")) { 65 trace = true; 66 } 67 68 long max_size = mbean.getTotalSwapSpaceSize(); 69 if (max_size > 0) { 70 max_size_for_pass = max_size; 71 } 72 73 long size = mbean.getFreeSwapSpaceSize(); 74 75 if (trace) { 76 System.out.println("Free swap space size in bytes: " + size); 77 } 78 79 if (size < MIN_SIZE_FOR_PASS || size > max_size_for_pass) { 80 throw new RuntimeException("Free swap space size " + 81 "illegal value: " + size + " bytes " + 82 "(MIN = " + MIN_SIZE_FOR_PASS + "; " + 83 "MAX = " + max_size_for_pass + ")"); 84 } 85 86 System.out.println("Test passed."); 87 } 88 }