getcpu

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

getcpu - 呼び出し元スレッドが動作している CPU と NUMA ノードを判定する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sched.h>

int getcpu(unsigned int *_Nullable cpu, unsigned int *_Nullable node);
 

説明

getcpu() システムコールは、呼び出し元のスレッドやプロセスが 現在動作しているプロセッサやノードの情報を特定し、 それぞれ引数 cpu と node が指す整数に書き込む。 プロセッサ情報は CPU を識別するための一意な小さな整数である。 ノード情報は NUMAノードを識別するための一意な小さな整数である。 cpu か node のいずれかが NULL であれば、 その引数に対応する情報の書き込みは行われない。

cpu に格納された情報が最新だと保証できるのは、システムコールが呼ばれ た時点だけである。sched_setaffinity(2) を使って CPU affinity が固定 されていない限り、カーネルはいつでも CPU を変更してもよい (スケジューラ はキャッシュが有効に働くように CPU の移動を最小限にしようとするので、 通常は CPU が変更されることはないが、起きる可能性はある)。 cpu や node で返された情報が呼び出しが返った時点ですでに 最新の状況と異なる可能性があり、呼び出し元はこの可能性を考慮して おかなければならない。  

返り値

成功すると、 0 を返す。エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。  

エラー

EFAULT
引数が呼び出したプロセスのアドレス空間外を指している。
 

標準規格

Linux.  

歴史

Linux 2.6.19 (x86-64 and i386), glibc 2.29.  

C library/kernel differences

The kernel system call has a third argument:

int getcpu(unsigned int *cpu, unsigned int *node,
           struct getcpu_cache *tcache);

The tcache argument is unused since Linux 2.6.24, and (when invoking the system call directly) should be specified as NULL, unless portability to Linux 2.6.23 or earlier is required.

In Linux 2.6.23 and earlier, if the tcache argument was non-NULL, then it specified a pointer to a caller-allocated buffer in thread-local storage that was used to provide a caching mechanism for getcpu(). Use of the cache could speed getcpu() calls, at the cost that there was a very small chance that the returned information would be out of date. The caching mechanism was considered to cause problems when migrating threads between CPUs, and so the argument is now ignored.  

注意

Linux makes a best effort to make this call as fast as possible. (On some architectures, this is done via an implementation in the vdso(7).) The intention of getcpu() is to allow programs to make optimizations with per-CPU data or for NUMA optimization.  

関連項目

mbind(2), sched_setaffinity(2), set_mempolicy(2), sched_getcpu(3), cpuset(7), vdso(7)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
標準規格
歴史
C library/kernel differences
注意
関連項目

This document was created by man2html, using the manual pages.