Path: blob/master/test/jdk/com/sun/management/OperatingSystemMXBean/GetProcessCpuTime.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.getProcessCpuTime()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/Linux:34* 1. In a shell, enter the command: "ps -ef"35* 2. The value (reported in seconds) is in the "TIME" column for the process36* Windows NT/XP/2000:37* 1. Hit Ctrl-Alt-Delete, select Task Manager, select Processes tab38* 2. The value (reported in seconds) is in the "CPU Time" column for the39* process. You may have to add this column via View menu-> Select40* Columns... item.41* Windows 98/ME:42* Not supported.43*/4445import com.sun.management.OperatingSystemMXBean;46import java.lang.management.*;4748public class GetProcessCpuTime {4950private static OperatingSystemMXBean mbean =51(com.sun.management.OperatingSystemMXBean)52ManagementFactory.getOperatingSystemMXBean();535455// Careful with these values.56private static final long MIN_TIME_FOR_PASS = 1;57private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE;5859// No max time.6061private static boolean trace = false;6263public static void main(String args[]) throws Exception {64if (args.length > 0 && args[0].equals("trace")) {65trace = true;66}6768// Consume cpu time69double sum=0;70for (int i=0; i <=2000000; i++) {71sum += i;72sum /= i;73}7475long ns = mbean.getProcessCpuTime();76if (ns == -1) {77System.out.println("getProcessCpuTime() is not supported");78return;79}8081if (trace) {82System.out.println("Process CPU time in ns: " + ns);83}8485if (ns < MIN_TIME_FOR_PASS || ns > MAX_TIME_FOR_PASS) {86throw new RuntimeException("Process CPU time " +87"illegal value: " + ns + " ns " +88"(MIN = " + MIN_TIME_FOR_PASS + "; " +89"MAX = " + MAX_TIME_FOR_PASS + ")");90}9192System.out.println("Test passed.");93}94}959697