Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 16 additions & 56 deletions src/hotspot/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ physical_memory_size_type os::Linux::_physical_memory = 0;
address os::Linux::_initial_thread_stack_bottom = nullptr;
uintptr_t os::Linux::_initial_thread_stack_size = 0;

int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = nullptr;
int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = nullptr;
pthread_t os::Linux::_main_thread;
bool os::Linux::_supports_fast_thread_cpu_time = false;
const char * os::Linux::_libc_version = nullptr;
const char * os::Linux::_libpthread_version = nullptr;

Expand Down Expand Up @@ -1529,29 +1527,6 @@ double os::elapsedVTime() {
}
}

void os::Linux::fast_thread_clock_init() {
clockid_t clockid;
struct timespec tp;
int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
(int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");

// Switch to using fast clocks for thread cpu time if
// the clock_getres() returns 0 error code.
// Note, that some kernels may support the current thread
// clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
// returned by the pthread_getcpuclockid().
// If the fast POSIX clocks are supported then the clock_getres()
// must return at least tp.tv_sec == 0 which means a resolution
// better than 1 sec. This is extra check for reliability.

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

// thread_id is kernel thread id (similar to Solaris LWP id)
intx os::current_thread_id() { return os::Linux::gettid(); }
int os::current_process_id() {
Expand Down Expand Up @@ -4377,7 +4352,7 @@ OSReturn os::get_native_priority(const Thread* const thread,
// For reference, please, see IEEE Std 1003.1-2004:
// http://www.unix.org/single_unix_specification

jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {
jlong os::Linux::total_thread_cpu_time(clockid_t clockid) {
struct timespec tp;
int status = clock_gettime(clockid, &tp);
assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
Expand Down Expand Up @@ -4690,8 +4665,6 @@ jint os::init_2(void) {

os::Posix::init_2();

Linux::fast_thread_clock_init();

if (PosixSignals::init() == JNI_ERR) {
return JNI_ERR;
}
Expand Down Expand Up @@ -5118,14 +5091,14 @@ int os::open(const char *path, int oflag, int mode) {
return fd;
}

static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);
static jlong user_thread_cpu_time(Thread *thread);

static jlong fast_cpu_time(Thread *thread) {
static jlong total_thread_cpu_time(Thread *thread) {
clockid_t clockid;
int rc = os::Linux::pthread_getcpuclockid(thread->osthread()->pthread_id(),
int rc = pthread_getcpuclockid(thread->osthread()->pthread_id(),
&clockid);
if (rc == 0) {
return os::Linux::fast_thread_cpu_time(clockid);
return os::Linux::total_thread_cpu_time(clockid);
} else {
// It's possible to encounter a terminated native thread that failed
// to detach itself from the VM - which should result in ESRCH.
Expand All @@ -5142,41 +5115,31 @@ static jlong fast_cpu_time(Thread *thread) {
// the fast estimate available on the platform.

jlong os::current_thread_cpu_time() {
if (os::Linux::supports_fast_thread_cpu_time()) {
return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
} else {
// return user + sys since the cost is the same
return slow_thread_cpu_time(Thread::current(), true /* user + sys */);
}
return os::Linux::total_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
}

jlong os::thread_cpu_time(Thread* thread) {
// consistent with what current_thread_cpu_time() returns
if (os::Linux::supports_fast_thread_cpu_time()) {
return fast_cpu_time(thread);
} else {
return slow_thread_cpu_time(thread, true /* user + sys */);
}
return total_thread_cpu_time(thread);
}

jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
if (user_sys_cpu_time) {
return os::Linux::total_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
} else {
return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
return user_thread_cpu_time(Thread::current());
}
}

jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
return fast_cpu_time(thread);
if (user_sys_cpu_time) {
return total_thread_cpu_time(thread);
} else {
return slow_thread_cpu_time(thread, user_sys_cpu_time);
return user_thread_cpu_time(thread);
}
}

// -1 on error.
static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
static jlong user_thread_cpu_time(Thread *thread) {
pid_t tid = thread->osthread()->thread_id();
char *s;
char stat[2048];
Expand Down Expand Up @@ -5213,11 +5176,8 @@ static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
&ldummy, &ldummy, &ldummy, &ldummy, &ldummy,
&user_time, &sys_time);
if (count != 13) return -1;
if (user_sys_cpu_time) {
return ((jlong)sys_time + (jlong)user_time) * (1000000000 / os::Posix::clock_tics_per_second());
} else {
return (jlong)user_time * (1000000000 / os::Posix::clock_tics_per_second());
}

return (jlong)user_time * (1000000000 / os::Posix::clock_tics_per_second());
}

void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
Expand Down
16 changes: 1 addition & 15 deletions src/hotspot/os/linux/os_linux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
class os::Linux {
friend class os;

static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
static int (*_pthread_setname_np)(pthread_t, const char*);

static address _initial_thread_stack_bottom;
Expand All @@ -41,8 +40,6 @@ class os::Linux {
static const char *_libc_version;
static const char *_libpthread_version;

static bool _supports_fast_thread_cpu_time;

static GrowableArray<int>* _cpu_to_node;
static GrowableArray<int>* _nindex_to_node;

Expand Down Expand Up @@ -145,18 +142,7 @@ class os::Linux {
static bool manually_expand_stack(JavaThread * t, address addr);
static void expand_stack_to(address bottom);

// fast POSIX clocks support
static void fast_thread_clock_init(void);

static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
}

static bool supports_fast_thread_cpu_time() {
return _supports_fast_thread_cpu_time;
}

static jlong fast_thread_cpu_time(clockid_t clockid);
static jlong total_thread_cpu_time(clockid_t clockid);

static jlong sendfile(int out_fd, int in_fd, jlong* offset, jlong count);

Expand Down
3 changes: 0 additions & 3 deletions src/hotspot/share/runtime/cpuTimeCounters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,5 @@ ThreadTotalCPUTimeClosure::~ThreadTotalCPUTimeClosure() {
}

void ThreadTotalCPUTimeClosure::do_thread(Thread* thread) {
// The default code path (fast_thread_cpu_time()) asserts that
// pthread_getcpuclockid() and clock_gettime() must return 0. Thus caller
// must ensure the thread exists and has not terminated.
_total += os::thread_cpu_time(thread);
}