Cannot allocate exclusively a CPU for my process

给你一囗甜甜゛ 提交于 2019-12-12 03:05:53

问题


I have a simple mono-threaded application that does almost pure processing

  1. It uses two int buffers of the same size
  2. It reads one-by-one all the values of the first buffer
    • each value is a random index in the second buffer
  3. It reads the value at the index in the second buffer
  4. It sums all the values taken from the second buffer
  5. It does all the previous steps for bigger and bigger
  6. At the end, I print the number of voluntary and involuntary CPU context switches

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"?

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

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: 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

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

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?


回答1:


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"?

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).

What must I do in order to get Number of involuntary context switch = 0?

Aren't you missing --cpu_exclusive option in cset set command?

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.



来源:https://stackoverflow.com/questions/40692637/cannot-allocate-exclusively-a-cpu-for-my-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!