Skip to content

Commit 43f89be

Browse files
author
duke
committedMay 29, 2020
Automatic merge of jdk:master into master
2 parents 6330c48 + 7228978 commit 43f89be

File tree

6 files changed

+62
-31
lines changed

6 files changed

+62
-31
lines changed
 

‎src/hotspot/os/linux/os_linux.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -1376,18 +1376,35 @@ double os::elapsedVTime() {
13761376
}
13771377

13781378
jlong os::javaTimeMillis() {
1379-
timeval time;
1380-
int status = gettimeofday(&time, NULL);
1381-
assert(status != -1, "linux error");
1382-
return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);
1379+
if (os::Posix::supports_clock_gettime()) {
1380+
struct timespec ts;
1381+
int status = os::Posix::clock_gettime(CLOCK_REALTIME, &ts);
1382+
assert_status(status == 0, status, "gettime error");
1383+
return jlong(ts.tv_sec) * MILLIUNITS +
1384+
jlong(ts.tv_nsec) / NANOUNITS_PER_MILLIUNIT;
1385+
} else {
1386+
timeval time;
1387+
int status = gettimeofday(&time, NULL);
1388+
assert(status != -1, "linux error");
1389+
return jlong(time.tv_sec) * MILLIUNITS +
1390+
jlong(time.tv_usec) / (MICROUNITS / MILLIUNITS);
1391+
}
13831392
}
13841393

13851394
void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {
1386-
timeval time;
1387-
int status = gettimeofday(&time, NULL);
1388-
assert(status != -1, "linux error");
1389-
seconds = jlong(time.tv_sec);
1390-
nanos = jlong(time.tv_usec) * 1000;
1395+
if (os::Posix::supports_clock_gettime()) {
1396+
struct timespec ts;
1397+
int status = os::Posix::clock_gettime(CLOCK_REALTIME, &ts);
1398+
assert_status(status == 0, status, "gettime error");
1399+
seconds = jlong(ts.tv_sec);
1400+
nanos = jlong(ts.tv_nsec);
1401+
} else {
1402+
timeval time;
1403+
int status = gettimeofday(&time, NULL);
1404+
assert(status != -1, "linux error");
1405+
seconds = jlong(time.tv_sec);
1406+
nanos = jlong(time.tv_usec) * (NANOUNITS / MICROUNITS);
1407+
}
13911408
}
13921409

13931410
void os::Linux::fast_thread_clock_init() {

‎src/hotspot/os/posix/os_posix.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,8 @@ void os::Posix::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
16381638
int (*os::Posix::_clock_gettime)(clockid_t, struct timespec *) = NULL;
16391639
int (*os::Posix::_clock_getres)(clockid_t, struct timespec *) = NULL;
16401640

1641+
bool os::Posix::_supports_monotonic_clock = false;
1642+
16411643
static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t) = NULL;
16421644

16431645
static bool _use_clock_monotonic_condattr = false;
@@ -1653,7 +1655,7 @@ void os::Posix::init(void) {
16531655

16541656
void* handle = NULL;
16551657

1656-
// For linux we need librt, for other OS we can find
1658+
// For older linux we need librt, for other OS we can find
16571659
// this function in regular libc.
16581660
#ifdef NEEDS_LIBRT
16591661
// We do dlopen's in this particular order due to bug in linux
@@ -1673,22 +1675,16 @@ void os::Posix::init(void) {
16731675
int (*clock_gettime_func)(clockid_t, struct timespec*) =
16741676
(int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
16751677
if (clock_getres_func != NULL && clock_gettime_func != NULL) {
1678+
_clock_gettime = clock_gettime_func;
1679+
_clock_getres = clock_getres_func;
16761680
// We assume that if both clock_gettime and clock_getres support
16771681
// CLOCK_MONOTONIC then the OS provides true high-res monotonic clock.
16781682
struct timespec res;
16791683
struct timespec tp;
16801684
if (clock_getres_func(CLOCK_MONOTONIC, &res) == 0 &&
16811685
clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
16821686
// Yes, monotonic clock is supported.
1683-
_clock_gettime = clock_gettime_func;
1684-
_clock_getres = clock_getres_func;
1685-
} else {
1686-
#ifdef NEEDS_LIBRT
1687-
// Close librt if there is no monotonic clock.
1688-
if (handle != RTLD_DEFAULT) {
1689-
dlclose(handle);
1690-
}
1691-
#endif
1687+
_supports_monotonic_clock = true;
16921688
}
16931689
}
16941690

‎src/hotspot/os/posix/os_posix.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,18 @@ class Posix {
128128
#ifdef SUPPORTS_CLOCK_MONOTONIC
129129

130130
private:
131+
static bool _supports_monotonic_clock;
131132
// These need to be members so we can access them from inline functions
132133
static int (*_clock_gettime)(clockid_t, struct timespec *);
133134
static int (*_clock_getres)(clockid_t, struct timespec *);
134135
public:
135136
static bool supports_monotonic_clock();
137+
static bool supports_clock_gettime();
136138
static int clock_gettime(clockid_t clock_id, struct timespec *tp);
137139
static int clock_getres(clockid_t clock_id, struct timespec *tp);
138-
139140
#else
140-
141141
static bool supports_monotonic_clock() { return false; }
142-
142+
static bool supports_clock_gettime() { return false; }
143143
#endif
144144

145145
static void to_RTC_abstime(timespec* abstime, int64_t millis);

‎src/hotspot/os/posix/os_posix.inline.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
// Exported clock functionality
3333

3434
inline bool os::Posix::supports_monotonic_clock() {
35+
return _supports_monotonic_clock;
36+
}
37+
38+
inline bool os::Posix::supports_clock_gettime() {
3539
return _clock_gettime != NULL;
3640
}
3741

‎test/jdk/java/time/test/java/time/TestClock_System.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -177,7 +177,8 @@ public void test_ClockResolution() {
177177
+ formatTime("\n\thighest1", highest1));
178178
}
179179

180-
int count=0;
180+
int countBetterThanMillisPrecision = 0;
181+
int countBetterThanMicrosPrecision = 0;
181182
// let's preheat the system a bit:
182183
int lastNanos = 0;
183184
for (int i = 0; i < 1000 ; i++) {
@@ -191,7 +192,10 @@ public void test_ClockResolution() {
191192
lastNanos = nanos;
192193

193194
if ((nanos % 1000000) > 0) {
194-
count++; // we have micro seconds
195+
countBetterThanMillisPrecision++; // we have microseconds
196+
}
197+
if ((nanos % 1000) > 0) {
198+
countBetterThanMicrosPrecision++; // we have nanoseconds
195199
}
196200
if ((sysnan % 1000000) > 0) {
197201
throw new RuntimeException("Expected only millisecconds "
@@ -200,13 +204,17 @@ public void test_ClockResolution() {
200204
}
201205
}
202206
System.out.println("\nNumber of time stamps which had better than"
203-
+ " millisecond precision: "+count+"/"+1000);
207+
+ " millisecond precision: "
208+
+ countBetterThanMillisPrecision + "/" + 1000);
209+
System.out.println("\nNumber of time stamps which had better than"
210+
+ " microsecond precision: "
211+
+ countBetterThanMicrosPrecision + "/" + 1000);
204212
System.out.println(formatTime("\nsystemUTC ", system1));
205213
System.out.println(formatTime("highestResolutionUTC ", highest1));
206-
if (count == 0) {
214+
if (countBetterThanMillisPrecision == 0) {
207215
System.err.println("Something is strange: no microsecond "
208-
+ "precision with highestResolutionUTC?");
209-
throw new RuntimeException("Micro second preccision not reached");
216+
+ "precision with highestResolutionUTC?");
217+
throw new RuntimeException("Micro second precision not reached");
210218
}
211219

212220
// check again

‎test/micro/org/openjdk/bench/java/lang/Systems.java ‎test/micro/org/openjdk/bench/java/lang/SystemTime.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,11 +27,12 @@
2727
import org.openjdk.jmh.annotations.Mode;
2828
import org.openjdk.jmh.annotations.OutputTimeUnit;
2929

30+
import java.time.Instant;
3031
import java.util.concurrent.TimeUnit;
3132

3233
@BenchmarkMode(Mode.AverageTime)
3334
@OutputTimeUnit(TimeUnit.NANOSECONDS)
34-
public class Systems {
35+
public class SystemTime {
3536

3637
@Benchmark
3738
public long currentTimeMillis() {
@@ -43,4 +44,9 @@ public long nanoTime() {
4344
return System.nanoTime();
4445
}
4546

47+
@Benchmark
48+
public long instantNowAsEpochNanos() {
49+
Instant now = Instant.now();
50+
return now.getEpochSecond() * 1_000_000_000L + now.getNano();
51+
}
4652
}

0 commit comments

Comments
 (0)
Please sign in to comment.