jdk/test/sun/misc/VM/GetNanoTimeAdjustment.java
author dfuchs
Wed, 28 Jan 2015 17:48:59 +0100
changeset 28765 8878e8455f2a
child 30820 0d4717a011d3
permissions -rw-r--r--
8068730: Increase the precision of the implementation of java.time.Clock.systemUTC() Summary: Changes the implementation of java.time.Clock.systemUTC() to take advantage of the maximum resolution of the underlying native clock on which System.currentTimeMillis() is based. Reviewed-by: dholmes, rriggs, scolebourne, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28765
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     1
/*
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     4
 *
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     8
 *
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    14
 *
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    18
 *
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    21
 * questions.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    22
 */
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    23
import java.util.Objects;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    24
import sun.misc.VM;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    25
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    26
/**
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    27
 * @test
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    28
 * @bug 8068730
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    29
 * @summary tests that VM.getgetNanoTimeAdjustment() works as expected.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    30
 * @run main GetNanoTimeAdjustment
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    31
 * @author danielfuchs
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    32
 */
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    33
public class GetNanoTimeAdjustment {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    34
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    35
    static final int MILLIS_IN_SECOND = 1000;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    36
    static final int NANOS_IN_MILLI = 1000_000;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    37
    static final int NANOS_IN_MICRO = 1000;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    38
    static final int NANOS_IN_SECOND = 1000_000_000;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    39
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    40
    static final boolean verbose = true;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    41
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    42
    static final class TestAssertException extends RuntimeException {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    43
        TestAssertException(String msg) { super(msg); }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    44
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    45
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    46
    private static void assertEquals(long expected, long received, String msg) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    47
        if (expected != received) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    48
            throw new TestAssertException("Unexpected result for " + msg
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    49
                    + ".\n\texpected: " + expected
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    50
                    +  "\n\tactual:   " + received);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    51
        } else if (verbose) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    52
            System.out.println("Got expected " + msg + ": " + received);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    53
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    54
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    55
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    56
    private static void assertEquals(Object expected, Object received, String msg) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    57
        if (!Objects.equals(expected, received)) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    58
            throw new TestAssertException("Unexpected result for " + msg
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    59
                    + ".\n\texpected: " + expected
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    60
                    +  "\n\tactual:   " + received);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    61
        } else if (verbose) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    62
            System.out.println("Got expected " + msg + ": " + received);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    63
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    64
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    65
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    66
    static final long MAX_OFFSET = 0x0100000000L;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    67
    static final long MIN_OFFSET = -MAX_OFFSET;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    68
    static enum Answer {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    69
        YES,   // isOffLimit = YES:   we must get -1
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    70
        NO,    // isOffLimit = NO:    we must not not get -1
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    71
        MAYBE  // isOffLimit = MAYBE: we might get -1 or a valid adjustment.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    72
    };
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    73
    static long distance(long one, long two) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    74
        return one > two ? Math.subtractExact(one, two)
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    75
                : Math.subtractExact(two, one);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    76
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    77
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    78
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    79
    static Answer isOffLimits(long before, long after, long offset) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    80
        long relativeDistanceBefore = distance(before, offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    81
        long relativeDistanceAfter  = distance(after, offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    82
        if (relativeDistanceBefore >= MAX_OFFSET && relativeDistanceAfter >= MAX_OFFSET) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    83
            return Answer.YES;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    84
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    85
        if (relativeDistanceBefore < MAX_OFFSET && relativeDistanceAfter < MAX_OFFSET) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    86
            if (relativeDistanceBefore == 0 || relativeDistanceAfter == 0) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    87
                return Answer.MAYBE; // unlucky case where
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    88
            }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    89
            return Answer.NO;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    90
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    91
        return Answer.MAYBE;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    92
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    93
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    94
    static void testWithOffset(String name, long offset) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    95
        System.out.println("Testing with offset: " + name);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    96
        long beforeMillis = System.currentTimeMillis();
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    97
        long adjustment = VM.getNanoTimeAdjustment(offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    98
        long afterMillis = System.currentTimeMillis();
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
    99
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   100
        if (offset >= beforeMillis/MILLIS_IN_SECOND
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   101
                && offset <= afterMillis/MILLIS_IN_SECOND) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   102
            if (adjustment == -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   103
                // it's possible that we have fallen in the unlucky case
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   104
                // where -1 was the genuine result. let's go backward a bit.
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   105
                offset = offset - 10;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   106
                beforeMillis = System.currentTimeMillis();
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   107
                adjustment = VM.getNanoTimeAdjustment(offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   108
                afterMillis = System.currentTimeMillis();
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   109
                if (adjustment == -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   110
                    throw new RuntimeException(name + ": VM says " + offset
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   111
                            + " secs is too far off, "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   112
                            + " when time in seconds is in ["
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   113
                            + beforeMillis/MILLIS_IN_SECOND + ", "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   114
                            + afterMillis/MILLIS_IN_SECOND
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   115
                            + "]");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   116
                }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   117
            }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   118
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   119
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   120
        Answer isOffLimit = isOffLimits(beforeMillis/MILLIS_IN_SECOND,
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   121
                afterMillis/MILLIS_IN_SECOND, offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   122
        switch (isOffLimit) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   123
            case YES:
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   124
                if (adjustment != -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   125
                    throw new RuntimeException(name
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   126
                        + ": VM should have returned -1 for "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   127
                        + offset
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   128
                        + " when time in seconds is in ["
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   129
                        + beforeMillis/MILLIS_IN_SECOND + ", "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   130
                        + afterMillis/MILLIS_IN_SECOND + "]");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   131
                }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   132
                System.out.println("Got expected exception value: " + adjustment);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   133
                break;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   134
            case NO:
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   135
                if (adjustment == -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   136
                    throw new RuntimeException(name
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   137
                            + "VM says "  + offset
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   138
                            + " secs is too far off, "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   139
                            + " when time in seconds is in ["
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   140
                            + beforeMillis/MILLIS_IN_SECOND + ", "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   141
                            + afterMillis/MILLIS_IN_SECOND
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   142
                            + "]");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   143
                }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   144
                break;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   145
            case MAYBE:
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   146
                System.out.println("Adjustment: " + adjustment);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   147
                System.out.println("Can't assert for -1 with offset "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   148
                        + offset + "(" + name + ")"
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   149
                        + " when time in seconds is in ["
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   150
                        + beforeMillis/MILLIS_IN_SECOND + ", "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   151
                        + afterMillis/MILLIS_IN_SECOND
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   152
                        + "]");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   153
                // not conclusive
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   154
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   155
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   156
        if (isOffLimit == Answer.NO || adjustment != -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   157
            System.out.println("Validating adjustment: " + adjustment);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   158
            long expectedMax = distance(offset, beforeMillis/MILLIS_IN_SECOND)
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   159
                      * NANOS_IN_SECOND
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   160
                    + (beforeMillis % MILLIS_IN_SECOND) * NANOS_IN_MILLI
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   161
                    + (afterMillis - beforeMillis + 1) * NANOS_IN_MILLI;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   162
            long absoluteAdjustment = distance(0, adjustment);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   163
            if (absoluteAdjustment > expectedMax) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   164
                long adjSec = absoluteAdjustment / NANOS_IN_SECOND;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   165
                long adjMil = (absoluteAdjustment % NANOS_IN_SECOND) / NANOS_IN_MILLI;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   166
                long adjMic = (absoluteAdjustment % NANOS_IN_MILLI) / NANOS_IN_MICRO;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   167
                long adjNan = (absoluteAdjustment % NANOS_IN_MICRO);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   168
                long expSec = expectedMax / NANOS_IN_SECOND;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   169
                long expMil = (expectedMax % NANOS_IN_SECOND) / NANOS_IN_MILLI;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   170
                long expMic = (expectedMax % NANOS_IN_MILLI) / NANOS_IN_MICRO;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   171
                long expNan = (expectedMax % NANOS_IN_MICRO);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   172
                System.err.println("Excessive adjustment: " + adjSec + "s, "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   173
                        + adjMil + "ms, " + adjMic + "mics, " + adjNan + "ns");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   174
                System.err.println("Epected max: " + expSec + "s, "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   175
                        + expMil + "ms, " + expMic + "mics, " + expNan + "ns");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   176
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   177
                throw new RuntimeException(name
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   178
                    + ": Excessive adjustment: " + adjustment
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   179
                    + " when time in millis is in ["
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   180
                    + beforeMillis + ", " + afterMillis
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   181
                    + "] and offset in seconds is " + offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   182
            }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   183
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   184
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   185
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   186
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   187
    static void regular() {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   188
        System.out.println("*** Testing regular cases ***");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   189
        final long start = System.currentTimeMillis();
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   190
        long offset = start/1000;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   191
        long adjustment = VM.getNanoTimeAdjustment(offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   192
        if (start != offset*1000) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   193
            if (adjustment == -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   194
                throw new RuntimeException("VM says " + offset
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   195
                        + " secs is too far off, but time millis is "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   196
                        + System.currentTimeMillis());
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   197
            }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   198
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   199
        if (adjustment == -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   200
            offset = System.currentTimeMillis()/1000 - 1024;
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   201
            adjustment = VM.getNanoTimeAdjustment(offset);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   202
            if (adjustment == -1) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   203
                throw new RuntimeException("VM says " + offset
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   204
                        + " secs is too far off, but time millis is "
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   205
                        + System.currentTimeMillis());
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   206
            }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   207
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   208
        if (adjustment > (start/1000 - offset + 20)*NANOS_IN_SECOND) {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   209
            throw new RuntimeException("Excessive adjustment: " + adjustment);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   210
        }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   211
        testWithOffset("System.currentTimeMillis()/1000",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   212
                System.currentTimeMillis()/1000);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   213
        testWithOffset("System.currentTimeMillis()/1000 - 1024",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   214
                System.currentTimeMillis()/1000 - 1024);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   215
        testWithOffset("System.currentTimeMillis()/1000 + 1024",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   216
                System.currentTimeMillis()/1000 + 1024);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   217
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   218
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   219
    static void testLimits() {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   220
        System.out.println("*** Testing limits ***");
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   221
        testWithOffset("System.currentTimeMillis()/1000 - MAX_OFFSET + 1",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   222
                System.currentTimeMillis()/1000 - MAX_OFFSET + 1);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   223
        testWithOffset("System.currentTimeMillis()/1000 + MAX_OFFSET - 1",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   224
                System.currentTimeMillis()/1000 + MAX_OFFSET - 1);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   225
        testWithOffset("System.currentTimeMillis()/1000 - MAX_OFFSET",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   226
                System.currentTimeMillis()/1000 - MAX_OFFSET);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   227
        testWithOffset("System.currentTimeMillis()/1000 + MAX_OFFSET",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   228
                System.currentTimeMillis()/1000 + MAX_OFFSET);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   229
        testWithOffset("System.currentTimeMillis()/1000 - MAX_OFFSET - 1024",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   230
                System.currentTimeMillis()/1000 - MAX_OFFSET - 1024);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   231
        testWithOffset("System.currentTimeMillis()/1000 + MAX_OFFSET + 1024",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   232
                System.currentTimeMillis()/1000 + MAX_OFFSET + 1024);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   233
        testWithOffset("0", 0);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   234
        testWithOffset("-1", -1);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   235
        testWithOffset("Integer.MAX_VALUE + System.currentTimeMillis()/1000",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   236
                ((long)Integer.MAX_VALUE) + System.currentTimeMillis()/1000);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   237
        testWithOffset("System.currentTimeMillis()/1000 - Integer.MIN_VALUE",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   238
                System.currentTimeMillis()/1000 - Integer.MIN_VALUE);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   239
        testWithOffset("Long.MAX_VALUE", Long.MAX_VALUE);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   240
        testWithOffset("System.currentTimeMillis()/1000 - Long.MIN_VALUE",
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   241
                (Long.MIN_VALUE + System.currentTimeMillis()/1000)*-1);
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   242
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   243
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   244
    public static void main(String[] args) throws Exception {
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   245
        regular();
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   246
        testLimits();
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   247
    }
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   248
8878e8455f2a 8068730: Increase the precision of the implementation of java.time.Clock.systemUTC()
dfuchs
parents:
diff changeset
   249
}