如何确保进程在特定的物理CPU内核和线程中运行? [英] How can I ensure that a process runs in a specific physical CPU core and thread?

查看:138
本文介绍了如何确保进程在特定的物理CPU内核和线程中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This question asks about ensuring two processes run on the same CPU. Using sched_setaffinity I can limit a process to a number of logical CPUs, but how can I ensure that these are mapped to specific physical CPUs and threads?

我希望映射为:

0-CPU 0线程0
1-CPU 0线程1
2-CPU 1线程0
3-CPU 1线程1
等等...

0 - CPU 0 thread 0
1 - CPU 0 thread 1
2 - CPU 1 thread 0
3 - CPU 1 thread 1
etc...

,其中左侧的数字是sched_setaffinity中使用的相关CPU.

where the number on the left is the relevant CPU used in sched_setaffinity.

但是,当我尝试对此进行测试时,似乎并不一定是这种情况.

However, when I tried to test this, it appeared that this is not necessarily the case.

为了测试这一点,我使用了CPUID指令,该指令返回了EBX中当前内核的初始APIC ID:

To test this, I used the CPUID instruction, which returns the initial APIC ID of the current core in EBX:

void print_cpu() 
{
    int cpuid_out;

    __asm__(
    "cpuid;"
        : "=b"(cpuid_out) 
        : "a"(1) 
        :);

    std::cout << "I am running on cpu " << std::hex << (cpuid_out >> 24) << std::dec << std::endl;
}

然后,我循环遍历cpu掩码中的位,并一次将它们设置为1,以便操作系统将进程依次迁移到每个逻辑CPU,然后打印出当前的CPU.

Then I looped over the bits in the cpu mask and set them one at a time so that the OS would migrate the process to each logical CPU in turn, and then I printed out the current CPU.

这就是我得到的:

cpu mask is 0 
I am running on cpu 0
cpu mask is 1 
I am running on cpu 4
cpu mask is 2 
I am running on cpu 2
cpu mask is 3 
I am running on cpu 6
cpu mask is 4 
I am running on cpu 1
cpu mask is 5 
I am running on cpu 5
cpu mask is 6 
I am running on cpu 3
cpu mask is 7 
I am running on cpu 7

假设CPU根据上面列出的方案分配了初始APIC ID,似乎cpu掩码实际上并不对应于物理内核和线程.

assuming that the CPU assigns initial APIC IDs according to the scheme I listed above, it would seem that the cpu mask doesn't actually correspond to the physical core and thread.

如何找到sched_setaffinity的掩码中的位到物理核心的正确映射?

How can I find the correct mapping of bits in the mask for sched_setaffinity to physical cores?

推荐答案

hwloc 是一个便携式C库,用于发现硬件/NUMA拓扑,并将进程/线程绑定到特定的内核.它具有发现物理/逻辑核心,然后将进程/线程绑定到它的功能.

hwloc is a portable C library for discovering hardware/NUMA topology, and also binding processes/threads to particular cores. It has functions to discover physical/logical cores, and then bind a process/thread to it.

看起来它也可以如果您想继续直接使用cpu_set_t,还可以返回cpu_set_tsched_setaffinity()一起使用.

It also looks like it can also return a cpu_set_t for use with sched_setaffinity(), if you want to keep using that directly.

这篇关于如何确保进程在特定的物理CPU内核和线程中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆