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