Files
RitzDaCat 99a1ff5aa9 scx_cake: docs: keep the nightly research and state records
Upstream carries cake 1.4.1 with a trimmed doc set. The nightly keeps
the dated research folders that STATE and the later nightly commits
link to. No source file changes.
2026-12-02 22:24:33 +01:00

73 lines
16 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Cake · Hardware fast paths</title>
<style>
:root{color-scheme:dark;font:17px/1.55 system-ui,sans-serif;background:#10151c;color:#e8edf3}*{box-sizing:border-box}body{margin:0}main{max-width:1160px;margin:auto;padding:44px 28px 90px}header{border-bottom:1px solid #344151;padding-bottom:28px}small,.muted{color:#adbacb}h1{font-size:clamp(32px,5vw,54px);line-height:1.12;margin:12px 0 20px;letter-spacing:-1.5px}h2{font-size:25px;margin:0 0 16px}h3{font-size:18px;margin:0 0 10px}p{margin:10px 0 18px}a{color:#8dceff}nav{display:flex;gap:10px;flex-wrap:wrap;margin:24px 0}nav a{padding:8px 14px;border:1px solid #40516a;border-radius:7px;text-decoration:none}section{scroll-margin-top:20px;margin-top:38px}.lead{font-size:21px;max-width:950px}.cards{display:grid;grid-template-columns:repeat(3,1fr);gap:16px}.card,.callout{background:#19222e;border:1px solid #344151;border-radius:10px;padding:22px}.callout{border-left:4px solid #8dceff}.tag{color:#89dfbc;font-size:13px;text-transform:uppercase;letter-spacing:1px}.warn{color:#ffce8b}table{border-collapse:collapse;width:100%;font-size:15px}th{text-align:left;color:#adbacb;font-weight:500}td,th{padding:14px 12px;border-bottom:1px solid #344151;vertical-align:top}td:first-child{font-weight:650}code{color:#b7dafb}li{margin:9px 0}.scroll{overflow-x:auto}.num{font-variant-numeric:tabular-nums;white-space:nowrap}footer{margin-top:40px;color:#adbacb;font-size:14px}@media(max-width:760px){.cards{grid-template-columns:1fr}main{padding:25px 18px}td,th{padding:12px 8px}}@media print{:root{color-scheme:light;background:white;color:#111}a,small,.muted,th,code,footer{color:#333}.card,.callout{background:#f5f5f5;border-color:#bbb}.tag{color:#165e45}nav{display:none}main{padding:0}section{break-inside:avoid}}
</style>
<main>
<header><small>SCX_CAKE · RYZEN 7 9800X3D / ZEN 5 · MEETING BRIEF · 29 SEPTEMBER 2026</small>
<h1>Getting Cake closer<br>to the hardware</h1>
<p class="lead">The CPU has fast bit operations. The opportunity is to expose them efficiently through BPF — and verify the whole scheduling path benefits.</p>
<p class="muted">Current source reviewed: <code>8fc5ec9b1</code> · Running kernel inspected: <code>7.2.8-1-cachyos</code> · Retained experiments: September 27</p></header>
<nav aria-label="Sections"><a href="#rationale">Why LZCNT?</a><a href="#priorities">Priorities</a><a href="#numbers">Recorded results</a><a href="#boundary">BPF → CPU</a><a href="#decision">Discussion & sources</a></nav>
<section id="walkthrough"><h2>Visual walkthrough: choosing CPU 3</h2>
<div class="callout"><h3>Experimental patches ready</h3><p>The kernel and optional Cake patches are built. VM JIT dumps confirm TZCNT and POPCNT; native and fallback paths pass the differential and register-preservation checks. Hardware performance and Cake native-mode activation remain unmeasured. <a href="https://github.com/RitzDaCat/experimental-patches/blob/main/kernel/sched-ext/research/jit-bitops-prototype/README.md">Patches, Kernel Manager instructions, validation and wall-clock runner</a>.</p></div>
<div class="callout"><h3>JIT investigation update</h3><p>The kernel’s JIT selects native instructions, but Clang has already expanded the bit-count operation into BPF arithmetic. Fresh Clang 22.1.8 probes show 9 BPF operations for nonzero CTZ and 19 for population count; compiling the same C for Zen 5 gives TZCNT and POPCNT directly. The inspected reference JIT does not recognize and collapse those sequences.</p><p>The installed kernel ELF matches the running build ID and BTF. Actual Cake native JIT output is still unavailable: direct BPF inspection is permission-blocked. <a href="../research/jit-20260929/README.md">Full investigation, disassembly and implementation options</a>.</p></div>
<p>Each bit represents a CPU. A 1 means that CPU survived Cake’s eligibility and preference checks. In this example, CPUs 3, 5 and 7 remain.</p>
<pre style="overflow-x:auto;background:#19222e;padding:20px;border-radius:8px"><code>CPU number 7 6 5 4 3 2 1 0
Candidate mask 1 0 1 0 1 0 0 0
↑ └───┘
CPU 3 three trailing zeros
Result: trailing-zero count = 3 → try to claim CPU 3</code></pre>
<div class="callout"><strong>Scheduling path</strong><p>Wakeup / enqueue / pool-work search<br>↓<br>Find idle CPUs allowed for this task and relevant LLC<br>↓<br>Prefer capacity, whole cores, unreserved seats, quieter CPUs and platform rank<br>↓<br><strong>Count trailing zeros of the surviving bitmap</strong><br>↓<br>Atomically try to claim that CPU<br>↓<br>Success: use it. Failure: remove its bit and try another candidate.</p></div>
<p>The source line in <code>cake_pick_cold()</code> is:</p>
<pre style="overflow-x:auto"><code>return (s32)__builtin_ctzll(
cake_rank_tier(cake_prefer_irq_clean(best, noisy)));</code></pre>
<p>The builtin states the desired calculation. It does not force the BPF backend to emit an x86 TZCNT instruction.</p>
<div class="cards" style="grid-template-columns:repeat(auto-fit,minmax(270px,1fr))"><article class="card"><h3>Software route · captured BPF</h3><ol><li>Copy the candidate mask.</li><li>Negate the copy.</li><li>AND with the original: isolate the lowest 1 bit.</li><li>Load a special constant.</li><li>Multiply the isolated bit by that constant.</li><li>Shift right by 58: produce a table index.</li><li>Load the lookup table’s address.</li><li>Add the index to the address.</li><li>Load the byte containing the CPU number.</li></ol><p><code>10101000 → 00001000 → index → table[index] → 3</code></p><small>This multiplication-and-table method is a de Bruijn bit scan. The table maps each possible single-bit position to its bit number.</small></article><article class="card"><h3>Proposed inline hardware route</h3><pre><code>tzcnt result, mask</code></pre><p>The CPU’s dedicated bit-count hardware directly returns the number of zeros below the lowest set bit.</p><p><code>10101000 → TZCNT → 3</code></p><p>No software multiplication, lookup-table address calculation or lookup-table memory read for the bit scan.</p><small>One native instruction for the core operation. Argument/result moves or surrounding spills can still add work. This is not an ordinary function call.</small></article></div>
<h3>Why we said 12, and the more precise count</h3><p>The retained standalone CTZ benchmark has <strong>12 x86 instructions</strong>: initialize the zero-input result, compare against zero, branch if zero, then the nine scan operations. Cake’s captured nonempty path omits those three zero-input instructions. Its nine BPF operations are not a verified count of today’s final native code.</p><p>The earlier generic claim that CTZ’s count included an exit jump was incorrect: this generated CTZ block has no exit jump. The benchmark’s dependency-feeding ADD is separate from the 12-instruction count.</p>
<p><strong>Only the highlighted bit scan gets replaced.</strong> Cake still needs the same CPU eligibility checks, ranking policy, atomic claim and race retry. POPCNT has a similar opportunity, replacing software bit arithmetic when counting idle CPUs; LZCNT belongs to the separate proposed division-replacement experiment.</p>
</section>
<section id="rationale"><h2>Why LZCNT was wanted</h2>
<div class="callout"><h3>To make an exact division-free slice calculation practical.</h3><p>Cake computes <code>q = work / n</code>, where <code>n</code> comes from the task’s voluntary-switch count. The earlier work explored finding the divisor’s magnitude with a leading-zero count, then using normalization, a reciprocal/table, and multiplication/refinement to recover the exact quotient.</p><p><strong>LZCNT accelerates normalization. It does not calculate the quotient.</strong> The retained prototype measures normalization only; a complete faster exact quotient was not demonstrated.</p></div>
<p>No direct CLZ use was found in current Cake. The earlier suggestion that LZCNT was wanted for highest-bit CPU selection did not capture the actual rationale.</p>
<div class="cards"><article class="card"><span class="tag">Immediate application</span><h3>TZCNT · choose a CPU</h3><p>Cake already uses trailing-zero counts to select a CPU from a bitmap and in its irregular SMT-map fallback.</p></article><article class="card"><span class="tag">Immediate application</span><h3>POPCNT · count idle CPUs</h3><p>Cake already counts set bits to determine whether at least three quarters of an LLC’s CPUs are idle.</p></article><article class="card"><span class="tag">Enables an experiment</span><h3>LZCNT · normalize a divisor</h3><p>Potentially removes an expensive software step from a proposed reciprocal-based quotient. Remaining arithmetic and exactness still need validation.</p></article></div></section>
<section id="priorities"><h2>Which fast paths deserve attention?</h2><div class="scroll"><table><thead><tr><th>Mechanism</th><th>Cake relevance</th><th>Assessment</th></tr></thead><tbody>
<tr><td>TZCNT / POPCNT</td><td>CPU bitmap selection; idle census</td><td>Strongest direct bitops candidates. Historical loaded BPF shows software bit operations. Compare inlined native operations including moves and spills.</td></tr>
<tr><td>LZCNT</td><td>Proposed exact slice/burst quotient</td><td>Promising normalization primitive; no complete quotient win yet.</td></tr>
<tr><td>High multiply · MUL / MULX</td><td>Exact wide starvation cross-products; reciprocal arithmetic</td><td>BPF low-product multiply does not provide the high half. Native high multiply could replace limb arithmetic. Current cross-products can wrap: correctness matters too.</td></tr>
<tr><td>Carry / borrow · ADC / SBB</td><td>Multiword arithmetic and comparison</td><td>Supporting capability for exact wide math. No complete Cake speedup demonstrated.</td></tr>
<tr><td>Conditional move / min-max</td><td>Ranking and caps</td><td>Not automatically better than branches. Retained predictable-branch test beat the CMOV chain.</td></tr>
<tr><td>Rotate / prefetch</td><td>Possible traversal or remote-state designs</td><td>Lower priority; no demonstrated hot rotate site or useful prefetch path. Prefetch needs valid pointers and lead time.</td></tr>
<tr><td>Atomic bit updates</td><td>Queue, idle, seat and frontier state</td><td>Fetching the old value for AND/OR/XOR can require a CMPXCHG retry loop. Non-fetch forms can use a locked instruction. Cake already uses several efficient forms.</td></tr>
<tr><td>Multiply / shifts / divide</td><td>Accounting and slice arithmetic</td><td>Already map to hardware operations in the reference JIT. Division is native DIV/IDIV with setup, not software division.</td></tr>
<tr><td>Helpers / DSQ operations</td><td>CPU ID, clocks, lookup, queue movement</td><td>Some helpers already inline; historical CPU ID does. DSQ lookup, locks and migration are separate kernel-service costs, not missing ALU instructions.</td></tr>
</tbody></table></div></section>
<section id="numbers"><h2>What the retained experiments actually show</h2><p class="muted">September 27, three-run cakesim medians. These are userspace timings of custom-translated BPF with explicit what-if instruction substitutions — not an installed kernel patch or whole-callback measurements.</p>
<div class="scroll"><table><thead><tr><th>Primitive / variant</th><th>Independent calls</th><th>Dependent chain</th><th>Meaning</th></tr></thead><tbody>
<tr><td>Hardware quotient</td><td class="num">0.577 ns</td><td class="num">1.903 ns</td><td>Complete <code>a / (n | 1)</code> primitive</td></tr>
<tr><td>BPF CLZ normalization</td><td class="num">1.382 ns</td><td class="num">4.333 ns</td><td>Normalization only</td></tr>
<tr><td>Byte-table normalization</td><td class="num">2.152 ns</td><td class="num">2.716 ns</td><td>Normalization only</td></tr>
<tr><td>What-if native LZCNT</td><td class="num">0.722 ns</td><td class="num">0.536 ns</td><td>Normalization only; quotient work remains</td></tr>
<tr><td>CPU picker → native TZCNT</td><td class="num">2.032 → 1.967 ns</td><td class="num">3.214 → 1.673 ns</td><td>Simulated picker, not all of select_cpu</td></tr>
<tr><td>Idle census → native POPCNT</td><td class="num">1.744 → 1.319 ns</td><td class="num">1.221 → 0.870 ns</td><td>Simulated census</td></tr>
</tbody></table></div><p>Independent operations overlap; dependent chains wait for the previous result. The difference matters when a CPU index feeds the next scheduling operation.</p>
<div class="callout"><h3 class="warn">Evidence limits found during review</h3><ul><li>All 16 retained native JIT dump files contain retrieval errors. The translated BPF dumps are valid and show software population count.</li><li>The raw instruction microbenchmark includes dependency-feeding instructions. Its printed “cycles” are ADD-calibrated chain costs, not isolated hardware-counter latency.</li><li>The 16-bit census table fails the general test domain. It requires a proven low-16-bit CPU mask and a portable fallback.</li><li>No new scheduler activation or performance test was run for this review. No FPS or input-latency gain is claimed.</li></ul></div></section>
<section id="boundary"><h2>One instruction at which layer?</h2><div class="callout"><code>C expression → LLVM BPF → verifier → x86 JIT → CPU µops</code><p>Native code can still contain a long emulation sequence. One x86 instruction can contain multiple µops. Latency and reciprocal throughput are different measurements.</p></div>
<div class="cards"><article class="card"><h3>BPF expansion</h3><p>The compiler emits shifts, masks and arithmetic. The JIT turns that sequence into native instructions.</p></article><article class="card"><h3>Ordinary kfunc</h3><p>A native function contains the operation, but calling it can add moves, spills, call and return costs.</p></article><article class="card"><h3>JIT-inlined kfunc</h3><p>The JIT substitutes the dedicated instruction. Surrounding argument/result moves and spills still need checking.</p></article></div>
<p>The host advertises ABM, BMI1, BMI2 and POPCNT. Its inspected vmlinux BTF does not expose the proposed <code>bpf_clz64</code>, <code>bpf_ctz64</code>, or <code>bpf_popcnt64</code>. This is a BPF access-path question, not missing CPU hardware.</p>
<p>Published Zen 5 register-to-register <a href="https://uops.info/html-instr/LZCNT_R64_R64.html#ZEN5">LZCNT</a> and <a href="https://uops.info/html-instr/POPCNT_R64_R64.html#ZEN5">POPCNT</a> measurements report one executed µop and one-cycle input-to-result latency. These are architecture reference measurements, separate from Cake’s full expression costs.</p></section>
<section id="decision"><h2>Concrete meeting questions</h2><ol><li>Can shared BPF bitops provide JIT-inlined CTZ, POPCNT and CLZ with portable fallbacks and useful verifier bounds?</li><li>Can the compiler and JIT avoid call-ABI spills around those operations?</li><li>Should unsigned high multiply be considered alongside bitops for exact wide comparisons and reciprocal arithmetic?</li><li>Can we compare the complete exact quotient against native division, rather than compare normalization alone?</li><li>Which costs belong in separate DSQ, clock, locking or migration fast-path work?</li></ol>
<p><strong>Other schedulers can benefit</strong> when they perform bitmap selection, priority-bucket searches, population counts or wide arithmetic. Shared BPF support also benefits programs outside sched_ext; the gain depends on actual use and generated code.</p>
<h3>References and retained evidence</h3><ul>
<li><a href="../AUDIT_OPERATION_COST_2026-09-15.md">Local cost document — sections 7 and 8</a></li>
<li><a href="../../../../../../scx_cake_bench/bench/cakesim/data/suite.md">Retained cakesim result table</a> · <a href="../../../../../../scx_cake_bench/runs/isa_map_20260927/run1.txt">Raw instruction benchmark</a></li>
<li><a href="https://www.mail-archive.com/linux-kernel%40vger.kernel.org/msg2612625.html">Leon Hwang’s bitops proposal</a> — proposal evidence; merge status not established here</li>
<li><a href="https://docs.amd.com/v/u/en-US/58455_1.00">AMD Zen 5 optimization guide</a> · <a href="https://www.uops.info/table.html">Full instruction timing table — select Zen 5</a></li>
</ul></section><footer>Prepared from current Cake source, benchmark sources and results. Static/reference review only. No kernel or scheduler changes.</footer>
</main>
</html>