[libcxx] std::chrono::steady_clock is not working as expected

Open
#767 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c

Research direction

Read the referenced implementation in libcxx/src/chrono.cpp around the steady_clock::now() call and the linked Microsoft documentation for QueryPerformanceFrequency behavior. Investigate how the Xbox kernel call can fail and how that affects the clock result; the issue also suggests disabling steady_clock as a temporary option. Done means steady clock behavior on Xbox is reliable, or its use is clearly disabled until that is resolved.

Written by the indexing model from the issue text.

Description

While working on my Kodi port I faced lot of issues with:

  • frame time (CTimeUtils::GetFrameTime)
  • stopwatch (CStopwatch)
  • events (CEvent)

I inspected this and everything leads to use of std::chrono::steady_clock::now(). This is used by std::condition_variable_any::wait_for (used in CEvent), it's used inside CStopwatch to calculate elapsed time and it's used to update frame time inside CTimeUtils. When app is started it behaves normally but after some time it starts behaving unpredictably giving negative and zero values which leads to crashes in case of std::condition_variable_any and brokes complete logic in Kodi because returned value of this functions should be always greater then previous one (somerhing like GetTickCount).

Astarivi helped me to debug this and we came to conslusion that problem is inside kernel functin KeQueryPerformanceFrequency which is invoked when std::chrono::steady_clock::now() is called. This is actually a know problem for Win 2K and older systems. In short, KeQueryPerformanceFrequency on Xbox can fail and LLVM assumes this will always succeed. See here. There are also more details on MS docs

Temporary solution for this is to replace steady_clock with system_clock but this should be fixed anyway. Or at least until this is sorted steady_clock should be disabled so it doesnt lead to confusion.

Dominant language
C
Stars
600
Forks
97
Avg merge
11h 48m
Merged PRs (30d)
2

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from XboxDev/nxdk

All issues in XboxDev/nxdk

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.