Repository navigation
Wire profiled lock wrappers into FileCache guards to measure contention wait time - #108806
Conversation
Replace ad-hoc `ProfileEventTimeIncrement` timing (which always measures, regardless of contention) with a unified `ProfiledLockBase<MutexType, LockType>` template that measures wait-time only when the lock was actually contended. Key changes: - `ProfiledLocks.h`: introduce `ProfiledLockBase<MutexType, LockType>`, a `TSA_SCOPED_LOCKABLE` template with contention-only profiling, move support, `owns_lock()`, and `operator bool()`. Four aliases: `ProfiledMutexLock`, `ProfiledTimedMutexLock`, `ProfiledExclusiveLock`, `ProfiledSharedLock`. - `Guards.h`: replace `std::unique_lock`/`std::shared_lock` in `CachePriorityGuard` and `CacheStateGuard` with the new profiled lock types; `CacheMetadataGuard`, `KeyGuard`, `FileSegmentGuard` `Lock` structs now inherit from `ProfiledMutexLock`. All factory methods annotated `TSA_NO_THREAD_SAFETY_ANALYSIS` (Clang TSA cannot model functions that return a scoped lock by value). - `FileSegment.h`: change `cv` from `std::condition_variable` to `std::condition_variable_any` since `FileSegmentGuard::Lock` is no longer a plain `std::unique_lock<std::mutex>`. - `FileCache.h`: annotate `doDynamicResizeImpl` with `TSA_NO_THREAD_SAFETY_ANALYSIS` — it takes a `CacheStateGuard::Lock` by reference and manually calls `unlock()`/`lock()` on it. - `FileSegment.cpp`, `Metadata.cpp`: remove now-redundant `ProfileEventTimeIncrement` wrappers at the call sites. Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [831e20b] Summary: ✅
AI ReviewSummaryThis PR replaces the ad-hoc FileCache and Keeper lock-wait timers with reusable Tests
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 86/122 (70.49%) · Uncovered code |
- Revert `FileSegment::cv` from `condition_variable_any` back to `condition_variable` to avoid a heap allocation per `FileSegment` construction. Add `mutex()` accessor to `ProfiledLockBase` and use the adopt-lock/release pattern at the `cv.wait_for` call site so the plain `unique_lock<mutex>` required by `condition_variable` can be formed from the profiled lock without double-ownership. - Restore `CacheStateGuard::Lock` as a distinct nested struct (`:Lock : public ProfiledTimedMutexLock`) so that any independently- constructed `ProfiledTimedMutexLock` over an unrelated `timed_mutex` no longer satisfies APIs taking `CacheStateGuard::Lock &`. - Add a `duration<Rep,Period>` constructor overload to `ProfiledLockBase` that measures the timed-wait path: tries the lock first, and if contended measures the `try_lock_for` wait with a `Stopwatch` before incrementing the profile event. This ensures `tryLockFor` callers (background free-space-keeping loops) contribute to `FilesystemCacheStateLockMicroseconds` as intended. Related: #108806 (comment)
- Revert `FileSegment::cv` from `condition_variable_any` back to `condition_variable` to avoid a heap allocation per `FileSegment` construction. Add `mutex()` accessor to `ProfiledLockBase` and use the adopt-lock/release pattern at the `cv.wait_for` call site so the plain `unique_lock<mutex>` required by `condition_variable` can be formed from the profiled lock without double-ownership. - Restore `CacheStateGuard::Lock` as a distinct nested struct (`:Lock : public ProfiledTimedMutexLock`) so that any independently- constructed `ProfiledTimedMutexLock` over an unrelated `timed_mutex` no longer satisfies APIs taking `CacheStateGuard::Lock &`. - Add a `duration<Rep,Period>` constructor overload to `ProfiledLockBase` that measures the timed-wait path: tries the lock first, and if contended measures the `try_lock_for` wait with a `Stopwatch` before incrementing the profile event. This ensures `tryLockFor` callers (background free-space-keeping loops) contribute to `FilesystemCacheStateLockMicroseconds` as intended. Related: #108806 (comment)
9cdd424 to
7002501
Compare
| public: | ||
| ProfiledSharedLock(SharedMutex & mutex, ProfileEvents::Event wait_event_) TSA_ACQUIRE_SHARED(mutex) | ||
| template <typename Rep, typename Period> | ||
| ProfiledLockBase(MutexType & mutex, ProfileEvents::Event wait_event_, std::chrono::duration<Rep, Period> timeout) |
There was a problem hiding this comment.
The observable contract here is that ProfiledLockBase leaves the wait event unchanged for uncontended acquisition, but increments it when acquisition really blocks, including the timed path used by CacheStateGuard::tryLockFor. There is no focused regression test for those semantics, and this timed constructor was already missing profiling in the first revision, so future changes can silently make the FileCache lock metrics misleading again.
Please add a small src/Common/tests gtest that compares ProfileEvents::global_counters deltas for at least an uncontended ProfiledMutexLock acquisition and a contended/timed-out ProfiledTimedMutexLock acquisition.
137c3a0 to
44bb2d7
Compare
| Stopwatch watch; | ||
| LockType::try_lock_for(timeout); | ||
| ProfileEvents::increment(wait_event, watch.elapsedMicroseconds()); |
There was a problem hiding this comment.
nit: could be put into tryLockAndProfileWait for consistency with lockAndProfileWait
The underlying lock type is fully determined by the mutex type plus the
exclusive/shared choice, so the four aliases (`ProfiledMutexLock`,
`ProfiledTimedMutexLock`, `ProfiledExclusiveLock`, `ProfiledSharedLock`)
collapse to two alias templates generic over the mutex type, with the
mutex deduced from the constructor argument via CTAD:
template <typename MutexType>
using ProfiledExclusiveLock = ProfiledLockBase<MutexType, std::unique_lock<MutexType>>;
template <typename MutexType>
using ProfiledSharedLock = ProfiledLockBase<MutexType, std::shared_lock<MutexType>>;
`ProfiledMutexLock`/`ProfiledTimedMutexLock` become `ProfiledExclusiveLock`
over `std::mutex`/`std::timed_mutex`. An explicit deduction guide is added to
`ProfiledLockBase` so alias-template CTAD does not trip
`-Wctad-maybe-unsupported`; the implicit constructor guides still perform the
actual deduction. Aliases (not derived classes) are used so the type identity
stays `ProfiledLockBase` and thread-safety analysis keeps recognizing the
`TSA_ACQUIRE` annotations.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
44bb2d7 to
fd10b55
Compare
|
📊 Cloud Performance Report ✅ AI verdict: This PR is a lock-profiling instrumentation refactor (templating ProfiledExclusiveLock/ProfiledSharedLock) plus Keeper coordination and FileCache/distributed-cache lock plumbing; it relocates or removes per-lock timing counters but does not change any query-execution compute path. None of the flagged queries exercise the changed code, and several deltas (×8.4 and ×2.2 faster, a -25% shift) are far too large to come from removing microsecond-scale lock timers. With only 6 source runs sharing a single benchmark host against 30 base runs, all flagged results — the improvements and the single +5.6% regression alike — are best read as colocation and run-to-run variance, so every one has been downgraded to not sure. clickbenchFlagged queries (9 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (2 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 78/134 (58.21%) · Uncovered code |
# Conflicts: # src/Interpreters/FileCache/FileSegment.cpp
|
Dear @kssenii, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Absolute blocker. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes22 object files changed (+49.38 KiB total), 0 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units405 translation units recompiled, 4672 s compile time in total, 405 of them have a recent master baseline. |
|
@antonio2368 @kssenii — title/description suggestions: Description (currently
Ignore if intentional. |
Previously,
FileCachelock guards measured lock wait time including uncontended acquisitions. The guards now useProfiledLockwrappers directly, so the corresponding profile events only increment when the lock was actually contended, making the metrics more meaningful.As a cleanup, the three profiled lock classes in
ProfiledLocks.hare consolidated into a single template with the original names kept as type aliases.Changelog category (leave one):
Version info
26.8.1.1676(included in26.8and later)