在linux了sched_setaffinity CPU亲和力 [英] sched_setaffinity cpu affinity in linux
问题描述
我已经做在Linux中了sched_setaffinity测试中的服务器与1插槽,4核心,
下面的/ proc内/ cpuinfo showes CPU的信息:
I have done a sched_setaffinity test in Linux in a server with 1 socket ,4 cores , the following /proc/cpuinfo showes the cpu information :
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
我有一个简单的测试应用程序:
I have a simple test application :
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
以下是测试结果:
compiled : gcc testsync.c -D_GNU_SOURCE -lpthread -o testsync.exe The following is the test results :
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 ;
我不知道为什么2个线程核心(0,1)运行或核心(2,3)是多少
在别人快?如果我运行2个线程在同一核心,核心一样(1,1),
核心(2,2),核心(3,3),这将是采取28秒,也困惑,为什么出现这种情况?
I wonder why 2 threads running in core (0,1) or in core(2,3) would be much faster in others ? if I running 2 threads at the same core , like core(1,1) , core(2,2),core(3,3) , that would be take 28 secs , also confused why this happen ?
推荐答案
核心0和1共用一个二级缓存,所以做芯2和3。运行在共享缓存使得共享变量留在L2双核高速缓存,这使得事情更快。
Cores 0 and 1 share an L2 cache, and so do cores 2 and 3. Running on two cores that share the cache makes the shared variable stay in the L2 cache, which makes things faster.
这是不是在当今的英特尔处理器true,其中L 2是每个核心。但是,你正在使用的CPU上,这是它如何工作的(它实际上是用胶粘在一起的两个双核CPU做了四核CPU)。
This is not true in today's Intel processors, where L2 is per core. But on the CPU you're using, this is how it works (it's actually a quad-core CPU made by gluing together two dual-core CPUs).
这篇关于在linux了sched_setaffinity CPU亲和力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!