Go运行时使用的线程数 [英] Number of threads used by Go runtime

查看:960
本文介绍了Go运行时使用的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Go运行时(调度程序,垃圾回收器等)可以使用多少线程?例如,如果 GOMAXPROCS 10 ,运行时将使用多少个内核线程?



编辑:



我在阅读 GOMAXPROCS 更改为 runtime.NumCPU()在Go 1.5。有一个句子声称由于运行时,尤其是垃圾收集器的并行性,通过提高 GOMAXPROCS 可以提高单程序程序的性能。



我真正的问题是:如果我有一个单程序程序运行在一个具有CPU配额的Docker容器中,那么我需要的逻辑处理器的最小数量是多少最大性能。

解决方案

没有直接的相关性。您的应用使用的主题可能小于,等于或大于10个。



runtime


GOMAXPROCS变量限制可以同时执行用户级Go代码的操作系统线程数。代表Go的系统调用中可以阻止的线程数没有限制码;那些不会计入GOMAXPROCS限制。这个包的GOMAXPROCS函数查询和更改限制。


所以如果你的应用程序没有启动任何新的goroutine,线程计数将少于10。如果您的应用程序启动许多goroutine(> 10),其中没有阻塞(例如在系统调用),10个操作系统线程将同时执行您的goroutine。



如果你的应用程序启动许多goroutine,其中许多(> 10)在系统调用中被阻塞,将产生超过10个操作系统线程(但最多只有10个将执行user-



查看此问题的示例和详细信息:为什么当许多goroutine在golang的写入文件中被阻塞时,它不创建许多线程?



编辑(响应您的修改)



我相信默认值 GOMAXPROCS 是逻辑CPU的数量原因:因为一般来说提供最高的性能。你可以离开它。一般来说,如果你只有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆