1/*
2 * Copyright (c) 2003, 2004, 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 * @bug     4858522
26 * @summary Basic unit test of UnixOperatingSystemMXBean.getOpenFileDescriptorCount()
27 * @author  Steve Bohne
28 */
29
30/*
31 * This test is just a sanity check and does not check for the correct
32 * value.  The correct value should be checked manually:
33 * Solaris/Linux:
34 *   1. Find the pid of the java process.
35 *   2. In a shell, enter the command: "ls -1 /proc/<pid>/fd | wc -l"
36 */
37
38import com.sun.management.UnixOperatingSystemMXBean;
39import java.lang.management.*;
40
41public class GetOpenFileDescriptorCount {
42
43    private static UnixOperatingSystemMXBean mbean =
44        (UnixOperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
45
46    // Careful with these values.
47    private static final long MIN_COUNT_FOR_PASS = 1;
48    // Max count for pass dynamically determined below
49    private static long       max_count_for_pass = Long.MAX_VALUE;
50
51    private static boolean trace = false;
52
53    public static void main(String args[]) throws Exception {
54        if (args.length > 0 && args[0].equals("trace")) {
55            trace = true;
56        }
57
58        long max_count = mbean.getMaxFileDescriptorCount();
59        if (max_count > 0) {
60            max_count_for_pass = max_count;
61        }
62
63        long count = mbean.getOpenFileDescriptorCount();
64
65        if (trace) {
66            System.out.println("Open file descriptor count: " + count);
67        }
68
69        if (count < MIN_COUNT_FOR_PASS || count > max_count_for_pass) {
70            throw new RuntimeException("Open file descriptor count " +
71                                       "illegal value: " + count + " bytes " +
72                                       "(MIN = " + MIN_COUNT_FOR_PASS + "; " +
73                                       "MAX = " + max_count_for_pass + ")");
74        }
75
76        System.out.println("Test passed.");
77    }
78}
79