Path: blob/master/test/jdk/com/sun/management/OperatingSystemMXBean/GetFreeSwapSpaceSize.java
41153 views
/*1* Copyright (c) 2003, 2015, 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.getFreeSwapSpaceSize()27* @author Steve Bohne28*/2930/*31* This test is just a sanity check and does not check for the correct32* value. The correct value should be checked manually:33* Solaris:34* 1. In a shell, enter the command: "swap -l"35* 2. The value (reported in blocks) is in the "free" column.36* Linux:37* 1. In a shell, enter the command: "cat /proc/meminfo"38* 2. The value (reported in bytes) is in "Swap" entry, "free" column.39* Windows NT/XP/2000:40* Unknown.41* Windows 98/ME:42* Unknown.43*/4445import com.sun.management.OperatingSystemMXBean;46import java.lang.management.*;4748public class GetFreeSwapSpaceSize {4950private static OperatingSystemMXBean mbean =51(com.sun.management.OperatingSystemMXBean)52ManagementFactory.getOperatingSystemMXBean();5354// Careful with these values.55// Min size is zero since the system can run of swap spaces56private static final long MIN_SIZE_FOR_PASS = 0;57// Max size for pass dynamically determined below.58private static long max_size_for_pass = Long.MAX_VALUE;5960private static boolean trace = false;6162public static void main(String args[]) throws Exception {63if (args.length > 0 && args[0].equals("trace")) {64trace = true;65}6667long max_size = mbean.getTotalSwapSpaceSize();68if (max_size > 0) {69max_size_for_pass = max_size;70}7172long size = mbean.getFreeSwapSpaceSize();7374if (trace) {75System.out.println("Free swap space size in bytes: " + size);76}7778if (size < MIN_SIZE_FOR_PASS || size > max_size_for_pass) {79throw new RuntimeException("Free swap space size " +80"illegal value: " + size + " bytes " +81"(MIN = " + MIN_SIZE_FOR_PASS + "; " +82"MAX = " + max_size_for_pass + ")");83}8485System.out.println("Test passed.");86}87}888990