Skip to content
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

Add accessors for container CPU shares #150

Merged
merged 4 commits into from
Dec 7, 2022

Conversation

carterkozak
Copy link
Contributor

This is difficult to test in a reasonable way.

@carterkozak carterkozak requested a review from schlosna December 6, 2022 20:53
@changelog-app
Copy link

changelog-app bot commented Dec 6, 2022

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Add accessors for container CPU shares

This is difficult to test in a reasonable way.

Check the box to generate changelog(s)

  • Generate changelog entry

Comment on lines +24 to +28
* Returns the relative-weighted CPU shares detected by the JVM. In k8s, this should be a millicore value.
* Returns {@link OptionalLong#empty()} if information is unavailable (and this accessor is not returned itself
* if cpu shares are not supported).
*/
OptionalLong getCpuShares();
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth adding to comments that https://bugs.openjdk.org/browse/JDK-8281181 / https://bugs.openjdk.org/browse/JDK-8281571 / https://bugs.openjdk.org/browse/JDK-8279484 / openjdk/jdk@e07fd39 immediately deprecates UseContainerCpuShares in JDK 19, 11.0.17, and 17.0.5

Copy link
Contributor

Choose a reason for hiding this comment

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

May be also worth mentioning https://danluu.com/cgroup-throttling/

private final jdk.internal.platform.Metrics metrics = jdk.internal.platform.Metrics.systemMetrics();

boolean isEnabled() {
return metrics.getCpuShares() != -2;
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to distinguish between "I know what cpu shares are, but I'm not using them" from "I'm not sure how to tell if cpu shares are in play". In the former case, we return an accessor that provides OptionalLong.emty, and in the latter case we return an empty Optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you prefer to avoid that distinction?

Copy link
Contributor

Choose a reason for hiding this comment

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

I assume you'll expose this as a gauge metric? Would you consume it as something like this?

JvmDiagnostics.cpuShares().ifPresent(shares -> registerGauge(() -> shares.getAsLong()))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both a gauge metric, and our startup logging (the latter is where we can distinguish between unavailable and unsupported)

* Returns an {@link HotspotCpuSharesAccessor}. This functionality is not supported on all java runtimes,
* and an {@link Optional#empty()} is returned in cases cpu share information is not supported.
*/
public static Optional<CpuSharesAccessor> cpuShares() {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we test this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a great way to do so with the tools we have in this repo, unless we match the environment circle uses -- unfortunately tests won't work locally in that case

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious what circle returns on say OpenJDK 17.0.5 vs 17.0.4. We could add some JUnit assumptions around JDK version, OS, and compare to Runtime number of processors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this value will return the same results, but processor count may not (certainly not in our metrics)

@bulldozer-bot bulldozer-bot bot merged commit 563a6bc into develop Dec 7, 2022
@bulldozer-bot bulldozer-bot bot deleted the ckozak/detect_cpu_shares branch December 7, 2022 15:42
@svc-autorelease
Copy link
Collaborator

Released 0.3.0

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

Successfully merging this pull request may close these issues.

4 participants