如何确定线程运行在哪个CPU? [英] How to determine which CPU a thread runs on?

查看:580
本文介绍了如何确定线程运行在哪个CPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以确定给定线程在哪个CPU上运行吗?

Is there a way to determine on which CPU a given thread runs on? Preferably in C#, but C++ would do.

.NET进程和ProcessThread类似乎不提供这些信息。

The .NET Process and ProcessThread classes don't seem to provide this information.

ETA澄清:

我们正在开发一个服务器应用程序,处理http组播流并产生多个视频编码器。它在具有12个物理内核的系统上运行,从而产生24个逻辑CPU(超线程)。通过TaskManager和ProcessExplorer,我们验证了我们产生的进程均匀分布在逻辑CPU上。然而,我们看到很多(内核?)活动只在一个CPU干扰通过吃掉异常的CPU时间。我们试图确定在这个特定的CPU上运行哪个进程/线程。 TaskManager和ProcessExplorer似乎都不提供这些信息。如果他们这样做,请解释如何获得这些信息。

We are developing a server application that processes http multicast streams and spawns multiple video encoders. This runs on a system with 12 physical cores, resulting in 24 logical CPUs (hyperthreading). Via TaskManager and ProcessExplorer we have verified that our spawned processes spread evenly over the logical CPUs. However, we are seeing a lot of (kernel?) activity on just one CPU that interferes by eating up unusual amounts of CPU time. We are trying to identify which process(es)/thread(s) are running on this particular CPU. Neither TaskManager nor ProcessExplorer seem to provide that information. If they do, please explain how such information can be obtained.

否则,我们正在考虑写我们自己的工具来获取这些信息。这是我们需要的帮助。

Otherwise, we are contemplating writing our own tool to get this information. And that is what we need help with.

我们知道如何改变线程的亲和力(我们知道,不能保证线程将保持与任何CPU ,虽然在这种特定情况下,吃掉CPU的线程仍然仅与一个CPU相关联,但是为了这样做,我们需要首先确定需要重新定位的WHICH进程/线程。这是此问题的唯一目标。

We know how to change a threads affinity (and we know that there is no guarantee that a thread will remain associated with any CPU, although in this particular case the thread(s) eating up CPU remain associated with only one CPU), but in order to do so, we need to first determine WHICH process/thread needs to be relocated. That is the sole objective of this question.

我希望这有助于澄清问题。

I hope this helps clarifying the issue.

推荐答案

这不可能以连续可靠的方式做。 OS任务调度器优化线程并拆分可用CPU内核之间的负载。在一般情况下,线程可以在任何CPU上执行。

This is not possible to do in a continuous reliable way. OS task scheduler optimises threads and splits load between available CPU cores. In general case a thread can be executed on any CPU. Moreover with context switches it can also change it's CPU.

如果你需要指定特定的线程或进程,你只能分配它的亲和力,所以你可以有合理的希望进程/线程将在特定的逻辑CPU上执行。

If you need to pin-point specific a thread or a process you can only assign its affinity, so you can have reasonable hope that the process/thread will be executed on a particular logical CPU.

这篇关于如何确定线程运行在哪个CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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