Compare the thread pointer instead of reading dynamic TLS in NS_IsMainThread on glibc
Categories
(Core :: Performance: Automated Findings, defect)
Tracking
()
People
(Reporter: perf-ai-bot, Unassigned, NeedInfo)
References
(Blocks 1 open bug)
Details
(Whiteboard: [foofrix:5c8df683-743a-4ec4-a7ee-731bd9c4a891])
Attachments
(1 external file)
On glibc x86-64/aarch64 builds whose compiler supports __builtin_thread_pointer, NS_IsMainThread compares the current thread pointer with a relaxed Atomic<void*> that NS_SetMainThread populates. A debug assertion rejects a later NS_SetMainThread call from a different thread. In release builds such a call would move main-thread status to the new thread instead of marking both threads, but every in-tree caller (NS_LogInit, NS_InitMinimalXPCOM) runs on the process's initial thread. The existing TLS declaration, initialization and assignment remain in source, and other targets keep the TLS getter. NS_IsMainThreadTLSInitialized() still returns sTLSIsMainThread.initialized(), which is a constant true (not constexpr) for native TLS. In an optimized x86-64 build, the unused TLS flag is eliminated and NS_IsMainThread performs a RIP-relative load, a comparison against %fs:0 and sete, plus function entry/exit instructions; on aarch64 it reads TPIDR_EL0 instead. Correctness relies on initialization consistently occurring on one thread whose thread pointer remains valid for Gecko's lifetime. Before initialization, the comparison returns false. The fork-server and sandbox clone paths (ForkWithFlags rejects CLONE_SETTLS and CLONE_VM) preserve the calling thread's thread pointer and copied process state.
Problem
The reported glibc x86-64 baseline reads sTLSIsMainThread through dynamic TLS, calling __tls_get_addr on every NS_IsMainThread call. This is a property of libxul's generated code with the default TLS model and dialect, not a universal consequence of compiling a shared library as PIC. On aarch64 the baseline uses a TLS descriptor call instead, so the saving there differs and was not measured. Frequent callers include non-atomic ALWAYS_PREF value getters through MOZ_DIAGNOSTIC_ASSERT; ONCE_PREF and data-mutex getters do not have that same unconditional check. The measured build enables diagnostic assertions, so this source of overhead does not directly represent optimized Release builds.
The submitted profile counts show NS_IsMainThread at 106/62,953 samples before (~0.168%) and 33/61,602 afterward (~0.054%). These inclusive sample shares support the mechanism but do not establish the overall Speedometer improvement. The submitted measurements also disagree: 12 interleaved pairs gave 9.0117 -> 9.0650 runs/min (+0.59%, reported standard error 0.24%), while the finding header reports 8.99 -> 9.0633 runs/min (+0.82%). Performance was not re-measured during this audit.
The libxul PT_TLS segment is 0x16d8 bytes (5,848 bytes). Initial-exec access would require static allocation for the module's TLS block, making it unsuitable as a generally reliable replacement in this dlopen-loaded library. Allocation depends on the entire available static-TLS surplus and loader configuration, not just the optional optimization allowance. glibc allocation implementation.
Performance
- Benchmark: speedometer3
- Test: sp3-full
- Component: Platform
- Local Improvement: 0.82%
Validation
| Reporter | ||
Updated•13 days ago
|
| Reporter | ||
Comment 1•13 days ago
|
||
Please move this bug to the correct component.
Once the Phabricator patch is ready for review, please commandeer it.
| Reporter | ||
Comment 2•13 days ago
|
||
Compare the thread pointer instead of reading dynamic TLS in NS_IsMainThread on glibc
Description
•