Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the affinity of a process from a Linux kernel mode?

How can the CPU affinity of a process be set in kernel module? In user mode there is a syscall sched_setaffinity, but I am looking for the kernel mode equivalent.

In the Linux kernel code, there is also a function called sched_setaffinity. It's called from the sys_sched_setaffinity function which is called by system_call. From what it seems, this is the function that I want to use. The fact that it has the same name as the system call makes me a bit uneasy, though.

But as we all know, the best thing to do is to just try it. So I did, and my module compiled. However, when I try to load the module, it complains that the name sched_setaffinity is undefined.

like image 574
Mike Mu Avatar asked Jul 20 '09 00:07

Mike Mu


People also ask

How do I set thread affinity in Linux?

To set the CPU affinity of a process, you can use taskset program and sched_setaffinity system call. To set the CPU affinity of a thread, you can use pthread_setaffinity_np and pthread_attr_setaffinity_np.

What is process affinity in Linux?

As per Wikipedia, Processor affinity, or CPU pinning or “cache affinity”, enables the binding and unbinding of a process or a thread to a central processing unit (CPU) or a range of CPUs, so that the process or thread will execute only on the designated CPU or CPUs rather than any CPU.

How do I change the affinity of a CPU?

Method 1: System-wide System-wide we can set the boot. ini setting to use /numproc=4 so that the machine only uses 4 cores irrespective of the number of cores available (provided that there are 4 or more cores available) Method 2: Task Manager First, select a process from Task Manager. Then, click “Set affinity”.


1 Answers

sched_setaffinity is not exported to modules.

If you modify /usr/src/linux/kernel/sched.c, you can cause sched_setaffinity to be exported to modules.

 long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
 {
...
 }
+EXPORT_SYMBOL_GPL(sched_setaffinity);
like image 119
ephemient Avatar answered Oct 01 '22 23:10

ephemient