0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DPDKアプリ用アフィニティ設定API

Posted at

PMD処理を行わせるための各種APIがDPDKには用意されている。
アフィニティ設定処理を隠蔽するための、便利APIもたくさん用意されていて、格好良いのだが、
「実際、コアをどう割り当てているの?」という疑問が残る。極端な例を上げると、

  • 1つのコアで2つのPMDを1:2の割合で動かしたい(普通やらないけど)

などの変態動作をサポートできてない。

ということで、アフィニティ設定は自力でやることにした。以下がそのコード。すっきりした!

#define _GNU_SOURCE
void *affinity_thread(void *core){
  //
  // スレッドのアフィニティを設定して
  cpu_set_t mask;  // 実行可能なCPUをビットマスクで指定する
  CPU_ZERO(&mask);  // まずマスクを全部オフにして
  CPU_SET(*(int*)core, &mask);  // 実行するコア番号を指定
  if(sched_setaffinity(0, sizeof(mask), &mask) == (-1)){  // アフィニティを設定
    perror("setaffinity:");
    exit(1);
  }
  // PMD処理を行う
  int64_t i;
  for(;;){
    for(i = 1; i < 0x100000000; i++) ;
    printf("zzz\n");
  }
}

// 呼び出し側。PMDに#4のコアを割り当てた例
  int core4 = 4;
  ret = pthread_create(&thread1, NULL, affinity_thread, &core4);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?