False sharing problem
6.1.10· Hardware › Parallelism & Multicore
What Is False Sharing?
False sharing tab hoti hai jab multiple processor cores logically independent variables ko modify karte hain jo usi cache line mein rehte hain. Yeh cache coherence protocols (jaise MESI) ko trigger karta hai ki woh line ko invalidate karein aur cores ke beech transfer karein — jis se bina actual data sharing ke bhi contention create hoti hai.
Yeh kyun hota hai:
- Cache coherence cache line granularity par operate karta hai (typically 64 bytes)
- Compilers/allocators data structures ko densely pack karte hain
- Adjacent array elements ya struct fields usi line mein aa jaate hain
- Har core ki write doosre cores ke caches mein line ko invalidate kar deti hai
Sabse important baat: Hardware nahin bata sakta ki tum alag bytes access kar rahe ho. Woh sirf itna jaanta hai: "Kisine is 64-byte block mein likha, toh mujhe baaki sabki copy invalidate karni padegi."
The Mechanism: Cache Line Ping-Pong

Derivation from First Principles
Aao samjhein ki yeh kyun hota hai:
Step 1: Cache coherence granularity
- Memory bytes ke blocks (cache lines) mein divided hoti hai (typically )
- Address cache line par map hota hai
- Do addresses ek line share karte hain agar:
Step 2: MESI protocol constraint
- Ek line zyada se zyada ek cache mein Modified ho sakti hai
- Modified line mein write: local, fast (1-2 cycles)
- Shared/Invalid line mein write: exclusive ownership leni padti hai
- Baaki sabhi caches ko invalidation bhejna
- Acknowledgments ka wait karna
- Modified mein transition karna
- Cost: 50-200 cycles (remote cache access)
Step 3: False sharing scenario
Socho do cores alag variables access kar rahe hain:
- Core 0 address par likhta hai (variable
counter[0]) - Core 1 address par likhta hai (variable
counter[1]) - Agar , toh woh ek cache line share karte hain
Timeline:
Time Core 0 Core 1 Line State
0 Write counter[0] - Modified in C0
1 - Write counter[1] → Invalidate C0
→ Modified in C1
2 Write counter[0] - → Invalidate C1
→ Modified in C0
3 - Write counter[1] → Invalidate C0
→ Modified in C1
... (ping-pong continues)
False sharing write ki cost:
Isolated write se compare karo:
Slowdown factor:
Concrete Examples
The Solution: Cache Line Padding
Goal: Ensure karo ki har core ka data alag cache line occupy kare.
Common Mistakes
Detection and Measurement
False sharing detect kaise karein:
-
Profiling tools:
- Linux
perf c2c(cache-to-cache): Dikhata hai kaunsi cache lines mein high coherence traffic hai - Intel VTune: "Memory Access Analysis" contended cache lines highlight karta hai
- AMD μProf: Cache line contention view
- Linux
-
Performance counters:
MEM_LOAD_RETIRED.FB_HITmonitor karo (load from fill buffer, recent invalidation ka sign)OFFCORE_RESPONSE.DEMAND_RFO.L3_MISS.REMOTE_HITM(remote cache hit-modified, false sharing signature)
-
Symptoms:
- Code poorly scale karta hai (2 cores, 1 core se throughput mein slower)
- Good locality hone ke bawajood high L3 cache miss rate
- CPU utilization low lekin obvious blocking bhi nahin
Cost quantify karna:
Maano = writes per core, = coherence latency (remote cache se line fetch karna), = local write latency.
False sharing ke bina — har write ek line hit karti hai jo already locally Modified state mein hai:
False sharing ke saath — worst case mein, har write line ko doosre core ne churaya hua paati hai, toh use coherence penalty ek baar pay karni padti hai usse wapas acquire karne ke liye:
Key correction — per-write penalty core count ke saath scale nahin karta. Har contended write exactly ek coherence miss pay karta hai (line ko jisne bhi churaya tha usse wapas fetch karne ke liye), chahe ping-pong mein kitne bhi cores participate karein. Zyada cores ka matlab hai ki tumhari line tumhare agle write se pehle chuarayi jaayegi (penalty pay karne ki probability 1 ki taraf badhti hai), lekin ek single write kabhi bhi multiple baar penalty nahin pay karta.
Per-write slowdown factor:
Yeh se independent hai. Isliye realistic worst-case slowdown ratio (~50–100×) se bounded hai, se nahin.
Total time (worst case, har write contended):
The Big Picture: When to Worry
High risk scenarios:
- Per-thread counters/statistics (thread ID se indexed arrays)
- Shared data structures with per-core fields (jaise
ThreadDatastruct) - Lock-free data structures mein adjacent metadata
- Producer-consumer queues jahaan head/tail pointers paas paas hain
Low risk scenarios:
- Read-only data (koi invalidations nahin)
- True data sharing (cores ko waise bhi ek doosre ke updates dekhne hain)
- Single-writer patterns (ek core modify karta hai, baaki padhte hain)
Recall Ek 12-saal ke bachche ko samjhao
Socho tum aur tumhara dost ek bade table par homework kar rahe ho. Tum math kar rahe ho, woh history — bilkul alag subjects. Lekin table par sirf ek bada eraser hai.
Jab bhi TUM galti karte ho aur eraser uthate ho, tumhare dost ko rukna padta hai. Aur jab bhi WOH eraser uthata hai, tumhe rukna padta hai. Bhale hi tum same homework sheet use nahin kar rahe, tum ek doosre ko block karte rehte ho kyunki tum same eraser ke liye fight kar rahe ho.
Yahi false sharing hai! Eraser computer ki "cache line" ki tarah hai. Bhale hi tum alag variables par kaam kar rahe ho (math vs. history), agar woh usi cache line (same eraser) mein stored hain, toh computer cores ek doosre ko block karte rehte hain. Aur ek subtle baat: eraser apne dost se wapas lene mein utna hi waqt lagta hai chahe table par 2 dost hon ya 10 — tum sirf usi se lete ho jiske paas abhi hai. Fix? Har insaan ko apna eraser do (variables ko alag cache lines mein pad karo), aur ab sab poori speed se kaam kar sakte hain!
Connections
- Cache coherence protocols — MESI/MOESI woh mechanisms hain jo false sharing overhead create karte hain
- Cache line size and alignment — 64-byte granularity samajhna foundational hai
- Lock-free data structures — Metadata mein aksar false sharing ke liye vulnerable hote hain
- NUMA architectures — False sharing cross-socket aur bhi expensive hoti hai
- Memory allocators — jemalloc/tcmalloc per-thread arenas se false sharing reduce kar sakte hain
- Atomic operations — Correctness provide karte hain lekin false sharing nahin rokते
- Multicore scaling — False sharing linear speedup mein ek bada barrier hai
#flashcards/hardware
False sharing kya hai? :: Jab multiple cores logically independent variables mein write karte hain jo usi cache line mein rehte hain, jis se unnecessary cache coherence traffic aur performance degradation hoti hai.
False sharing slowdown kyun cause karta hai jab koi data race bhi nahin hai?
x86 processors par typical cache line size kya hai?
False sharing parallel code ko kitna slow kar sakta hai?
Kya per-write false-sharing penalty cores ki sankhya N ke saath scale karta hai?
Do addresses cache line share karte hain ya nahin yeh check karne ka formula kya hai?
False sharing fix kaise karte hain?
alignas(64) ya struct padding use karo.Portable cache line padding ke liye C++17 ka kaunsa feature help karta hai?
std::hardware_destructive_interference_size — current architecture ke liye cache line size deta hai.Kya atomic operations false sharing rok sakte hain?
False sharing detect karne ka Linux tool kaunsa hai?
perf c2c (cache-to-cache) — dikhata hai kaunsi cache lines cores ke beech high coherence traffic rakhti hain.False sharing mein "ping-pong" effect kya hai?
Agar 64 ki jagah 128 bytes tak pad karo, toh downside kya hai?
Intel par false sharing indicate karne wala performance counter kaunsa hai?
OFFCORE_RESPONSE.DEMAND_RFO.L3_MISS.REMOTE_HITM — remote cache hit-modified dikhata hai, jo false sharing ka signature hai.Per-thread counter array mein false sharing kyun hoti hai?
counter[0], counter[1]) sirf 4 bytes apart hain, toh unke 16 ek 64-byte cache line mein fit ho jaate hain. Saare cores usi line ke liye contend karte hain.