sched_setaffinity在Linux中的cpu亲和力

barfatchen:

我在具有1个套接字,4个内核的服务器中的Linux中完成了sched_setaffinity测试,以下/ 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 ;

我想知道为什么在核心(0,1)或在核心(2,3)中运行的2个线程在其他线程中会更快吗?如果我在同一个内核上运行2个线程,例如core(1,1),core(2,2),core(3,3),那将花费28秒,那么为什么会这样呢?

ugoren:

核心0和1共享一个L2缓存,核心2和3也共享。在共享缓存的两个内核上运行会使共享变量保留在L2缓存中,这使处理速度更快。

在当今的Intel处理器(每个内核为L2)中,情况并非如此。但是在您使用的CPU上,它是这样工作的(实际上是通过将两个双核CPU粘合在一起而制成的四核CPU)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过sched_setaffinity在多个CPU上设置亲和力

如何在Linux中的C或C ++中为进程设置CPU亲和力?

Linux线程和进程-CPU亲和力

如何在Linux中从C使用sched_getaffinity和sched_setaffinity?

如何在Wine或CrossOver中设置CPU亲和力?

在Windows中设置永久进程CPU核心亲和力

如何设置与进程的CPU亲和力

全局设置CPU亲和力

sched_setaffinity()如何工作?

从一开始就在Linux上设置进程的cpu亲和力

在可加载的Linux内核模块上设置cpu亲和力

设置(系统范围的)CPU亲和力,以在Linux平台上运行进程

CPU亲和力掩码(将线程放入不同的CPU)

如何设置Java线程的cpu核心亲和力?

为特定程序设置CPU亲和力吗?

函数“ sched_setaffinity”的隐式声明

将sched_setaffinity的最大CPU数作为基准的正确值是什么?

R:亲和力传播中的特征选择

Flink 中的任务管理器亲和力

在被调用的core_pattern过程中调用sched_setaffinity失败

无法避免子进程继承父进程的cpu亲和力

为什么设置CPU亲和力会使线程运行更慢?

通过克隆创建的子代是否继承其父代的CPU亲和力掩码?

设置std :: threads的线程亲和力

在Flex中调用Win32 API设置窗口显示亲和力

如何分析R中的亲和力传播聚类的图结果?

如何在容器中实现处理器亲和力?

如何使用从laravel中的数组获取的各种术语进行mysql亲和力查询

.NET x64中的for循环性能奇数:偶数迭代亲和力?