RFR: 8372625: [Linux] Remove unnecessary logic for supports_fast_thread_cpu_time [v2]

Stefan Johansson sjohanss at openjdk.org
Thu Nov 27 14:01:57 UTC 2025


On Thu, 27 Nov 2025 07:06:31 GMT, Jonas Norlinder <jnorlinder at openjdk.org> wrote:

>> For Linux we test if we can use `CLOCK_THREAD_CPUTIME_ID` but not the clocks supported by `pthread_getcpuclockid()`.
>> 
>> The following comment has been around for at least 18 years according to the git commit history (points to the initial git commit from Dec 1, 2007):
>> 
>> 
>> // Switch to using fast clocks for thread cpu time if
>> // the sys_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 sys_clock_getres()
>> // must return at least tp.tv_sec == 0 which means a resolution
>> // better than 1 sec. This is extra check for reliability.
>> 
>> 
>> There was a time when the Linux kernel did not provide an implementation for `CLOCK_THREAD_CPUTIME_ID` (that arrived with v2.6.12). During this period, glibc emulated support for `clock_gettime(CLOCK_THREAD_CPUTIME_ID)` in userspace for the current thread only. `clock_gettime(CLOCK_THREAD_CPUTIME_ID)` will call `pthread_getcpuclockid` in https://elixir.bootlin.com/glibc/glibc-2.2.5/source/linuxthreads/sysdeps/pthread/getcpuclockid.c. The checks and comment exists because `CLOCK_THREAD_CPUTIME_ID` worked (via emulation) but `pthread_getcpuclockid()` failed. 
>> 
>> During that period of the early 2000s this check was indeed needed. But now sufficient time has passed and we don't support OpenJDK for that old kernel versions/glibc. Therefore, this check should be removed and some code can be cleaned up.
>
> Jonas Norlinder has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Remove dlsym and fix to correct return statement

I think the change looks good. Just a couple of small things that I would like you to fix before integrating.

Found this code in `cpuTimeCounters.cpp`, the comment is no longer correct and can probably be completely removed. Think it makes sense to do it in this change since we now have renamed `fast_thread_cpu_time()` to `total_thread_cpu_time()`:

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);
}

src/hotspot/os/linux/os_linux.cpp line 4954:

> 4952:                  &user_time, &sys_time);
> 4953:   if (count != 13) return -1;
> 4954:   return (jlong)user_time * (1000000000 / os::Posix::clock_tics_per_second());

Please add an additional newline here for readability:
Suggestion:

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

-------------

Marked as reviewed by sjohanss (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/28516#pullrequestreview-3515467117
PR Review Comment: https://git.openjdk.org/jdk/pull/28516#discussion_r2568767743


More information about the hotspot-runtime-dev mailing list