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

fix(win): inconsistent boot_time #1372

Merged
merged 2 commits into from
Oct 31, 2024
Merged

Conversation

samvdst
Copy link
Contributor

@samvdst samvdst commented Oct 31, 2024

I noticed inconsistent boot times when using the System::boot_time() function on Windows. This PR aims to fix this.

Refines the boot_time function on Windows to improve consistency by enhancing precision in the uptime calculation. The updated version uses nanoseconds throughout to avoid rounding errors, and it caps the result safely within u64 limits.

@@ -63,7 +63,14 @@ unsafe impl<T> Sync for Wrap<T> {}

unsafe fn boot_time() -> u64 {
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n.as_secs().saturating_sub(GetTickCount64() / 1_000),
Ok(n) => {
let system_time_ns = n.as_nanos();
Copy link
Owner

Choose a reason for hiding this comment

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

Please add your explanation here as a comment as future me will likely wonder what the hell is going on in here. 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We were effectively rounding twice: Duration::as_secs discards nanosecond precision, and GetTickCount64() / 1_000 truncates due to integer division. This leads to inconsistent seconds depending on how each value is rounded.

By using nanoseconds for the subtraction and converting to seconds only once at the end, we achieve consistent rounding and stable results.

Hi future Guillaume! 👋

Copy link
Owner

Choose a reason for hiding this comment

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

No I meant as a code comment in the code. 😛

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 must admit I was slightly confused 🤣 I'll add a comment!

@GuillaumeGomez
Copy link
Owner

Apart from the code comment, looks good to me, thanks!

@GuillaumeGomez
Copy link
Owner

Awesome, thanks! Once CI run is done, I'll merge.

@GuillaumeGomez GuillaumeGomez merged commit dc79f96 into GuillaumeGomez:master Oct 31, 2024
67 checks passed
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.

2 participants