numa

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 9 Imported by: 3

README

NUMA

Build Status GoDoc codecov Go Report Card

NUMA is a utility library, which is written in go. It help us to write some NUMA-AWARED code.

example gist:

package main

import (
	"github.com/lrita/numa"
)

type object struct {
	X int
	_ [...]byte // padding to page size.
 }

var objects = make([]object, numa.CPUCount())

func fnxxxx() {
	cpu, node := numa.GetCPUAndNode()
	objects[cpu].X = xx
}

Documentation

Index

Constants

View Source
const (
	// The memory policy of GetMemPolicy/SetMemPolicy.
	MPOL_DEFAULT = iota
	MPOL_PREFERRED
	MPOL_BIND
	MPOL_INTERLEAVE
	MPOL_LOCAL
	MPOL_MAX

	// MPOL_F_STATIC_NODES since Linux 2.6.26
	// A nonempty nodemask specifies physical node ids. Linux does will not
	// remap the nodemask when the process moves to a different cpuset context,
	// nor when the set of nodes allowed by the process's current cpuset
	// context changes.
	MPOL_F_STATIC_NODES = 1 << 15

	// MPOL_F_RELATIVE_NODES since Linux 2.6.26
	// A nonempty nodemask specifies node ids that are relative to the set
	// of node ids allowed by the process's current cpuset.
	MPOL_F_RELATIVE_NODES = 1 << 14

	// MPOL_MODE_FLAGS is the union of all possible optional mode flags passed
	// to either SetMemPolicy() or mbind().
	MPOL_MODE_FLAGS = MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES
)
View Source
const (
	// Flags for get_mem_policy
	// return next IL node or node of address
	// Warning: MPOL_F_NODE is unsupported and subject to change. Don't use.
	MPOL_F_NODE = 1 << iota
	// look up vma using address
	MPOL_F_ADDR
	// query nodes allowed in cpuset
	MPOL_F_MEMS_ALLOWED
)
View Source
const (
	// Flags for mbind
	// Verify existing pages in the mapping
	MPOL_MF_STRICT = 1 << iota
	// Move pages owned by this process to conform to mapping
	MPOL_MF_MOVE
	// Move every page to conform to mapping
	MPOL_MF_MOVE_ALL
	// Modifies '_MOVE: lazy migrate on fault
	MPOL_MF_LAZY
	// Internal flags start here
	POL_MF_INTERNAL

	MPOL_MF_VALID = MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL
)

Variables

This section is empty.

Functions

func Available

func Available() bool

Available returns current platform is whether support NUMA. @ int numa_available(void)

func Bind

func Bind(mask Bitmask) error

Bind bind current process on those nodes which given by a bitmask. @numa_bind_v2

func CPUCount

func CPUCount() int

CPUCount returns the current configured(enabled/detected) cpu count, which is different with runtime.NumCPU().

func CPUPossibleCount

func CPUPossibleCount() int

CPUPossibleCount returns the possible cpu count of current platform supported.

func CPUToNode

func CPUToNode(cpu int) (int, error)

CPUToNode returns the node id by given cpu id.

func GetCPUAndNode

func GetCPUAndNode() (cpu int, node int)

GetCPUAndNode returns the node id and cpu id which current caller running on. https://man7.org/linux/man-pages/man2/getcpu.2.html

equal:

if fastway {
	call RDTSCP
 The linux kernel will fill the node cpu id in the private data of each cpu.
 arch/x86/kernel/vsyscall_64.c@vsyscall_set_cpu
}

call vdsoGetCPU

func GetMemPolicy

func GetMemPolicy(nodemask Bitmask, addr unsafe.Pointer, flags int) (mode int, err error)

GetMemPolicy retrieves the NUMA policy of the calling process or of a memory address, depending on the setting of flags. Details to see manpage of get_mempolicy.

If flags is specified as 0, then information about the calling process's default policy (as set by set_mempolicy(2)) is returned. The policy returned [mode and nodemask] may be used to restore the process's policy to its state at the time of the call to get_mempolicy() using set_mempolicy(2).

If flags specifies MPOL_F_MEMS_ALLOWED (available since Linux 2.6.24), the mode argument is ignored and the set of nodes [memories] that the process is allowed to specify in subsequent calls to mbind(2) or set_mempolicy(2) [in the absence of any mode flags] is returned in nodemask. It is not permitted to combine MPOL_F_MEMS_ALLOWED with either MPOL_F_ADDR or MPOL_F_NODE.

If flags specifies MPOL_F_ADDR, then information is returned about the policy governing the memory address given in addr. This policy may be different from the process's default policy if mbind(2) or one of the helper functions described in numa(3) has been used to establish a policy for the memory range containing addr.

If flags specifies both MPOL_F_NODE and MPOL_F_ADDR, get_mempolicy() will return the node ID of the node on which the address addr is allocated into the location pointed to by mode. If no page has yet been allocated for the specified address, get_mempolicy() will allocate a page as if the process had performed a read [load] access to that address, and return the ID of the node where that page was allocated.

If flags specifies MPOL_F_NODE, but not MPOL_F_ADDR, and the process's current policy is MPOL_INTERLEAVE, then get_mempolicy() will return in the location pointed to by a non-NULL mode argument, the node ID of the next node that will be used for interleaving of internal kernel pages allocated on behalf of the process. These allocations include pages for memory mapped files in process memory ranges mapped using the mmap(2) call with the MAP_PRIVATE flag for read accesses, and in memory ranges mapped with the MAP_SHARED flag for all accesses.

func GetSchedAffinity

func GetSchedAffinity(pid int, cpumask Bitmask) (int, error)

GetSchedAffinity writes the affinity mask of the process whose ID is pid into the input mask. If pid is zero, then the mask of the calling process is returned.

func MBind

func MBind(addr unsafe.Pointer, length, mode, flags int, nodemask Bitmask) (err error)

MBind sets the NUMA memory policy, which consists of a policy mode and zero or more nodes, for the memory range starting with addr and continuing for length bytes. The memory policy defines from which node memory is allocated. Details to see manpage of mbind.

If the memory range specified by the addr and length arguments includes an "anonymous" region of memory that is a region of memory created using the mmap(2) system call with the MAP_ANONYMOUS or a memory mapped file, mapped using the mmap(2) system call with the MAP_PRIVATE flag, pages will be allocated only according to the specified policy when the application writes [stores] to the page. For anonymous regions, an initial read access will use a shared page in the kernel containing all zeros. For a file mapped with MAP_PRIVATE, an initial read access will allocate pages according to the process policy of the process that causes the page to be allocated. This may not be the process that called mbind().

The specified policy will be ignored for any MAP_SHARED mappings in the specified memory range. Rather the pages will be allocated according to the process policy of the process that caused the page to be allocated. Again, this may not be the process that called mbind().

If the specified memory range includes a shared memory region created using the shmget(2) system call and attached using the shmat(2) system call, pages allocated for the anonymous or shared memory region will be allocated according to the policy specified, regardless which process attached to the shared memory segment causes the allocation. If, however, the shared memory region was created with the SHM_HUGETLB flag, the huge pages will be allocated according to the policy specified only if the page allocation is caused by the process that calls mbind() for that region.

By default, mbind() has an effect only for new allocations; if the pages inside the range have been already touched before setting the policy, then the policy has no effect. This default behavior may be overridden by the MPOL_MF_MOVE and MPOL_MF_MOVE_ALL flags described below.

func MaxNodeID

func MaxNodeID() int

MaxNodeID returns the max id of current configured NUMA nodes. @numa_max_node_int

func MaxPossibleNodeID

func MaxPossibleNodeID() int

MaxPossibleNodeID returns the max possible node id of this platform supported. The possible node id always larger than max node id.

func NodeCount

func NodeCount() int

NodeCount returns the count of current configured NUMA nodes.

NOTE: this function's behavior matches the documentation (ie: it returns a count of nodes with memory) despite the poor function naming. We also cannot use the similarly poorly named numa_all_nodes_ptr as it only tracks nodes with memory from which the calling process can allocate. Think sparse nodes, memory-less nodes, cpusets... @numa_num_configured_nodes

func NodeMemSize64

func NodeMemSize64(node int) (total int64, free int64, err error)

NodeMemSize64 return the memory total size and free size of given node.

func NodePossibleCount

func NodePossibleCount() int

NodePossibleCount returns the possible NUMA nodes count of current platform supported.

func RunOnNode

func RunOnNode(node int) (err error)

RunOnNode set current process run on given node. The special node -1 will set current process on all available nodes. @numa_run_on_node

func RunOnNodeMask

func RunOnNodeMask(mask Bitmask) error

RunOnNodeMask run current process to the given nodes. @numa_run_on_node_mask_v2

func SetMemPolicy

func SetMemPolicy(mode int, nodemask Bitmask) (err error)

This system call defines the default policy for the process. The process policy governs allocation of pages in the process's address space outside of memory ranges controlled by a more specific policy set by mbind(2). The process default policy also controls allocation of any pages for memory mapped files mapped using the mmap(2) call with the MAP_PRIVATE flag and that are only read [loaded] from by the process and of memory mapped files mapped using the mmap(2) call with the MAP_SHARED flag, regardless of the access type. The policy is applied only when a new page is allocated for the process. For anonymous memory this is when the page is first touched by the application.

The mode argument must specify one of MPOL_DEFAULT, MPOL_BIND, MPOL_INTERLEAVE or MPOL_PREFERRED. All modes except MPOL_DEFAULT require the caller to specify via the nodemask argument one or more nodes.

The mode argument may also include an optional mode flag. The supported mode flags are: MPOL_F_STATIC_NODES and MPOL_F_RELATIVE_NODES.

Where a nodemask is required, it must contain at least one node that is on-line, allowed by the process's current cpuset context, [unless the MPOL_F_STATIC_NODES mode flag is specified], and contains memory. If the MPOL_F_STATIC_NODES is set in mode and a required nodemask contains no nodes that are allowed by the process's current cpuset context, the memory policy reverts to local allocation. This effectively overrides the specified policy until the process's cpuset context includes one or more of the nodes specified by nodemask.

func SetSchedAffinity

func SetSchedAffinity(pid int, cpumask Bitmask) error

SetSchedAffinity sets the CPU affinity mask of the process whose ID is pid to the value specified by mask. If pid is zero, then the calling process is used.

Types

type Bitmask

type Bitmask []uint64

Bitmask is used for syscall or other operation in NUMA API.

func GetMemAllowedNodeMask

func GetMemAllowedNodeMask() (Bitmask, error)

GetMemAllowedNodeMask returns the bitmask of current process allowed running nodes. @numa_get_mems_allowed

func NewBitmask

func NewBitmask(n int) Bitmask

NewBitmask returns a bitmask, which length always rounded to a multiple of sizeof(uint64). The input param n represents the bit count of this bitmask.

func NodeMask

func NodeMask() Bitmask

NodeMask returns the mask of current configured nodes.

func NodeToCPUMask

func NodeToCPUMask(node int) (Bitmask, error)

NodeToCPUMask returns the cpumask of given node id. @numa_node_to_cpus_v2

func RunningCPUMask

func RunningCPUMask() (Bitmask, error)

RunningCPUMask return the cpu bitmask of current process running on.

func RunningNodesMask

func RunningNodesMask() (Bitmask, error)

RunningNodesMask return the bitmask of current process using NUMA nodes. @numa_get_run_node_mask_v2

func (Bitmask) ClearAll

func (b Bitmask) ClearAll()

ClearAll clear all bits of this bitmask.

func (Bitmask) Clone

func (b Bitmask) Clone() Bitmask

Clone return a duplicated bitmask.

func (Bitmask) Get

func (b Bitmask) Get(i int) bool

Get returns the No.i bit of this bitmask.

func (Bitmask) Len

func (b Bitmask) Len() int

Len returns the bitmask length.

func (Bitmask) OnesCount

func (b Bitmask) OnesCount() (n int)

OnesCount returns the number of one bits ("population count") in this bitmask.

func (Bitmask) Set

func (b Bitmask) Set(i int, v bool)

Set sets the No.i bit to v.

func (Bitmask) SetAll

func (b Bitmask) SetAll()

SetAll clear all bits of this bitmask.

func (Bitmask) String

func (b Bitmask) String() string

String returns the hex string of this bitmask.

func (Bitmask) Text

func (b Bitmask) Text() string

Text returns the digital bit text of this bitmask.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL