jdk/test/java/lang/management/RuntimeMXBean/UpTime.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4530538
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of RuntimeMXBean.getUptime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Alexei Guibadoulline
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
    29
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class UpTime {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30376
diff changeset
    35
    static final long DELAY = 5; // Seconds
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30376
diff changeset
    36
    static final long TIMEOUT = 30; // Minutes
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30376
diff changeset
    37
    static final long MULTIPLIER = 1000; // millisecond ticks
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    38
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    39
    private static final RuntimeMXBean metrics
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        = ManagementFactory.getRuntimeMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String argv[]) throws Exception {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    43
        long jvmStartTime = metrics.getStartTime();
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    44
        // this will get an aproximate JVM uptime before starting this test
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    45
        long jvmUptime = System.currentTimeMillis() - jvmStartTime;
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    46
        long systemStartOuter = System_milliTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        long metricsStart = metrics.getUptime();
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    48
        long systemStartInner = System_milliTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    50
        // This JVM might have been running for some time if this test runs
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    51
        // in samevm mode.  The sanity check should apply to the test uptime.
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    52
        long testUptime = metricsStart - jvmUptime;
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    53
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // If uptime is more than 30 minutes then it looks like a bug in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // the method
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    56
        if (testUptime > TIMEOUT * 60 * MULTIPLIER)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw new RuntimeException("Uptime of the JVM is more than 30 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                     + "minutes ("
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    59
                                     + (metricsStart / 60 / MULTIPLIER)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                     + " minutes).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // Wait for DELAY seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Object o = new Object();
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    64
        while (System_milliTime() < systemStartInner + DELAY * MULTIPLIER) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            synchronized (o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    o.wait(DELAY * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    75
        long systemEndInner = System_milliTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        long metricsEnd = metrics.getUptime();
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    77
        long systemEndOuter = System_milliTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        long systemDifferenceInner = systemEndInner - systemStartInner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        long systemDifferenceOuter = systemEndOuter - systemStartOuter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        long metricsDifference = metricsEnd - metricsStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // Check the flow of time in RuntimeMXBean.getUptime(). See the
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    84
        // picture below.
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    85
        // The measured times can be off by 1 due to conversions from
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    86
        // nanoseconds to milliseconds, using different channels to read the
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    87
        // HR timer and rounding error. Bigger difference will make the test
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    88
        // fail.
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    89
        if (metricsDifference - systemDifferenceInner < -1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new RuntimeException("Flow of the time in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                     + "RuntimeMXBean.getUptime() ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                     + metricsDifference + ") is slower than "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                     + " in system (" + systemDifferenceInner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                     + ")");
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
    95
        if (metricsDifference - systemDifferenceOuter > 1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new RuntimeException("Flow of the time in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                     + "RuntimeMXBean.getUptime() ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                     + metricsDifference + ") is faster than "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                     + "in system (" + systemDifferenceOuter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                     + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
21640
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
   104
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
   105
    private static long System_milliTime() {
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
   106
        return System.nanoTime() / 1000000; // nanoseconds / milliseconds;
6bbdcc430310 6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents: 7668
diff changeset
   107
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
   A picture to describe the second testcase that checks the flow of time in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
   RuntimeMXBean.getUptime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
   start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
             o1  u1  i1    Sleep for DELAY minutes    i2  u2  o2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     |-------|---|---|--------------------------------|---|---|---------------> time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
   The following inequality (checked by the test) must always be true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
       o2-o1 >= u2-u1 >= i2-i1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   In the test:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
   i1 - systemStartInner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
   i2 - systemEndInner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
   o1 - systemStartOuter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
   o2 - systemEndOuter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
   u1 - metricsStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
   u2 - metricsEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
*/