Go运行时使用的线程数 [英] Number of threads used by Go runtime
问题描述
GOMAXPROCS
是 10
,那么这些内核线程将被运行时使用多少? 编辑:
我的真正的问题是:如果我有一个运行在具有CPU配额的Docker容器中的单goroutine程序,我需要的逻辑处理器的最小数量是多少最高性能。
没有直接的关联。您应用程式使用的线程可能小于等于或超过10个。
从 运行时
:
GOMAXPROCS变量限制可以同时执行用户级Go代码的操作系统线程数。代表系统调用可阻止的线程数量没有限制去代码那些不反对GOMAXPROCS限制。这个软件包的GOMAXPROCS函数查询并更改了限制。
所以如果您的应用程序没有启动任何新的goroutines,线程计数将小于如果您的应用程序启动了许多goroutine(> 10),其中没有阻止(例如在系统调用中),10个操作系统线程将同时执行您的goroutines。
p>
如果您的应用程序启动了许多(> 10)在系统调用中被阻止的goroutine,将会产生超过10个操作系统线程(但是最多只有10个将执行用户 - 级别Go代码)。
有关示例和详细信息,请参阅此问题:为什么在golang中写入文件中阻止许多goroutine时,它不会创建许多线程?
编辑(响应您的修改):
我相信默认值< codeG OMAXPROCS 是逻辑CPU的数量,因为一般情况下提供最高的性能。你可以离开它。一般来说,如果您只有1个goroutine,并确保您的代码不会产生更多,则 GOMAXPROCS = 1
是足够的,但您应该测试并且不要
How many threads can the Go runtime (scheduler, garbage collector, etc.) use? For example, if GOMAXPROCS
is 10
, how many of those kernel threads would be used by the runtime?
Edit:
I was reading the rationale for changing GOMAXPROCS
to runtime.NumCPU()
in Go 1.5. There was a sentence that claimed that "the performance of single-goroutine programs can improve by raising GOMAXPROCS
due to parallelism of the runtime, especially the garbage collector."
My real question is: If I have a single-goroutine program running in a Docker container that has a CPU quota, what is the minimum number of logical processors that I need in order to have maximum performance.
There is no direct correlation. Threads used by your app may be less than, equal to or more than 10.
Quoting from the package doc of runtime
:
The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count against the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes the limit.
So if your application does not start any new goroutines, threads count will be less than 10.
If your app starts many goroutines (>10) where none is blocking (e.g. in system calls), 10 operating system threads will execute your goroutines simultaneously.
If your app starts many goroutines where many (>10) are blocked in system calls, more than 10 OS threads will be spawned (but only at most 10 will be executing user-level Go code).
See this question for an example and details: Why does it not create many threads when many goroutines are blocked in writing file in golang?
Edit (in response to your edit):
I believe the default value of GOMAXPROCS
is the number of logical CPUs for a reason: because in general that provides the highest performance. You may leave it at that. In general if you only have 1 goroutine and you're sure your code doesn't spawn more, GOMAXPROCS=1
is sufficient, but you should test and don't take any word for it.
这篇关于Go运行时使用的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!