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

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

问题描述

Go 运行时(调度程序、垃圾收集器等)可以使用多少个线程?例如,如果 GOMAXPROCS10,那么运行时将使用多少个内核线程?

我正在阅读更改GOMAXPROCS> 到 Go 1.5 中的 runtime.NumCPU().有一句话声称由于运行时的并行性,尤其是垃圾收集器,可以通过提高GOMAXPROCS来提高单goroutine程序的性能."

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

没有直接关联.您的应用使用的线程可能小于、等于或大于 10.

引用runtime的包文档:

<块引用>

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

因此,如果您的应用程序没有启动任何新的 goroutine,线程数将小于 10.

如果您的应用启动了许多没有阻塞的 goroutine (>10)(例如在系统调用中),则 10 个操作系统线程将同时执行您的 goroutine.

如果您的应用启动了许多 goroutine,其中许多 (>10) 个在系统调用中被阻塞,则会产生 10 个以上的操作系统线程(但最多只有 10 个将执行用户级 Go 代码).

有关示例和详细信息,请参阅此问题:为什么在golang写文件的时候很多goroutine都被阻塞了,为什么没有创建很多线程?

编辑(响应您的编辑):

我相信 GOMAXPROCS 的默认值是逻辑 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屋!

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