英文:
How to bind a process to a set of cpu in golang?
问题
你可以使用os/exec
包来运行进程。如果你想要检查进程的CPU亲和性并将其绑定到特定的CPU集合,你可以使用golang.org/x/sys/unix
包中的SchedSetaffinity
函数。然而,文档中提到该函数只能将线程绑定到特定的CPU,不清楚是否适用于进程。另外,你想知道如何获取CPUSet
。CPUSet
是一个需要你自己定义的值。
英文:
I use os/exec pkg to have a process run. I want to check it cpu affinity and modify it to bind the process to a specific cpu set. I find
func SchedSetaffinity(pid int, set *CPUSet) error
This function is in golang.org/x/sys/unix package. However, it says it just bind a thread to a specific cpu. I don't know wheter it works on process. And I wonder how to get the CPUSet. Is it a value I need to define?
答案1
得分: 4
Taskset: 在Linux中,要使进程在特定的CPU上运行,可以使用命令'taskset'。根据"taskset -p [mask] [pid]"的逻辑,其中mask表示特定进程应该运行的核心,前提是整个程序以GOMAXPROCS=1运行。
pthread_setaffinity_np: 可以使用cgo并采用调用pthread_setaffinity_np的逻辑,因为Go在cgo模式下使用pthread(pthread_attr_setaffinity_np()函数将线程属性对象attr的CPU亲和力掩码属性设置为cpuset中指定的值)。
Go通过"SchedSetaffinity"帮助实现亲和性控制,可以检查是否将线程限制在特定核心上。因此,可以根据使用"SchedSetaffinity(pid int, set *CPUSet)"的逻辑来设置由pid指定的线程的CPU亲和力掩码。如果pid为0,则使用调用线程。
需要注意的是,GOMAXPROCS变量限制了可以同时执行用户级Go代码的操作系统线程数。如果它大于1,则可以使用Go的runtime.LockOSThread将当前goroutine固定到当前正在运行的线程上。调用的goroutine将始终在该线程中执行,并且没有其他goroutine将在其中执行,直到调用的goroutine对LockOSThread的调用次数与UnlockOSThread的调用次数相同。
cgroups: 还可以使用cgroups来以受控且可配置的方式对进程进行层次化组织和系统资源分配。在这里,有一个称为cpuset的子系统,它可以将单个CPU(在多核系统上)和内存节点分配给cgroup中的进程。cpuset列出了此cgroup中任务要使用的CPU。CPU编号是逗号分隔的数字或范围。例如:
#cat cpuset.cpus
0-4,6,8-10
进程被限制只能在其所属的cpuset中的CPU上运行,并且只能在该cpuset中的内存节点上分配内存。需要注意的是,所有进程都被放置在父进程所属的cgroup中,创建时的时间,并且进程可以迁移到另一个cgroup。进程的迁移不会影响已经存在的子进程。
英文:
Taskset : To enable a process run on a specific CPU, you use the command 'taskset' in linux. 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.
pthread_setaffinity_np : You can use cgo and arrive on a logic that calls pthread_setaffinity_np, as Go uses pthreads in cgo mode. (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. )
Go helps in incorporation of affinity control via "SchedSetaffinity" that can be checked for confining a thread to specific cores. 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. If pid is 0 the calling thread is used.
It should be noted that GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. 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 . 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.
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. Here, there is subsystem termed as cpuset that enables assigning individual CPUs (on a multicore system) and memory nodes to process in a cgroup. The cpuset lists CPUs to be used by tasks within this cgroup. The CPU numbers are comma-separated numbers or ranges. 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. 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. Migration of a process doesn't affect already existing descendant processes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论