author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 30376 | 2ccf2cf7ea48 |
child 44423 | 306c020eb154 |
permissions | -rw-r--r-- |
2 | 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 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4530538 |
|
27 |
* @summary Basic unit test of RuntimeMXBean.getUptime() |
|
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 | 30 |
*/ |
31 |
||
32 |
import java.lang.management.*; |
|
33 |
||
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 | 40 |
= ManagementFactory.getRuntimeMXBean(); |
41 |
||
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 | 47 |
long metricsStart = metrics.getUptime(); |
21640
6bbdcc430310
6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents:
7668
diff
changeset
|
48 |
long systemStartInner = System_milliTime(); |
2 | 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 | 54 |
// If uptime is more than 30 minutes then it looks like a bug in |
55 |
// the method |
|
21640
6bbdcc430310
6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents:
7668
diff
changeset
|
56 |
if (testUptime > TIMEOUT * 60 * MULTIPLIER) |
2 | 57 |
throw new RuntimeException("Uptime of the JVM is more than 30 " |
58 |
+ "minutes (" |
|
21640
6bbdcc430310
6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents:
7668
diff
changeset
|
59 |
+ (metricsStart / 60 / MULTIPLIER) |
2 | 60 |
+ " minutes)."); |
61 |
||
62 |
// Wait for DELAY seconds |
|
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 | 65 |
synchronized (o) { |
66 |
try { |
|
67 |
o.wait(DELAY * 1000); |
|
68 |
} catch (Exception e) { |
|
69 |
e.printStackTrace(); |
|
70 |
throw e; |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
||
21640
6bbdcc430310
6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents:
7668
diff
changeset
|
75 |
long systemEndInner = System_milliTime(); |
2 | 76 |
long metricsEnd = metrics.getUptime(); |
21640
6bbdcc430310
6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents:
7668
diff
changeset
|
77 |
long systemEndOuter = System_milliTime(); |
2 | 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 |
|
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 | 90 |
throw new RuntimeException("Flow of the time in " |
91 |
+ "RuntimeMXBean.getUptime() (" |
|
92 |
+ metricsDifference + ") is slower than " |
|
93 |
+ " in system (" + systemDifferenceInner |
|
94 |
+ ")"); |
|
21640
6bbdcc430310
6523160: RuntimeMXBean.getUptime() returns negative values
jbachorik
parents:
7668
diff
changeset
|
95 |
if (metricsDifference - systemDifferenceOuter > 1) |
2 | 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 |
} |
|
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 | 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 |
*/ |