1 /* 2 * Copyright (c) 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 import java.util.Objects; 24 import sun.misc.VM; 25 26 /** 27 * @test 28 * @bug 8068730 29 * @summary tests that VM.getgetNanoTimeAdjustment() works as expected. 30 * @run main GetNanoTimeAdjustment 31 * @author danielfuchs 32 */ 33 public class GetNanoTimeAdjustment { 34 35 static final int MILLIS_IN_SECOND = 1000; 36 static final int NANOS_IN_MILLI = 1000_000; 37 static final int NANOS_IN_MICRO = 1000; 38 static final int NANOS_IN_SECOND = 1000_000_000; 39 40 static final boolean verbose = true; 41 42 static final class TestAssertException extends RuntimeException { 43 TestAssertException(String msg) { super(msg); } 44 } 45 46 private static void assertEquals(long expected, long received, String msg) { 47 if (expected != received) { 48 throw new TestAssertException("Unexpected result for " + msg 49 + ".\n\texpected: " + expected 50 + "\n\tactual: " + received); 51 } else if (verbose) { 52 System.out.println("Got expected " + msg + ": " + received); 53 } 54 } 55 56 private static void assertEquals(Object expected, Object received, String msg) { 57 if (!Objects.equals(expected, received)) { 58 throw new TestAssertException("Unexpected result for " + msg 59 + ".\n\texpected: " + expected 60 + "\n\tactual: " + received); 61 } else if (verbose) { 62 System.out.println("Got expected " + msg + ": " + received); 63 } 64 } 65 66 static final long MAX_OFFSET = 0x0100000000L; 67 static final long MIN_OFFSET = -MAX_OFFSET; 68 static enum Answer { 69 YES, // isOffLimit = YES: we must get -1 70 NO, // isOffLimit = NO: we must not not get -1 71 MAYBE // isOffLimit = MAYBE: we might get -1 or a valid adjustment. 72 }; 73 static long distance(long one, long two) { 74 return one > two ? Math.subtractExact(one, two) 75 : Math.subtractExact(two, one); 76 } 77 78 79 static Answer isOffLimits(long before, long after, long offset) { 80 long relativeDistanceBefore = distance(before, offset); 81 long relativeDistanceAfter = distance(after, offset); 82 if (relativeDistanceBefore >= MAX_OFFSET && relativeDistanceAfter >= MAX_OFFSET) { 83 return Answer.YES; 84 } 85 if (relativeDistanceBefore < MAX_OFFSET && relativeDistanceAfter < MAX_OFFSET) { 86 if (relativeDistanceBefore == 0 || relativeDistanceAfter == 0) { 87 return Answer.MAYBE; // unlucky case where 88 } 89 return Answer.NO; 90 } 91 return Answer.MAYBE; 92 } 93 94 static void testWithOffset(String name, long offset) { 95 System.out.println("Testing with offset: " + name); 96 long beforeMillis = System.currentTimeMillis(); 97 long adjustment = VM.getNanoTimeAdjustment(offset); 98 long afterMillis = System.currentTimeMillis(); 99 100 if (offset >= beforeMillis/MILLIS_IN_SECOND 101 && offset <= afterMillis/MILLIS_IN_SECOND) { 102 if (adjustment == -1) { 103 // it's possible that we have fallen in the unlucky case 104 // where -1 was the genuine result. let's go backward a bit. 105 offset = offset - 10; 106 beforeMillis = System.currentTimeMillis(); 107 adjustment = VM.getNanoTimeAdjustment(offset); 108 afterMillis = System.currentTimeMillis(); 109 if (adjustment == -1) { 110 throw new RuntimeException(name + ": VM says " + offset 111 + " secs is too far off, " 112 + " when time in seconds is in [" 113 + beforeMillis/MILLIS_IN_SECOND + ", " 114 + afterMillis/MILLIS_IN_SECOND 115 + "]"); 116 } 117 } 118 } 119 120 Answer isOffLimit = isOffLimits(beforeMillis/MILLIS_IN_SECOND, 121 afterMillis/MILLIS_IN_SECOND, offset); 122 switch (isOffLimit) { 123 case YES: 124 if (adjustment != -1) { 125 throw new RuntimeException(name 126 + ": VM should have returned -1 for " 127 + offset 128 + " when time in seconds is in [" 129 + beforeMillis/MILLIS_IN_SECOND + ", " 130 + afterMillis/MILLIS_IN_SECOND + "]"); 131 } 132 System.out.println("Got expected exception value: " + adjustment); 133 break; 134 case NO: 135 if (adjustment == -1) { 136 throw new RuntimeException(name 137 + "VM says " + offset 138 + " secs is too far off, " 139 + " when time in seconds is in [" 140 + beforeMillis/MILLIS_IN_SECOND + ", " 141 + afterMillis/MILLIS_IN_SECOND 142 + "]"); 143 } 144 break; 145 case MAYBE: 146 System.out.println("Adjustment: " + adjustment); 147 System.out.println("Can't assert for -1 with offset " 148 + offset + "(" + name + ")" 149 + " when time in seconds is in [" 150 + beforeMillis/MILLIS_IN_SECOND + ", " 151 + afterMillis/MILLIS_IN_SECOND 152 + "]"); 153 // not conclusive 154 } 155 156 if (isOffLimit == Answer.NO || adjustment != -1) { 157 System.out.println("Validating adjustment: " + adjustment); 158 long expectedMax = distance(offset, beforeMillis/MILLIS_IN_SECOND) 159 * NANOS_IN_SECOND 160 + (beforeMillis % MILLIS_IN_SECOND) * NANOS_IN_MILLI 161 + (afterMillis - beforeMillis + 1) * NANOS_IN_MILLI; 162 long absoluteAdjustment = distance(0, adjustment); 163 if (absoluteAdjustment > expectedMax) { 164 long adjSec = absoluteAdjustment / NANOS_IN_SECOND; 165 long adjMil = (absoluteAdjustment % NANOS_IN_SECOND) / NANOS_IN_MILLI; 166 long adjMic = (absoluteAdjustment % NANOS_IN_MILLI) / NANOS_IN_MICRO; 167 long adjNan = (absoluteAdjustment % NANOS_IN_MICRO); 168 long expSec = expectedMax / NANOS_IN_SECOND; 169 long expMil = (expectedMax % NANOS_IN_SECOND) / NANOS_IN_MILLI; 170 long expMic = (expectedMax % NANOS_IN_MILLI) / NANOS_IN_MICRO; 171 long expNan = (expectedMax % NANOS_IN_MICRO); 172 System.err.println("Excessive adjustment: " + adjSec + "s, " 173 + adjMil + "ms, " + adjMic + "mics, " + adjNan + "ns"); 174 System.err.println("Epected max: " + expSec + "s, " 175 + expMil + "ms, " + expMic + "mics, " + expNan + "ns"); 176 177 throw new RuntimeException(name 178 + ": Excessive adjustment: " + adjustment 179 + " when time in millis is in [" 180 + beforeMillis + ", " + afterMillis 181 + "] and offset in seconds is " + offset); 182 } 183 } 184 185 } 186 187 static void regular() { 188 System.out.println("*** Testing regular cases ***"); 189 final long start = System.currentTimeMillis(); 190 long offset = start/1000; 191 long adjustment = VM.getNanoTimeAdjustment(offset); 192 if (start != offset*1000) { 193 if (adjustment == -1) { 194 throw new RuntimeException("VM says " + offset 195 + " secs is too far off, but time millis is " 196 + System.currentTimeMillis()); 197 } 198 } 199 if (adjustment == -1) { 200 offset = System.currentTimeMillis()/1000 - 1024; 201 adjustment = VM.getNanoTimeAdjustment(offset); 202 if (adjustment == -1) { 203 throw new RuntimeException("VM says " + offset 204 + " secs is too far off, but time millis is " 205 + System.currentTimeMillis()); 206 } 207 } 208 if (adjustment > (start/1000 - offset + 20)*NANOS_IN_SECOND) { 209 throw new RuntimeException("Excessive adjustment: " + adjustment); 210 } 211 testWithOffset("System.currentTimeMillis()/1000", 212 System.currentTimeMillis()/1000); 213 testWithOffset("System.currentTimeMillis()/1000 - 1024", 214 System.currentTimeMillis()/1000 - 1024); 215 testWithOffset("System.currentTimeMillis()/1000 + 1024", 216 System.currentTimeMillis()/1000 + 1024); 217 } 218 219 static void testLimits() { 220 System.out.println("*** Testing limits ***"); 221 testWithOffset("System.currentTimeMillis()/1000 - MAX_OFFSET + 1", 222 System.currentTimeMillis()/1000 - MAX_OFFSET + 1); 223 testWithOffset("System.currentTimeMillis()/1000 + MAX_OFFSET - 1", 224 System.currentTimeMillis()/1000 + MAX_OFFSET - 1); 225 testWithOffset("System.currentTimeMillis()/1000 - MAX_OFFSET", 226 System.currentTimeMillis()/1000 - MAX_OFFSET); 227 testWithOffset("System.currentTimeMillis()/1000 + MAX_OFFSET", 228 System.currentTimeMillis()/1000 + MAX_OFFSET); 229 testWithOffset("System.currentTimeMillis()/1000 - MAX_OFFSET - 1024", 230 System.currentTimeMillis()/1000 - MAX_OFFSET - 1024); 231 testWithOffset("System.currentTimeMillis()/1000 + MAX_OFFSET + 1024", 232 System.currentTimeMillis()/1000 + MAX_OFFSET + 1024); 233 testWithOffset("0", 0); 234 testWithOffset("-1", -1); 235 testWithOffset("Integer.MAX_VALUE + System.currentTimeMillis()/1000", 236 ((long)Integer.MAX_VALUE) + System.currentTimeMillis()/1000); 237 testWithOffset("System.currentTimeMillis()/1000 - Integer.MIN_VALUE", 238 System.currentTimeMillis()/1000 - Integer.MIN_VALUE); 239 testWithOffset("Long.MAX_VALUE", Long.MAX_VALUE); 240 testWithOffset("System.currentTimeMillis()/1000 - Long.MIN_VALUE", 241 (Long.MIN_VALUE + System.currentTimeMillis()/1000)*-1); 242 } 243 244 public static void main(String[] args) throws Exception { 245 regular(); 246 testLimits(); 247 } 248 249 }