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

Support CLOCK_MONOTONIC in clock_gettime #9

Closed
ian-h-chamberlain opened this issue Feb 1, 2022 · 1 comment · Fixed by #10
Closed

Support CLOCK_MONOTONIC in clock_gettime #9

ian-h-chamberlain opened this issue Feb 1, 2022 · 1 comment · Fixed by #10

Comments

@ian-h-chamberlain
Copy link
Member

ian-h-chamberlain commented Feb 1, 2022

From discussion starting at Meziu/rust-horizon#6 (comment)


Looking at the implementation of osGetTime (which is used by gettimeofday, which in turn is used by our clock_gettime impl for CLOCK_REALTIME), they appear to do some adjustment for clock drift in an attempt to get monotonic values, but I'm not sure if that implies the value from svcGetSystemTick() is not normally monotonic? There is also an epoch adjustment from 1900 to 1970 here for gettimeofday, but I don't think that would be strictly needed for a monotonic clock_gettime...

So maybe we could try our own impl that basically looks like this?

let now = svcGetSystemTick();
// Not sure if the math is quite right but you get the idea...
(*tp).tv_sec = now / SYSCLOCK_ARM11;
(*tp).tv_nsec = 1000 * (now - SYSCLOCK_ARM11*(*tp).tv_sec) / CPU_TICKS_PER_USEC;

I have to imagine that svcGetSystemTick() is monotonic, since I think it's supposed to be the number of clock cycles since boot, based on this docstring

Originally posted by @ian-h-chamberlain in Meziu/rust-horizon#6 (comment)


I can work on this soon but wanted to track it here, also wanted to wait for the discussion in #8 to be finalized so we can come up with a best practice for exposing libctru APIs through libc like this, since the error handling will probably be similar.

@AzureMarker
Copy link
Member

I just ran into this when trying to write a Tokio example. It needs monotonic time for its "time" feature.

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 a pull request may close this issue.

2 participants