Files
Alexey MilovidovandClaude Opus 4.8 3b3c99a08d Fix jemalloc background thread busy-spin on macOS (clock mismatch)
On macOS, `nstime_get` used `mach_absolute_time` (a monotonic uptime clock)
because `JEMALLOC_HAVE_MACH_ABSOLUTE_TIME` was defined in the Darwin config
headers, while the background thread's condition variable is created with the
default clock (`CLOCK_REALTIME`): macOS has no `pthread_condattr_setclock`, and
jemalloc only attempts to bind the condvar to a monotonic clock when
`JEMALLOC_HAVE_CLOCK_MONOTONIC[_COARSE]` is defined (it is not, on Darwin).

`background_thread_sleep` builds the `pthread_cond_timedwait` deadline from the
`nstime` (uptime) clock and passes it to a `CLOCK_REALTIME` wait. The uptime
value is decades behind the realtime epoch, so the deadline is always in the
past and the wait returns `ETIMEDOUT` immediately - the background thread
busy-spins a CPU core during the whole decay window instead of sleeping for
`dirty_decay_ms`. This would replace the old `madvise` storm with a hot
background-thread loop on exactly the allocation-churning workload that
enabling `background_thread:true` was meant to speed up.

Fix: undef `JEMALLOC_HAVE_MACH_ABSOLUTE_TIME` in the Darwin headers so
`nstime_get` falls back to `gettimeofday` (`CLOCK_REALTIME`) - the same clock
the condvar waits on - so the timed-wait deadline is honoured. This is the
canonical clock for `pthread_cond_timedwait` on macOS. Linux and FreeBSD are
unaffected (they use their own config headers).

This is a prerequisite for `JEMALLOC_BACKGROUND_THREAD` to work correctly on
macOS, enabled in the previous commits of this branch.

See https://github.com/ClickHouse/ClickHouse/issues/108429

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
2026-06-29 00:08:24 +00:00
..