Skip to content

Conversation

@JonasNorlinder
Copy link
Member

@JonasNorlinder JonasNorlinder commented Nov 28, 2025

Since kernel v2.6.12 the Linux ABI have had support for encoding the clock types in the last three bits. Setting bit to 001 (CPUCLOCK_VIRT) will result in the kernel returning only user time. POSIX compliant implementations of pthread_getcpuclockid for the Linux kernel defaults to construct a clockid that with 010 (CPUCLOCK_SCHED) set, which return system+user time, which is what the POSIX standard mandates, see POSIX.1-2024/IEEE Std 1003.1-2024 §3.90. This patch joins the family of glibc, musl etc. that utilities this bit pattern.

This PR also results in improved performance and thus a reduced observer effect, especially for the 100th percentile (max).

Before patch:

Benchmark                  Mode      Cnt  Score    Error  Units
CPUTime.execute          sample  7506555  0.008 ±  0.001  ms/op
CPUTime.execute:p0.00    sample           0.008           ms/op
CPUTime.execute:p0.50    sample           0.008           ms/op
CPUTime.execute:p0.90    sample           0.008           ms/op
CPUTime.execute:p0.95    sample           0.008           ms/op
CPUTime.execute:p0.99    sample           0.012           ms/op
CPUTime.execute:p0.999   sample           0.015           ms/op
CPUTime.execute:p0.9999  sample           0.021           ms/op
CPUTime.execute:p1.00    sample           1.030           ms/op

After patch:

Benchmark                  Mode      Cnt   Score    Error  Units
CPUTime.execute          sample  8984189  ≈ 10⁻³           ms/op
CPUTime.execute:p0.00    sample           ≈ 10⁻³           ms/op
CPUTime.execute:p0.50    sample           ≈ 10⁻³           ms/op
CPUTime.execute:p0.90    sample           ≈ 10⁻³           ms/op
CPUTime.execute:p0.95    sample           ≈ 10⁻³           ms/op
CPUTime.execute:p0.99    sample            0.001           ms/op
CPUTime.execute:p0.999   sample            0.001           ms/op
CPUTime.execute:p0.9999  sample            0.006           ms/op
CPUTime.execute:p1.00    sample            0.054           ms/op

Testing: java/lang/management/ThreadMXBean/ThreadUserTime.java and the added microbenchmark.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8372584: [Linux]: Replace reading proc to get thread user CPU time with clock_gettime (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28556/head:pull/28556
$ git checkout pull/28556

Update a local copy of the PR:
$ git checkout pull/28556
$ git pull https://git.openjdk.org/jdk.git pull/28556/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28556

View PR using the GUI difftool:
$ git pr show -t 28556

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28556.diff

Using Webrev

Link to Webrev Comment

Fixy

Remove imports added by IDE

Remove imports added by IDE

Don't touch bit 3

Fix name
@bridgekeeper
Copy link

bridgekeeper bot commented Nov 28, 2025

👋 Welcome back jnorlinder! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Nov 28, 2025

@JonasNorlinder This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8372584: [Linux]: Replace reading proc to get thread user CPU time with clock_gettime

Reviewed-by: dholmes, kevinw, redestad

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 69 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@dholmes-ora, @cl4es, @kevinjwalls) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Nov 28, 2025
@openjdk
Copy link

openjdk bot commented Nov 28, 2025

@JonasNorlinder The following label will be automatically applied to this pull request:

  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@JonasNorlinder JonasNorlinder marked this pull request as ready for review November 28, 2025 12:05
@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 28, 2025
@mlbridge
Copy link

mlbridge bot commented Nov 28, 2025

@JonasNorlinder
Copy link
Member Author

/issue add JDK-8210452

@openjdk
Copy link

openjdk bot commented Nov 28, 2025

@JonasNorlinder
Adding additional issue to issue list: 8210452: getCurrentThreadUserTime is 30x-400x times slower then getCurrentThreadCpuTime.

@dholmes-ora
Copy link
Member

@JonasNorlinder this PR isn't fixing two issue. I think JDK-8372584 should just be closed as a duplicate of JDK-8210452 (which I had forgotten about and which @larry-cable did not get further with).

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Overall looks good. I'd forgotten that I found about this in 2018.

A few minor nits.

Can't really comment on the benchmark.

// set, which return system+user time, which is what the POSIX standard mandates, see
// POSIX.1-2024/IEEE Std 1003.1-2024 §3.90.
static clockid_t get_thread_clockid(Thread* thread, bool total, bool* success) {
constexpr clockid_t CLOCK_TYPE_MASK = 3;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the mask be covering 3-bits?

Copy link
Member Author

Choose a reason for hiding this comment

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

No we should not touch bit 3 which encodes if the clock is for a thread of process. See here https://elixir.bootlin.com/linux/v6.17.9/source/include/linux/posix-timers_types.h#L9-L19.

Copy link
Member

Choose a reason for hiding this comment

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

Okay so

encoding the clock types in the last three bits

needs a bit more explanation.

// It's possible to encounter a terminated native thread that failed
// to detach itself from the VM - which should result in ESRCH.
assert_status(rc == ESRCH, rc, "pthread_getcpuclockid failed");
*success = false;
Copy link
Member

Choose a reason for hiding this comment

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

The normal way I've seen this pattern used is to set it to true rather than assuming it was true to begin with.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using a positive outcome like success to encode the outcome make it possible to return like so return success ? os::Linux::thread_cpu_time(clockid) : -1;. I prefer having the -1 at the end as I find this reads easier. If we encode a failure we would need to write return !failure ? os::Linux::thread_cpu_time(clockid) : -1;. Hence, I would prefer keeping this as is as double-negatives may be harder to parse.

Copy link
Member

Choose a reason for hiding this comment

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

Okay but you are setting up a usage requirement without documenting anywhere that that requirement exists.

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer if we kept using the standard. When in Rome...

What we often do is this:

bool get_thread_clockid(Thread* thread, bool total, clock_id_t* is);

or, alternatively, what David wrote.

Copy link
Member

@cl4es cl4es left a comment

Choose a reason for hiding this comment

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

Microbenchmark looks good

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 1, 2025
@JonasNorlinder
Copy link
Member Author

/issue remove JDK-8210452

@openjdk
Copy link

openjdk bot commented Dec 1, 2025

@JonasNorlinder
Removing additional issue from issue list: 8210452.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Dec 2, 2025
@JonasNorlinder
Copy link
Member Author

Thanks for the comments @dholmes-ora and @tstuefe. I changed to aligning the signature to the standard. That way we don't have to document any requirements.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for the updates.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 3, 2025
@JonasNorlinder
Copy link
Member Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Dec 3, 2025
@openjdk
Copy link

openjdk bot commented Dec 3, 2025

@JonasNorlinder
Your change (at version d70c8ca) is now ready to be sponsored by a Committer.

Copy link
Contributor

@kevinjwalls kevinjwalls left a comment

Choose a reason for hiding this comment

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

Looks good - I remember that fix for parsing the program binary name containing brackets, good to have it gone.

@kevinjwalls
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented Dec 3, 2025

Going to push as commit 858d2e4.
Since your change was applied there have been 69 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Dec 3, 2025
@openjdk openjdk bot closed this Dec 3, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Dec 3, 2025
@openjdk
Copy link

openjdk bot commented Dec 3, 2025

@kevinjwalls @JonasNorlinder Pushed as commit 858d2e4.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@mlbridge
Copy link

mlbridge bot commented Dec 23, 2025

Mailing list message from Jaromir Hamala on hotspot-runtime-dev:

On Wed, Dec 3, 2025 at 10:35?AM Kevin Walls <kevinw at openjdk.org> wrote:

Apologies for reviving an old treat. I was experimenting with this change,
and I believe there is a further optimisation opportunity: When clockid has
TID set to 0, then the kernel treats it as 'the current task' (=which is
what getCurrentThreadUserTime() requires) and avoids a radix lookup
required for an arbitrary TID.

The change:
https://github.com/jerrinot/jdk/compare/master...jerrinot:jdk:jh_faster_getCurrentThreadUserTime
The benchmark from https://github.com//pull/28556 (switched to
nanos + more iterations + fork count):

Before:
Benchmark Mode Cnt
Score Error Units
ThreadMXBeanBench.getCurrentThreadUserTime sample 4347067
81.746 ? 0.510 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.00 sample
69.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.50 sample
80.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.90 sample
90.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.95 sample
90.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.99 sample
90.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.999 sample
230.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.9999 sample
1980.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p1.00 sample
653312.000 ns/op

After:
Benchmark Mode Cnt
Score Error Units
ThreadMXBeanBench.getCurrentThreadUserTime sample 5081223
70.813 ? 0.325 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.00 sample
59.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.50 sample
70.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.90 sample
70.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.95 sample
70.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.99 sample
80.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.999 sample
170.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.9999 sample
1830.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p1.00 sample
425472.000 ns/op

There is around 13% latency improvement on average.
It increases coupling to kernel internals a bit further, but the original
patch already does that by poking the lower bits + Linux has a strong
policy on ABI stability.

Would you be interested in merging a similar patch?

Cheers,
Jaromir Hamala

--
?Perfection is achieved, not when there is nothing more to add, but when
there is nothing left to take away.?
Antoine de Saint Exup?ry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/hotspot-runtime-dev/attachments/20251223/a1ae5ed7/attachment-0001.htm>

@mlbridge
Copy link

mlbridge bot commented Jan 5, 2026

Mailing list message from Jonas Norlinder on hotspot-runtime-dev:

Hi Jaromir,

That sounds interesting :), as long as we are confident that your observation is part of the user ABI. Feel free to submit a PR and I will happily review it. Also add a link or reasoning to confirm that it is part of the user ABI.

Thank you,
Jonas

From: hotspot-runtime-dev <hotspot-runtime-dev-retn at openjdk.org> on behalf of Jaromir Hamala <jaromir.hamala at gmail.com>
Date: Tuesday, 23 December 2025 at 14:36
To: Kevin Walls <kevinw at openjdk.org>
Cc: hotspot-runtime-dev at openjdk.org <hotspot-runtime-dev at openjdk.org>
Subject: Re: RFR: 8372584: [Linux]: Replace reading proc to get thread user CPU time with clock_gettime [v7]

On Wed, Dec 3, 2025 at 10:35?AM Kevin Walls <kevinw at openjdk.org<mailto:kevinw at openjdk.org>> wrote:
On Tue, 2 Dec 2025 20:59:41 GMT, Jonas Norlinder <jnorlinder at openjdk.org<mailto:jnorlinder at openjdk.org>> wrote:

Since kernel v2.6.12 the Linux ABI have had support for encoding the clock types in the last three bits. Setting bit to 001 (CPUCLOCK_VIRT) will result in the kernel returning only user time. POSIX compliant implementations of pthread_getcpuclockid for the Linux kernel defaults to construct a clockid that with 010 (CPUCLOCK_SCHED) set, which return system+user time, which is what the POSIX standard mandates, see POSIX.1-2024/IEEE Std 1003.1-2024 ?3.90. This patch joins the family of glibc, musl etc. that utilities this bit pattern.

This PR also results in improved performance and thus a reduced observer effect, especially for the 100th percentile (max).

Before patch:

Benchmark Mode Cnt Score Error Units
CPUTime.execute sample 7506555 0.008 ? 0.001 ms/op
CPUTime.execute:p0.00 sample 0.008 ms/op
CPUTime.execute:p0.50 sample 0.008 ms/op
CPUTime.execute:p0.90 sample 0.008 ms/op
CPUTime.execute:p0.95 sample 0.008 ms/op
CPUTime.execute:p0.99 sample 0.012 ms/op
CPUTime.execute:p0.999 sample 0.015 ms/op
CPUTime.execute:p0.9999 sample 0.021 ms/op
CPUTime.execute:p1.00 sample 1.030 ms/op

After patch:

Benchmark Mode Cnt Score Error Units
CPUTime.execute sample 8984189 ? 10?? ms/op
CPUTime.execute:p0.00 sample ? 10?? ms/op
CPUTime.execute:p0.50 sample ? 10?? ms/op
CPUTime.execute:p0.90 sample ? 10?? ms/op
CPUTime.execute:p0.95 sample ? 10?? ms/op
CPUTime.execute:p0.99 sample 0.001 ms/op
CPUTime.execute:p0.999 sample 0.001 ms/op
CPUTime.execute:p0.9999 sample 0.006 ms/op
CPUTime.execute:p1.00 sample 0.054 ms/op

Testing: `java/lang/management/ThreadMXBean/ThreadUserTime.java` and the added microbenchmark.

Jonas Norlinder has updated the pull request incrementally with one additional commit since the last revision:

Align signature to standard

Looks good - I remember that fix for parsing the program binary name containing brackets, good to have it gone.

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

Marked as reviewed by kevinw (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/28556#pullrequestreview-3534064399

Apologies for reviving an old treat. I was experimenting with this change, and I believe there is a further optimisation opportunity: When clockid has TID set to 0, then the kernel treats it as 'the current task' (=which is what getCurrentThreadUserTime() requires) and avoids a radix lookup required for an arbitrary TID.

The change: https://github.com/jerrinot/jdk/compare/master...jerrinot:jdk:jh_faster_getCurrentThreadUserTime
The benchmark from https://github.com//pull/28556 (switched to nanos + more iterations + fork count):

Before:
Benchmark Mode Cnt Score Error Units
ThreadMXBeanBench.getCurrentThreadUserTime sample 4347067 81.746 ? 0.510 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.00 sample 69.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.50 sample 80.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.90 sample 90.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.95 sample 90.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.99 sample 90.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.999 sample 230.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.9999 sample 1980.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p1.00 sample 653312.000 ns/op

After:
Benchmark Mode Cnt Score Error Units
ThreadMXBeanBench.getCurrentThreadUserTime sample 5081223 70.813 ? 0.325 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.00 sample 59.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.50 sample 70.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.90 sample 70.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.95 sample 70.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.99 sample 80.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.999 sample 170.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p0.9999 sample 1830.000 ns/op
ThreadMXBeanBench.getCurrentThreadUserTime:p1.00 sample 425472.000 ns/op

There is around 13% latency improvement on average.
It increases coupling to kernel internals a bit further, but the original patch already does that by poking the lower bits + Linux has a strong policy on ABI stability.

Would you be interested in merging a similar patch?

Cheers,
Jaromir Hamala

--
?Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.?
Antoine de Saint Exup?ry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/hotspot-runtime-dev/attachments/20260105/3d537e63/attachment-0001.htm>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

6 participants