简体   繁体   English

为什么我的电脑更喜欢偶数核心?

[英]Why does my pc prefer even numbered cores?

My pc has a 10th gen Core i7 vPRO with virtualization enabled.我的电脑有一个启用了虚拟化的第 10 代 Core i7 vPRO。 8 cores + 8 virtual cores. 8 个核心 + 8 个虚拟核心。 ( i7-10875H , Comet Lake) ( i7-10875H , 彗星湖)

Each physical core is split into pairs, so Core 1 hosts virtual cores 0 & 1, core 2 hosts virtual cores 2 & 3. I've noticed that in task manager, the first item of each core pair seems to be the preferred core, judging by the higher usage.每个物理核心被分成对,所以核心 1 托管虚拟核心 0 和 1,核心 2 托管虚拟核心 2 和 3。我注意到在任务管理器中,每个核心对的第一项似乎是首选核心,从较高的使用率来看。 I do set some affinities manually for certain heavy programs but I always set these in groups of 4, either from 0-3, 4-7, 8-11, 12-15, and never mismatch different logical processors.我确实为某些繁重的程序手动设置了一些关联,但我总是将它们设置为 4 个一组,从 0-3、4-7、8-11、12-15 开始,并且从不与不同的逻辑处理器不匹配。

I'm wondering why this behaviour happens - do the even numbered cores equate to physical cores, which could be slightly faster?我想知道为什么会发生这种行为 - 偶数核心是否等同于物理核心,这可能会稍微快一些? If so, would I get slightly better clock speeds without virtualisation if I'm running programs that don't have a high thread count?如果是这样,如果我运行的程序没有高线程数,我会在没有虚拟化的情况下获得更好的时钟速度吗?

在此处输入图像描述 在此处输入图像描述

In general (for "scheduler theory"):一般来说(对于“调度器理论”):

  • if you care about performance, spread the tasks across physical cores where possible.如果您关心性能,请尽可能将任务分散到物理内核中。 This prevents a "2 tasks run slower because they're sharing a physical core, while a whole physical core is idle" situation.这可以防止“两个任务运行速度变慢,因为它们共享一个物理核心,而整个物理核心处于空闲状态”。

  • if you care about power consumption and not performance, make tasks use logical processors in the same physical core where possible.如果您关心功耗而不是性能,请尽可能让任务使用同一物理内核中的逻辑处理器。 This may allow you to put entire core/s into a very power efficient "do nothing" state.这可以让您将整个内核/s 放入一个非常节能的“无所事事”state。

  • if you care about security (and not performance or power consumption), don't let unrelated tasks use logical processors in the same physical core at all (because information, like what kinds of instructions are currently being used, can be "leaked" from one logical processor to another logical process in the same physical core).如果您关心安全性(而不是性能或功耗),则根本不要让不相关的任务使用同一物理内核中的逻辑处理器(因为信息,例如当前正在使用的指令类型,可能会从一个逻辑处理器到同一物理内核中的另一个逻辑进程)。 Note that it would be fine for related tasks to use logical processes in the same physical core (eg 2 threads that belong to the same process that do trust each other, but not threads that belong to different processes that don't trust each other).请注意,相关任务可以在同一物理内核中使用逻辑进程(例如,属于同一进程的 2 个线程相互信任,但不属于不同进程但不相互信任的线程) .

Of course a good OS would know the preference for each task (if each task cares about performance or power consumption or security), and would make intelligent decisions to handle a mixture of tasks with difference preferences.当然,一个好的操作系统会知道每个任务的偏好(如果每个任务都关心性能、功耗或安全性),并且会做出明智的决定来处理具有不同偏好的混合任务。 Sadly there are no good operating systems - most operating systems and APIs were designed in the 1990s or earlier (back when SMP was just starting and all CPUs were identical anyway) and lack the information about tasks that would be necessary to make intelligent decisions;遗憾的是,没有好的操作系统——大多数操作系统和 API 是在 1990 年代或更早的时候设计的(当时 SMP 刚刚启动,所有 CPU 都是相同的),并且缺乏做出明智决策所需的任务信息; so they assume performance is the only thing that matters for all tasks, leading to the "tasks spread across physical cores where possible, even when it's not ideal" behavior you're seeing.所以他们认为性能是对所有任务来说唯一重要的事情,导致你看到的“任务在可能的情况下分布在物理核心上,即使它并不理想”。

My guess is that's due to hyperthreading.我的猜测是由于超线程。

Hyperthreading doesn't double CPU capacity (according to Intel, it adds ~30% on average), so it makes sense to spread the work among physical cores first, and use hyperthreading as a last resort when the overall CPU demand starts exceeding 50%.超线程不会使 CPU 容量翻倍(据英特尔称,它平均增加了约 30%),因此首先在物理内核之间分配工作是有意义的,当整体 CPU 需求开始超过 50% 时使用超线程作为最后的手段.

Fun fact: a reported 50% overall CPU load on a hyperthreaded system is in fact a load of around ~70%, and the remaining 50% equate to the remaining ~30%.有趣的事实:报告的超线程系统上 50% 的总体 CPU 负载实际上是大约 70% 的负载,剩余的 50% 等于剩余的 30%。

If we query the OS to see how logical processors are assigned to cores 1 , we will see a situation like this:如果我们查询操作系统以查看逻辑处理器是如何分配给核心1的,我们将看到如下情况:

Core 0: mask 0x3
Core 1: mask 0xc
Core 2: mask 0x30
Core 3: mask 0xc0
. . .

That means logical processors 0 and 1 are on core 0, 2 and 3 on core 1, etc.这意味着逻辑处理器 0 和 1 位于核心 0 上,核心 1 上的核心 2 和 3 等等。

You can disable hyperthreading in the BIOS.您可以在 BIOS 中禁用超线程。 But since it adds performance, it's is a nice to have feature.但是因为它增加了性能,所以有一个很好的功能。 Just need to be careful not to pin work such that it is competing for the same core.只需要注意不要固定工作,以免它竞争相同的核心。


1 To check core assignment I use a small C program below. 1为了检查核心分配,我使用下面的一个小型 C 程序。 The information might also be available via WMIC.这些信息也可以通过 WMIC 获得。

#include <stdio.h>
#include <stdlib.h>
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x601
#include <Windows.h>

int main() {
    DWORD len = 65536;
    char *buf = (char*)malloc(len);
    if (!GetLogicalProcessorInformationEx(RelationProcessorCore, (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)buf, &len)) {
        return GetLastError();
    }
    union {
        PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info;
        PBYTE infob;
    };
    info = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)buf;
    for (size_t i = 0, n = 0; n < len; i++, n += info->Size, infob += info->Size) {
        switch (info->Relationship) {
        case RelationProcessorCore:
            printf("Core %zd:", i);
            for (int j = 0; j < info->Processor.GroupCount; j++)
                printf(" mask 0x%llx", info->Processor.GroupMask[j].Mask);
            printf("\n");
            break;
        }
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Ruby线程演示不使用两个内核? - Why does my Ruby thread demo not use both cores? 当主UI线程冻结时,为什么我的PC&#39;冻结&#39;? - Why does my PC 'freeze' when the main UI thread freezes? 为什么我的网站在PC上与Mac上的显示不同? - Why does my website display different on a PC than a Mac? 为什么我的非常简单的Chrome扩展程序可以在Mac上运行而不是PC? - Why does my very simple Chrome extension work on Mac but not PC? 为什么我的代码在我的电脑上不起作用,但在其他人身上? - Why doesn't my code dont work on my pc, but does on others? 为什么我的应用在另一台PC上启动时崩溃? - Why is my app crashing at startup on another PC? 为什么即使在非管理员访问权限下,我的Qt应用程序也会写入受保护的位置? - Why does my Qt application write to protected locations even in non-admin access privilege? 为什么当我使用&amp;&amp;时cmd运行我的第二个命令,即使第一个命令失败了? - Why does cmd run my second command when I use && even though the first one failed? 为什么即使我的文件已签名,Google 仍会将其检测为恶意文件? - Why does Google detect my file as malicious even if it has been signed? 为什么我的exe文件无法在其他PC上运行? [调试] - Why did my exe file not run in other pc? [Debug]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM