为什么单线程进程在多个处理器/内核上执行?

发布于 2024-12-21 06:58:41 字数 1188 浏览 6 评论 0 原文

假设我运行一个简单的单线程进程,如下所示:(

public class SirCountALot {
    public static void main(String[] args) {
        int count = 0;
        while (true) {
            count++;
        }
    }
}

这是 Java,因为这是我熟悉的,但我怀疑这并不重要)

我有一个 i7 处理器(4 核,或 8 计数超线程 ) ),并且我运行的是 Windows 7 64 位,因此我启动了 Sysinternals Process Explorer 来查看 CPU 使用情况,正如预期的那样,我看到它使用了所有可用 CPU 的大约 20%。

图表显示所有核心的 20% CPU 使用率

但是当我切换选项以显示每个 CPU 1 个图表时,我看到了这一点使用 4 个“核心”中的 1 个,CPU 使用率分布在所有核心上:

显示不稳定 CPU 使用率的图表每个核心的总使用率约为 20%

相反,我期望的是 1 个核心已达到最大,但这仅在我将进程的亲和力设置为单个核心时才会发生。

图表显示最近的 CPU 使用情况仅限于第一个核心

为什么工作负载会分配到各个单独的核心上?将工作负载分散到多个核心上是否会扰乱缓存或导致其他性能损失?

仅仅是为了防止某一核心过热吗?还是有什么更深层次的原因?

编辑:我知道操作系统负责调度,但我想知道为什么它“打扰”。当然,从天真的角度来看,将(大多数*)单线程进程坚持到 1 个核心是更简单的方法。更有效的方法是什么?

*我说主要是单线程,因为这里有多个线程,但只有其中两个在做任何事情:

显示 Process Explorer 进程属性中线程数的屏幕截图

Say I run a simple single-threaded process like the one below:

public class SirCountALot {
    public static void main(String[] args) {
        int count = 0;
        while (true) {
            count++;
        }
    }
}

(This is Java because that's what I'm familiar with, but I suspect it doesn't really matter)

I have an i7 processor (4 cores, or 8 counting hyperthreading), and I'm running Windows 7 64-bit so I fired up Sysinternals Process Explorer to look at the CPU usage, and as expected I see it is using around 20% of all available CPU.

Graph showing 20% CPU usage across all cores

But when I toggle the option to show 1 graph per CPU, I see that instead of 1 of the 4 "cores" being used, the CPU usage is spread all over the cores:

Graph showing erratic CPU usage on each core totaling around 20% usage

Instead what I would expect is 1 core maxed out, but this only happens when I set the affinity for the process to a single core.

Graph showing most of recent CPU usage to be confined to first core

Why is the workload split over the separate cores? Wouldn't splitting the workload over several cores mess with the caching or incur other performance penalties?

Is it for the simple reason of preventing overheating of one core? Or is there some deeper reason?

Edit: I'm aware that the operating system is responsible for the scheduling, but I want to know why it "bothers". Surely from a naive viewpoint, sticking a (mostly*) single-threaded process to 1 core is the simpler & more efficient way to go?

*I say mostly single-threaded because there's multiple theads here, but only 2 of them are doing anything:

Screenshot showing number of threads from Eclipse
Screenshot showing number of threads in Process Explorer process properties

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

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

发布评论

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

评论(2

我一向站在原地 2024-12-28 06:58:41

操作系统负责调度。可以自由地停止线程并在另一个 CPU 上重新启动它。即使机器没有做任何其他事情,它也会执行此操作。

该进程在 CPU 周围移动,因为操作系统不认为有任何理由每次都在同一 CPU 上继续运行线程。

出于这个原因,我编写了一个库,用于将线程锁定到 CPU,这样它就不会移动,也不会被其他线程中断。这会减少延迟并提高吞吐量,但会导致该线程的 CPU 疲劳。这适用于 Linux,也许您可​​以将其改编为 Windows。 https://github.com/peter-lawrey/Java-Thread -Affinity/wiki/入门

The OS is responsible for scheduling. It is free to stop a thread and start it again on another CPU. It will do this even if there is nothing else the machine is doing.

The process is moved around the CPUs because the OS doesn't assume there is any reason to continue running the thread on the same CPU each time.

For this reason I have written a library for lock threads to a CPU so it won't move around and won't be interrupted by other threads. This reduces latency and improve throughput but does tire up a CPU for that thread. This works for Linux, perhaps you can adapt it for Windows. https://github.com/peter-lawrey/Java-Thread-Affinity/wiki/Getting-started

梦里人 2024-12-28 06:58:41

我还预计这很可能是由 CPU 和操作系统故意完成的,以便尝试将热负载分散到 CPU 芯片上......

因此它将(唯一/单个)线程从一个核心轮换到另一个核心。

诚然,这可能是反对过于努力地解决这个问题的一个论据(特别是在实践中,您通常会通过简单地调整/改进应用程序本身来看到更好的改进)

I would also expect this could well be done on purpose by the CPU and OS so as to try and spread the thermal load on the CPU die...

So it would rotate the (unique/single) thread from core to core.

And that could admittedly be an argument against trying to fight this too hard (especially as, in practice, you often will see better improvements by simply tuning / improving the app itself anyway)

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