How is each process pinned to a specific core by scheduler (Linux)
I am now studying about the scheduler of Linux. Regarding CPU core affinity, I would like to know the following:
1) How is each process(thread) pinned to a core?
there is a system call sched_setaffinity
to change the core affinity on which a process is executed. But internally, when a process(or a thread) is generated, how does the default Linux scheduler assign the process(thread) to a specific core? I modified sched_setaffinity
system call to dump information about the task being moved from one core to another.
printk(KERN_INFO "%d %d %ld %lu %sn", current->pid, current->tgid,
current->state, current->cpus_allowed,
current->comm);
It seems that there is no dump of the above information in /var/log/messages
. So the default scheduler pins each process in a different way, but I cannot figure out how.
2) Is it possible to get core ID by PID or other information?
This is what I want to implement inside of Linux kernel. In task_struct
, there is a member called cpus_allowed
. But this is a mask for setting affinity, not core ID. I want to retrieve a data identifying the core on which specified process is running.
Thanks,
Each CPU has its own runqueue, AFAIK we can find out current CPU of a process by looking for which runqueue it belongs to. Given task_struct *p
, we can get its runqueue by struct rq = task_rq(p)
, and struct rq has a field named cpu, I guess this should be the answer.
I have not tried this in practice, just read some code online, and am not quite sure it it will work or not. Wish it could help you.
您可以通过使用其task_struct来确定正在运行线程的CPU ID:
#include <linux/sched.h>
task_struct *p;
int cpu_id = task_cpu(p);
Field 39 in /proc/pid/stat
tells the current core/cpu of the process.
eg:
#cat /proc/6128/stat
6128 (perl) S 3390 6128 3390 34821 6128 4202496 2317 268 0 0 1621 59 0 0 16 0 1 0 6860821 10387456 1946 18446744073709551615 1 1 0 0 0 0 0 128 0 18446744073709551615 0 0 17 8 0 0 0
Process 6128 is running on core 8.
链接地址: http://www.djcxy.com/p/9590.html上一篇: 使用MVVM处理WPF中的对话框