unix: add SetMemPolicy and its mode/flag values

This reuses CPUSet type as it fits perfectly.

No GetMemPolicy support yet.

Change-Id: I549361bc1ed95ee73fd38d00277ec0f9f348a9ba
Reviewed-on: https://go-review.googlesource.com/c/sys/+/706917
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Antti Kervinen <antti.kervinen@gmail.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
Kir Kolyshkin
2025-09-16 18:52:47 -07:00
committed by Gopher Robot
parent b731f782ac
commit 28c5bda5d4
4 changed files with 75 additions and 0 deletions

View File

@@ -135,6 +135,7 @@ struct termios2 {
#include <linux/landlock.h>
#include <linux/loop.h>
#include <linux/lwtunnel.h>
#include <linux/mempolicy.h>
#include <linux/mpls_iptunnel.h>
#include <linux/ncsi.h>
#include <linux/net_namespace.h>
@@ -6250,3 +6251,34 @@ type SockDiagReq C.struct_sock_diag_req
// Removed in Linux 6.13, kept for backwards compatibility.
const RTM_NEWNVLAN = 0x70
// Memory policy modes and flags for [SetMemPolicy], as defined in /usr/include/linux/mempolicy.h.
// Not all can be used, see set_mempolicy(2).
// Generated by:
// $ { perl -nlE '/^#define (MPOL_\w+)/ && say "\t$1 = C.$1"' /usr/include/linux/mempolicy.h; perl -nlE '/^\s*(MPOL_\w+)/ && say "\t$1 = C.$1"' /usr/include/linux/mempolicy.h; } | sort | uniq
const (
MPOL_BIND = C.MPOL_BIND
MPOL_DEFAULT = C.MPOL_DEFAULT
MPOL_F_ADDR = C.MPOL_F_ADDR
MPOL_F_MEMS_ALLOWED = C.MPOL_F_MEMS_ALLOWED
MPOL_F_MOF = C.MPOL_F_MOF
MPOL_F_MORON = C.MPOL_F_MORON
MPOL_F_NODE = C.MPOL_F_NODE
MPOL_F_NUMA_BALANCING = C.MPOL_F_NUMA_BALANCING
MPOL_F_RELATIVE_NODES = C.MPOL_F_RELATIVE_NODES
MPOL_F_SHARED = C.MPOL_F_SHARED
MPOL_F_STATIC_NODES = C.MPOL_F_STATIC_NODES
MPOL_INTERLEAVE = C.MPOL_INTERLEAVE
MPOL_LOCAL = C.MPOL_LOCAL
MPOL_MAX = C.MPOL_MAX
MPOL_MF_INTERNAL = C.MPOL_MF_INTERNAL
MPOL_MF_LAZY = C.MPOL_MF_LAZY
MPOL_MF_MOVE_ALL = C.MPOL_MF_MOVE_ALL
MPOL_MF_MOVE = C.MPOL_MF_MOVE
MPOL_MF_STRICT = C.MPOL_MF_STRICT
MPOL_MF_VALID = C.MPOL_MF_VALID
MPOL_MODE_FLAGS = C.MPOL_MODE_FLAGS
MPOL_PREFERRED = C.MPOL_PREFERRED
MPOL_PREFERRED_MANY = C.MPOL_PREFERRED_MANY
MPOL_WEIGHTED_INTERLEAVE = C.MPOL_WEIGHTED_INTERLEAVE
)

View File

@@ -2643,3 +2643,9 @@ func SchedGetAttr(pid int, flags uint) (*SchedAttr, error) {
//sys Cachestat(fd uint, crange *CachestatRange, cstat *Cachestat_t, flags uint) (err error)
//sys Mseal(b []byte, flags uint) (err error)
//sys setMemPolicy(mode int, mask *CPUSet, size int) (err error) = SYS_SET_MEMPOLICY
func SetMemPolicy(mode int, mask *CPUSet) error {
return setMemPolicy(mode, mask, _CPU_SETSIZE)
}

View File

@@ -2238,3 +2238,13 @@ func Mseal(b []byte, flags uint) (err error) {
}
return
}
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
func setMemPolicy(mode int, mask *CPUSet, size int) (err error) {
_, _, e1 := Syscall(SYS_SET_MEMPOLICY, uintptr(mode), uintptr(unsafe.Pointer(mask)), uintptr(size))
if e1 != 0 {
err = errnoErr(e1)
}
return
}

View File

@@ -6332,3 +6332,30 @@ type SockDiagReq struct {
}
const RTM_NEWNVLAN = 0x70
const (
MPOL_BIND = 0x2
MPOL_DEFAULT = 0x0
MPOL_F_ADDR = 0x2
MPOL_F_MEMS_ALLOWED = 0x4
MPOL_F_MOF = 0x8
MPOL_F_MORON = 0x10
MPOL_F_NODE = 0x1
MPOL_F_NUMA_BALANCING = 0x2000
MPOL_F_RELATIVE_NODES = 0x4000
MPOL_F_SHARED = 0x1
MPOL_F_STATIC_NODES = 0x8000
MPOL_INTERLEAVE = 0x3
MPOL_LOCAL = 0x4
MPOL_MAX = 0x7
MPOL_MF_INTERNAL = 0x10
MPOL_MF_LAZY = 0x8
MPOL_MF_MOVE_ALL = 0x4
MPOL_MF_MOVE = 0x2
MPOL_MF_STRICT = 0x1
MPOL_MF_VALID = 0x7
MPOL_MODE_FLAGS = 0xe000
MPOL_PREFERRED = 0x1
MPOL_PREFERRED_MANY = 0x5
MPOL_WEIGHTED_INTERLEAVE = 0x6
)