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

时间:2015-06-25 18:26:04

标签: c++ linux multithreading affinity

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 个答案:

答案 0 :(得分:2)

您已经描述了所需的解决方案:

  

也许我可以将进程的默认关联设置为Core 0和特定线程 - 将其固定为Core 1?

但是,也许问题是你不确定如何实现这一点。

Linux提供sched_setaffinity来设置当前进程的亲和力。

要让新创建的线程在特定核心上运行,最简单的方法是初始化pthread_attr_t,并使用pthread_attr_setaffinity_np设置所需的核心关联。

答案 1 :(得分:-1)

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