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