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