Commit Graph
51 Commits
Author SHA1 Message Date
Henrik RydgårdandClaude Opus 5 72d85f2b25 Common: Document the cross-platform suspend semantics of the monotonic clock
Comment only. Whether the monotonic clock counts time spent asleep differs per
platform, and the names invite exactly the wrong assumption: Apple's
CLOCK_MONOTONIC behaves like Linux's CLOCK_BOOTTIME, not like Linux's
CLOCK_MONOTONIC. We now skip suspended time on Linux/Android and Mac/iOS, but
not on Windows, where QPC is documented to include standby and hibernate.

Writing down why that's deliberate, so the Apple branch doesn't get "fixed"
back to CLOCK_MONOTONIC by someone who reads it as the portable spelling.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZk5y4Fzw811WJoNWZb8Sc
2026-08-07 12:47:55 +02:00
Henrik RydgårdandClaude Opus 5 b25979a12d Common: Document the sleep_precise overshoot on Apple platforms
No behavior change - recording what was measured so the next person doesn't
have to rediscover it. Darwin's usleep overshoots by ~25% of the requested
interval, mach_wait_until doesn't improve on it, and the fix that does work
costs CPU every frame.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZk5y4Fzw811WJoNWZb8Sc
2026-08-07 12:47:55 +02:00
Henrik RydgårdandClaude Opus 5 102d629095 Common: Use CLOCK_UPTIME_RAW via clock_gettime_nsec_np on Apple platforms
CLOCK_MONOTONIC is the expensive clock on Apple - it keeps counting while the
system is asleep, so it can't be a plain counter read. CLOCK_UPTIME_RAW is the
raw counter (the clock_gettime man page notes it's identical to
mach_absolute_time() after the timebase conversion), and the _nsec_np variant
returns nanoseconds directly instead of filling in a timespec we then have to
recombine - which is exactly what time_now_raw() wants.

Measured on an M-series Mac, per time_now_d():
  clock_gettime(CLOCK_MONOTONIC)      23-31 ns
  clock_gettime_nsec_np(UPTIME_RAW)   14-16 ns
  mach_absolute_time + double mult    10.3 ns

mach_absolute_time is a little faster still, but needs mach headers, a cached
timebase and a second time origin; this is a one-function change that keeps
time_now_raw()'s nanosecond contract. Available since macOS 10.12 / iOS 10, and
our deployment targets are 10.13 and 11.0.

Timing risk: this clock stops while the system is asleep, where CLOCK_MONOTONIC
kept running. Deltas across a sleep/wake will now be small rather than huge,
which is the better behavior for frame pacing. time_now_unix_utc() still uses
CLOCK_REALTIME, so wall-clock time is unaffected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZk5y4Fzw811WJoNWZb8Sc
2026-08-07 12:47:55 +02:00
Henrik RydgårdandClaude Opus 5 669935d9e3 Common: Simplify Instant to a single timestamp
It stored a split seconds/nanoseconds pair on POSIX and hand-rolled the borrow
in ElapsedNanos, duplicating what time_now_raw() already does. Just store the
nanosecond value, so there's one clock read per platform to keep correct.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZk5y4Fzw811WJoNWZb8Sc
2026-08-07 12:47:55 +02:00
Henrik RydgårdandClaude Opus 5 c6965cebdb Common: Establish the time origin in TimeInit(), and call it on iOS
time_now_d() lazily initialized g_startTime on its first call, which is a data
race between threads, and left from_time_raw() subtracting zero (returning
seconds since boot) if it happened to run before any time_now_d(). Set it in
TimeInit() instead, matching what the Windows path already does with
frequencyMult.

That only works if TimeInit() is actually called, and iOS was the one entry
point that never did - Windows, UWP, SDL, Qt, Android, libretro, headless and
the unit tests all do it as the first thing in main(). Added it there too.

Timing risk: anything calling time_now_d() before TimeInit() now gets seconds
since boot rather than a value near zero. All entry points call TimeInit()
first, so this only bites code running from a static initializer; deltas
between two timestamps are unaffected either way.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZk5y4Fzw811WJoNWZb8Sc
2026-08-07 12:47:55 +02:00
Henrik RydgårdandClaude Opus 5 3a55d9b02f Common: Remove the broken generic clock fallback in TimeUtil
The fallback path (anything that isn't Windows/Linux/Mac/iOS/Android - so
Switch, OpenBSD, FreeBSD) was thoroughly broken:

- time_now_raw() computed a double of *seconds* and returned it as a uint64_t,
  where every caller expects nanoseconds. from_time_raw() then scaled it by
  1/1e9, so time came out roughly 1e9 times too small.
- Instant() seeded itself from gettimeofday (realtime epoch) while
  ElapsedNanos() read CLOCK_MONOTONIC (since boot), so every elapsed span was
  the difference between two unrelated clocks - decades, in practice.
- On top of that it mixed units, assigning tv_usec to nsecs_ and subtracting it
  from ts.tv_nsec.

Since that code already called clock_gettime(CLOCK_MONOTONIC) itself, any
platform reaching it necessarily has POSIX clocks, so just fold those platforms
into the branch that works instead of fixing three bugs in a duplicate
implementation. Also drops the now-unused "micros" constant.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZk5y4Fzw811WJoNWZb8Sc
2026-08-07 12:47:55 +02:00
Henrik RydgårdandClaude Opus 5 0b4ab5a68f Common: Implement time_to_unix_utc on Linux/Mac/iOS/Android
It's declared in TimeUtil.h and defined for Windows and for the generic
fallback path, but not in the branch that Linux, Mac, iOS and Android actually
compile - so the first caller on any of those platforms would have failed to
link. Nothing calls it today, which is why nobody noticed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZk5y4Fzw811WJoNWZb8Sc
2026-08-07 12:47:55 +02:00
Henrik Rydgård c14959b0b6 Buildfix/testfix 2026-07-27 21:33:14 +02:00
Henrik Rydgård 5ef736f6a7 More command line improvements 2026-07-27 18:37:28 +02:00
Kevin Reinholz d6060d4b3b Build fixes for clang on FreeBSD 2026-05-31 13:27:45 -07:00
Henrik Rydgård 992bd1b12d Qt buildfix, remove unused function 2026-05-19 14:36:47 +02:00
Henrik Rydgård 07efc7f7a3 Buildfix, remove some .c_str() 2026-05-19 13:59:04 +02:00
Henrik Rydgård c20d20ccba Show timestamps when loading rewind states 2026-05-19 11:58:01 +02:00
Herman Semenoff f655ddc622 timeutil: fix 32-bit overflow for avoiding year 2038 problem
Affected Unix and Windows platforms

References:
- https://en.wikipedia.org/wiki/Year_2038_problem
- https://www.gnu.org/software/gnulib/manual/html_node/Avoiding-the-year-2038-problem.html
2026-01-29 04:22:27 +03:00
Henrik Rydgård c29eb5097b Use the more accurate sleep function in the UI throttling 2025-10-21 12:11:23 +02:00
Henrik Rydgård 7cc4a0f3a3 Add more accurate sleep function (well, not more accurate on Windows unfortunately) 2025-10-21 12:11:23 +02:00
Henrik Rydgård 4e0b6ac3ec More misc cleanup 2025-05-14 15:14:03 +02:00
Henrik Rydgård 80a11e8cbe Add sleep_random function for debugging purposes 2025-05-13 10:41:13 +02:00
Henrik Rydgård 1f96c3a2b3 Switch to sleep_precise for WaitUntil() which is used for frame timing.
This can improve frame pacing slightly, not just on Windows actually.
2025-03-01 19:13:06 +01:00
oltolm 02e767866a fix compiler warnings 2025-02-22 14:15:15 +01:00
Henrik Rydgård e93c80db4e Cleaning up our SIMD header includes, using the new header 2024-12-19 16:08:48 +01:00
Henrik Rydgård 5a3eeb9d9b ImDebugger: Fix issue with HLE Modules window always showing, more granular sleep logging 2024-11-21 15:48:18 +01:00
Henrik Rydgård 59a56d66c7 Add a "reason" argument to sleep_ms().
sleep_ms() should generally be avoided when possible. This can be used to try
to track down unnecessary sleeps by adding some logging.

This commit on its own doesn't actually add any logging.
2024-11-21 15:28:51 +01:00
Henrik Rydgård 592d9c5d69 Replace SetWaitableTimerEx with SetWaitableTimer
This makes the build work on retroarch's build server, and keeps
compatibility with Windows Vista (although we hardly test for that).
2024-07-29 08:11:19 -06:00
Henrik Rydgård 5c368cb965 More buildfixes 2024-07-26 15:22:35 +02:00
Henrik Rydgård 9fb97add3f Bugfixes 2024-07-26 14:22:31 +02:00
Henrik Rydgård 96c4ae4457 TimeUtil: Minor cleanup, add precise_sleep() 2024-07-26 11:25:58 +02:00
Henrik Rydgård fa5ec667ef Add new TimeSpan class for more accurate timing.
Minimizes the amount of value conversions and performs subtractions in
integer space.
2024-06-05 12:38:39 +02:00
Henrik Rydgård 3e26866f76 Renaming 2024-06-05 10:28:49 +02:00
Henrik Rydgård 31c85ae0a5 Add the basics of a played-time tracker. 2023-11-26 19:15:38 +01:00
Henrik Rydgård 5a9a2bf6fe Merge pull request #17779 from EmulatorJS/master
Cleanup emscripten libretro target
2023-08-13 21:40:24 +02:00
Ethan O'Brien 8426b35a80 Cleanup emscripten libretro build target 2023-08-12 14:38:35 -05:00
Henrik Rydgård 3e682ea733 Take out the "yield" arm64 implementation, that uses a builtin that some compilers miss.
It's not used anyway yet.

Fixes #17877
2023-08-09 12:20:27 +02:00
Henrik Rydgård 096c168dd7 Add yield() function to tell the CPU that we're busy-waiting (rare) (#17862)
* Add yield() function to tell the CPU that we're busy-waiting (rare)

Use it only for the busy-wait in lag sync, which only happens in
Windows.

* Buildfix attempt
2023-08-07 21:38:03 +02:00
Henrik Rydgård 0530dc57a8 Implement frame time measurement on Android using VK_GOOGLE_display_timing 2023-08-03 12:59:25 +02:00
Henrik Rydgård e16cac6548 Initial work on supporting VK_GOOGLE_display_timing. Not working yet. 2023-08-03 11:11:16 +02:00
Henrik Rydgård 1dab6e5bef Linux/Mac/iOS: Switch from gettimeofday() to clock_gettime(CLOCK_MONOTONIC)
More appropriate, and adds a raw function that can be used to match up
with  VK_GOOGLE_display_timing.
2023-08-03 00:00:07 +02:00
Henrik Rydgård feb4f9477a Remove newly added dependencies on PPSSPP 2022-10-18 10:35:42 +02:00
Unknown W. Brackets e3763d9c4e Common: Avoid format warning with log timestamp. 2022-08-07 07:51:27 -07:00
Henrik Rydgård 97ee888aab Add TODO to see if we can make better choices of time sources 2022-06-10 23:57:59 +02:00
Henrik Rydgård e0f73507f9 Time the stress test 2022-04-08 11:55:49 +02:00
Unknown W. Brackets 63e623ecb2 Build: Fix some format truncation warnings.
Generally all should be safe already, but better to be sure.
2021-12-11 10:45:27 -08:00
Unknown W. Brackets 0ffac20fcd Common: Include unistd.h before ctime on mingw.
This is required for localtime_r and similar to be available.
2021-02-14 10:30:10 -08:00
Henrik Rydgård e97baa503a Avoid a division in time_now_d(). Minor optimizations. 2020-10-10 19:05:46 +02:00
Henrik Rydgård 886a8b1ac6 Remove Timer.cpp/h. Move various collections into Common/Data/Collections. 2020-10-05 21:05:23 +02:00
Henrik Rydgård 17a9767585 Buildfixes 2020-09-29 15:51:51 +02:00
Henrik Rydgård 3162f30158 Merge base/basictypes.h into Common/Common.h (mostly). 2020-09-29 15:51:51 +02:00
Henrik Rydgård 5411a40f2a TimeUtil.cpp cleanup 2020-09-29 00:36:29 +02:00
Henrik Rydgård 054acf768c Don't cache time in a variable.
A little weirdness in the code that has stuck around for a long, long
time. It's really not necessary and mostly just confusing.
2020-09-24 23:52:43 +02:00
Henrik Rydgård 7aacf3df37 Remove time_now() - time_now_d() is the replacement. Absolute time values can't be stored reliably in floats. 2020-08-16 09:37:18 +02:00