jdk/test/com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java
author iignatyev
Wed, 15 Mar 2017 22:48:59 -0700
changeset 44423 306c020eb154
parent 30376 2ccf2cf7ea48
permissions -rw-r--r--
8176176: fix @modules in jdk_svc tests Reviewed-by: shurailine, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 9559
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     4
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     7
 * published by the Free Software Foundation.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     8
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    13
 * accompanied this code).
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    14
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    18
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    21
 * questions.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    22
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    23
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    24
/*
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    25
 * @test
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    26
 * @bug     7028071
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    27
 * @summary Basic unit test of OperatingSystemMXBean.getProcessCpuLoad()
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    28
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    29
 * @run main GetSystemCpuLoad
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    30
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    31
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    32
import java.lang.management.*;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    33
import com.sun.management.OperatingSystemMXBean;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    34
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    35
public class GetSystemCpuLoad {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    36
    public static void main(String[] argv) throws Exception {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    37
        OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    38
            ManagementFactory.getOperatingSystemMXBean();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    39
        double load;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    40
        for(int i=0; i<10; i++) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    41
            load = mbean.getSystemCpuLoad();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    42
            if((load<0.0 || load>1.0) && load != -1.0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    43
                throw new RuntimeException("getSystemCpuLoad() returns " + load
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    44
                       +  " which is not in the [0.0,1.0] interval");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    45
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    46
            try {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    47
                Thread.sleep(200);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    48
            } catch(InterruptedException e) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    49
                e.printStackTrace();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    50
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    51
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    52
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    53
}