Skip to content

Add syscall/syscall_getcpu #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/syscall/syscall_getcpu/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app
57 changes: 57 additions & 0 deletions examples/syscall/syscall_getcpu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# これは何?

syscall(2)を用いてgetcpu(2)を呼び出すサンプルです。

getcpu(2)システムコールはLinuxに存在しますが、glibc(GNU C ライブラリ)には対応するラッパー関数が提供されていません。
そのため、他の多くのシステムコールのように単純に関数として呼び出すことができません。
なので、syscall(2)を使って、glibcにラッパー関数が存在しないシステムコールを呼び出す必要があります。

```sh
$ task
task: [default] rm -f ./app
task: [default] go build -o app .
task: [default] ./app
CPU: 10, NUMA: 0
```

## C言語での実装

C言語では以下のようになります。

```c
#include <stdio.h> // printf, perror
#include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
#include <unistd.h> // syscall
#include <sys/syscall.h> // SYS_getcpu
#include <errno.h> // errno

/**
* @brief getcpu(2)をsyscall(2)を使って呼び出すサンプルプログラム
*
* このプログラムはsyscall(2)を使用してgetcpu(2)システムコールを直接呼び出し、
* 現在のプロセスが実行されているCPU IDとNUMAノードを取得します。
*
* getcpu(2)のプロトタイプ:
* int getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache);
*
* @return 正常終了時は0、エラー時は-1
*/
int main(void)
{
unsigned int cpu = 0;
unsigned int node = 0;

// getcpuシステムコールを呼び出す
// 第3引数は通常NULLで良い(getcpu_cacheは非推奨)
int result = syscall(SYS_getcpu, &cpu, &node, NULL);
if (result == -1) {
perror("syscall(SYS_getcpu) failed");
return EXIT_FAILURE;
}

printf("現在のCPU ID: %u\n", cpu);
printf("現在のNUMAノード: %u\n", node);

return EXIT_SUCCESS;
}
```
10 changes: 10 additions & 0 deletions examples/syscall/syscall_getcpu/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://taskfile.dev

version: '3'

tasks:
default:
cmds:
- rm -f ./app
- go build -o app .
- ./app
47 changes: 47 additions & 0 deletions examples/syscall/syscall_getcpu/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"log"
"unsafe"

"golang.org/x/sys/unix"
)

func main() {
log.SetFlags(0)

if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
var (
// syscall(2)のための引数とポインタ情報
trap = uintptr(unix.SYS_GETCPU)
cpu uint32
node uint32
ptrCpu = uintptr(unsafe.Pointer(&cpu))
ptrNode = uintptr(unsafe.Pointer(&node))

// syscall(2)の結果
r1 uintptr
errno unix.Errno
err error
)
// getcpu(2)はブロッキングしない単純なシステムコールなのでRawSyscall()を使っても問題無い
r1, _, errno = unix.RawSyscall(trap, ptrCpu, ptrNode, uintptr(0))
if errno != unix.Errno(0) {
err = errno
return err
}

if int(r1) < 0 {
return fmt.Errorf("getcpu syscall returned %d", r1)
}

log.Printf("CPU: %d, NUMA: %d", cpu, node)

return nil
}
Loading