为什么单个线程进程在多个处理器/内核上执行? [英] Why does a single threaded process execute on several processors/cores?

查看:225
本文介绍了为什么单个线程进程在多个处理器/内核上执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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核心是更简单和更简单的。更有效的方法?



*我说大多数是单线程的,因为这里有多个theads,但只有2个在做任何事情:




解决方案

操作系统负责安排。它可以自由地停止一个线程并在另一个CPU上再次启动它。即使机器没有其他功能,它也会这样做。



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



因此我已经为CPU锁定了一个库线程,因此它不会移动而不会移动被其他线程打断。这样可以减少延迟并提高吞吐量,但会使该线程的CPU疲劳。这适用于Linux,也许你可以适应Windows。 https://github.com/peter-lawrey/Java-Thread -Affinity / wiki / Getting-started


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:

解决方案

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

这篇关于为什么单个线程进程在多个处理器/内核上执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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