[英]Cache line alignment optimization not reducing cache miss
I got this piece of code demonstrating how cache line alignment optimization works by reducing 'false sharing' from http://blog.kongfy.com/2016/10/cache-coherence-sequential-consistency-and-memory-barrier/我得到了这段代码,演示了缓存行 alignment 优化如何通过减少http://blog.kongfy.com/2016/10/cache-coherence-sequential-consistency-and-memory-barrier/中的“错误共享”来工作
Code:代码:
/*
* 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;
}
Expected perf result:预期性能结果:
[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
However, I ran the code myself and got very close run time, the cache miss count is even lower when NOT ALIGNED:但是,我自己运行代码并获得了非常接近的运行时间,当未对齐时,缓存未命中计数甚至更低:
My result:我的结果:
$ 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
How should I understand this inconsistency?我应该如何理解这种不一致?
It's hard to help since the blog you reference to is in Chinese.由于您引用的博客是中文的,因此很难提供帮助。 Still, I've noticed that the first figure seems to show a multi-socket architecture.不过,我注意到第一个图似乎显示了多套接字架构。 So I made a few experiments.所以我做了一些实验。
a) my PC, Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz, single socket, two cores, two threeds per core: a) 我的电脑,Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz,单插槽,两个内核,每个内核两个三个:
0: 0:
time : 195389 us
Performance counter stats for './a.out 100000000 0':
8 980 cache-misses:u
0,198584628 seconds time elapsed
0,391694000 seconds user
0,000000000 seconds sys
and 1:和 1:
time : 191413 us
Performance counter stats for './a.out 100000000 1':
9 020 cache-misses:u
0,192953853 seconds time elapsed
0,378434000 seconds user
0,000000000 seconds sys
Not much difference.差别不大。
b) Now a 2-socket workstation b) 现在是 2 路工作站
Thread(s) per core: 2每个内核的线程数:2
Core(s) per socket: 12每个插槽的核心数:12
Socket(s): 2插座:2
NUMA node(s): 2 NUMA 节点:2
Model name: Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz Model 名称:Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz
0: 0:
time : 454679 us
Performance counter stats for './a.out 100000000 0':
5,644,133 cache-misses
0.456665966 seconds time elapsed
0.738173000 seconds user
1: 1:
time : 346871 us
Performance counter stats for './a.out 100000000 1':
42,217 cache-misses
0.348814583 seconds time elapsed
0.539676000 seconds user
0.000000000 seconds sys
The difference is huge.差异是巨大的。
One final remark.最后一句话。 You write:你写:
the cache miss count is even lower when NOT ALIGNED未对齐时,缓存未命中计数甚至更低
No, it isn't.不,不是。 Your processor is running various tasks besides your program.除了程序之外,您的处理器正在运行各种任务。 Also, you're running 2 threads that may access the cache at different time sequences.此外,您正在运行 2 个线程,这些线程可能会以不同的时间顺序访问缓存。 All this may influence cache utilization.所有这些都可能影响缓存利用率。 You'd need to repeat your measurements several times and compare.您需要多次重复测量并进行比较。 Personally, when I see any performance results differing by less than 10%, I consider them indistinguishable.就个人而言,当我看到任何性能结果的差异小于 10% 时,我认为它们无法区分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.