How does Linux kernel migrate the process among multiple cores?

你说的曾经没有我的故事 提交于 2021-02-19 08:13:05

问题


Context:

Process-1 is executing on core-0. Core-1 is idle.

Now, process-1 uses sched_setaffinity() to change its CPU affinity as core-1.

Question:

Which kernel function(s) migrate the process-1 to execute on core-1?


回答1:


Here is the call sequence starting from the sched_setaffinity system call entry point in the kernel:

  1. sys_sched_setaffinity.
  2. sched_setaffinity.
  3. __set_cpus_allowed_ptr.

In the last function, there are two cases as shown in the code at line 1101:

if (task_running(rq, p) || p->state == TASK_WAKING) {
    // ...
    stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
    // ...
} else if (task_on_rq_queued(p)) {
    rq = move_queued_task(rq, &rf, p, dest_cpu);
}

If the task to be migrated is currently running or waking up, then it is migrated by calling stop_one_cpu, which calls the following functions in order:

  1. migration_cpu_stop.
  2. __migrate_task.
  3. move_queued_task.

The last function, move_queued_task, is the one that actually moves the task from the current runqueue to the target runqueue. Note that this is the same function that is called from the other branch of __set_cpus_allowed_ptr. That branch handles the case where the task is in any of the other states.



来源:https://stackoverflow.com/questions/49707124/how-does-linux-kernel-migrate-the-process-among-multiple-cores

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