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