1 /* 2 * Copyright (c) 2005, 2012, 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 * 26 * 27 * @bug 6336608 6511738 28 * @summary Basic unit test of OperatingSystemMXBean.getSystemLoadAverage() 29 * @author Mandy Chung 30 */ 31 32 /* 33 * This test tests the load average on linux and solaris. On Windows, 34 * getSystemLoadAverage() returns -1. 35 * 36 * Usage: GetSystemLoadAverage ["-1.0"] 37 * Arguments: 38 * o If no argument is specified, the test will verify the system load 39 * average with the /usr/bin/uptime command. 40 * o Otherwise, the input argument must be "-1.0" indicating the 41 * expected system load average. This would only be the case when 42 * running on Windows. 43 */ 44 45 import java.lang.management.*; 46 import java.io.*; 47 48 public class GetSystemLoadAverage { 49 50 private static OperatingSystemMXBean mbean = 51 ManagementFactory.getOperatingSystemMXBean(); 52 53 // The system load average may be changing due to other jobs running. 54 // Allow some delta. 55 private static double DELTA = 0.05; 56 main(String args[])57 public static void main(String args[]) throws Exception { 58 if (args.length > 1) { 59 throw new IllegalArgumentException("Unexpected number of args " + args.length); 60 } 61 62 if (args.length == 0) { 63 // On Linux or Solaris 64 checkLoadAvg(); 65 } else { 66 // On Windows, the system load average is expected to be -1.0 67 if (!args[0].equals("-1.0")) { 68 throw new IllegalArgumentException("Invalid argument: " + args[0]); 69 } else { 70 double loadavg = mbean.getSystemLoadAverage(); 71 if (loadavg != -1.0) { 72 throw new RuntimeException("Expected load average : -1.0" + 73 " but getSystemLoadAverage returned: " + 74 loadavg); 75 } 76 } 77 } 78 79 System.out.println("Test passed."); 80 } 81 82 private static String LOAD_AVERAGE_TEXT 83 = System.getProperty("os.name").contains("OS X") 84 ? "load averages:" 85 : "load average:"; 86 checkLoadAvg()87 private static void checkLoadAvg() throws Exception { 88 // Obtain load average from OS command 89 ProcessBuilder pb = new ProcessBuilder("/usr/bin/uptime"); 90 Process p = pb.start(); 91 String output = commandOutput(p); 92 93 // obtain load average from OperatingSystemMXBean 94 double loadavg = mbean.getSystemLoadAverage(); 95 96 // verify if two values are close 97 output = output.substring(output.lastIndexOf(LOAD_AVERAGE_TEXT) + 98 LOAD_AVERAGE_TEXT.length() + 1); 99 System.out.println("Load average returned from uptime = " + output); 100 System.out.println("getSystemLoadAverage() returned " + loadavg); 101 102 String[] lavg = System.getProperty("os.name").contains("OS X") 103 ? output.split(" ") 104 : output.split(","); 105 double expected = Double.parseDouble(lavg[0]); 106 double lowRange = expected * (1 - DELTA); 107 double highRange = expected * (1 + DELTA); 108 109 if (loadavg < lowRange || loadavg > highRange) { 110 throw new RuntimeException("Expected load average : " + 111 expected + 112 " but getSystemLoadAverage returned: " + 113 loadavg); 114 } 115 } 116 commandOutput(Reader r)117 private static String commandOutput(Reader r) throws Exception { 118 StringBuilder sb = new StringBuilder(); 119 int c; 120 while ((c = r.read()) > 0) { 121 if (c != '\r') { 122 sb.append((char) c); 123 } 124 } 125 return sb.toString(); 126 } 127 commandOutput(Process p)128 private static String commandOutput(Process p) throws Exception { 129 Reader r = new InputStreamReader(p.getInputStream(),"UTF-8"); 130 String output = commandOutput(r); 131 p.waitFor(); 132 p.exitValue(); 133 return output; 134 } 135 136 } 137