1 /* |
|
2 * Copyright (c) 2003, 2005, 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 * |
|
26 * |
|
27 * @bug 4858522 |
|
28 * @summary Basic unit test of OperatingSystemMXBean.getTotalSwapSpaceSize() |
|
29 * @author Steve Bohne |
|
30 */ |
|
31 |
|
32 /* |
|
33 * This test tests the actual swap size on linux and solaris. |
|
34 * 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 "blocks" 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, "total" column. |
|
41 * Windows NT/XP/2000: |
|
42 * 1. Run Start->Accessories->System Tools->System Information. |
|
43 * 2. The value (reported in Kbytes) is in the "Page File Space" entry |
|
44 * Windows 98/ME: |
|
45 * Unknown. |
|
46 * |
|
47 * Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace] |
|
48 */ |
|
49 |
|
50 import com.sun.management.OperatingSystemMXBean; |
|
51 import java.lang.management.*; |
|
52 |
|
53 public class GetTotalSwapSpaceSize { |
|
54 |
|
55 private static OperatingSystemMXBean mbean = |
|
56 (com.sun.management.OperatingSystemMXBean) |
|
57 ManagementFactory.getOperatingSystemMXBean(); |
|
58 |
|
59 // Careful with these values. |
|
60 // Min size for pass dynamically determined below. |
|
61 // zero if no swap space is configured. |
|
62 private static long min_size_for_pass = 0; |
|
63 private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE; |
|
64 |
|
65 private static boolean trace = false; |
|
66 |
|
67 public static void main(String args[]) throws Exception { |
|
68 if (args.length > 1 && args[1].equals("trace")) { |
|
69 trace = true; |
|
70 } |
|
71 |
|
72 long expected_swap_size = 0; |
|
73 |
|
74 if (args.length < 1 || args.length > 2) { |
|
75 throw new IllegalArgumentException("Unexpected number of args " + args.length); |
|
76 } |
|
77 |
|
78 |
|
79 long min_size = mbean.getFreeSwapSpaceSize(); |
|
80 if (min_size > 0) { |
|
81 min_size_for_pass = min_size; |
|
82 } |
|
83 |
|
84 long size = mbean.getTotalSwapSpaceSize(); |
|
85 |
|
86 if (trace) { |
|
87 System.out.println("Total swap space size in bytes: " + size); |
|
88 } |
|
89 |
|
90 if (!args[0].matches("sanity-only")) { |
|
91 expected_swap_size = Long.parseLong(args[0]); |
|
92 if (size != expected_swap_size) { |
|
93 throw new RuntimeException("Expected total swap size : " + |
|
94 expected_swap_size + |
|
95 " but getTotalSwapSpaceSize returned: " + |
|
96 size); |
|
97 } |
|
98 } |
|
99 |
|
100 if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) { |
|
101 throw new RuntimeException("Total swap space size " + |
|
102 "illegal value: " + size + " bytes " + |
|
103 "(MIN = " + min_size_for_pass + "; " + |
|
104 "MAX = " + MAX_SIZE_FOR_PASS + ")"); |
|
105 } |
|
106 |
|
107 System.out.println("Test passed."); |
|
108 } |
|
109 } |
|