多线程下的false sharing问题+编程实践(待完善)

1.false sharing原因:

CPU loads memory into cache by "line"

Linux下获取cache line :

cat /proc/cpuinfo | grep cache_alignment

or cat /sys/devices/system/cpu/cpuN/cache/indexN/coherency_line_size 以及其他详尽的cpu cache信息

 

Maybe : get cacheline programatically ( via C language )

Multi-cpu contention algorithm "hardware write lock"

Maybe : cache algorithms "MESI"

 

2.程序示例

根据开启的线程数量,观察完成时间的scaling情况,同时观察CPU load

实践:验证the time waiting for memory is counted into CPU time?

 

3.数据在同一cache line的原因

Array is continuous

The linker lay out global or static data closely in the memory ( in the data segment? )

structs and C++ object layout is compact

Two individual objects on the heap happens to be nearby.(Especially for same kind of objects allocated using its own memory-pool or slab-allocator)

 

4.解决办法

1)reduce access of the false-sharing structure <= use thread stack or local storage

2)using alignment + pading to eliminate false-sharing

Reminder : the CACHE_LINE_SIZE must be defined at compiling time

c++0x syntax : [[ align(CACHE_LINZE_SIZE) ]] T data;

(可惜GCC现在仍然不支持alignment,在此查看GCC支持的C++0x功能列表)

GCC's externsion : __attribute__(( aligned(CACHE_LINE_SIZE) )) T data;

实践:A general wrapper to ensure 'correct individual ' : CacheLineStorage<T> (Maybe only useful for data object)

 

5. 最佳实践+总结

1)尽量减少线程间共享结构的频繁读写(尽管无需锁,依然会造成cache-line miss)

2)如果共享结构必须,则注意使在不同线程中频繁读取的数据位于不同的cache line上

 

6. 数据 + Profiling方法

Intel Pentium M:

Register <= 1;  L1d ~ 3;  L2 ~ 14; Main-Memory ~ 240; (cycles)

按2GHz CPU计算,cache-miss-time最大约为100ns (0.1us)?

TODO : 使用profiling工具来检查CPI和cache miss rate

 

参考文献:

Eliminate false sharing by Herb Sutter (nice blog and author!)

http://drdobbs.com/go-parallel/article/217500206?pgno=1

 

 

posted @   PromisE_谢  阅读(1350)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示