简体   繁体   English

是否可以在“保留”的cpu核心上运行代码?

[英]Is it possible to run code on a “reserved” cpu core?

Simplified background: 简化背景:

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调用的本机代码。

The problem: 问题:

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时间。

The question: 问题:

It is possible to somehow "reserve" one core for the "realtime" thread, and limit all the other threads (tasks) to other cores? 有可能以某种方式为“实时”线程“保留”一个核心,并将所有其他线程(任务)限制到其他核心?

  • Of course, there are other processes running, consuming CPU time as well, but let's assume they consume little and constant resources. 当然,还有其他进程在运行,也消耗了CPU时间,但我们假设它们消耗的资源很少且不变。
  • This is a real problem that can be solved by "throw more cpu on it"... Not an option... 这是一个真正的问题,可以通过“抛出更多的CPU”来解决......不是一个选择......

EDIT - Answering many useful comments: 编辑 - 回答许多有用的评论:

  • We use WinPcap to capture all packets, which can be many (many). 我们使用WinPcap捕获所有数据包,这可能是很多(很多)。
  • The "realtime" thread is not really "realtime" (I think "realtime" is for processes - In .net ThreadPriority uses "Normal", "AboveNormal", etc..) “实时”线程并非真正“实时”(我认为“实时”是针对进程的 - 在.net ThreadPriority中使用“Normal”,“AboveNormal”等。)
  • The "realtime" thread calls WinPcap serially, packet after packet. “实时”线程以串行方式串行调用WinPcap。 We suspect that since it is starved enough, it doesn't keep up, and WinPcap's buffer is overflowed. 我们怀疑,由于它已经足够饿,它无法跟上,WinPcap的缓冲区溢出。

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.

相关问题 openmp是否可以在同一cpu(core)上运行不同的线程 - Is it possible for openmp to run different threads on a same cpu(core) 这个 OpenCV 代码是在 GPU 还是 CPU 上运行? - Does this OpenCV code run on the GPU or the CPU? CUDA:是否可以将一个内核视为“主”来执行 memory malloc,并运行其他“逻辑代码”? - CUDA: Does it possible to treate one core as “master” to do memory malloc, and run other “logic code”? 当我运行几个与CPU核心/线程数相匹配的线程时,每个线程是否会在一个单独的核心/线程上运行? - When I run several threads that match the number of CPU core/threads, will each thread run on a separate core/thread? 是否可以计算使用代码块的CPU和RAM数量 - Is it possible to calculate how much CPU and RAM uses block of code 是否可以在代码的一部分上运行valgrind? - Is it possible to run valgrind on a portion of the code? 是否有可能同时运行相同的未更改的代码,有时不会? - is it possible for the same unchanged code to sometimes run and sometimes not? 是否可以直接在浏览器中运行C代码? - Is it possible to run C code directly in the browser? 在单CPU指令中可以在0和1之间翻转位/整数/布尔值的任何可能代码 - Any possible code that can flip a bit/integer/bool between 0 and 1 in single CPU instruction 将线程分配给特定的CPU核心 - Assigning a thread to specific CPU core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM