简体   繁体   English

golang中如何将一个进程绑定到一组cpu上?

[英]How to bind a process to a set of cpu in golang?

I use os/exec pkg to have a process run.我使用 os/exec pkg 来运行进程。 I want to check it cpu affinity and modify it to bind the process to a specific cpu set.我想检查它的 cpu 亲和力并修改它以将进程绑定到特定的 cpu 集。 I find我发现

func SchedSetaffinity(pid int, set *CPUSet) error

This function is in golang.org/x/sys/unix package .这个函数在golang.org/x/sys/unix 包中 However, it says it just bind a thread to a specific cpu.但是,它说它只是将线程绑定到特定的 cpu。 I don't know wheter it works on process.我不知道它是否适用于流程。 And I wonder how to get the CPUSet.我想知道如何获得 CPUSet。 Is it a value I need to define?这是我需要定义的值吗?

Taskset : To enable a process run on a specific CPU, you use the command 'taskset' in linux. Taskset :要启用在特定 CPU 上运行的进程,您可以在 linux 中使用命令“taskset”。 Accordingly you can arrive on a logic based on "taskset -p [mask] [pid]" where the mask represents the cores in which the particular process shall run, provided the whole program runs with GOMAXPROCS=1.因此,您可以得出基于“taskset -p [mask] [pid]”的逻辑,其中掩码表示特定进程应在其中运行的核心,前提是整个程序以 GOMAXPROCS=1 运行。

pthread_setaffinity_np : You can use cgo and arrive on a logic that calls pthread_setaffinity_np, as Go uses pthreads in cgo mode. pthread_setaffinity_np :您可以使用 cgo 并到达调用 pthread_setaffinity_np 的逻辑,因为 Go 在 cgo 模式下使用 pthreads。 (The pthread_attr_setaffinity_np() function sets the CPU affinity mask attribute of the thread attributes object referred to by attr to the value specified in cpuset. ) (pthread_attr_setaffinity_np() 函数将 attr 引用的线程属性对象的 CPU 关联掩码属性设置为 cpuset 中指定的值。)

Go helps in incorporation of affinity control via "SchedSetaffinity" that can be checked for confining a thread to specific cores. Go 有助于通过“SchedSetaffinity” 合并关联控制,可以检查将线程限制到特定内核。 Accordingly , you can arrive on a logic for usage of "SchedSetaffinity(pid int, set *CPUSet)" that sets the CPU affinity mask of the thread specified by pid.因此,您可以得出使用“SchedSetaffinity(pid int, set *CPUSet)”的逻辑,该逻辑设置由 pid 指定的线程的 CPU 关联掩码。 If pid is 0 the calling thread is used.如果 pid 为 0,则使用调用线程。

It should be noted that GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously.需要注意的是,GOMAXPROCS 变量限制了可以同时执行用户级 Go 代码的操作系统线程数。 If it is > 1 then, you may use runtime.LockOSThread of Go that shall pin the current goroutine to the current thread that is is running on .如果它 > 1,则可以使用 Go 的runtime.LockOSThread将当前 goroutine 固定到正在运行的当前线程。 The calling goroutine will always execute in that thread, and no other goroutine will execute in it, until the calling goroutine has made as many calls to UnlockOSThread as to LockOSThread.调用 goroutine 将始终在该线程中执行,并且不会在其中执行其他 goroutine,直到调用 goroutine 对 UnlockOSThread 进行了与 LockOSThread 一样多的调用。

cgroups : There is also option of using cgroups that helps in organizing the processes hierarchically and distribution of system resources along the hierarchy in a controlled and configurable manner. cgroups :还有使用cgroups 的选项,它有助于以受控和可配置的方式按层次结构组织进程并沿层次结构分配系统资源。 Here, there is subsystem termed as cpuset that enables assigning individual CPUs (on a multicore system) and memory nodes to process in a cgroup.在这里,有一个称为 cpuset 的子系统,它可以分配单个 CPU(在多核系统上)和内存节点以在 cgroup 中进行处理。 The cpuset lists CPUs to be used by tasks within this cgroup. cpuset 列出了此 cgroup 中的任务要使用的 CPU。 The CPU numbers are comma-separated numbers or ranges. CPU 编号是逗号分隔的编号或范围。 For example:例如:

#cat cpuset.cpus
0-4,6,8-10

A process is confined to run only on the CPUs in the cpuset it belongs to, and to allocate memory only on the memory nodes in that cpuset.一个进程只能在它所属的 cpuset 中的 CPU 上运行,并且只能在该 cpuset 中的内存节点上分配内存。 It should be noted that all processes are put in the cgroup that the parent process belongs to at the time on creation and a process can be migrated to another cgroup.需要注意的是,所有进程在创建时都放在父进程所属的cgroup中,一个进程可以迁移到另一个cgroup。 Migration of a process doesn't affect already existing descendant processes.进程的迁移不会影响已经存在的后代进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM