首页 > 其他分享 >CPU Cache伪共享(False Sharing)

CPU Cache伪共享(False Sharing)

时间:2023-03-16 11:48:05浏览次数:26  
标签:std __ Sharing False struct int32 Cache aligned

出现伪共享原因:多个线程同时读写同一个 Cache Line 的不同变量时,而导致 CPU Cache 失效的现象称为伪共享(False Sharing)

查看系统Cache Line加载数据大小

cat /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size 
64

解决方式:
结构体对齐Cache Line大小(一般为64 bytes)
避免的方式一般有 Cache Line 大小字节对齐,以及字节填充等方法

#include <iostream>
#include <thread>
#include <chrono>


using namespace std;
using namespace std::chrono;

#define ____cacheline_aligned __attribute__((__aligned__(64)))
#define LOOP 10000 * 10000
struct data {   // 4 bytes
    int32_t x ;
}/*____cacheline_aligned*/;
 
struct data2 {  // 64 bytes
    int32_t x ____cacheline_aligned;
} ;

struct data3 {      // 128 bytes
    int32_t y ;
    int32_t x ____cacheline_aligned;    
} ;

typedef struct data3 Data;
 
Data dArray[2];
 
void f1() {
    int64_t i = 0;
    for(i = 0; i < LOOP; i++) {
        dArray[0].x = 2;
    }
   
    printf("f1 complete\n");
}
 
void f2() {
    int64_t i = 0;
    for(i= 0; i < LOOP; i++) {
        dArray[1].x = 1;
    }
    printf("f2 complete\n");
}
 
int main() {
    printf("sizeof(Data):%ld\n", sizeof(Data)); 
    auto time1 = steady_clock::now();

    std::thread t1(f1);
    std::thread t2(f2);
    t1.join();
    t2.join();
    auto time2 = steady_clock::now();
    auto ts = duration_cast<chrono::milliseconds>(time2-time1).count();
    std::cout << "f1 time:"<<ts<<" ms"<<std::endl;
    printf("complete\n");
    return 0;
}



标签:std,__,Sharing,False,struct,int32,Cache,aligned
From: https://www.cnblogs.com/xiaohuidi/p/17221704.html

相关文章

  • Google Guava Cache
    JAVA8guava31.1-jre--- 序章Guavaisasuiteofcoreandexpandedlibrariesthatincludeutilityclasses,Google'scollections,I/Oclasses,andmuchmor......
  • 虚假新闻检测(CALN)《Open-Topic False Information Detection on Social Networks with
    论文信息论文标题:Open-TopicFalseInformationDetectiononSocialNetworkswithContrastiveAdversarialLearning论文作者:GuanghuiMa,ChunmingHu,LingGe,Hon......
  • CDS即Class-Data Sharing
    引用:https://docs.oracle.com/en/java/javase/17/vm/class-data-sharing.html#GUID-7EAA3411-8CF0-4D19-BD05-DF5E1780AA91 类数据共享(CDS)功能,该功能有助于......
  • C# File.Exist 返回false 无法正确判断文件是否存在
    问题描述:首先澄清一下File.Exist并没有问题,为了让更多没有思路的朋友进来看明白什么原因导致这个问题的,我才会起这么一个标题现在我给大家讲一下为何会发生这个问题。在......
  • 什么是cache一致性?
    1.什么是cache一致性? 1)cache的作用。Zynq7000系列的架构图部分内容如下图所示,它的PS侧有两个cpuarm核,分别成为0核、1核,每个cpu核都有一个D-Cache(数据缓冲区......
  • Linux register cache
    Cache在regmap子系统中的概念  regmap中的cache并不是通常意义上的cache。 我认为一般谈到的cache是介于内存与CPU之间的那块存储设备(指硬件)。 regmap中的cache......
  • MemoryCache 7.0.0.0 版本下获取所有缓存Key
    在使用.Net7.0的过程种,用到微软的MemoryCache,在封装通用接口的时候需要获取所有CacheKey。目前搜索到的方案都是直接取“_entries”私有字段。但在7.0版本之后被包裹在......
  • Unity 测试Transform cache和不cache的区别
    关于Transform的cache问题:大致看了下:https://forum.unity.com/threads/cache-transform-really-needed.356875/Transformcache的却会好一点点 有种Lazy的方式可以......
  • php的pcre使用的NFA引擎可利用pcre.backtrack_limit(最大回溯次数)返回false绕过
    看P神的文章,学习web安全知识的前沿技术栈和各种tricks,这真是一个充满乐趣的过程。这是codebreaking上的第二题:pcrewaf首先先回顾一下php文件上传的相关代码:前端form表......
  • Ehcache初体验
    前言读张开涛写的《亿级流量网站架构核心技术》里面讲到使用Java缓存:堆内缓存,堆外缓存,磁盘缓存,分布式缓存。介绍了几种缓存工具:GauvaCache,Ehcache和MapDB。其中Gauva......