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  * @test
  26  * @bug     4858522
  27  * @summary Basic unit test of OperatingSystemMXBean.getProcessCpuTime()
  28  * @author  Steve Bohne
  29  */
  30 
  31 /*
  32  * This test is just a sanity check and does not check for the correct
  33  * value.  The correct value should be checked manually:
  34  * Solaris/Linux:
  35  *   1. In a shell, enter the command: "ps -ef"
  36  *   2. The value (reported in seconds) is in the "TIME" column for the process
  37  * Windows NT/XP/2000:
  38  *   1. Hit Ctrl-Alt-Delete, select Task Manager, select Processes tab
  39  *   2. The value (reported in seconds) is in the "CPU Time" column for the
  40  *      process.  You may have to add this column via View menu-> Select
  41  *      Columns... item.
  42  * Windows 98/ME:
  43  *   Not supported.
  44  */
  45 
  46 import com.sun.management.OperatingSystemMXBean;
  47 import java.lang.management.*;
  48 
  49 public class GetProcessCpuTime {
  50 
  51     private static OperatingSystemMXBean mbean =
  52         (com.sun.management.OperatingSystemMXBean)
  53         ManagementFactory.getOperatingSystemMXBean();
  54 
  55 
  56     // Careful with these values.
  57     private static final long MIN_TIME_FOR_PASS = 1;
  58     private static final long MAX_TIME_FOR_PASS = Long.MAX_VALUE;
  59 
  60     // No max time.
  61 
  62     private static boolean trace = false;
  63 
  64     public static void main(String args[]) throws Exception {
  65         if (args.length > 0 && args[0].equals("trace")) {
  66             trace = true;
  67         }
  68 
  69         // Consume cpu time
  70         double sum=0;
  71         for (int i=0; i <=2000000; i++) {
  72           sum += i;
  73           sum /= i;
  74         }
  75 
  76         long ns = mbean.getProcessCpuTime();
  77         if (ns == -1) {
  78             System.out.println("getProcessCpuTime() is not supported");
  79             return;
  80         }
  81 
  82         if (trace) {
  83             System.out.println("Process CPU time in ns: " + ns);
  84         }
  85 
  86         if (ns < MIN_TIME_FOR_PASS || ns > MAX_TIME_FOR_PASS) {
  87             throw new RuntimeException("Process CPU time " +
  88                                        "illegal value: " + ns + " ns " +
  89                                        "(MIN = " + MIN_TIME_FOR_PASS + "; " +
  90                                        "MAX = " + MAX_TIME_FOR_PASS + ")");
  91         }
  92 
  93         System.out.println("Test passed.");
  94     }
  95 }