问题
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:
- sys_sched_setaffinity.
- sched_setaffinity.
- __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:
- migration_cpu_stop.
- __migrate_task.
- 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