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

build: Annotate with units from Checker Framework #1712

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jasonschroeder-sfdc
Copy link
Collaborator

This is a proof-of-concept as I wanted to learn more about this framework.

It's a bit verbose so I'm not convinced. I didn't notice any build time degredations.

If you want to pull it and try, just switch one of those @s(Prefix.micro) to @s(Prefix.nano) and watch the bazel build //src/... will fail.

If we do like this, I can try to tidy it up by adding a convenience @microseconds or @us(not very read-able) Annotations per https://checkerframework.org/manual/#extending-units

This is a proof-of-concept as I wanted to learn more about this
framework.
Comment on lines +44 to +47
@SuppressWarnings("units")
public static @s(Prefix.micro) long stopwatchToMicroseconds(Stopwatch sw) {
return sw.elapsed(MICROSECONDS);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Convenience method to convert a stopwatch to a long annotated as microseconds.

Comment on lines +341 to 343
@SuppressWarnings("units")
@s(Prefix.micro)
long stallUSecs = 0;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Assigning constants needed a warning suppression, including in the Checker Framework examples. https://github.com/typetools/checker-framework/blob/master/docs/examples/units-extension/UnitsExtensionDemo.java

@@ -101,7 +104,7 @@ private long runInterruptible(Stopwatch stopwatch, ResourceLimits limits)
metadata = operationContext.operation.getMetadata().unpack(ExecuteOperationMetadata.class);
} catch (InvalidProtocolBufferException e) {
log.log(Level.SEVERE, "invalid execute operation metadata", e);
return 0;
return ZERO_MICROSECONDS;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was rather annoying as Checker Framework refused to return "unit-less" 0 constant.

(arguably, 0 probably doesn't need units as 0 seconds == 0 minutes, but I suppose 0 degrees Celsius is not the same as 0 degrees Fahrenheit)

Comment on lines -108 to +115
executionTime.observe(usecs / 1000.0);
executionStallTime.observe(stallUSecs / 1000.0);
executionTime.observe(TimeUnit.MILLISECONDS.convert(uSecs, TimeUnit.MICROSECONDS));
executionStallTime.observe(TimeUnit.MILLISECONDS.convert(stallUSecs, TimeUnit.MICROSECONDS));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This wasn't part of the CheckerFramework, but I just didn't trust myself to do unit conversions :) Can revert..

@jasonschroeder-sfdc jasonschroeder-sfdc marked this pull request as ready for review May 4, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant