-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8372584: [Linux]: Replace reading proc to get thread user CPU time with clock_gettime #28556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Fixy Remove imports added by IDE Remove imports added by IDE Don't touch bit 3 Fix name
|
👋 Welcome back jnorlinder! A progress list of the required criteria for merging this PR into |
|
@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: 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 24 new commits pushed to the
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) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
|
@JonasNorlinder The following label will be automatically applied to this pull request:
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. |
Webrevs
|
|
/issue add JDK-8210452 |
|
@JonasNorlinder |
|
@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). |
dholmes-ora
left a comment
There was a problem hiding this 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
cl4es
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Microbenchmark looks good
|
/issue remove JDK-8210452 |
|
@JonasNorlinder |
| // Since kernel v2.6.12 the Linux ABI has 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 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the bit encoding could be made somewhat clearer.
// Since kernel v2.6.12 the Linux ABI has had support for encoding the clock types in
// the last three bits. Bit 2 indicates whether a cpu clock refers to a thread or a process.
// Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
// The clock CPUCLOCK_VIRT (0b001) reports the thread's consumed user time.
// POSIX compliant implementations of pthread_getcpuclockid return the clock CPUCLOCK_SCHED
// (0b010) which reports the thread's consumed system+user time (as mandated by the POSIX
// standard POSIX.1-2024/IEEE Std 1003.1-2024 §3.90)
| // for the Linux kernel defaults to construct a clockid 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. | ||
| static clockid_t get_thread_clockid(Thread* thread, bool total, bool* success) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| static clockid_t get_thread_clockid(Thread* thread, bool total, bool* success) { | |
| // The out parameter `success` is required to be initialized to `true`. | |
| static clockid_t get_thread_clockid(Thread* thread, bool total, bool* success) { | |
| assert(success != nullptr && *success, "incorrect initialization"); |
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:
After patch:
Testing:
java/lang/management/ThreadMXBean/ThreadUserTime.javaand the added microbenchmark.Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28556/head:pull/28556$ git checkout pull/28556Update a local copy of the PR:
$ git checkout pull/28556$ git pull https://git.openjdk.org/jdk.git pull/28556/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 28556View PR using the GUI difftool:
$ git pr show -t 28556Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28556.diff
Using Webrev
Link to Webrev Comment