【问题标题】:sched_setaffinity cpu affinity in linuxsched_setaffinity linux中的cpu亲和力
【发布时间】:2013-01-18 05:01:05
【问题描述】:

我在 Linux 中进行了 sched_setaffinity 测试,在具有 1 个套接字、4 个内核的服务器中, 以下 /proc/cpuinfo 显示了 cpu 信息:

processor       : 0
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

processor       : 1
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

processor       : 2
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

processor       : 3
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

我有一个简单的测试应用程序:

struct foo {
    int x;
    int y;
}  ;

//globar var
volatile struct foo fvar ;

pid_t gettid( void )
{
    return syscall( __NR_gettid );
}

void *test_func0(void *arg)
{
    int proc_num = (int)(long)arg;
    cpu_set_t set;

    CPU_ZERO( &set );
    CPU_SET( proc_num, &set );
    printf("proc_num=(%d)\n",proc_num) ;
    if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))
    {
        perror( "sched_setaffinity" );
        return NULL;
    }


    int i=0;
    for(i=0;i<1000000000;++i){
        __sync_fetch_and_add(&fvar.x,1);
    }
    return NULL;
} //test_func0

编译: gcc testsync.c -D_GNU_SOURCE -lpthread -o testsync.exe 以下是测试结果:

2 threads running test_func0 in core 0,1  take 35 secs ;
2 threads running test_func0 in core 0,2  take 55 secs ;
2 threads running test_func0 in core 0,3  take 55 secs ;
2 threads running test_func0 in core 1,2  take 55 secs ;
2 threads running test_func0 in core 1,3  take 55 secs ;
2 threads running test_func0 in core 2,3  take 35 secs ;

我想知道为什么在 core (0,1) 或 core(2,3) 中运行的 2 个线程会很多 在别人身上更快?如果我在同一个核心上运行 2 个线程,例如 core(1,1) , core(2,2),core(3,3) ,这需要 28 秒,也很困惑为什么会这样?

【问题讨论】:

  • 附带问题:声明void *test_func0() 并始终返回NULL 有什么意义?
  • 感谢您的cmets,我只是使用alexonlinux.com/…的样本进行这次测试!

标签: c linux cpu


【解决方案1】:

核心 0 和 1 共享 L2 缓存,核心 2 和 3 也是如此。在共享缓存的两个核心上运行会使共享变量保留在 L2 缓存中,从而加快速度。

在当今的英特尔处理器中,情况并非如此,其中 L2 是每个内核。但是在你使用的 CPU 上,它是这样工作的(它实际上是一个由两个双核 CPU 粘合在一起制成的四核 CPU)。

【讨论】:

  • 谢谢你,ugoren,请问,除了做测试,在哪里以及如何获得更多关于你所描述的cpu的详细信息,通过将两个双核CPU粘合在一起制成的四核?
  • 在英特尔的网站上,例如intel.com/design/corei5/documentation.htm
猜你喜欢
  • 2011-11-20
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 2022-12-05
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多