<feed xmlns='http://www.w3.org/2005/Atom'>
<title>ydb/library/cpp/yt/rseq/rseq.cpp, branch CLI_2.32.0</title>
<subtitle>Mirror of YDB github repos</subtitle>
<id>https://code.mastervirt.ru/ydb/atom?h=CLI_2.32.0</id>
<link rel='self' href='https://code.mastervirt.ru/ydb/atom?h=CLI_2.32.0'/>
<link rel='alternate' type='text/html' href='https://code.mastervirt.ru/ydb/'/>
<updated>2026-06-19T12:12:00Z</updated>
<entry>
<title>Add lock-free per-CPU primitives to library/cpp/yt/rseq</title>
<updated>2026-06-19T12:12:00Z</updated>
<author>
<name>babenko</name>
<email>babenko@yandex-team.com</email>
</author>
<published>2026-06-19T11:27:43Z</published>
<link rel='alternate' type='text/html' href='https://code.mastervirt.ru/ydb/commit/?id=89c0e29c8f9ba29ecdc736fefda87286482ac213'/>
<id>urn:sha1:89c0e29c8f9ba29ecdc736fefda87286482ac213</id>
<content type='text'>
Introduce AddPerCpu and StorePerCpu over an rseq-sharded per-CPU array.

On the x86-64 Linux fast path the update is committed by a hand-rolled
rseq critical section (non-atomic, migration-safe): addq for the 8-byte
accumulate, movq / movdqu for the 8- or 16-byte store. The kernel
restarts the sequence on preemption or migration, and only one thread
runs on a CPU at a time, so no atomic or lock is needed. Off the fast
path (other arches, no kernel rseq) the operation falls back to an
atomic on the slot indexed by sched_getcpu().

A naturally-aligned 8-byte store is single-copy atomic on x86-64, so it
is never observed torn; the 16-byte store may be, which is acceptable for
a last-writer-wins gauge.
commit_hash:6250f6e9e35cf3895ebafe0b534ec12cca50b03b
</content>
</entry>
<entry>
<title>Fix rseq fast path on glibc &lt; 2.35: read the shared __rseq_abi area</title>
<updated>2026-06-14T16:17:43Z</updated>
<author>
<name>babenko</name>
<email>babenko@yandex-team.com</email>
</author>
<published>2026-06-14T15:52:20Z</published>
<link rel='alternate' type='text/html' href='https://code.mastervirt.ru/ydb/commit/?id=52f00b11c4259dfa810ecdcba576a801dd043a87'/>
<id>urn:sha1:52f00b11c4259dfa810ecdcba576a801dd043a87</id>
<content type='text'>
The own-area approach did not deliver the fast path on glibc 2.31 (YT's current
runtime). There tcmalloc registers the conventional `__rseq_abi` area for every
thread; our attempt to register a separate area was rejected by the kernel with
EINVAL (a thread may have only one rseq area), so `cpu_id` stayed -1 and every
`GetCurrentCpuId()` fell back to `sched_getcpu()` (~17-20 ns, slower than the
rdtscp it replaced).

Read the shared `__rseq_abi` symbol instead -- the area tcmalloc, librseq and
pre-2.35 glibc all register. Our definition is weak, so it coalesces with theirs
when present (the common case -- tcmalloc owns it) and stands alone otherwise
(e.g. musl), with us registering it. We register with the conventional signature
`0x53053053` and size 32, so re-registering an already-registered area returns
EBUSY (treated as success) rather than EINVAL -- coexisting cleanly with tcmalloc.

glibc &gt;= 2.35 still takes the `__rseq_offset` path unchanged.

Measured on sas2-2769 (glibc 2.31 + tcmalloc): `GetCurrentCpuId()` 20.0 ns -&gt; 0.60 ns,
verified via strace that our registration now returns EBUSY against tcmalloc's
`__rseq_abi` (was EINVAL against a separate area).
commit_hash:509809deeb5f7c671817fcd9ebcc8499eabf096e
</content>
</entry>
<entry>
<title>Add library/cpp/yt/rseq: NYT::GetCurrentCpuId() via Linux rseq</title>
<updated>2026-06-13T22:35:19Z</updated>
<author>
<name>babenko</name>
<email>babenko@yandex-team.com</email>
</author>
<published>2026-06-13T22:15:56Z</published>
<link rel='alternate' type='text/html' href='https://code.mastervirt.ru/ydb/commit/?id=450b2fac082943e2029b3390c99501c365795d64'/>
<id>urn:sha1:450b2fac082943e2029b3390c99501c365795d64</id>
<content type='text'>
Self-contained current-CPU-id reader backed by Linux **rseq** (restartable
sequences), with **no third-party dependency** (no librseq):

* The rseq ABI is hand-defined; the calling thread is registered lazily via the
  rseq syscall.
* Fast path is a single inlined, **branch-free** thread-local read. The offset
  always points at a readable `cpu_id` -- the glibc-owned area when glibc registers
  rseq (&gt;= 2.35, via the weak `__rseq_offset`/`__rseq_size`), otherwise our own
  area -- so an unregistered thread reads `-1` and routes to the slow path.
* Falls back to `sched_getcpu()` (Linux) or `0` (darwin/windows). Works on glibc
  **and musl** alike (librseq does not build on musl).

Fiber-TLS contract: the inlined read must be reached only via a non-inlinable,
fiber-switch-free frame (a virtual call or `YT_PREVENT_TLS_CACHING`).

#### Benchmark -- cost of one cpu-id read

| source | time / call |
|---|---|
| `GetCurrentCpuId()` (rseq) | **0.34 ns** |
| `sched_getcpu()` (vDSO) | 3.5 ns |
| `rdtscp` (what `TTscp::Get()` does) | 23 ns |

This is an alternative to the librseq-based review/13886037 -- same speed, but no
contrib dependency and it also covers musl. The unit test pins to each allowed CPU
and asserts the reported id matches.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
commit_hash:09d282c2f48755836b1cd68cedbffc3c6a662eed
</content>
</entry>
</feed>
