无法为我的进程专门分配CPU [英] Cannot allocate exclusively a CPU for my process
问题描述
我有一个简单的单线程应用程序,几乎可以进行纯处理
I have a simple mono-threaded application that does almost pure processing
- 它使用两个int缓冲区相同大小的
- 它逐个读取第一个缓冲区的所有值
- 每个值是第二个缓冲区中的随机索引
如果缓冲区的大小变为很大,我的PC开始慢下来:为什么?我有4个带有超线程的内核,所以剩下3个内核。只有一个人100%忙。是因为我的进程几乎将100%用于 RAM总线吗?
If the size of the buffers become quite big, my PC starts to slow down: why? I have 4 cores with hyper-threading so 3 cores are remaing. Only one is 100% busy. Is it because my process uses almost 100% for the "RAM-bus"?
然后,我创建了一个我要专用于我的进程的CPU集(我的CPU集包含相同内核的两个CPU线程)
Then, I created a CPU-set that I want to dedicate to my process (my CPU-set contains both CPU-threads of the same core)
$ cat /sys/devices/system/cpu/cpu3/topology/core_id
3
$ cat /sys/devices/system/cpu/cpu7/topology/core_id
3
$ cset set -c 3,7 -s my_cpuset
$ cset set -l
cset:
Name CPUs-X MEMs-X Tasks Subs Path
------------ ---------- - ------- - ----- ---- ----------
root 0-7 y 0 y 934 1 /
my_cpuset 3,7 n 0 n 0 0 /my_cpuset
似乎绝对没有任务一切都在我的CPU上运行。我可以重新启动我的流程,并在其运行时启动:
It seems that absolutely no task at all is running on my CPU-set. I can relaunch my process and while it is running, I launch:
$ taskset -c 7 ./TestCpuset # Here, I launch my process
...
$ ps -mo pid,tid,fname,user,psr -p 25244 # 25244 being the PID of my process
PID TID COMMAND USER PSR
25244 - TestCpus phil -
- 25244 - phil 7
PSR = 7:我的过程在预期的CPU线程上运行良好。我希望它是运行它的唯一一个,但是最后,我的过程显示为:
PSR = 7: my process is well running on the expected CPU-thread. I hope it is the only one running on it but at the end, my process displays:
Number of voluntary context switch: 2
Number of involuntary context switch: 1231
如果我有非自愿上下文切换,则意味着其他进程正在我的核心上运行:这怎么可能?为了使非自愿上下文切换数= 0,我该怎么办?
If I had involuntary context switches, it means that other processes are running on my core: How is it possible? What must I do in order to get Number of involuntary context switch = 0?
最后一个问题:当进程运行时,是否启动
Last question: When my process is running, if I launch
$ cset set -l
cset:
Name CPUs-X MEMs-X Tasks Subs Path
------------ ---------- - ------- - ----- ---- ----------
root 0-7 y 0 y 1031 1 /
my_cpuset 3,7 n 0 n 0 0 /my_cpuset
再次在我的CPU集中获得0个任务。但是我知道上面运行着一个进程:看来任务不是一个进程?
Once again I get 0 tasks on my CPU-set. But I know that there is a process running on it: it seems that a task is not a process?
推荐答案
如果缓冲区的大小变得很大,我的PC开始变慢:为什么?我有4个具有超线程的内核,所以剩下3个内核。只有一个人100%忙。是因为我的进程几乎将100%用于 RAM总线吗?
If the size of the buffers become quite big, my PC starts to slow down: why? I have 4 cores with hyper-threading so 3 cores are remaing. Only one is 100% busy. Is it because my process uses almost 100% for the "RAM-bus"?
您达到了单机的硬件性能极限,线程应用程序,即程序分配到的单个CPU上的CPU时间为100%。您的应用程序线程一次不会在一个以上的CPU上运行(引用)。
You reached the hardware performance limit of a single-threaded application, that is 100% CPU time on the single CPU your program is allocated to. Your application thread will not run on more than one CPU at a time (reference).
为了获得非自愿上下文切换次数我必须做什么= 0?
您是否不错过-cpu_exclusive
cset set
命令中的选项?
Aren't you missing --cpu_exclusive
option in cset set
command?
顺便说一句,如果您想缩短执行时间,我建议您创建一个多线程应用程序,然后让操作系统以及下面的硬件并行执行。将进程锁定到CPU集并阻止其执行上下文切换可能会降低操作系统性能,而不是便携式解决方案。
By the way, if you want to achieve lower execution time, i suggest you to make a multithreaded application and let operating system, and the hardware beneath parallelize execution instead. Locking a process to a CPU set and preventing it from doing context-switch might degrade the operating system performance and is not a portable solution.
这篇关于无法为我的进程专门分配CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!