Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3202350
  • 博文数量: 1805
  • 博客积分: 135
  • 博客等级: 入伍新兵
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 20:01
文章分类

全部博文(1805)

文章存档

2017年(19)

2016年(80)

2015年(341)

2014年(438)

2013年(349)

2012年(332)

2011年(248)

分类: C/C++

2013-08-25 10:01:43

前阵子接触到一道关于数组内部链表(多用于内存池技术)的数据结构的题, 这种数据结构能够比普通链表在cache中更容易命中, 理由很简单, 就是因为其在地址上是连续的(=.=!), 借这个机会, 就对cpu cache进行了一个研究, 今天做一个简单的分享, 首先先来普及一下cpu cache的知识, 这里的cache是指cpu的高速缓存. 在我们程序员看来, 缓存是一个透明部件. 因此, 程序员通常无法直接干预对缓存的操作. 但是, 确实可以根据缓存的特点对程序代码实施特定优化, 从而更好地利用高速缓存. 
高速缓存的置换策略会尽可能地将访问频繁的数据放入cache中, 这是一个动态的过程, 所以cache中的数据不会一直不变. 目前一般的机器的cpu cache可分为一级缓存和二级缓存. 一级缓存更靠近cpu, 速度比二级缓存更快. 二级缓存比一级缓存速度更慢, 容量更大, 主要就是做一级缓存和内存之间数据临时交换的地方用.
这两者和RAM在空间和效率上的关系如下:
L1 Cache ---> L2 Cache ---> RAM
------------> 容量递增 ------------>
------------> 速度递减 ------------>
-----> CPU访问优先级递减 ----->

在linux系统中, 我们可以使用cat /proc/cpuinfo 来获知机器的cpu和核数.
而cpu cache的信息, 我们通过dmesg | grep cache来获知.
例如:
CPU: L1 I Cache: 64K (64 bytes/line), D cache 64K (64 bytes/line)
CPU: L1 I Cache: 64K (64 bytes/line), D cache 64K (64 bytes/line)
说明我这台机器有两个处理器, 并只有一级缓存, 大小为 64K, 缓存行/快 大小为64 bytes.

由于不同的处理器之间都具有自己的高速缓存, 所以当两个cpu的cache中都存有数据a, 那么就有可能需要进行同步数据, 而cache之间同步数据的最小单元为cache行大小, 可以把一个cache想象成一张表, 表的每一行都是64bytes(假设), 当cpu被告知cache第一行的第一个byte为脏数据时, cpu会将第一行都进行同步.
例如以下场景:

CPU1读取了数据a(假设a小于cache行大小),并存入CPU1的高速缓存.

CPU2也读取了数据a,并存入CPU2的高速缓存.

CPU1修改了数据a, a被放回CPU1的高速缓存行. 但是该信息并没有被写入RAM.

CPU2访问a, 但由于CPU1并未将数据写入RAM, 导致了数据不同步.

为了解决这个问题, 芯片设计者制定了一个规则. 当一个CPU修改高速缓存行中的字节时, 计算机中的其它CPU会被通知, 它们的高速缓存将视为无效. 于是, 在上面的情况下, CPU2发现自己的高速缓存中数据已无效, CPU1将立即把自己的数据写回RAM, 然后CPU2重新读取该数据. 这样就完成了一次两个cpu之间cache的同步.

为了测试上述场景, 我编写了如下程序进行测试:

--------------------------------------------------------

  1. #include<sys/types.h>
  2. #include<sys/sysinfo.h>
  3. #include <sys/time.h>
  4. #include<unistd.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <pthread.h>

  8. #include <iostream>
  9. using namespace std;

  10. #define ENABLE_WHCIH_CPU
  11. #define ENABLE_SET_CPU

  12. #define EXEC_COUNT (100 * 1000 * 1000)

  13. struct bits_t
  14. {
  15.  int a;
  16.     char placeholder[64];
  17.  int b;
  18. };

  19. struct bits_t bits;

  20. int which_cpu(const char* prefix_)
  21. {
  22.     #ifdef ENABLE_WHCIH_CPU
  23.  cpu_set_t cur_cpu;
  24.  CPU_ZERO(&cur_cpu);
  25.  if (sched_getaffinity(0, sizeof(cur_cpu), &cur_cpu) == -1)
  26.  {
  27.   printf("warning: cound not get cpu affinity, continuing...\n");
  28.   return -1;
  29.  }
  30.  int num = sysconf(_SC_NPROCESSORS_CONF);
  31.  for (int i = 0; i < num; i++)
  32.  {
  33.   if (CPU_ISSET(i, &cur_cpu))
  34.   {
  35.    printf("[%s] this process %d is running processor : %d\n", prefix_, getpid(), i);
  36.   }
  37.  }
  38.     #endif

  39.  return 0;
  40. }

  41. int set_cpu(int cpu_id_)
  42. {
  43.     #ifdef ENABLE_SET_CPU
  44.  cpu_set_t mask;
  45.  CPU_ZERO(&mask);
  46.  CPU_SET(cpu_id_, &mask);
  47.  if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
  48.  {
  49.   printf("warning: could not set CPU affinity, continuing...\n");
  50.   return -1;
  51.     }
  52.     #endif

  53.  return 0;
  54. }

  55. void* thd_func1(void* arg_)
  56. {
  57.  set_cpu(0);
  58.  which_cpu("thread 1 start");
  59.     timeval begin_tv;
  60.     gettimeofday(&begin_tv, NULL);

  61.     for (int i = 0; i < EXEC_COUNT; i++)
  62.     {
  63.         bits.a += 1;
  64.         int a = bits.a;
  65.     }

  66.     timeval end_tv;
  67.     gettimeofday(&end_tv, NULL);
  68.     printf("thd1 perf:[%lu]us\n", (end_tv.tv_sec * 1000 * 1000 + end_tv.tv_usec) - (begin_tv.tv_sec * 1000 * 1000 + begin_tv.tv_usec));
  69.  which_cpu("thread 1 end");

  70.     return NULL;
  71. }

  72. void* thd_func2(void* arg_)
  73. {
  74.  set_cpu(1);
  75.  which_cpu("thread 2 start");
  76.     timeval begin_tv;
  77.     gettimeofday(&begin_tv, NULL);

  78.     for (int i = 0; i < EXEC_COUNT; i++)
  79.     {
  80.         bits.b += 2;
  81.         int b = bits.b;
  82.     }

  83.     timeval end_tv;
  84.     gettimeofday(&end_tv, NULL);
  85.     printf("thd2 perf:[%lu]us\n", (end_tv.tv_sec * 1000 * 1000 + end_tv.tv_usec) - (begin_tv.tv_sec * 1000 * 1000 + begin_tv.tv_usec));
  86.  which_cpu("thread 2 end");

  87.     return NULL;
  88. }


  89. int main(int argc_, char* argv_[])
  90. {
  91.  int num = sysconf(_SC_NPROCESSORS_CONF);
  92.  printf("system has %d processor(s).\n", num);
  93.  cpu_set_t cpu_mask;
  94.  cpu_set_t cur_cpu_info;

  95.  memset((void*)&bits, 0, sizeof(bits_t));
  96.  set_cpu(0);
  97.  which_cpu("main thread");

  98.     pthread_t pid1;
  99.     pthread_create(&pid1, NULL, thd_func1, NULL);

  100.     pthread_t pid2;
  101.     pthread_create(&pid2, NULL, thd_func2, NULL);

  102.     pthread_join(pid1, NULL);
  103.     pthread_join(pid2, NULL);

  104.     return 0;
  105. }

--------------------------------------------------------
该程序中会创建两个线程, 分别对全局变量bits的a和b成员进行1亿次加法操作.
在这里我分别针对四种情况进行了测试 -
1. 两个线程分别跑在不同的cpu上, bits_t结构体没有placeholder这64个填充字节.
2. 两个线程分别跑在不同的cpu上, bits_t结构体有placeholder这64个填充字节.
3. 两个线程分别跑在相同的cpu上, bits_t结构体没有placeholder这64个填充字节.
4. 两个线程分别跑在相同的cpu上, bits_t结构体有placeholder这64个填充字节.
程序可以通过set_cpu函数来将线程绑定到指定的cpu上去.
为了大家阅读的方便, 我已将测试结果报告整理成以下四个表格.
情况一测试结果:
 线程id CPU绑定 有无placeholder 平均耗时(微妙)
 1 cpu0 无 2186931
 2 cpu1 无 2033496
   
情况二测试结果:
线程id CPU绑定 有无placeholder 平均耗时(微妙)
 1 cpu0 有 402144
 2 cpu1 有 392745
   
我们先来看情况一和情况二的结果对比, 显然, 后者要比前者效率高得多的多, 可以验证在有 placeholder填充字节之后, bit_t的a和b域被划分到了cache的不同两行, 所以当在cpu0执行的线程1修改a后, cpu1在读b时, 不需要去同步cache. 而情况一因为a和b在cache中的同一行, 导致两个cpu要互相进行大量的cache行同步.

情况三测试结果:
线程id CPU绑定 有无placeholder 平均耗时(微妙)
 1 cpu0 无 716056
 2 cpu0 无 686804
   
情况四测试结果:
线程id CPU绑定 有无placeholder 平均耗时(微妙)
 1 cpu0 有 761421
 2 cpu0 有 884969


可以看出, 情况三和四, 因为两个线程运行在同一个cpu上, 有和没有placeholder填充字节在性能上几乎没有什么区别, 因为不存在cache之间行同步的问题, 但是由于是一个cpu在调度切换两个线程, 所以要比情况一慢一点.

从上面测试结果看来, 某些特定情况下, 对于cache的优化还是很重要的, 但是也不能一味地为了追求性能都将所有共享数据加入填充字节, 毕竟cache就那么大, 如果不是某些特定的读写非常频繁的场景下, 没有必要这么做.

PS: 由于不同的硬件架构体系之间会有差别, 例如某些硬件架构同一个cpu下的两个物理核之间共享cache, 所以测试时要试具体环境而定.

阅读(337) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~