0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A *
0N/A * @bug 4858522
0N/A * @summary Basic unit test of OperatingSystemMXBean.getTotalSwapSpaceSize()
0N/A * @author Steve Bohne
0N/A */
0N/A
0N/A/*
0N/A * This test tests the actual swap size on linux and solaris.
0N/A * The correct value should be checked manually:
0N/A * Solaris:
0N/A * 1. In a shell, enter the command: "swap -l"
0N/A * 2. The value (reported in blocks) is in the "blocks" column.
0N/A * Linux:
0N/A * 1. In a shell, enter the command: "cat /proc/meminfo"
0N/A * 2. The value (reported in bytes) is in "Swap" entry, "total" column.
0N/A * Windows NT/XP/2000:
0N/A * 1. Run Start->Accessories->System Tools->System Information.
0N/A * 2. The value (reported in Kbytes) is in the "Page File Space" entry
0N/A * Windows 98/ME:
0N/A * Unknown.
0N/A *
0N/A * Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace]
0N/A */
0N/A
0N/Aimport com.sun.management.OperatingSystemMXBean;
0N/Aimport java.lang.management.*;
0N/A
0N/Apublic class GetTotalSwapSpaceSize {
0N/A
0N/A private static OperatingSystemMXBean mbean =
0N/A (com.sun.management.OperatingSystemMXBean)
0N/A ManagementFactory.getOperatingSystemMXBean();
0N/A
0N/A // Careful with these values.
0N/A // Min size for pass dynamically determined below.
0N/A // zero if no swap space is configured.
0N/A private static long min_size_for_pass = 0;
0N/A private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;
0N/A
0N/A private static boolean trace = false;
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A if (args.length > 1 && args[1].equals("trace")) {
0N/A trace = true;
0N/A }
0N/A
0N/A long expected_swap_size = 0;
0N/A
0N/A if (args.length < 1 || args.length > 2) {
0N/A throw new IllegalArgumentException("Unexpected number of args " + args.length);
0N/A }
0N/A
0N/A
0N/A long min_size = mbean.getFreeSwapSpaceSize();
0N/A if (min_size > 0) {
0N/A min_size_for_pass = min_size;
0N/A }
0N/A
0N/A long size = mbean.getTotalSwapSpaceSize();
0N/A
0N/A if (trace) {
0N/A System.out.println("Total swap space size in bytes: " + size);
0N/A }
0N/A
0N/A if (!args[0].matches("sanity-only")) {
0N/A expected_swap_size = Long.parseLong(args[0]);
0N/A if (size != expected_swap_size) {
0N/A throw new RuntimeException("Expected total swap size : " +
0N/A expected_swap_size +
0N/A " but getTotalSwapSpaceSize returned: " +
0N/A size);
0N/A }
0N/A }
0N/A
0N/A if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {
0N/A throw new RuntimeException("Total swap space size " +
0N/A "illegal value: " + size + " bytes " +
0N/A "(MIN = " + min_size_for_pass + "; " +
0N/A "MAX = " + MAX_SIZE_FOR_PASS + ")");
0N/A }
0N/A
0N/A System.out.println("Test passed.");
0N/A }
0N/A}