当前位置:网站首页>缓存--伪共享问题
缓存--伪共享问题
2022-08-03 11:10:00 【zpv2jdfc】
接上一篇 CPU缓存一致性问题
什么是伪共享
伪共享问题指两个线程A和B,他们俩写入同一个cache block的不同变量时,会导致另一个cpu核心的缓存失效的问题。我们来详细看一下伪共享问题到底是怎么产生的:
假设A线程要访问变量A,B线程要访问变量B,并且变量A和B会被分配到同一个cache line中:
接下来,A线程要读取变量A。此时A、B所在的cache line被加载到核心1的cache中,并且状态被标记为独占:
此时,B线程要访问变量B,那么这个cache line被加载到核心2的cache中。并且两个核心的cache line都标记为共享:
接下来,问题来了。假设A线程修改了变量A,为了保证数据一致性,就需要把核心2的cache line标记为失效:
这样一来,如果B要读取变量B的值,就需要A先将cache line写回内存,然后B再从内存中读取。也就是说,明明B变量自始至终都没有改变过,但是在访问时却需要重新从内存读取。如果A、B两个线程轮流修改变量A、B的话,伪共享问题会严重影响性能。
解决伪共享的办法
1.字节填充
对上面这种情况,如果A、B不被分配在同一个cache line中自然就不存在伪共享问题了。
如何让A、B分配在不同cache line中呢?我们可以通过这个命令查看cache line的大小
more /sys/devices/system/cpu/cpu1/cache/index0/coherency_line_size

可以看到cache line大小为64字节。接下来,我们来对比一下使用字节填充后程序性能会提高多少。
不使用字节填充:
public class Main {
public static void main(String[] args) throws InterruptedException {
Pair pair=new Pair();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 0x7fff_ffff; i++) {
pair.x++;
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 0x7fff_ffff; i++) {
pair.y++;
}
});
long start = System.currentTimeMillis();
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(System.currentTimeMillis() - start);
}
}
class Pair{
// long x1,x2,x3,x4,x5,x6,x7;
volatile long x=0;
// long y1,y2,y3,y4,y5,y6,y7;
volatile long y=0;
}
63615
使用字节填充:
public class Main {
public static void main(String[] args) throws InterruptedException {
Pair pair=new Pair();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 0x7fff_ffff; i++) {
pair.x++;
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 0x7fff_ffff; i++) {
pair.y++;
}
});
long start = System.currentTimeMillis();
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(System.currentTimeMillis() - start);
}
}
class Pair{
long x1,x2,x3,x4,x5,x6,x7;
volatile long x=0;
long y1,y2,y3,y4,y5,y6,y7;
volatile long y=0;
}
10781
注意这里要用volatile关键字防止编译器优化指令。
2.使用@Contented 注解
@Contented注解原理也是字节填充,注解既可以加在字段,也可以加在类上。加在字段上表示这个字段单独占一个缓存行,加在类上表示类中所有字段都独占一个缓存行。
使用@Contented注解需要配置jvm参数 -XX:-RestrictContended,通过 -XX:ContendedPaddingWidth 可以修改填充的字节大小,有效值范围0 - 8192,默认是128字节。
边栏推荐
- Dry goods!A highly structured and sparse linear transformation called Deformable Butterfly (DeBut)
- MATLAB programming and application 2.7 Structural data and unit data
- numpy
- 实现2d人物在跳跃的同时左右移动
- 巴比特 | 元宇宙每日必读:玩家离场,平台关停,数字藏品市场正逐渐降温,行业的未来究竟在哪里?...
- 科普大佬说 | 黑客帝国与6G有什么关系?
- Traceback (most recent call last): File
- Realize 2d characters move left and right while jumping
- 【一起学Rust】Rust学习前准备——注释和格式化输出
- LyScript implements memory stack scanning
猜你喜欢
随机推荐
Activiti产生的背景和作用
优维低代码:Provider 构件
ERC20通证标准是什么?
thymeleaf中的日期格式转化
VRRP协议的作用及VRRP+OSPF配置方法
科普大佬说 | 黑客帝国与6G有什么关系?
MySQL之json数据操作
MapReduce中ETL数据清洗案例
JS快速高效开发技巧指南(持续更新)
build --repot
MySQL - 2059 - Authentication plugin ‘caching_sha2_password‘ cannot be loaded
How to retrieve IDC research reports?
试题G:单词分析 ← 第十一届蓝桥杯大赛第二场省赛赛题
87.(cesium之家)cesium热力图(贴地形)
LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
Fastjson反序列化
Matplotlib
MATLAB Programming and Applications 2.6 Strings
Android 技术面试准备(含面试题及答案)
第四周学习 HybridSN,MobileNet V1,V2,V3,SENet








