为什么False Sharing测试中同核SMT兄弟线程L1d缓存未命中远高于独立核线程
问题背景
- 此前检索未找到同方向的公开技术讨论:聚焦SMT/超线程场景下L1d缓存未命中与False Sharing的关联现象。
- 学习MESI/MOESI缓存一致性协议、False Sharing原理数日后,编写基准测试程序验证False Sharing实际表现,测试设备为Ryzen 5 3600 CPU。
- 测试核心设计:使用长度为8的double类型数组(刚好匹配单Cache Line大小),由两个线程分别自增数组内相邻元素;线程绑定不同逻辑核,分别更新指定数组位置(例:线程A更新array[2]、线程B更新array[3])。
两组对照测试场景
- 同物理核SMT兄弟线程绑定:将线程绑定到共享L1d缓存的硬件线程#0与#6,运行耗时约5秒
- 跨独立物理核线程绑定:将线程绑定到无任何共享缓存的硬件线程#0与#11,运行耗时约9.5秒
运行时长差异符合常规认知:跨物理核场景下存在Cache Line Ping-Pong行为,因此整体耗时更长。
反常观测现象
跨物理核场景的L1d缓存未命中数,反而显著低于同物理核SMT线程场景:
- 同SMT兄弟线程场景:L1d未命中率2.34%,绝对未命中数3.96亿
- 跨独立物理核场景:L1d未命中率0.75%,绝对未命中数1.18亿
初步推导与排除的影响因素
跨物理核场景下,共享Cache Line被修改后,另一核的缓存副本会按MOESI协议被Invalidate,虽然存在频繁的Cache Line Ping-Pong行为,但统计到的L1d未命中数反而更少,过程仅伴随缓存无效化、核间缓存块传输。
目前已排除两个可能的干扰因素:
- 同物理核共享L2的影响:缓存行被无效化后,要么从主内存、要么从其他核缓存获取,共享L2既无法留存对应无效数据、也无法直接响应请求
- 同核内dirty缓存行的访问问题:同一物理核内线程写入产生的L1d dirty状态缓存行,对同核另一线程的读取是完全可见的,不存在额外访问障碍
核心待解问题
SMT兄弟线程(同物理核两个逻辑线程)运行False Sharing负载时,为什么L1d缓存未命中数是跨独立物理核场景的3倍?
注:所有L1d缓存未命中统计数据均通过Linux perf工具采集。
次要疑问
CPU的逻辑线程ID为什么存在固定间隔:例如线程0的SMT兄弟是线程6,线程i的SMT兄弟为线程i+6,该编号规则在Intel、AMD CPU上均有观测到,这种设计存在什么特定作用?
测试代码与运行配置
测试代码
#define _GNU_SOURCE #include <stdio.h> #include <sched.h> #include <stdlib.h> #include <sys/random.h> #include <time.h> #include <pthread.h> #include <unistd.h> #include <errno.h> #include <string.h> struct timespec tstart, tend; static cpu_set_t cpuset; typedef struct arg_s { int index; double *array_ptr; } arg_t; void *work(void *arg) { pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); int array_index = ((arg_t*)arg)->index; double *ptr = ((arg_t*)arg)->array_ptr; for(unsigned long i=0; i<1000000000; i++) { // 仅访问相邻数组位置,不影响False Sharing核心测试逻辑 ptr[array_index] ++; // ptr[array_index] += 1.0e5 * 4; } return NULL; } int main() { pthread_t tid[2]; srand(time(NULL)); static int cpu0 = 0; static int cpu6 = 6; // 将值改为11即可切换为跨核(#0、#11)测试场景 CPU_ZERO(&cpuset); CPU_SET(cpu0, &cpuset); CPU_SET(cpu6, &cpuset); double array[8]; for(int i=0; i<8; i++) array[i] = drand48(); arg_t *arg0 = malloc(sizeof(arg_t)); arg_t *arg1 = malloc(sizeof(arg_t)); arg0->index = 0; arg0->array_ptr = array; arg1->index = 1; arg1->array_ptr = array; clock_gettime(CLOCK_REALTIME, &tstart); pthread_create(&tid[0], NULL, work, (void*)arg0); pthread_create(&tid[1], NULL, work, (void*)arg1); pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); clock_gettime(CLOCK_REALTIME, &tend); }
编译与运行命令
- 编译命令:
gcc -pthread p.c -o p - 测试统计命令:同核场景为
perf stat -d ./p --cpu=0,6,跨核场景为perf stat -d ./p --cpu=0,11
实测性能统计结果
同SMT兄弟线程(绑定#0、#6)运行统计
Performance counter stats for './p --cpu=0,6': 9437,29 msec task-clock # 1,997 CPUs utilized 64 context-switches # 0,007 K/sec 2 cpu-migrations # 0,000 K/sec 912 page-faults # 0,097 K/sec 39569031046 cycles # 4,193 GHz (75,00%) 5925158870 stalled-cycles-frontend # 14,97% frontend cycles idle (75,00%) 2300826705 stalled-cycles-backend # 5,81% backend cycles idle (75,00%) 24052237511 instructions # 0,61 insn per cycle # 0,25 stalled cycles per insn (75,00%) 2010923861 branches # 213,083 M/sec (75,00%) 357725 branch-misses # 0,02% of all branches (75,03%) 16930828846 L1-dcache-loads # 1794,034 M/sec (74,99%) 396121055 L1-dcache-load-misses # 2,34% of all L1-dcache accesses (74,96%) <not supported> LLC-loads <not supported> LLC-load-misses 4,725786281 seconds time elapsed 9,429749000 seconds user 0,000000000 seconds sys
跨独立物理核线程(绑定#0、#11)运行统计
Performance counter stats for './p --cpu=0,11': 18693,31 msec task-clock # 1,982 CPUs utilized 114 context-switches # 0,006 K/sec 1 cpu-migrations # 0,000 K/sec 903 page-faults # 0,048 K/sec 78404951347 cycles # 4,194 GHz (74,97%) 1763001213 stalled-cycles-frontend # 2,25% frontend cycles idle (74,98%) 71054052070 stalled-cycles-backend # 90,62% backend cycles idle (74,98%) 24055983565 instructions # 0,31 insn per cycle # 2,95 stalled cycles per insn (74,97%) 2012326306 branches # 107,650 M/sec (74,96%) 553278 branch-misses # 0,03% of all branches (75,07%) 15715489973 L1-dcache-loads # 840,701 M/sec (75,09%) 118455010 L1-dcache-load-misses # 0,75% of all L1-dcache accesses (74,98%) <not supported> LLC-loads <not supported> LLC-load-misses 9,430223356 seconds time elapsed 18,675328000 seconds user 0,000000000 seconds sys
内容的提问来源于stack exchange,提问作者Zeosleus
相关产品推荐
相关产品推荐

