Memory Models
Why Memory Models ?
Sequential Consistency (SC) Model
Interleaving semantics
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lNwEL7X5-1668869667086)(https://s2.loli.net/2022/09/23/brFgsuhX9Bo1xpP.png)]
每一次读取的都是上一次写的值
The need of weak memory models
SC model prohibits many optimization
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BCljSdaV-1668869667086)(https://s2.loli.net/2022/09/23/FW6EVArtHReiCTx.png)]
Impossible in SC model, but allowed in x86 or Java (because of 编译优化)
Weak memory model allow more behaviors
Design Criteria
- Usability: DRF guarantee
- DRF programs have the same behaviors as in SC model
- Not too strong
- Allow common optimization techniques
- Allow standard compilation schemes to major modern architectures
- In some sense hijacked by the mainstream complier
- Preserve type-safety and security guarantee
- Cannot be too weak (out-of-thin-air)
Data-Race-Freedom(DRF)
Data-race: read-write / write-write conflicts
-
A data race occurs when we have two concurrent conflicting operations
- Conflicting: the two operations both access the same memory location and at least one is a write
- Concurrent ?
- Differs across memory models
- Java: the two operations are not ordered by “happens-before”
SC下 的 happens-before : P O ∪ S W PO\cup SW PO∪SW
-
program-order
-
synchronizes-with
DRF Guarantee
- DRF programs have the same behaviors as in SC
- For DRF programs, the programmer does not need to worry that reorders will affect her code
Compiler Optimization Can Be Smart
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bN0iUCjg-1668869667087)(https://s2.loli.net/2022/09/23/EOWYhvDaP7qnUsM.png)]
Efforts for Java Memory Model (JMM)
- First edition in Java Language Spec
- Current JMM
Happens-Before Order
- Program execution: a set of events, and some orders between them
**Happens-before order (hb): **transitive closure of p o ∪ s w po\cup sw po∪sw
w 1 ⟶ h b w 2 w_1 \stackrel{hb}{\longrightarrow} w_2 w1⟶hbw2
w 1 ⟶ h b r w_1 \stackrel{hb}{ \longrightarrow }r w1⟶hbr
Happens-Before Memory Model (HMM)
Read can see
- the most recent write that happens-before it, or
- a write that has no happens-before relation
r could see both w 1 w_1 w1 ( which happens-before it )
and w 2 w_2 w2 ( with which there is no happens-before relation )
HMM - Relaxed Ordering
HMM - Examples with Global Analysis
HMM - Out-of-Thin-Air-Read
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uCbOZ89y-1668869667088)(https://s2.loli.net/2022/09/23/HLyMbXV3Eu98txB.png)]
- Good speculation -> should allow
- Bad speculation -> disallow!
JMM
- Take HMM as the core, and try hard to distinguish good speculation from bad speculation!
- Introduce 9 axioms to constrain causality.
- Very complex, with surprising results and bugs.
Surprising Results
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RpA1bQio-1668869667088)(https://s2.loli.net/2022/09/23/MDBY7CIpPozTy2O.png)]
Inlining threads may increase behaviors!
More:
Re-ordering independent operations may change behaviors
Adding/removing redundant reads may change behaviors.
More Examples
Load buffering (LB)
Allowed!
Independent reads of independent writes (IRIW)
-
SC下不可能产生
-
HB下Allowed
Out of thin air read
OOTA的行为
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EHBY2CJJ-1668869667089)(https://s2.loli.net/2022/09/23/yjI7Yqk1PBWCR8u.png)]
如何判断 o u t − o f − t h i n − a i r out-of-thin-air out−of−thin−air行为:具体分析
Allowed in HMM !
HMM does not have DRF-guarantee
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BR4NfbzA-1668869667089)(https://s2.loli.net/2022/09/23/iuXxcDGNLQtvUTw.png)]
Summary
- Why need weak memory models
- Design criteria of weak memory models
- The happens-before memory model
- Out-of-thin-air read