Linux - make sure a core is exclusively saved for critical tasks

764 views Asked by At

I have a process that is launched on a Linux-based machine with exactly two cores.

Let's assume my process is the only process in the system (I will ignore other processes and even the system's ones).

My process is divided to two parts:

  1. Critical performance code
  2. Low priority code

Also let's assume my main process was launched on Core 0, and I want to exclusively reserve Core 1 for the critical performance code.

I'd like to divide the question to two:

  1. How can I make sure that every thread in my process (including 3rd party libraries which I have linked my code with that might call pthread_create and etc.) will always open new threads on Core 0 ?

  2. How can I write a test that can verify that Core 1 is doing absolutely nothing besides the performance critical path ?

I am familiar with APIs such as:

pthread_setaffinity_np

that can set a specific thread affinity but I want to know if there is a more low level way to make sure even threads that 3rd party libraries create (from inside the process) will also be pinned to Core 0.

Perhaps I can set the default affinity for the process to be Core 0 and for a specific thread - pin it to Core 1?

2

There are 2 answers

4
jxh On BEST ANSWER

You have already described the solution you want:

Perhaps I can set the default affinity for the process to be Core 0 and for a specific thread - pin it to Core 1?

But, perhaps the question is you are not sure how to achieve this.

Linux provides sched_setaffinity to set the affinity of the current process.

To get newly created threads to run on a specific core, the easiest way is to initialize a pthread_attr_t, and set the desired core affinity with pthread_attr_setaffinity_np.

0
Slava On

One of the solution is to install (if you do not have it already) and run Cpuset utility. Details can be found here