Skip to content

Add new self-profiling event to cheaply aggregate query cache hit counts #142978

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 3 commits into from
Jul 2, 2025

Conversation

Kobzol
Copy link
Member

@Kobzol Kobzol commented Jun 24, 2025

Self-profile can record various types of things, some of them are not enabled, like query cache hits. Rustc currently records cache hits as "instant" measureme events, which records the thread ID, current timestamp, and constructs an individual event for each such cache hit. This is incredibly expensive, in a small hello world benchmark that just depends on serde, it makes compilation with nightly go from ~3s (with -Zself-profile) to ~15s (with -Zself-profile -Zself-profile-events=default,query-cache-hit).

We'd like to add query cache hits to rustc-perf (rust-lang/rustc-perf#2168), but there we only need the actualy cache hit counts, not the timestamp/thread ID metadata associated with it.

This PR adds a new query-cache-hit-count event. Instead of generating individual instant events, it simply aggregates cache hit counts per query invocation (so a combination of a query and its arguments, if I understand it correctly) using an atomic counter. At the end of the compilation session, these counts are then dumped to the self-profile log using integer events (in a similar fashion as how we record artifact sizes). I suppose that we could dedup the query invocations in rustc directly, but I don't think it's really required. In local experiments with the hello world + serde case, the query invocation records generated ~30 KiB more data in the self-profile, which was ~10% increase in this case.

With this PR, the overhead of -Zself-profile seems to be the same as before, at least on my machine, so I also enabled query cache hit counts by default when self profiling is enabled.

We should also modify analyzeme, specifically this, and make it load the integer events with query cache hit counts. I can do that as a follow-up, it's not required to be done in sync with this PR, and it doesn't require changes in rustc.

CC @cjgillot

r? @oli-obk

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 24, 2025
@Kobzol
Copy link
Member Author

Kobzol commented Jun 24, 2025

https://github.com/search?type=code&q=query-cache-hits looks like no one used this anyway.. 😆

/// With this approach, we don't know the individual thread IDs and timestamps
/// of cache hits, but it has very little overhead on top of `-Zself-profile`.
/// Recording the cache hits as individual events made compilation 3-5x slower.
query_hits: RwLock<FxHashMap<QueryInvocationId, AtomicU64>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you switch this to using a dense map, e.g. IndexVec? QueryInvocationId should be monotonically assigned I think and so this should end up dense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they allocated monotonically in the order of executed queries though? 🤔 We don't know before the start of rustc how many invocations there will be (I assume, since it includes queries combined with the unique argument combinations), so we can't preallocate it. So the only thing we could do is .push() on demand (if the new ID is one larger than the size of the vec), and lookup by index. Is that what you meant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like they are strictly monotonic:

ID: 2
ID: 2
ID: 4
ID: 1
ID: 0
ID: 4
ID: 7
ID: 8
ID: 1
ID: 9
ID: 10
ID: 11
ID: 12
ID: 13
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1
ID: 1

I guess that it depends on the invocations being cached or not, loaded from disk, etc. I don't think we can count on them actually arriving in order.

That being said, instead of push, I suppose that we could do something like query_hits.resize(new_observed_max_id, 0). Do you want me to do that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a change to vec, let me know what do you think (I didn't use IndexVec, because we index it with QueryInvocationId just once, the rest of operations (like resize or iterating) works with usize anyway. Also, we would need to implement Idx for it, which requires working with usize, but he invocation ID only stores u32.

I wonder if we can hit some pathological cases here if we keep resize_withing a vec by one each time...

|profiler| profiler.query_cache_hit_event_kind,
query_invocation_id,
);
profiler_ref.profiler.as_ref().unwrap().increment_query_cache_hit(query_invocation_id);
}

if unlikely(self.event_filter_mask.contains(EventFilter::QUERY_CACHE_HITS)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to change the existing event rather than adding a new one that only tracks counts?

I think this is losing the information for the query "tree" that was previously present, right? It used to be possible to generate a flamegraph of queries but now since there's no timing/thread information we can't track the parent relationships.

That doesn't seem consistently useful, but it also doesn't seem useless to me...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I figured that it wasn't really used in practice (haven't found anything on GitHub code search), and it was quite expensive. A practical reason to avoid adding a new filter event was to avoid having two mask checks in this very hot function. But the cost of that (with -Zself-profile enabled) is probably still miniscule in comparison to what was happening before, and without self-profiling, we could just ask if QUERY_CACHE_HITS | QUERY_CACHE_HITS_COUNT is enabled, to keep a single check in the fast path, so probably it would be fine.

Happy to add a new filter event though, should be simple enough, and wouldn't break backwards compatibility.

How do you generate such a flamegraph that takes query hits into account, btw?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh I need to try this out, it could be very useful to see the timestamp difference between query hit counts within another query to analyze performance changes

@Kobzol Kobzol changed the title Make recording of query cache hits in self-profiler much cheaper Add new self-profiling event to cheaply aggregate query cache hit counts Jun 25, 2025
@Kobzol
Copy link
Member Author

Kobzol commented Jun 25, 2025

I changed the implementation to use a vec instead of a map (although I think map perf. is also ~fine), and added a new event filter (enabled by default), instead of changing the behavior of the old one.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 1, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 1, 2025

📌 Commit e8fc30e has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 1, 2025
@Kobzol
Copy link
Member Author

Kobzol commented Jul 1, 2025

@bors rollup=never

Just in case there's some perf. effect.

bors added a commit that referenced this pull request Jul 2, 2025
Add new self-profiling event to cheaply aggregate query cache hit counts

Self-profile can record various types of things, some of them are not enabled, like query cache hits. Rustc currently records cache hits as "instant" measureme events, which records the thread ID, current timestamp, and constructs an individual event for each such cache hit. This is incredibly expensive, in a small hello world benchmark that just depends on serde, it makes compilation with nightly go from ~3s (with `-Zself-profile`) to ~15s (with `-Zself-profile -Zself-profile-events=default,query-cache-hit`).

We'd like to add query cache hits to rustc-perf (rust-lang/rustc-perf#2168), but there we only need the actualy cache hit counts, not the timestamp/thread ID metadata associated with it.

This PR adds a new `query-cache-hit-count` event. Instead of generating individual instant events, it simply aggregates cache hit counts per *query invocation* (so a combination of a query and its arguments, if I understand it correctly) using an atomic counter. At the end of the compilation session, these counts are then dumped to the self-profile log using integer events (in a similar fashion as how we record artifact sizes). I suppose that we could dedup the query invocations in rustc directly, but I don't think it's really required. In local experiments with the hello world + serde case, the query invocation records generated ~30 KiB more data in the self-profile, which was ~10% increase in this case.

With this PR, the overhead of `-Zself-profile` seems to be the same as before, at least on my machine, so I also enabled query cache hit counts by default when self profiling is enabled.

We should also modify `analyzeme`, specifically [this](https://github.com/rust-lang/measureme/blob/master/analyzeme/src/analysis.rs#L139), and make it load the integer events with query cache hit counts. I can do that as a follow-up, it's not required to be done in sync with this PR, and it doesn't require changes in rustc.

CC `@cjgillot`

r? `@oli-obk`
@bors
Copy link
Collaborator

bors commented Jul 2, 2025

⌛ Testing commit e8fc30e with merge ece9f3f...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 2, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 2, 2025
@Kobzol
Copy link
Member Author

Kobzol commented Jul 2, 2025

Oops, apparently 32-bit platforms are a thing. I decided to use portable AtomicU64 instead of AtomicUsize. But maybe it's unreasonable to expect more than 4 billion query cache hits in a single compilation session?

@bors2 try jobs=dist-powerpc-linux

@rust-bors
Copy link

rust-bors bot commented Jul 2, 2025

⌛ Trying commit b49ca02 with merge fc33cd0

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jul 2, 2025
Add new self-profiling event to cheaply aggregate query cache hit counts

Self-profile can record various types of things, some of them are not enabled, like query cache hits. Rustc currently records cache hits as "instant" measureme events, which records the thread ID, current timestamp, and constructs an individual event for each such cache hit. This is incredibly expensive, in a small hello world benchmark that just depends on serde, it makes compilation with nightly go from ~3s (with `-Zself-profile`) to ~15s (with `-Zself-profile -Zself-profile-events=default,query-cache-hit`).

We'd like to add query cache hits to rustc-perf (rust-lang/rustc-perf#2168), but there we only need the actualy cache hit counts, not the timestamp/thread ID metadata associated with it.

This PR adds a new `query-cache-hit-count` event. Instead of generating individual instant events, it simply aggregates cache hit counts per *query invocation* (so a combination of a query and its arguments, if I understand it correctly) using an atomic counter. At the end of the compilation session, these counts are then dumped to the self-profile log using integer events (in a similar fashion as how we record artifact sizes). I suppose that we could dedup the query invocations in rustc directly, but I don't think it's really required. In local experiments with the hello world + serde case, the query invocation records generated ~30 KiB more data in the self-profile, which was ~10% increase in this case.

With this PR, the overhead of `-Zself-profile` seems to be the same as before, at least on my machine, so I also enabled query cache hit counts by default when self profiling is enabled.

We should also modify `analyzeme`, specifically [this](https://github.com/rust-lang/measureme/blob/master/analyzeme/src/analysis.rs#L139), and make it load the integer events with query cache hit counts. I can do that as a follow-up, it's not required to be done in sync with this PR, and it doesn't require changes in rustc.

CC `@cjgillot`

r? `@oli-obk`
try-job: dist-powerpc-linux
@rust-bors
Copy link

rust-bors bot commented Jul 2, 2025

☀️ Try build successful (CI)
Build commit: fc33cd0 (fc33cd09cf4f19b8314985e513201c5758a0d506, parent: f51c9870bab634afb9e7a262b6ca7816bb9e940d)

@Kobzol
Copy link
Member Author

Kobzol commented Jul 2, 2025

Looks good.

@bors r=oli-obk

@bors
Copy link
Collaborator

bors commented Jul 2, 2025

📌 Commit b49ca02 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 2, 2025
@bors
Copy link
Collaborator

bors commented Jul 2, 2025

⌛ Testing commit b49ca02 with merge b94bd12...

@bors
Copy link
Collaborator

bors commented Jul 2, 2025

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing b94bd12 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 2, 2025
@bors bors merged commit b94bd12 into rust-lang:master Jul 2, 2025
11 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 2, 2025
@Kobzol Kobzol deleted the query-hit branch July 2, 2025 14:43
Copy link
Contributor

github-actions bot commented Jul 2, 2025

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing f51c987 (parent) -> b94bd12 (this PR)

Test differences

Show 3 test diffs

3 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard b94bd12401d26ccf1c3b04ceb4e950b0ff7c8d29 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-apple-1: 9434.0s -> 7843.5s (-16.9%)
  2. dist-ohos-x86_64: 4597.3s -> 4123.1s (-10.3%)
  3. mingw-check-1: 1787.7s -> 1954.1s (9.3%)
  4. mingw-check-tidy: 78.5s -> 72.2s (-8.1%)
  5. dist-arm-linux-gnueabi: 4544.3s -> 4881.7s (7.4%)
  6. dist-armv7-linux: 5284.2s -> 4892.3s (-7.4%)
  7. mingw-check-2: 2093.7s -> 1950.1s (-6.9%)
  8. dist-various-1: 3764.0s -> 3991.5s (6.0%)
  9. dist-powerpc64-linux: 5022.6s -> 5318.7s (5.9%)
  10. dist-loongarch64-linux: 6292.1s -> 5948.5s (-5.5%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b94bd12): comparison URL.

Overall result: ❌ regressions - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.4%] 17
Regressions ❌
(secondary)
0.4% [0.3%, 0.5%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [0.1%, 0.4%] 17

Max RSS (memory usage)

Results (primary -2.5%, secondary -3.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.5% [-2.7%, -2.4%] 2
Improvements ✅
(secondary)
-3.1% [-3.1%, -3.1%] 1
All ❌✅ (primary) -2.5% [-2.7%, -2.4%] 2

Cycles

Results (primary 1.6%, secondary 1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.2% [1.4%, 4.7%] 6
Regressions ❌
(secondary)
3.4% [2.1%, 4.4%] 6
Improvements ✅
(primary)
-1.9% [-1.9%, -1.9%] 1
Improvements ✅
(secondary)
-1.8% [-2.6%, -0.7%] 4
All ❌✅ (primary) 1.6% [-1.9%, 4.7%] 7

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 462.08s -> 462.452s (0.08%)
Artifact size: 372.26 MiB -> 372.20 MiB (-0.02%)

@rustbot rustbot added the perf-regression Performance regression. label Jul 3, 2025
@Kobzol
Copy link
Member Author

Kobzol commented Jul 3, 2025

Might be genuine regressions, although the fast path shouldn't have changed much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants