-
Notifications
You must be signed in to change notification settings - Fork 0
Expose cache stats #79
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,6 +187,11 @@ unordered_set<string> TableFilterKeyIndex::Take() { | |
| return std::move(filter_keys); | ||
| } | ||
|
|
||
| unordered_set<string> TableFilterKeyIndex::Snapshot() { | ||
| concurrency::lock_guard<concurrency::mutex> guard(lock); | ||
| return filter_keys; | ||
| } | ||
|
|
||
| // ------- CONDITION_CACHE_STORE ------- | ||
|
|
||
| string ConditionCacheStore::MakeCacheKeyString(const CacheKey &key) { | ||
|
|
@@ -275,11 +280,59 @@ void ConditionCacheStore::ClearAll(ClientContext &context) { | |
| cache.Delete(MakeFilterKeyIndexKey(table_oid)); | ||
| } | ||
| cached_table_oids.clear(); | ||
| ResetStats(); | ||
| } | ||
|
|
||
| shared_ptr<ConditionCacheStore> ConditionCacheStore::GetOrCreate(ClientContext &context) { | ||
| auto &cache = ObjectCache::GetObjectCache(context); | ||
| return cache.GetOrCreate<ConditionCacheStore>(CACHE_KEY); | ||
| } | ||
|
|
||
| void ConditionCacheStore::RecordAccess(bool hit) { | ||
| total_accesses.fetch_add(1, std::memory_order_relaxed); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for performance?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Opus generated the code and I think it's fine. For cache access stats we don't need to be 100% precise, but I could change it |
||
| if (hit) { | ||
| total_hits.fetch_add(1, std::memory_order_relaxed); | ||
| } | ||
| } | ||
|
|
||
| void ConditionCacheStore::ResetStats() { | ||
| total_accesses.store(0, std::memory_order_relaxed); | ||
| total_hits.store(0, std::memory_order_relaxed); | ||
| } | ||
|
|
||
| idx_t ConditionCacheStore::ComputeTotalMemoryBytes(ClientContext &context) const { | ||
| auto &cache = ObjectCache::GetObjectCache(context); | ||
|
|
||
| unordered_set<idx_t> oid_snapshot; | ||
| { | ||
| concurrency::lock_guard<concurrency::mutex> guard(lock); | ||
| oid_snapshot = cached_table_oids; | ||
| } | ||
|
|
||
| idx_t total = 0; | ||
| for (auto table_oid : oid_snapshot) { | ||
| auto index = cache.Get<TableFilterKeyIndex>(MakeFilterKeyIndexKey(table_oid)); | ||
| if (!index) { | ||
| continue; | ||
| } | ||
| for (const auto &filter_key : index->Snapshot()) { | ||
| auto entry = cache.Get<ConditionCacheEntry>(MakeCacheKeyString(CacheKey {table_oid, filter_key})); | ||
| if (entry) { | ||
| auto mem = entry->GetEstimatedCacheMemory(); | ||
| ALWAYS_ASSERT(mem.IsValid()); | ||
| total += mem.GetIndex(); | ||
| } | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| CacheStoreStats ConditionCacheStore::GetStats(ClientContext &context) const { | ||
| return CacheStoreStats { | ||
| .total_memory_bytes = ComputeTotalMemoryBytes(context), | ||
| .hit_count = total_hits.load(std::memory_order_relaxed), | ||
| .access_count = total_accesses.load(std::memory_order_relaxed), | ||
| }; | ||
| } | ||
|
|
||
| } // namespace duckdb | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # name: test/sql/condition_cache_stats.test | ||
| # description: Test predicate cache stats | ||
| # group: [sql] | ||
|
|
||
| require query_condition_cache | ||
|
|
||
| # Before any cache activity: all stats are zero | ||
| query III | ||
| SELECT total_memory_bytes, hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 0 0 0 | ||
|
|
||
| statement ok | ||
| CREATE TABLE t AS SELECT i AS id, i % 100 AS val FROM range(500000) t(i); | ||
|
|
||
| statement ok | ||
| SELECT * FROM condition_cache_build('t', 'val = 42'); | ||
|
|
||
| query I | ||
| SELECT total_memory_bytes > 0 FROM condition_cache_stats(); | ||
| ---- | ||
| true | ||
|
|
||
| query II | ||
| SELECT hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 0 0 | ||
|
|
||
| # First auto query: optimizer finds the pre-built entry (hit) | ||
| statement ok | ||
| SELECT count(*) FROM t WHERE val = 42; | ||
|
|
||
| query II | ||
| SELECT hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 1 1 | ||
|
|
||
| # Second auto query: another hit | ||
| statement ok | ||
| SELECT count(*) FROM t WHERE val = 42; | ||
|
|
||
| query II | ||
| SELECT hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 2 2 | ||
|
|
||
| # Capture memory before reset to verify it is unchanged afterwards | ||
| statement ok | ||
| CREATE TABLE mem_before AS SELECT total_memory_bytes FROM condition_cache_stats(); | ||
|
|
||
| # Reset stats: clears counters but does NOT clear cache entries | ||
| query I | ||
| SELECT condition_cache_reset_stats(); | ||
| ---- | ||
| true | ||
|
|
||
| query II | ||
| SELECT hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 0 0 | ||
|
|
||
| # Memory is unchanged after reset: must equal the value captured before reset | ||
| query I | ||
| SELECT (SELECT total_memory_bytes FROM condition_cache_stats()) = | ||
| (SELECT total_memory_bytes FROM mem_before); | ||
| ---- | ||
| true | ||
|
|
||
| # First query on a new predicate (not pre-built): optimizer misses, builds entry | ||
| statement ok | ||
| SELECT count(*) FROM t WHERE val = 99; | ||
|
|
||
| # access_count = 1, hit_count = 0 (first lookup was a miss) | ||
| query II | ||
| SELECT hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 0 1 | ||
|
|
||
| # Second query on the same new predicate: optimizer finds the entry | ||
| statement ok | ||
| SELECT count(*) FROM t WHERE val = 99; | ||
|
|
||
| query II | ||
| SELECT hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 1 2 | ||
|
|
||
| # Disabling the setting calls ClearAll, which drops all cache entries and resets all stats | ||
| statement ok | ||
| SET use_query_condition_cache = false; | ||
|
|
||
| query III | ||
| SELECT total_memory_bytes, hit_count, access_count FROM condition_cache_stats(); | ||
| ---- | ||
| 0 0 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this should be refactored after we introduce #78, otherwise the lookup hit doesnt mean query can actually use them to reduce the IO and materalize overhead