cmcfarlen opened a new issue, #14032:
URL: https://github.com/apache/trafficserver/issues/14032

   This issue collects the purpose and the benchmark results for six PRs that 
reduce cross-core contention on the per-transaction path. Reviewers asked for 
one place that explains what each change is for and shows the measurements 
behind it.
   
   | PR | Change | What it removes from the hot path |
   |---|---|---|
   | #13993 | Fix remap plugin context save racing across threads | Shared 
write to `RemapPluginInfo::_tempContext` on every remap (also a correctness 
bug) |
   | #13992 | Remove unread remap rule hit counting | Shared atomic increment 
of `url_mapping::_hitCount` on every remap |
   | #13997 | Add per-thread caches for 4K-32K IOBuffer blocks | 
`ioBufAllocator` freelist head CAS on every block alloc/free |
   | #13999 | Lease the remap table per thread instead of per txn | 
`AtomicSharedPtr` lock bit and control-block refcount on `rewrite_table` in 
`HttpSM::init` / `~HttpSM` |
   | #14000 | Defer counter increments per event thread | Atomic adds to dozens 
of global counters per transaction |
   | #14005 | Pin configs per event thread instead of refcounting per use | 
`ConfigProcessor` acquire/release refcounts (`scoped_config`, `HttpConfig`, 
`ParentConfig`, SNI, `VirtualHost`, ...) |
   
   They're independent PRs and can be reviewed and merged in any order, but 
they compound: each one moves contention onto the next shared cache line, so 
most of them only show their full gain once the others are in. The numbers 
below are reported both ways where we have them.
   
   ## Summary
   
   On an HTTP/2 cache-hit benchmark at 32 event threads, the six changes 
together:
   
   | | `master` | with all six | Δ |
   |---|---:|---:|---:|
   | Throughput | ~918K req/s | ~1,293K req/s | **+40%** |
   | ATS CPU per request | 34.6 µs | 25.2 µs | **−27%** |
   | perf c2c load HITMs (6 s capture) | 35,228 | 12,497 | **−65%** |
   
   On the same series, #13420 (not ours) adds another +7.5% in cache mode and 
+5.2% in static mode, while on plain `master` it measured −3% to +2%. With 
#13997 in place, the IOBuffer freelist pressure that #13420 adds on `master` 
(3,057 → 5,801 HITMs on that line) goes away.
   
   ## Method
   
   - **Hardware.** Server and client are separate identical hosts: 2 × Intel 
Xeon Silver 4216 (16C/32T each, 64 CPUs), 192 GB, Mellanox ConnectX-5 100GbE, 
direct L2 path, EL9.6 kernel 5.14. ATS runs in a container with `--network 
host`.
   - **Build.** `master` and each PR built from exact commits on Ubuntu 26.04 / 
clang 22, Release, ThinLTO, static core libraries, malloc allocator. 
`ENABLE_ALLOCATOR_METRICS` is off unless noted; see the caveat on #13997.
   - **ATS config.** `exec_thread.limit: 32`, `autoconfig.enabled: 0`, threads 
pinned to CPUs 0–31, `accept_threads: 0`, kTLS on (TLS 1.3, AES-128-GCM), 
logging off, 512 MB RAM cache.
   - **Workload.** H2 over TLS, 192 connections × 30 concurrent streams, a 
single URL, `/ip`, answered by `generator.so` with a 26-byte body. Two modes:
     - **cache**: `map https://<host>/ip http://127.0.0.1/cache/26 
@plugin=generator.so`, so after the first fill every request is a RAM-cache hit;
     - **static**: `.../nocache/26`, so every request goes to the generator 
plugin with the cache bypassed.
   - **Runs.** 3 s warmup + 2 × 10 s per leg. Base and PR alternate (ABAB) 
within one session, 2–3 legs per side for cache and 1–2 for static. Every leg 
had 0 errors. Throughput is the load generator's; CPU per request is ATS 
process utime+stime over the run divided by req/s.
   - **Noise.** Run-to-run spread is about ±5% in cache mode and ±3% in static 
mode. Between sessions the baseline drifts up to ~5%, so only compare rows 
within the same A/B. I treat a delta as real only if it's outside that band or 
every PR leg beats every base leg.
   - **Contention.** `perf c2c record` (6 s, cache mode, under the same load), 
reporting load HITMs (loads that hit a line modified in another core's cache) 
and the top contended lines with their symbols.
   
   ## Per-PR results
   
   The "stacked" A/Bs measure each change on top of the ones before it, in the 
order the series was built: #13997 → #13999 → #13993 + #13992 → #14000 → 
#14005. The "on `master`" A/Bs measure one PR alone.
   
   ### #13993 remap plugin context race (bug fix)
   
   `RemapPluginInfo::setPluginContext()` saved the caller's thread-local plugin 
context in a member of the shared plugin object, so concurrent remaps through 
one plugin overwrite each other's saved value. That's a correctness bug 
(details in the PR) and also a write to a shared line on every remap. The fix 
keeps the saved context on the caller's stack.
   
   - c2c: the `_tempContext` / plugin callback line, **3.87% of HITMs, gone**.
   - Throughput: measured together with the other small fixes, ±0 (within 
noise). This one is mainly the bug fix.
   
   ### #13992 remove remap hit counting
   
   `url_mapping::_hitCount` is incremented on every remapped request and hasn't 
been read since #10710 removed the `{stat}` page. It shares a line with the 
plugin instance list that `run_single_remap` reads per request, so those reads 
miss too.
   
   - c2c: the hit-count / plugin-list line was **4.37% of HITMs**. With the 
counter moved off that line, it dropped out of the top 30, and this PR removes 
it completely.
   - Throughput: within noise by itself. It's in the deferred-counters step 
below.
   
   ### #13997 per-thread IOBuffer block cache
   
   Each thread keeps up to `proxy.config.allocator.iobuf_thread_cache_size` 
bytes (default 512 KB, `0` disables) of free blocks per size class, 4K–32K, and 
trims back to a quarter when full. That caps it at 2 MB per thread, which 
answers the memory concern that closed #2995.
   
   | A/B | Mode | base req/s | PR req/s | Δ req/s | Δ CPU/req |
   |---|---|---:|---:|---:|---:|
   | on `master`, metrics off | cache (3 legs) | 918,589 (mean) | 925,590 
(mean) | +0.8% (noise) | ±0 |
   | | static (2 legs) | 502,275 | 538,642 | **+7.2%** | **−6.9%** |
   | on `master`, allocator metrics on | cache (3 legs) | 458,635 | 468,924 | 
+2.2% (noise) | −2.4% |
   | | static (2 legs) | 247,899 | 252,921 | +2.0% (noise) | −1.9% |
   
   - c2c (cache, on `master`): the IOBuffer freelist head (`freelist_new` / 
`freelist_free` `lock cmpxchg16b`) was **the #1 contended line, 7.55% of 
HITMs**, and isn't in the top 12 with the PR. Total HITMs fall only 3% because 
that contention moves to the remap-table and stats lines (#13999, #14000), so 
cache-mode throughput doesn't move until those land.
   - **Caveat, allocator metrics:** with `ENABLE_ALLOCATOR_METRICS=ON`, 
throughput at 32 threads is about **half** of the unmetered build in both modes 
(cache 459K vs 986K req/s). Each alloc/free still writes the shared metric 
atomics, which then dominate, so this PR's gain shrinks to +2%. That's a 
separate problem, but worth knowing for anyone who runs metered builds: making 
allocator metrics per-thread would likely be the next big win there.
   
   ### #13999 per-thread remap table lease
   
   Each transaction took its `rewrite_table` reference through 
`AtomicSharedPtr::load()`, which writes the same lock bit and control-block 
refcount from every thread. `ThreadLeasedSharedPtr` hands out references 
through a per-thread control block, revalidated against a generation counter 
that only reloads change. Replaced tables are still freed when their last 
transaction finishes.
   
   | A/B | Mode | base req/s | PR req/s | Δ req/s | Δ CPU/req |
   |---|---|---:|---:|---:|---:|
   | stacked on #13997 | cache (2 legs) | 974,877 / 973,953 | 991,753 / 
1,027,405 | +3.6% | −4.0% |
   | | static (1 leg) | 572,645 | 570,493 | ±0 | ±0 |
   
   - c2c: two lines, the `rewrite_table` lock bit (**7.44%**) and the 
`UrlRewrite` control block (**5.82%**), are **both gone**. Total HITMs −10%. A 
residual 0.8–1.9% is left on the slow path, taken when a thread had no live 
transaction and its lease expired.
   - Throughput by itself is within noise (both PR legs beat both base legs). 
It matters more than that number shows, though: the remap table's spin lock 
**collapses** once threads get faster. With #14000 applied to `master` without 
this PR, a variant that flushed counters less often made cache throughput 
**drop 58%**, with 55% of cycles spinning on `rewrite_table`'s lock bit in 
`HttpSM::init`. With this PR in, the same variant was only −5%. The spin lock 
is the reason the other PRs need this one.
   
   ### #14000 deferred counters
   
   Event threads sum counter increments in a thread-local table and apply them 
every `proxy.config.stats.deferred_counter_interval_ms` (default 5 ms), and 
also just before going idle. Gauges stay direct, and so do plugin stats from 
`TSStatCreate`. `0` turns deferral off.
   
   | A/B | Mode | base req/s | PR req/s | Δ req/s | Δ CPU/req |
   |---|---|---:|---:|---:|---:|
   | stacked (includes #13992) | cache (2 legs) | 1,039,557 / 1,027,762 | 
1,215,943 / 1,242,678 | **+18.9%** | **−13.8%** |
   | | static (1 leg) | 576,736 | 612,649 | **+6.2%** | **−5.8%** |
   
   - c2c: total HITMs **−47%** (28,645 → 15,169). Before this PR, the 
`update_size_and_time_stats` / `client_result_stat` counter lines were **~45% 
of all HITMs (14 of the top 15 lines)**. With it, none of them are in the top 
200.
   - This is the largest single gain in the series. The cost: a counter read 
through `traffic_ctl` or a stats plugin can lag by up to the interval while a 
thread is busy. It's published right away when the thread goes idle. A smoke 
check reading `incoming_requests` right after 4 requests saw exactly 4.
   
   ### #14005 per-thread config pins
   
   Each event thread holds one reference per `ConfigProcessor` slot it uses and 
drops it between events once the config is replaced. `scoped_config` reads 
through that pin with no atomics, and `HttpSM` / `HttpTransact::State` keep 
`HttpConfig` / `ParentConfig` alive through a lease on it.
   
   | A/B | Mode | base req/s | PR req/s | Δ req/s | Δ CPU/req |
   |---|---|---:|---:|---:|---:|
   | stacked on the other five | cache (2 legs) | 1,211,628 / 1,219,292 | 
1,285,349 / 1,300,330 | **+6.4%** | **−6.2%** |
   | | static (1 leg) | 605,124 | 633,253 | +4.6% | −2.9% |
   | on `master` alone | cache (3 legs) | 913,457 (mean) | 935,108 (mean) | 
+2.4% (noise) | −0.7% |
   | | static (2 legs) | 511,607 (mean) | 526,399 (mean) | +2.9% | −2.1% |
   
   - c2c (stacked): total HITMs −18% (15,169 → 12,497). Gone from the top 100: 
`TLSSNISupport::would_have_actions_for` / `SNIConfigParams::get` (**8.7%**), 
the `HttpSM::init` / `ConfigProcessor::release` refcount lines (**~7.5%**), and 
the `VirtualHost` lookup lines (2.5%). The plugin invocation counter (4.1%, 
deferred in this PR's second commit) is gone too.
   - On plain `master` the gain is smaller because the global counters and the 
remap table are still the bottleneck there. No lock collapse was seen.
   - The bench smoke check ran `traffic_ctl config reload` and then sent 
traffic. @bryancall's review also covers ASAN, a 300-reload stress run, and the 
reload urtests.
   
   ## Cumulative (cache mode, stacked)
   
   | Step | req/s | CPU µs/req | Load HITMs |
   |---|---:|---:|---:|
   | `master` | ~918K | 34.6 | 35,228 |
   | + #13997 IOBuffer cache | ~926K | 34.6 | 34,096 |
   | + #13999 remap lease | ~1,010K | 32.2 | 30,743 |
   | + #13993, #13992 | ~1,014K | 32.5 | 28,645 |
   | + #14000 deferred counters | ~1,229K | 26.7 | 15,169 |
   | + #14005 config pins | ~1,293K | 25.2 | 12,497 |
   | + #13420 (for reference) | ~1,333K | 24.5 | 11,621 |
   
   These rows come from different sessions (up to ~5% drift between them), so 
the per-PR A/Bs above are the precise numbers. The trend is the point: about 
+40% throughput, −27% CPU per request, and a third of the original contention.
   
   ## What's still contended
   
   With all six in, the top c2c lines are mostly cache internals: 
`free_CacheVCCommon` (~8–17%), the per-stripe `Cache::open_read` lines, 
`HttpCacheSM::state_cache_open_read`, then 
`Http2ConnectionState::restart_streams` and the global `next_sm_id`. 
Per-connection config acquires (`Http2ConnectionState`, `Http1ClientSession`, 
`ProtocolProbeSessionAccept`) aren't converted to pins yet. These would be 
follow-ups, not part of this set.
   
   ## Limits of these measurements
   
   - One workload shape: tiny objects, H2 + kTLS, 32 threads. That's the shape 
where shared cache lines matter most. With fewer threads, larger objects, or 
origin-bound traffic, the gains will be smaller, and those weren't measured 
here.
   - Static-mode c2c wasn't captured, so the static gains aren't split into 
fewer HITMs vs. shorter code paths.
   - All numbers come from one pair of hosts. I'm happy to rerun any A/B, or a 
different workload, if a reviewer wants something specific.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to