Linux 整体一核专用于单进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13583146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whole one core dedicated to single process
提问by akp
Is there any way in Linux to assign one CPU core to a particular given process and there should not be any other processes or interrupt handlers to be scheduled on this core?
Linux 中是否有任何方法可以将一个 CPU 内核分配给特定的给定进程,并且不应在该内核上调度任何其他进程或中断处理程序?
I have read about process affinity in Linux Binding Processes to CPUs using the taskset utilitybut that's not solving my problem because it just try to affine the given process to that core but it is possible that other processes may be scheduled on this coreand this is what I want to avoid.
我已经阅读了 Linux Binding Processes to CPUs using the taskset 实用程序中的进程关联,但这并没有解决我的问题,因为它只是尝试将给定的进程仿射到该核心,但有可能在该核心上安排其他进程,这是我想避免什么。
Should we change the kernel code for scheduling?
我们应该更改内核代码以进行调度吗?
回答by Sergei Nikulov
As per documentation
根据文档
The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs.
Linux 调度程序将遵守给定的 CPU 关联性,并且该进程不会在任何其他 CPU 上运行。
There is no mention that specific processor will be given to process exclusively.
没有提到将给出特定的处理器来专门处理。
回答by louxiu
There is Redhat articletalking about it. It modifies the boot parameter isolcpus.
有Redhat 文章在谈论它。它修改引导参数isolcpus。
And an old articlewritten by Robert Love. And there is solution in that article.
还有罗伯特·洛夫 (Robert Love) 写的一篇旧文章。在那篇文章中有解决方案。
All of a process' children receive the same CPU affinity mask as their parent.
Then, all we need to do is have init bind itself to one processor. All other processes, by nature of init being the root of the process tree and thus the superparent of all processes, are then likewise bound to the one processor.
进程的所有子进程都收到与其父进程相同的 CPU 关联掩码。
然后,我们需要做的就是让 init 将自己绑定到一个处理器。所有其他进程,本质上 init 是进程树的根,因此是所有进程的父进程,然后同样绑定到一个处理器。
回答by gby
Yes there is. In fact, there are two separate ways to do it :-)
就在这里。事实上,有两种不同的方法可以做到:-)
Right now, the best way to accomplish what you want is to do the following:
现在,完成您想要的最佳方法是执行以下操作:
Add the parameter isolcpus=[cpu_number] to the Linux kernel command line from the boot loader during boot. This will instruct the Linux scheduler not to run any regular tasks on that CPU unless specifically requested using cpu affinity.
Use IRQ affinity to set other CPUs to handle all interrupts so that your isolated CPU will not receive any interrupts.
Use CPU affinity to fix your specific task to the isolated CPU.
在引导期间从引导加载程序向 Linux 内核命令行添加参数 isolcpus=[cpu_number]。这将指示 Linux 调度程序不要在该 CPU 上运行任何常规任务,除非使用 cpu 关联性特别请求。
使用 IRQ 关联来设置其他 CPU 来处理所有中断,以便您的隔离 CPU 不会收到任何中断。
使用 CPU 亲和性将您的特定任务固定到隔离的 CPU。
This will give you the best that Linux can provide with regard to CPU isolation without out-of-tree and in-development patches.
这将为您提供 Linux 在 CPU 隔离方面所能提供的最佳性能,而无需外挂和开发中的补丁。
Your task will still get interrupted from time to time by Linux code, including other tasks - such as the timer tick interrupt and the scheduler code, IPIs from other CPUs and stuff like work queue kernel threads, although the interruption should be quite minimal.
您的任务仍然会不时被 Linux 代码中断,包括其他任务 - 例如计时器滴答中断和调度程序代码、来自其他 CPU 的 IPI 以及工作队列内核线程之类的东西,尽管中断应该非常小。
For an (almost) complete list of interruption sources, check out my page at https://github.com/gby/linux/wiki
有关(几乎)完整的中断源列表,请查看我的页面https://github.com/gby/linux/wiki
The alternative method is to use cpusets which is way more elegant and dynamic but suffers from some weaknesses at this point in time (no migration of timers for example) which makes me recommend the old, crude but effective isolcpus parameter.
另一种方法是使用 cpuset ,它更优雅和动态,但在这个时间点有一些弱点(例如没有计时器的迁移),这让我推荐旧的、粗糙但有效的 isolcpus 参数。
Note that work is currently being done by the Linux community to address all these issues and more to give even better isolation.
请注意,Linux 社区目前正在开展工作以解决所有这些问题,以及更多的工作,以提供更好的隔离。
回答by bofh.at
Even if you follow the steps in gby's answer, kernel tasks are executed on the isolated CPU core. Work is underway in the linux RT_PREEMPT real time project to improve this. So if you are not using a bleeding edge real time kernel from RP_PREEMPT, it might not be possible to completelyisolate a CPU core.
即使您按照 gby 的回答中的步骤进行操作,内核任务也会在隔离的 CPU 内核上执行。linux RT_PREEMPT 实时项目正在进行工作以改进这一点。因此,如果您不使用来自 RP_PREEMPT 的前沿实时内核,则可能无法完全隔离 CPU 内核。
回答by Vasu
Dedicate a Whole CPU Core to a Particular Program
将整个 CPU 内核专用于特定程序
While taskset allows a particular program to be assigned to certain CPUs, that does not mean that no other programs or processes will be scheduled on those CPUs. If you want to prevent this and dedicate a whole CPU core to a particular program, you can use "isolcpus" kernel parameter, which allows you to reserve the CPU core during boot.
尽管 taskset 允许将特定程序分配给某些 CPU,但这并不意味着不会在这些 CPU 上调度其他程序或进程。如果您想防止这种情况并将整个 CPU 内核专用于特定程序,您可以使用“isolcpus”内核参数,它允许您在启动期间保留 CPU 内核。
Add the kernel parameter "isolcpus=" to the boot loader during boot or GRUB configuration file. Then the Linux scheduler will not schedule any regular process on the reserved CPU core(s), unless specifically requested with taskset. For example, to reserve CPU cores 0 and 1, add "isolcpus=0,1" kernel parameter. Upon boot, then use taskset to safely assign the reserved CPU cores to your program.
在引导期间将内核参数“isolcpus=”添加到引导加载程序或 GRUB 配置文件。然后 Linux 调度程序将不会在保留的 CPU 内核上调度任何常规进程,除非任务集特别请求。例如,要保留 CPU 内核 0 和 1,请添加“isolcpus=0,1”内核参数。启动时,然后使用 taskset 将保留的 CPU 内核安全地分配给您的程序。
Source(s)
来源