[英]Is it possible to run code on a “reserved” cpu core?
My app runs a lot of tasks. 我的应用程序运行了很多任务。 Most of them are CPU intensive. 其中大多数都是CPU密集型的。
One task (which is actually a single thread running in a loop, listening to packets from the network), is very much a "realtime" task. 一个任务(实际上是在循环中运行的单个线程,从网络中侦听数据包),是一个非常“实时”的任务。 To make it more interesting, that thread is native code called using pinvoke
. 为了使它更有趣,该线程是使用pinvoke
调用的本机代码。
When a lot of traffic is showing, the tasks are working very hard, and all the cores are maxing out. 当显示大量流量时,任务正在非常努力,并且所有核心都在最大化。 When that happens, the "realtime" thread (which runs on a 100% cpu core), starts to drop packets, because it doesn't get enough cpu time. 当发生这种情况时,“实时”线程(在100%cpu核心上运行)开始丢弃数据包,因为它没有获得足够的CPU时间。
It is possible to somehow "reserve" one core for the "realtime" thread, and limit all the other threads (tasks) to other cores? 有可能以某种方式为“实时”线程“保留”一个核心,并将所有其他线程(任务)限制到其他核心?
EDIT - Answering many useful comments: 编辑 - 回答许多有用的评论:
You can specify an affinity mask for processes and threads. 您可以为进程和线程指定关联掩码。 This allows you to prevent the scheduler using specific processors. 这允许您阻止使用特定处理器的调度程序。 So you could set the thread affinity mask for one thread to a single processor, and set the thread affinity mask for all other threads to be all processors other than the reserved processor. 因此,您可以将一个线程的线程关联掩码设置为单个处理器,并将所有其他线程的线程关联掩码设置为除保留处理器之外的所有处理器。 Since .net insulates you from directly creating system threads it is actually not that easy to impose the necessary masks on all system threads. 由于.net使您无法直接创建系统线程,因此在所有系统线程上强加必要的掩码实际上并不容易。
Another way to persuade the scheduler to give your critical thread special treatment would be to boost its priority. 说服调度程序为您的关键线程提供特殊处理的另一种方法是提高其优先级。 That is something that you should only do with care and significant forethought. 这是你应该谨慎和重要的预见。 However, it's a much simpler way to make sure that your thread never has to wait for other threads in your process. 但是,这是一种更简单的方法,可以确保您的线程永远不必等待进程中的其他线程。
Perhaps it would be better for your app to use fewer threads than processors, but create a dedicated thread for the critical work. 也许你的应用程序使用比处理器更少的线程更好,但为关键工作创建一个专用线程。 That way you could avoid any affinity mask or priority skullduggery and let the system's thread scheduler do the work for you 这样你可以避免任何亲和力掩码或优先级skullduggery让系统的线程调度程序为你工作
Yes. 是。 That concept is called "processor affinity", and exists at both the process level and the thread level. 该概念称为“处理器关联”,并且存在于进程级别和线程级别。 It is a little awkward to invoke, but nothing scary: How Can I Set Processor Affinity in .NET? 调用它有点尴尬,但没有什么可怕的: 我如何在.NET中设置处理器亲和性?
Note that you would have to set the affinity for all the threads that you see. 请注意,您必须为所看到的所有线程设置亲缘关系。 However, you are not the only creator of threads - things like the ThreadPool
and GC also have some threads. 但是,你并不是线程的唯一创建者 - 像ThreadPool
和GC这样的东西也有一些线程。 I do not suggest it is a great idea to mess with those, but the ProcessThread
class won't actively stop you from doing this. 我并不认为ProcessThread
那些是一个好主意,但ProcessThread
类不会主动阻止你这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.