c - 如何通过 sched_setaffinity 在多个 CPU 上设置亲和性

标签 c linux setthreadaffinitymask

我想通过 sched_affinity 在多个 CPU 上设置亲和性,如下所示。

void
pin(pid_t t, int cpu)
{
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
  CPU_SET(cpu, &cpuset);
  sched_setaffinity(t, sizeof(cpu_set_t), &cpuset);
}

我的环境是32核,其中有4个CPU,单CPU有8核。
我希望线程 0 ~ 7 运行在同一个 cpu 上,线程 8 ~ 15 运行在同一个 cpu 上,依此类推。
我想知道如何在 CPU_SET 中设置变量 cpu。
这被设置为线程 id,如果核心数分配为天真,即 cpu0 有第 0 个核心,第 1 个核心,第 2 个核心,...,而 cpu1 有第 8 个核心,第 9 个核心,...。
一方面,cpu设置为循环规则,如果核心数分配为循环规则,即cpu0有第0个核心,第4个核心,第8个核心,...,cpu1有第 1 个核心和第 5 个核心,...。

我应该设置可变cpu,朴素规则还是轮询规则?

最佳答案

在 Linux(和其他操作系统)下,程序员可以设置 CPU 亲和性,即内核可以将此进程调度到允许的 CPU。在 fork() 之后,进程继承父 CPU 亲和性。 如果出于某种原因想要限制 CPU,这会非常方便。

例如一个可能会限制

  • 某些用户的进程只使用一个 CPU,而其他用户则使用其余的 CPU(参见 man 7 cpuset)。
  • 一个与 CPU 更“接近”某物的进程,例如限制与直接连接到网卡(NIC 或 HCA)的套接字上的核心进行通信的进程。

一般来说,将进程/线程限制在某些内核或套接字上可能是有益的,以免它们被操作系统调度——最大化 L1/L2 缓存的好处(当固定到内核时)或 L3/LLC 缓存(固定到套接字时)。

关于您关于“线程分布”的问题: 处理器开发引入了对称多线程 (SMT) 或超线程(英特尔称之为),每个物理核心引入了 2 个逻辑内核(例如英特尔至强)甚至 4 个逻辑内核(例如英特尔骑士登陆、IBM Power)。 这些逻辑核心在上面的 cpuset 中也被表示为“CPU”。 此外,一些处理器强加了 NUMA 域,其中从一个内核到它“自己的”内存的内存访问速度很快,而访问另一个 NUMA 域中的另一个内核内存则较慢......

所以,正如上面的一些评论所暗示的那样:这取决于! 您的线程是否相互通信(通过共享内存),那么它们应该保持在同一个缓存中。 您的线程是否使用相同的功能单元(例如 FPU),然后在同一物理内核(具有 2 个逻辑内核,即超线程)上调度两个线程可能会对性能产生不利影响。

要尝试,请找到以下代码:

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <pthread.h>

// The following is Linux-specific
#include <syscall.h>            // For syscall to gettid()
#include <sched.h>      // sched_[gs]etaffinity require _GNU_SOURCE

#define ERROR(t, e) do { \
    const int __error = (e); \
    fprintf (stderr, "ERROR: %s error:%d [%s] errno:%d [%s]\n", \
             (t), __error, strerror(__error), errno, strerror(errno)); \
    exit(__error); \
  } while(0)

#ifndef MAX
#define MAX(a,b)  ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b)  ((a) < (b) ? (a) : (b))
#endif



/* Local function definitions */
void print_schedaffinity(const char * text, const cpu_set_t cpuset, const int max_cpus);
void * thread_func(void * arg);

/* Local type definitions */
struct thread_data {
  pthread_t thread;
  int max_cpu;
  int thread_num;
  void * thread_work;
};

/* The highest value for CPU to be specified in cpuset in a call to
 * sched_setaffinity -- otherwise, we get returned -1 and errno==EINVAL
 */
static int max_cpu_available = 0;


/* Local function declarations */
void print_schedaffinity(const char * text, const cpu_set_t cpuset, const int max_cpus) {
  const int max = MIN(8*sizeof(cpu_set_t), max_cpus);
  int i;

  printf("PRINT CPU AFFINITY %s:\n", text);
  printf("cpus:\t");
  for (i = 0; i < max; i++) {
    printf (" %3d", i);
    if (i % 8 == 7)
      printf(" | ");
  }

  printf("\nmask:\t");
  for (i = 0; i < max; i++) {
    if (CPU_ISSET(i, &cpuset))
      printf ("   X");
    else
      printf ("    ");

    if (i % 8 == 7)
      printf(" | ");
  }
  printf("\n");
}


void * thread_func(void * arg) {
  struct thread_data * thread_data = (struct thread_data *)arg;
  const size_t sizeof_cpuset = sizeof(cpu_set_t);
  char print_buffer[64];
  cpu_set_t cpuset;
  long tid;
  int rc;

  CPU_ZERO(&cpuset);
  CPU_SET(thread_data->thread_num % max_cpu_available, &cpuset);

  /* We set the affinity of the CALLING thread, aka 0 */
  tid = syscall(SYS_gettid);
  printf("PID:%ld tid:%ld thread_num:%d\n",
         getpid(), tid, thread_data->thread_num);
  rc = sched_setaffinity(0, sizeof_cpuset, &cpuset);
  if (0 != rc)
    ERROR("sched_setaffinity", rc);


  /* Dooo SCHTUF now */

  /* Somewhat sort the output... */
  sleep (thread_data->thread_num);

  snprintf (print_buffer, sizeof(print_buffer),
            "in thread %d after sched_setaffinity", thread_data->thread_num);

  print_schedaffinity(print_buffer, cpuset, 8);

  return NULL;
}


int main (int argc, char * argv[])
{
  const int NUM = 8;
  const pid_t pid = getpid();
  const size_t size_cpu_set = sizeof(cpu_set_t);
  cpu_set_t cpuset;
  int rc;
  int i;

  /* Get, and print the original CPU affinity setting (scheduling is not limited, i.e. all cores may run this PID) */
  CPU_ZERO (&cpuset);
  rc = sched_getaffinity(pid, size_cpu_set, &cpuset);
  if (0 != rc)
    ERROR("sched_getaffinity", rc);
  print_schedaffinity("in main", cpuset, 8);

  /* Search for the last / highest cpu being set -- claim, that this is the max cpu to be set, cough */
  for (i = 0; i < 8 * size_cpu_set; i++) {
    if (!CPU_ISSET(i, &cpuset)) {
      max_cpu_available = i;
      break;
    }
  }


  /* Limit the process to the first core, only */
  CPU_ZERO (&cpuset);
  CPU_SET (0, &cpuset);
  rc = sched_setaffinity (pid, size_cpu_set, &cpuset);
  if (0 != rc)
    ERROR("sched_setaffinity", rc);
  print_schedaffinity("in main after sched_setaffinity", cpuset, 8);


  /* Let's start NUM threads and have them limit their scheduling */
  sleep(1);
  struct thread_data * thread_data = (struct thread_data*)malloc(sizeof(struct thread_data) * NUM);
  for (i = 0; i < NUM; i++) {
    thread_data[i].thread_num = i;
    pthread_create (&thread_data[i].thread, NULL, thread_func, &thread_data[i]);
  }

  /* And wait for them to finish... */
  for (i = 0; i < NUM; i++) {
    pthread_join (thread_data[i].thread, NULL);
  }
  return 0;
}

编辑:应该澄清 Apple,因为 OSX 10.5 (Leopard) 提供亲和性,如 https://developer.apple.com/library/mac/releasenotes/Performance/RN-AffinityAPI/

关于c - 如何通过 sched_setaffinity 在多个 CPU 上设置亲和性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523936/

相关文章:

c++ - 发布exe应用程序

linux - pthread_setaffinity_np : Invalid argument?

c - 如何从c中的文件中读取多种数据类型

linux - SSH 和 maxstartups 混淆

linux - debian apache http 服务器抛出错误的组名 nobody 错误

在 Linux 上使用 C 检查目录是否为空

kernel-module - 在 linux 内核模块中设置线程亲和性

c - 添加节点时如何重置指向头节点的指针?

c - 如何在 vector 范围内实现排序?