Navigation Menu

Skip to content

Commit

Permalink
8246112: Remove build-time and run-time checks for clock_gettime and …
Browse files Browse the repository at this point in the history
…CLOCK_MONOTONIC

Reviewed-by: ihse, erikj, gziemski, hseigel
  • Loading branch information
David Holmes committed Jan 27, 2021
1 parent 19b6f61 commit 6f2be9c
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 357 deletions.
21 changes: 1 addition & 20 deletions make/autoconf/flags-cflags.m4
@@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -605,25 +605,6 @@ AC_DEFUN([FLAGS_SETUP_CFLAGS_HELPER],
fi
fi
# Optional POSIX functionality needed by the JVM
#
# Check if clock_gettime is available and in which library. This indicates
# availability of CLOCK_MONOTONIC for hotspot. But we don't need to link, so
# don't let it update LIBS.
save_LIBS="$LIBS"
AC_SEARCH_LIBS(clock_gettime, rt, [HAS_CLOCK_GETTIME=true], [])
if test "x$LIBS" = "x-lrt "; then
CLOCK_GETTIME_IN_LIBRT=true
fi
LIBS="$save_LIBS"
if test "x$HAS_CLOCK_GETTIME" = "xtrue"; then
OS_CFLAGS_JVM="$OS_CFLAGS_JVM -DSUPPORTS_CLOCK_MONOTONIC"
if test "x$CLOCK_GETTIME_IN_LIBRT" = "xtrue"; then
OS_CFLAGS_JVM="$OS_CFLAGS_JVM -DNEEDS_LIBRT"
fi
fi
# Extra flags needed when building optional static versions of certain
# JDK libraries.
STATIC_LIBS_CFLAGS="-DSTATIC_BUILD=1"
Expand Down
9 changes: 8 additions & 1 deletion make/autoconf/flags-ldflags.m4
@@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -108,6 +108,13 @@ AC_DEFUN([FLAGS_SETUP_LDFLAGS_HELPER],
OS_LDFLAGS_JVM_ONLY="-Wl,-rpath,@loader_path/. -Wl,-rpath,@loader_path/.."
OS_LDFLAGS="-mmacosx-version-min=$MACOSX_VERSION_MIN"
fi
if test "x$OPENJDK_TARGET_OS" = xlinux; then
# Hotspot needs to link librt to get the clock_* functions.
# But once our supported minimum build and runtime platform
# has glibc 2.17, this can be removed as the functions are
# in libc.
OS_LDFLAGS_JVM_ONLY="-lrt"
fi
fi
# Setup debug level-dependent LDFLAGS
Expand Down
18 changes: 1 addition & 17 deletions src/hotspot/os/aix/os_aix.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -959,21 +959,6 @@ double os::elapsedVTime() {
}
}

jlong os::javaTimeMillis() {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "aix error at gettimeofday()");
return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);
}

void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "aix error at gettimeofday()");
seconds = jlong(time.tv_sec);
nanos = jlong(time.tv_usec) * 1000;
}

// We use mread_real_time here.
// On AIX: If the CPU has a time register, the result will be RTC_POWER and
// it has to be converted to real time. AIX documentations suggests to do
Expand Down Expand Up @@ -3290,4 +3275,3 @@ bool os::supports_map_sync() {
}

void os::print_memory_mappings(char* addr, size_t bytes, outputStream* st) {}

5 changes: 0 additions & 5 deletions src/hotspot/os/aix/os_aix.inline.hpp
Expand Up @@ -107,11 +107,6 @@ inline struct hostent* os::get_host_by_name(char* name) {
return ::gethostbyname(name);
}

inline bool os::supports_monotonic_clock() {
// mread_real_time() is monotonic on AIX (see os::javaTimeNanos() comments)
return true;
}

inline void os::exit(int num) {
::exit(num);
}
Expand Down
72 changes: 6 additions & 66 deletions src/hotspot/os/bsd/os_bsd.cpp
Expand Up @@ -123,8 +123,6 @@ julong os::Bsd::_physical_memory = 0;
#ifdef __APPLE__
mach_timebase_info_data_t os::Bsd::_timebase_info = {0, 0};
volatile uint64_t os::Bsd::_max_abstime = 0;
#else
int (*os::Bsd::_clock_gettime)(clockid_t, struct timespec *) = NULL;
#endif
pthread_t os::Bsd::_main_thread;
int os::Bsd::_page_size = -1;
Expand Down Expand Up @@ -789,40 +787,13 @@ double os::elapsedVTime() {
return elapsedTime();
}

jlong os::javaTimeMillis() {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "bsd error");
return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);
}

void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "bsd error");
seconds = jlong(time.tv_sec);
nanos = jlong(time.tv_usec) * 1000;
}

#ifndef __APPLE__
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC (1)
#endif
#endif

#ifdef __APPLE__
void os::Bsd::clock_init() {
mach_timebase_info(&_timebase_info);
}
#else
void os::Bsd::clock_init() {
struct timespec res;
struct timespec tp;
if (::clock_getres(CLOCK_MONOTONIC, &res) == 0 &&
::clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
// yes, monotonic clock is supported
_clock_gettime = ::clock_gettime;
}
// Nothing to do
}
#endif

Expand Down Expand Up @@ -854,45 +825,15 @@ jlong os::javaTimeNanos() {
return (prev == obsv) ? now : obsv;
}

#else // __APPLE__

jlong os::javaTimeNanos() {
if (os::supports_monotonic_clock()) {
struct timespec tp;
int status = Bsd::_clock_gettime(CLOCK_MONOTONIC, &tp);
assert(status == 0, "gettime error");
jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
return result;
} else {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "bsd error");
jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
return 1000 * usecs;
}
void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
info_ptr->max_value = ALL_64_BITS;
info_ptr->may_skip_backward = false; // not subject to resetting or drifting
info_ptr->may_skip_forward = false; // not subject to resetting or drifting
info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time
}

#endif // __APPLE__

void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
if (os::supports_monotonic_clock()) {
info_ptr->max_value = ALL_64_BITS;

// CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
info_ptr->may_skip_backward = false; // not subject to resetting or drifting
info_ptr->may_skip_forward = false; // not subject to resetting or drifting
} else {
// gettimeofday - based on time in seconds since the Epoch thus does not wrap
info_ptr->max_value = ALL_64_BITS;

// gettimeofday is a real time clock so it skips
info_ptr->may_skip_backward = true;
info_ptr->may_skip_forward = true;
}

info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time
}

// Return the real, user, and system times in seconds from an
// arbitrary fixed point in the past.
bool os::getTimesSecs(double* process_real_time,
Expand Down Expand Up @@ -2812,4 +2753,3 @@ bool os::start_debugging(char *buf, int buflen) {
}

void os::print_memory_mappings(char* addr, size_t bytes, outputStream* st) {}

4 changes: 1 addition & 3 deletions src/hotspot/os/bsd/os_bsd.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -37,8 +37,6 @@ class Bsd {
// mach_absolute_time
static mach_timebase_info_data_t _timebase_info;
static volatile uint64_t _max_abstime;
#else
static int (*_clock_gettime)(clockid_t, struct timespec *);
#endif

static GrowableArray<int>* _cpu_to_node;
Expand Down
8 changes: 0 additions & 8 deletions src/hotspot/os/bsd/os_bsd.inline.hpp
Expand Up @@ -109,14 +109,6 @@ inline struct hostent* os::get_host_by_name(char* name) {
return ::gethostbyname(name);
}

inline bool os::supports_monotonic_clock() {
#ifdef __APPLE__
return true;
#else
return Bsd::_clock_gettime != NULL;
#endif
}

inline void os::exit(int num) {
::exit(num);
}
Expand Down
11 changes: 6 additions & 5 deletions src/hotspot/os/bsd/os_perf_bsd.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -56,12 +56,13 @@ class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
int _active_processor_count;

bool now_in_nanos(long* resultp) {
timeval current_time;
if (gettimeofday(&current_time, NULL) != 0) {
// Error getting current time
struct timespec tp;
int status = clock_gettime(CLOCK_REALTIME, &tp);
assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
if (status != 0) {
return false;
}
*resultp = current_time.tv_sec * NANOS_PER_SEC + 1000L * current_time.tv_usec;
*resultp = tp.tv_sec * NANOS_PER_SEC + tp.tv_nsec;
return true;
}

Expand Down
84 changes: 3 additions & 81 deletions src/hotspot/os/linux/os_linux.cpp
Expand Up @@ -1268,10 +1268,6 @@ void os::Linux::capture_initial_stack(size_t max_size) {
////////////////////////////////////////////////////////////////////////////////
// time support

#ifndef SUPPORTS_CLOCK_MONOTONIC
#error "Build platform doesn't support clock_gettime and related functionality"
#endif

// Time since start-up in seconds to a fine granularity.
// Used by VMSelfDestructTimer and the MemProfiler.
double os::elapsedTime() {
Expand Down Expand Up @@ -1300,38 +1296,6 @@ double os::elapsedVTime() {
}
}

jlong os::javaTimeMillis() {
if (os::Posix::supports_clock_gettime()) {
struct timespec ts;
int status = os::Posix::clock_gettime(CLOCK_REALTIME, &ts);
assert_status(status == 0, status, "gettime error");
return jlong(ts.tv_sec) * MILLIUNITS +
jlong(ts.tv_nsec) / NANOUNITS_PER_MILLIUNIT;
} else {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "linux error");
return jlong(time.tv_sec) * MILLIUNITS +
jlong(time.tv_usec) / (MICROUNITS / MILLIUNITS);
}
}

void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {
if (os::Posix::supports_clock_gettime()) {
struct timespec ts;
int status = os::Posix::clock_gettime(CLOCK_REALTIME, &ts);
assert_status(status == 0, status, "gettime error");
seconds = jlong(ts.tv_sec);
nanos = jlong(ts.tv_nsec);
} else {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "linux error");
seconds = jlong(time.tv_sec);
nanos = jlong(time.tv_usec) * (NANOUNITS / MICROUNITS);
}
}

void os::Linux::fast_thread_clock_init() {
if (!UseLinuxPosixThreadCPUClocks) {
return;
Expand All @@ -1352,47 +1316,12 @@ void os::Linux::fast_thread_clock_init() {

if (pthread_getcpuclockid_func &&
pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
os::Posix::clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
_supports_fast_thread_cpu_time = true;
_pthread_getcpuclockid = pthread_getcpuclockid_func;
}
}

jlong os::javaTimeNanos() {
if (os::supports_monotonic_clock()) {
struct timespec tp;
int status = os::Posix::clock_gettime(CLOCK_MONOTONIC, &tp);
assert(status == 0, "gettime error");
jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
return result;
} else {
timeval time;
int status = gettimeofday(&time, NULL);
assert(status != -1, "linux error");
jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
return 1000 * usecs;
}
}

void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
if (os::supports_monotonic_clock()) {
info_ptr->max_value = ALL_64_BITS;

// CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
info_ptr->may_skip_backward = false; // not subject to resetting or drifting
info_ptr->may_skip_forward = false; // not subject to resetting or drifting
} else {
// gettimeofday - based on time in seconds since the Epoch thus does not wrap
info_ptr->max_value = ALL_64_BITS;

// gettimeofday is a real time clock so it skips
info_ptr->may_skip_backward = true;
info_ptr->may_skip_forward = true;
}

info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time
}

// Return the real, user, and system times in seconds from an
// arbitrary fixed point in the past.
bool os::getTimesSecs(double* process_real_time,
Expand Down Expand Up @@ -4344,9 +4273,8 @@ OSReturn os::get_native_priority(const Thread* const thread,

jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {
struct timespec tp;
int rc = os::Posix::clock_gettime(clockid, &tp);
assert(rc == 0, "clock_gettime is expected to return 0 code");

int status = clock_gettime(clockid, &tp);
assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;
}

Expand Down Expand Up @@ -4454,12 +4382,6 @@ void os::init(void) {
os::Posix::init();

initial_time_count = javaTimeNanos();

// Always warn if no monotonic clock available
if (!os::Posix::supports_monotonic_clock()) {
warning("No monotonic clock was available - timed services may " \
"be adversely affected if the time-of-day clock changes");
}
}

// To install functions for atexit system call
Expand Down
4 changes: 0 additions & 4 deletions src/hotspot/os/linux/os_linux.inline.hpp
Expand Up @@ -101,10 +101,6 @@ inline struct hostent* os::get_host_by_name(char* name) {
return ::gethostbyname(name);
}

inline bool os::supports_monotonic_clock() {
return os::Posix::supports_monotonic_clock();
}

inline void os::exit(int num) {
::exit(num);
}
Expand Down

1 comment on commit 6f2be9c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.