【发布时间】:2022-01-20 17:44:50
【问题描述】:
我得到了这段代码,展示了缓存行对齐优化如何通过减少http://blog.kongfy.com/2016/10/cache-coherence-sequential-consistency-and-memory-barrier/ 的“错误共享”来工作
代码:
/*
* Demo program for showing the drawback of "false sharing"
*
* Use it with perf!
*
* Compile: g++ -O2 -o false_share false_share.cpp -lpthread
* Usage: perf stat -e cache-misses ./false_share <loopcount> <is_aligned>
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#define CACHE_ALIGN_SIZE 64
#define CACHE_ALIGNED __attribute__((aligned(CACHE_ALIGN_SIZE)))
int gLoopCount;
inline int64_t current_time()
{
struct timeval t;
if (gettimeofday(&t, NULL) < 0) {
}
return (static_cast<int64_t>(t.tv_sec) * static_cast<int64_t>(1000000) + static_cast<int64_t>(t.tv_usec));
}
struct value {
int64_t val;
};
value data[2] CACHE_ALIGNED;
struct aligned_value {
int64_t val;
} CACHE_ALIGNED;
aligned_value aligned_data[2] CACHE_ALIGNED;
void* worker1(int64_t *val)
{
printf("worker1 start...\n");
volatile int64_t &v = *val;
for (int i = 0; i < gLoopCount; ++i) {
v += 1;
}
printf("worker1 exit...\n");
}
// duplicate worker function for perf report
void* worker2(int64_t *val)
{
printf("worker2 start...\n");
volatile int64_t &v = *val;
for (int i = 0; i < gLoopCount; ++i) {
v += 1;
}
printf("worker2 exit...\n");
}
int main(int argc, char *argv[])
{
pthread_t race_thread_1;
pthread_t race_thread_2;
bool is_aligned;
/* Check arguments to program*/
if(argc != 3) {
fprintf(stderr, "USAGE: %s <loopcount> <is_aligned>\n", argv[0]);
exit(1);
}
/* Parse argument */
gLoopCount = atoi(argv[1]); /* Don't bother with format checking */
is_aligned = atoi(argv[2]); /* Don't bother with format checking */
printf("size of unaligned data : %d\n", sizeof(data));
printf("size of aligned data : %d\n", sizeof(aligned_data));
void *val_0, *val_1;
if (is_aligned) {
val_0 = (void *)&aligned_data[0].val;
val_1 = (void *)&aligned_data[1].val;
} else {
val_0 = (void *)&data[0].val;
val_1 = (void *)&data[1].val;
}
int64_t start_time = current_time();
/* Start the threads */
pthread_create(&race_thread_1, NULL, (void* (*)(void*))worker1, val_0);
pthread_create(&race_thread_2, NULL, (void* (*)(void*))worker2, val_1);
/* Wait for the threads to end */
pthread_join(race_thread_1, NULL);
pthread_join(race_thread_2, NULL);
int64_t end_time = current_time();
printf("time : %d us\n", end_time - start_time);
return 0;
}
预期性能结果:
[jingyan.kfy@OceanBase224006 work]$ perf stat -e cache-misses ./false_share 100000000 0
size of unaligned data : 16
size of aligned data : 128
worker2 start...
worker1 start...
worker1 exit...
worker2 exit...
time : 452451 us
Performance counter stats for './false_share 100000000 0':
3,105,245 cache-misses
0.455033803 seconds time elapsed
[jingyan.kfy@OceanBase224006 work]$ perf stat -e cache-misses ./false_share 100000000 1
size of unaligned data : 16
size of aligned data : 128
worker1 start...
worker2 start...
worker1 exit...
worker2 exit...
time : 326994 us
Performance counter stats for './false_share 100000000 1':
27,735 cache-misses
0.329737667 seconds time elapsed
但是,我自己运行代码并且运行时间非常接近,当未对齐时,缓存未命中计数甚至更低:
我的结果:
$ perf stat -e cache-misses ./false_share 100000000 0
size of unaligned data : 16
size of aligned data : 128
worker1 start...
worker2 start...
worker2 exit...
worker1 exit...
time : 169465 us
Performance counter stats for './false_share 100000000 0':
37,698 cache-misses:u
0.171625603 seconds time elapsed
0.334919000 seconds user
0.001988000 seconds sys
$ perf stat -e cache-misses ./false_share 100000000 1
size of unaligned data : 16
size of aligned data : 128
worker2 start...
worker1 start...
worker2 exit...
worker1 exit...
time : 118798 us
Performance counter stats for './false_share 100000000 1':
38,375 cache-misses:u
0.121072715 seconds time elapsed
0.230043000 seconds user
0.001973000 seconds sys
我应该如何理解这种不一致?
【问题讨论】:
-
我在 CentOS 上运行我的代码,CPU 是 Intel(R) Core(TM) i3-10105 CPU @ 3.70GHz。
-
不一致是什么意思?每个人都有不同的机器,因此每个数值实验都会产生不同的结果。请更具体地说明我们应该寻找什么。
-
@zkoza 他正在对齐数据,因此可以更快地获取数据。在大多数硬件上应该是这样。
标签: c++ gcc caching optimization g++