假设我运行一个简单的单线程进程,如下所示:(
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%。

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

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

为什么工作负载会分配到各个单独的核心上?将工作负载分散到多个核心上是否会扰乱缓存或导致其他性能损失?
仅仅是为了防止某一核心过热吗?还是有什么更深层次的原因?
编辑:我知道操作系统负责调度,但我想知道为什么它“打扰”。当然,从天真的角度来看,将(大多数*)单线程进程坚持到 1 个核心是更简单的方法。更有效的方法是什么?
*我说主要是单线程,因为这里有多个线程,但只有其中两个在做任何事情:

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.

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:

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.

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:


发布评论
评论(2)
操作系统负责调度。可以自由地停止线程并在另一个 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
我还预计这很可能是由 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)