Path: blob/master/test/jdk/com/sun/management/UnixOperatingSystemMXBean/GetOpenFileDescriptorCount.java
41154 views
/*1* Copyright (c) 2003, 2004, 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* @bug 485852225* @summary Basic unit test of UnixOperatingSystemMXBean.getOpenFileDescriptorCount()26* @author Steve Bohne27*/2829/*30* This test is just a sanity check and does not check for the correct31* value. The correct value should be checked manually:32* Solaris/Linux:33* 1. Find the pid of the java process.34* 2. In a shell, enter the command: "ls -1 /proc/<pid>/fd | wc -l"35*/3637import com.sun.management.UnixOperatingSystemMXBean;38import java.lang.management.*;3940public class GetOpenFileDescriptorCount {4142private static UnixOperatingSystemMXBean mbean =43(UnixOperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();4445// Careful with these values.46private static final long MIN_COUNT_FOR_PASS = 1;47// Max count for pass dynamically determined below48private static long max_count_for_pass = Long.MAX_VALUE;4950private static boolean trace = false;5152public static void main(String args[]) throws Exception {53if (args.length > 0 && args[0].equals("trace")) {54trace = true;55}5657long max_count = mbean.getMaxFileDescriptorCount();58if (max_count > 0) {59max_count_for_pass = max_count;60}6162long count = mbean.getOpenFileDescriptorCount();6364if (trace) {65System.out.println("Open file descriptor count: " + count);66}6768if (count < MIN_COUNT_FOR_PASS || count > max_count_for_pass) {69throw new RuntimeException("Open file descriptor count " +70"illegal value: " + count + " bytes " +71"(MIN = " + MIN_COUNT_FOR_PASS + "; " +72"MAX = " + max_count_for_pass + ")");73}7475System.out.println("Test passed.");76}77}787980