如何保证进程运行在特定的物理CPU核心和线程上?

发布于 2025-01-07 19:30:08 字数 1297 浏览 1 评论 0原文

这个问题询问如何确保两个进程在同一个 CPU 上运行。使用 sched_setaffinity 可以将进程限制为多个逻辑 CPU,但如何确保这些映射到特定的物理 CPU 和线程?

我期望映射为:

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

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

然而,当我尝试测试这一点时,情况似乎不一定如此。

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

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 掩码中的位并设置它们一次一个,以便操作系统将进程依次迁移到每个逻辑CPU,然后我打印出当前的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 掩码实际上并不对应于物理核心和线程。

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

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?

I expect that the mapping would be:

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

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.

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;
}

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.

This is what I got:

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2025-01-14 19:30:08

hwloc 是一个可移植的 C 库,用于发现硬件/NUMA 拓扑,以及绑定进程/线程到特定的核心。它具有发现物理/逻辑核心,然后将进程/线程绑定到其上的功能。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文