英文:
Run on the specific CPU, not one goroutine?
问题
golang
只有一个方法runtime.GOMAXPROCS(1)
可以让应用程序同时运行一个goroutine
,但我想让应用程序只在特定的一个CPU上运行。
英文:
golang
just has a method runtime.GOMAXPROCS(1)
to set the application run one goroutine
at the same time,But i want to let the application just run on the specificone cpu?
答案1
得分: 3
要启动你的Go二进制文件,让它只在Windows的CPU 0上运行,你可以使用带有"affinity"参数的"start"命令:
start /affinity 1 example.exe
我不知道你具体想要实现什么,但是可以参考这个描述GOMAXPROCS的文档,也许Go运行时会以更高效的方式完成相同的任务。
英文:
To launch you Go binary, say example.exe
so that it runs only on CPU 0 on Windows, you can use start
command with "affinity" parameter:
start /affinity 1 example.exe
I don't know what exactly are you trying to achieve but refer to this document describing GOMAXPROCS - it might be the case that Go runtime will do the same thing more efficiently.
答案2
得分: 0
在gc编译器(6g或8g)下,您必须将GOMAXPROCS设置为大于默认值1,以允许运行时支持利用多个操作系统线程,也就是说,除非将GOMAXPROCS设置为大于1的值,否则所有的goroutine都共享同一个线程。
当GOMAXPROCS大于1时,它们在具有相应数量线程的线程池上运行。对于gccgo编译器,GOMAXPROCS实际上等于正在运行的goroutine的数量。假设n
是机器上的处理器或核心数量。如果您设置环境变量GOMAXPROCS >= n
,或调用runtime.GOMAXPROCS(n)
,那么goroutine将被分配(分布)在n个处理器上。
然而,更多的处理器并不一定意味着性能线性提升,主要是因为需要更多的通信:消息传递开销增加。经验法则似乎是,对于n个核心,将GOMAXPROCS设置为n-1可以获得最佳性能,并且还应遵循以下规则:
goroutine的数量 > 1 + GOMAXPROCS > 1
因此,如果在某个时间点只有一个goroutine在执行,请不要设置GOMAXPROCS!
如果您希望将goroutine锁定到特定的操作系统线程,可以使用runtime.LockOSThread实现。
> LockOSThread将调用的goroutine绑定到其当前的操作系统线程。直到调用的goroutine退出或调用UnlockOSThread之前,它将始终在该线程中执行,并且没有其他goroutine可以执行。
更多信息请参阅https://golang.org/pkg/runtime/。
英文:
Under the gc compilers (6g or 8g) you must set GOMAXPROCS to more than the default value 1 to allow the run-time support to utilize more than one OS thread, that is all goroutines share the same thread unless GOMAXPROCS is set to a value greater than 1.
When GOMAXPROCS is greater than 1, they run on a thread pool with that many threads. With the gccgo compiler GOMAXPROCS is effectively equal to the number of running goroutines. Suppose n
is the number of processors or cores on the machine. If you set the environment variable GOMAXPROCS >= n
, or call runtime.GOMAXPROCS(n)
, then the goroutines are divided (distributed)
among the n processors.
More processors however don’t mean necessarily a linear improvement in performance, mainly because more communication is needed: the message-passing overhead increases. An experiential rule of thumb seems to be that for n cores setting GOMAXPROCS to n-1 yields the best performance, and the following should also be followed:
number of goroutines > 1 + GOMAXPROCS > 1
So if there is only one goroutine executing at a certain point in time, don’t set GOMAXPROCS!
If you wish to lock a goroutine to a particular OS thread you can do this with runtime.LockOSThread.
> LockOSThread wires the calling goroutine to its current operating
> system thread. Until the calling goroutine exits or calls
> UnlockOSThread, it will always execute in that thread, and no other
> goroutine can.
More about on https://golang.org/pkg/runtime/.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论