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

CLOCK_REALTIME should be CLOCK_MONOTONIC #86

Open
diablodale opened this issue Aug 20, 2024 · 2 comments
Open

CLOCK_REALTIME should be CLOCK_MONOTONIC #86

diablodale opened this issue Aug 20, 2024 · 2 comments

Comments

@diablodale
Copy link
Contributor

diablodale commented Aug 20, 2024

Throughout depthai-core and Xlink I believe CLOCK_REALTIME is wrong. It should be CLOCK_MONOTONIC but there is a problem in POSIX's semaphore API. 😕

The code everywhere is relative time when CLOCK_xxxx is used with apis like clock_gettime().

However...

  • CLOCK_REALTIME is like a clock on your kitchen wall. That clock can move forward or backward due to LAN servers updating network time, NTP servers changing time, leap seconds, etc. It is not reliable for use in depthai-core and xlink.
  • CLOCK_MONOTONIC is monotonic time since some unspecified starting point, it can never be changed or set by an admin, server process, NTP, leap seconds, or anything else

Changing all the code to use CLOCK_MONOTONIC will ensure that watchdogs, semaphore timeouts, PoE devices, and all kinds of other sensitive timing thing are not affected by clock changes.

There is a problem. XLink uses POSIX apis like pthread_mutex_timedlock(), pthread_cond_timedwait(), and sem_timedwait(). They all accept an epoch-time parameter. But clocks based on epoch are realtime wall-clocks. They vary positive and negative as described above. This is a known issue with those POSIX apis.
https://www.club.cc.cmu.edu/~cmccabe/blog_time_and_time_again.html
https://bugs.eclipse.org/bugs/show_bug.cgi?id=569084

Other mutex and semaphore apis like Boost, Windows, etc. do not have this bug.

@diablodale
Copy link
Contributor Author

@diablodale
Copy link
Contributor Author

I've see alternate pthreads solutions for timed waiting which can use MONOTONIC clock as discussed https://stackoverflow.com/a/29153526. These would likely work on the non-Windows platform. Windows has os apis that can do similar. That leaves the OAK device itself...of which I don't know its os/runtime capabilities.

Since the device has its system clock constantly sync/changed to match the host, this could be a source of occassional random device errors if a device is preparing/processing a wait while its clock is changed due to sync with host. Only a device developer can understand if this is a problem since that code is closed-source.

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

No branches or pull requests

1 participant