Path: blob/master/test/jdk/java/lang/management/RuntimeMXBean/UpTime.java
41154 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 453053826* @summary Basic unit test of RuntimeMXBean.getUptime()27* @author Alexei Guibadoulline28*/2930import java.lang.management.*;3132public class UpTime {33static final long DELAY = 5; // Seconds34static final long TIMEOUT = 30; // Minutes35static final long MULTIPLIER = 1000; // millisecond ticks3637private static final RuntimeMXBean metrics38= ManagementFactory.getRuntimeMXBean();3940public static void main(String argv[]) throws Exception {41long jvmStartTime = metrics.getStartTime();42// this will get an aproximate JVM uptime before starting this test43long jvmUptime = System.currentTimeMillis() - jvmStartTime;44long systemStartOuter = System_milliTime();45long metricsStart = metrics.getUptime();46long systemStartInner = System_milliTime();4748// This JVM might have been running for some time if this test runs49// in samevm mode. The sanity check should apply to the test uptime.50long testUptime = metricsStart - jvmUptime;5152// If uptime is more than 30 minutes then it looks like a bug in53// the method54if (testUptime > TIMEOUT * 60 * MULTIPLIER)55throw new RuntimeException("Uptime of the JVM is more than 30 "56+ "minutes ("57+ (metricsStart / 60 / MULTIPLIER)58+ " minutes).");5960// Wait for DELAY seconds61Object o = new Object();62while (System_milliTime() < systemStartInner + DELAY * MULTIPLIER) {63synchronized (o) {64try {65o.wait(DELAY * 1000);66} catch (Exception e) {67e.printStackTrace();68throw e;69}70}71}7273long systemEndInner = System_milliTime();74long metricsEnd = metrics.getUptime();75long systemEndOuter = System_milliTime();7677long systemDifferenceInner = systemEndInner - systemStartInner;78long systemDifferenceOuter = systemEndOuter - systemStartOuter;79long metricsDifference = metricsEnd - metricsStart;8081// Check the flow of time in RuntimeMXBean.getUptime(). See the82// picture below.83// The measured times can be off by 1 due to conversions from84// nanoseconds to milliseconds, using different channels to read the85// HR timer and rounding error. Bigger difference will make the test86// fail.87if (metricsDifference - systemDifferenceInner < -1)88throw new RuntimeException("Flow of the time in "89+ "RuntimeMXBean.getUptime() ("90+ metricsDifference + ") is slower than "91+ " in system (" + systemDifferenceInner92+ ")");93if (metricsDifference - systemDifferenceOuter > 1)94throw new RuntimeException("Flow of the time in "95+ "RuntimeMXBean.getUptime() ("96+ metricsDifference + ") is faster than "97+ "in system (" + systemDifferenceOuter98+ ")");99100System.out.println("Test passed.");101}102103private static long System_milliTime() {104return System.nanoTime() / 1000000; // nanoseconds / milliseconds;105}106}107108/*109110A picture to describe the second testcase that checks the flow of time in111RuntimeMXBean.getUptime()112113114start115o1 u1 i1 Sleep for DELAY minutes i2 u2 o2116|-------|---|---|--------------------------------|---|---|---------------> time117118119The following inequality (checked by the test) must always be true:120121o2-o1 >= u2-u1 >= i2-i1122123In the test:124125i1 - systemStartInner126i2 - systemEndInner127o1 - systemStartOuter128o2 - systemEndOuter129u1 - metricsStart130u2 - metricsEnd131132*/133134135