Skip to content

Add monotonic_usec #12

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

Merged
merged 1 commit into from
Jan 16, 2025
Merged

Conversation

Firstyear
Copy link
Contributor

Resolve #11 - add support for the monotonic-usec notification that is required from v253 and onwards for reload to succeed.

@mbuesch
Copy link
Contributor

mbuesch commented Jan 14, 2025

Are you sure that SystemTime uses clock_gettime + CLOCK_MONOTONIC internally?
The documentation of SystemTime says:

Distinct from the Instant type, this time measurement is not monotonic

and the implementation is not guaranteed:

Disclaimer: These system calls might change over time.

https://doc.rust-lang.org/std/time/struct.SystemTime.html

Does systemd compare the sent time stamp to CLOCK_MONOTONIC internally? That would not work, if we sent it a time stamp based off of a different clock.

I think sd-notify should call libc::clock_gettime(CLOCK_MONOTONIC) directly.

@lnicola
Copy link
Owner

lnicola commented Jan 14, 2025

Thanks for the PR. I agree, that should be Instant, but it doesn't expose the values, so clock_gettime is needed.

I'm not sure how I feel about us sending it automatically. At this time (heh), it's documented as:

This is typically used in combination with "RELOADING=1"

I don't see any other use case for it, but:

  • if we send it and add the variant, the user might send it twice by mistake
  • if we send it and don't add the variant, some future use might still require the variant
  • if we don't send it, having to call libc::clock_gettime will be pretty annoying for the user
  • we could expose a function that calls libc::clock_gettime

Overall, I was in favour of not sending it automatically, but it feels like we should just handle it on our side.

@Firstyear
Copy link
Contributor Author

Firstyear commented Jan 15, 2025

Seems we were not the only one bitten:

https://gitlab.isc.org/isc-projects/bind9/-/issues/4377

So I think if a reload message is sent you have to send a monotonic clock message too :(

@Firstyear
Copy link
Contributor Author

I think sd-notify should call libc::clock_gettime(CLOCK_MONOTONIC) directly.

Yep, my mistake :)

@Firstyear
Copy link
Contributor Author

Need to finish fixing the error handling now btw.

@Firstyear
Copy link
Contributor Author

@lnicola With the call to monotonic_time_usecs since that returns io::Result, but in Display we need fmt::Result, do you think we make MonotonicUsec take the i128 and we expose the monotonic_usec fn?

Alternately we could do a constructor on NotifyState::monotonic_usec_now()

@lnicola
Copy link
Owner

lnicola commented Jan 15, 2025

Yeah, let's store it in there and add NotifyState::monotonic_usec_now().

@Firstyear
Copy link
Contributor Author

Okay, should be good for another review :)

@lnicola
Copy link
Owner

lnicola commented Jan 15, 2025

I just pushed a commit to your branch to clean things up a little, can you take a look?

Also r? @mbuesch since you're here 😛.

@Firstyear
Copy link
Contributor Author

@lnicola Yep, thank you! Looks good :)

@lnicola lnicola force-pushed the 20250114-11-monotonic-usec branch 3 times, most recently from 683a62c to 64ca63c Compare January 15, 2025 09:31
/// # Example
///
/// ```
/// # use sd_notify::NotifyState;
Copy link
Owner

Choose a reason for hiding this comment

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

This didn't build (no_run is "don't run", not "don't build"), but should be fine to run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean?

cargo test
...
test src/lib.rs - NotifyState<'_>::monotonic_usec_now (line 116) ... ok

Copy link
Owner

Choose a reason for hiding this comment

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

I had to add the use, otherwise:

---- src/lib.rs - NotifyState<'_>::monotonic_usec_now (line 116) stdout ----
error[E0433]: failed to resolve: use of undeclared type `NotifyState`
 --> src/lib.rs:118:9
  |
4 | let _ = NotifyState::monotonic_usec_now().expect("Failed to read monotonic time");
  |         ^^^^^^^^^^^ use of undeclared type `NotifyState`
  |
help: consider importing this enum
  |
3 + use sd_notify::NotifyState;
  |

And we can remove no_run since it doesn't do anything bad.

Not sure how it passed CI, maybe it didn't run?

@lnicola lnicola force-pushed the 20250114-11-monotonic-usec branch from 64ca63c to 6777699 Compare January 15, 2025 09:58
Copy link
Contributor

@mbuesch mbuesch 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 to me. Thanks to all of you involved :)

@Firstyear
Copy link
Contributor Author

No problem, happy to have helped!

@lnicola lnicola force-pushed the 20250114-11-monotonic-usec branch from 6777699 to 5462699 Compare January 16, 2025 08:33
@lnicola
Copy link
Owner

lnicola commented Jan 16, 2025

I squashed and force-pushed, but tried to preserve the attribution, hope you don't mind. Thanks for the PR!

@lnicola lnicola merged commit 8eb2c5c into lnicola:master Jan 16, 2025
@lnicola
Copy link
Owner

lnicola commented Jan 16, 2025

Available at https://crates.io/crates/sd-notify/0.4.4.

@Firstyear
Copy link
Contributor Author

Thank you so much!

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.

Support monotonic_usec call
3 participants