我有一台64位NUMA机器,有2个库。我知道如果1个银行的CPU试图访问其他银行的内存,会有性能下降。我想通过一些示例代码来测量它。我试着写下面的内容(CPU1和CPU7在不同的库中)
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define BUFSZ 1048576*500
#define SCRATCHSZ 1048576*10
int main()
{
char *scratch;
char *buf;
char *buf2;
struct timeval tv1, tv2;
cpu_set_t affinity_mask;
CPU_ZERO(&affinity_mask);
CPU_SET(1, &affinity_mask);
if (sched_setaffinity(0, sizeof(cpu_set_t), &affinity_mask)) {
return -1;
}
buf = malloc(BUFSZ);
buf2 = malloc(BUFSZ);
scratch = malloc(SCRATCHSZ);
/* Let page fault happen */
memset(buf, 0, BUFSZ);
memset(buf2, 0, BUFSZ);
/* clear the cpu cache */
memset(scratch, 0, SCRATCHSZ);
gettimeofday(&tv1, NULL);
memcpy(buf2, buf, BUFSZ);
gettimeofday(&tv2, NULL);
printf ("Total time 1 = %f useconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) +
(double) (tv2.tv_sec - tv1.tv_sec)*1000000);
free(buf2);
free(scratch);
CPU_ZERO(&affinity_mask);
CPU_SET(7, &affinity_mask);
if (sched_setaffinity(0, sizeof(cpu_set_t), &affinity_mask)) {
return -1;
}
buf2 = malloc(BUFSZ);
scratch = malloc(SCRATCHSZ);
memset(buf2, 0, BUFSZ);
memset(scratch, 0, SCRATCHSZ);
gettimeofday(&tv1, NULL);
memcpy(buf2, buf, BUFSZ);
gettimeofday(&tv2, NULL);
printf ("Total time 2 = %f useconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) +
(double) (tv2.tv_sec - tv1.tv_sec)*1000000);
free(buf);
free(buf2);
free(scratch);
return 0;
}
执行此操作时:
prmpt产品:&gt; ./a.out总时间1 = 169583.000000 useconds总时间2 = 129527.000000 useconds
这不是我所期待的。时间2必须大于time1,就像time2 CPU7试图从不同的存储区访问存储器一样。 有什么想法吗?