1

The affinity of user processes could be set by cpuset(7).

Could the affinity of thread created by kernel be set through the cpuset(7)?

I found that the affinity of some kthreads could be set by cpuset indeed([rcu_sched],[rcu_bh]),some kthreads couldn't([nvme-delete-wq],[kthreadd],i got the error: " echo: write error: Invalid argument").

If you have a better solution, please let me know.

sunshilong369
  • 646
  • 1
  • 5
  • 17

1 Answers1

1

cpuset(7) is a manual page that describes a Linux userspace API in general. As the page states, you can use the sched_setaffinity(2) syscall to restrict a task to a specific set of CPUs.

The fact that sched_setaffinity(2) is a syscall should already make you notice that the functionality is intended for userspace usage. If you are writing kernel code, kernel threads have different internal APIs for this purpose (see kthread.h):

  • kthread_bind(), which can be used to bind the kthread to a single CPU specified by its numerical ID.
  • kthread_bind_mask(), which can be used to bind the kthread to one or more CPUs defined by a struct cpumask. You can initialize the right struct cpumask through cpumask_set_cpu(). This API is similar to the sched_setaffinity(2) syscall, but for kthreads.
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Glad to see you again.Thank you for your help.I found that **the affinity of some kthreads could be set by `cpuset` indeed(`[rcu_sched],[rcu_bh]`)**,some kthreads couldn't(`[nvme-delete-wq],[kthreadd]`,i got the error: " echo: write error: Invalid argument"). – sunshilong369 May 22 '20 at 02:09
  • For some reason I thought you were writing kernel code and wanted to set the priority from within the code that creates the threads. Well, I am confused, why would you ever want to modify a kthread's affinity? I would really warn you against it, it could crash your system. – Marco Bonelli May 22 '20 at 02:15
  • I mostly develop user space applications.I know a little about kernel.What i would like to do is that i want to have one core reserved only and only for my process. I want to migrate the threads include kthreads to other cpu as many as i could. – sunshilong369 May 22 '20 at 02:24
  • @sunshilong369 I wouldn't worry about kthreads at all. Move all userspace processes on other CPUs and keep your process fixed on that CPU. kernel threads are sometimes essential (in that e.g. they might be created to service syscalls or other things) and will anyway re-appear, and in any case normally have nearly zero contribution to the CPU load. In fact, if you look at the cpu time with tools like `htop` for kernel threads you will see that it is really low (e.g. on my pc running for 1 day the highest I see is `rcu_sched` with 28 minutes of cpu time). – Marco Bonelli May 22 '20 at 02:36
  • I see.Thank you for your help. – sunshilong369 May 22 '20 at 02:42