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 4530538 27 * @summary Basic unit test of RuntimeMXBean.getUptime() 28 * @author Alexei Guibadoulline 29 */ 30 31import java.lang.management.*; 32 33public class UpTime { 34 static final long DELAY = 5; // Seconds 35 static final long TIMEOUT = 30; // Minutes 36 static final long MULTIPLIER = 1000; // millisecond ticks 37 38 private static final RuntimeMXBean metrics 39 = ManagementFactory.getRuntimeMXBean(); 40 41 public static void main(String argv[]) throws Exception { 42 long jvmStartTime = metrics.getStartTime(); 43 // this will get an aproximate JVM uptime before starting this test 44 long jvmUptime = System.currentTimeMillis() - jvmStartTime; 45 long systemStartOuter = System_milliTime(); 46 long metricsStart = metrics.getUptime(); 47 long systemStartInner = System_milliTime(); 48 49 // This JVM might have been running for some time if this test runs 50 // in samevm mode. The sanity check should apply to the test uptime. 51 long testUptime = metricsStart - jvmUptime; 52 53 // If uptime is more than 30 minutes then it looks like a bug in 54 // the method 55 if (testUptime > TIMEOUT * 60 * MULTIPLIER) 56 throw new RuntimeException("Uptime of the JVM is more than 30 " 57 + "minutes (" 58 + (metricsStart / 60 / MULTIPLIER) 59 + " minutes)."); 60 61 // Wait for DELAY seconds 62 Object o = new Object(); 63 while (System_milliTime() < systemStartInner + DELAY * MULTIPLIER) { 64 synchronized (o) { 65 try { 66 o.wait(DELAY * 1000); 67 } catch (Exception e) { 68 e.printStackTrace(); 69 throw e; 70 } 71 } 72 } 73 74 long systemEndInner = System_milliTime(); 75 long metricsEnd = metrics.getUptime(); 76 long systemEndOuter = System_milliTime(); 77 78 long systemDifferenceInner = systemEndInner - systemStartInner; 79 long systemDifferenceOuter = systemEndOuter - systemStartOuter; 80 long metricsDifference = metricsEnd - metricsStart; 81 82 // Check the flow of time in RuntimeMXBean.getUptime(). See the 83 // picture below. 84 // The measured times can be off by 1 due to conversions from 85 // nanoseconds to milliseconds, using different channels to read the 86 // HR timer and rounding error. Bigger difference will make the test 87 // fail. 88 if (metricsDifference - systemDifferenceInner < -1) 89 throw new RuntimeException("Flow of the time in " 90 + "RuntimeMXBean.getUptime() (" 91 + metricsDifference + ") is slower than " 92 + " in system (" + systemDifferenceInner 93 + ")"); 94 if (metricsDifference - systemDifferenceOuter > 1) 95 throw new RuntimeException("Flow of the time in " 96 + "RuntimeMXBean.getUptime() (" 97 + metricsDifference + ") is faster than " 98 + "in system (" + systemDifferenceOuter 99 + ")"); 100 101 System.out.println("Test passed."); 102 } 103 104 private static long System_milliTime() { 105 return System.nanoTime() / 1000000; // nanoseconds / milliseconds; 106 } 107} 108 109/* 110 111 A picture to describe the second testcase that checks the flow of time in 112 RuntimeMXBean.getUptime() 113 114 115 start 116 o1 u1 i1 Sleep for DELAY minutes i2 u2 o2 117 |-------|---|---|--------------------------------|---|---|---------------> time 118 119 120 The following inequality (checked by the test) must always be true: 121 122 o2-o1 >= u2-u1 >= i2-i1 123 124 In the test: 125 126 i1 - systemStartInner 127 i2 - systemEndInner 128 o1 - systemStartOuter 129 o2 - systemEndOuter 130 u1 - metricsStart 131 u2 - metricsEnd 132 133*/ 134