sysmon
ℹ️ This article is based on Go 1.14.
This️ 本文基于Go 1.14。
The Go standard library dedicates a thread to watch after your application and help with bottlenecks your program could face. This thread, called sysmon
, stands for system monitor, is not linked to any P
in the G,M,P model, meaning it is not taken into account in the scheduler and therefore is always running. Here is an updated diagram with this special thread:
Go标准库专用于监视您的应用程序的线程,并帮助您解决程序可能遇到的瓶颈。 此线程称为sysmon
,代表系统监视器,未链接到G,M,P模型中的任何P
,这意味着在调度程序中未将其考虑在内,因此始终处于运行状态。 这是带有此特殊线程的更新图:
For more information about the G, M, P model, I suggest you read my article “Go: Goroutine, OS Thread and CPU Management.”
有关G,M,P模型的更多信息,建议您阅读我的文章“ Go:Goroutine,OS线程和CPU管理” 。
Also, you will not see this thread in the traces via the Go tool trace
.
同样,您将不会通过Go工具trace
在跟踪中看到此线程。
范围 (Scope)
The role of this thread is wide and steps in with:
该线程的作用很广泛,并涉及以下方面:
The timers created by application. The thread looks at the timers that are supposed to be running but are still waiting for running time. In that case, Go will look at the list of idle
M
andP
in order to run them as soon as possible.由应用程序创建的计时器。 线程查看应该在运行但仍在等待运行时间的计时器。 在这种情况下,Go将查看空闲的
M
和P
列表,以便尽快运行它们。- The net poller and the system calls. It runs the goroutines blocked in network operations. 网络轮询器和系统调用。 它运行网络操作中阻止的goroutine。
The garbage collector if it has not run for quite a long time. If the garbage collector has not run for two minutes,
sysmon
will force one round of garbage collection. Here is an example from traces generated by the go tooltrace
:垃圾收集器(如果已运行很长时间)。 如果垃圾收集器没有运行两分钟,则
sysmon
将强制执行一轮垃圾收集。 这是go工具trace
生成的trace
示例:
- Preemption of the long-running goroutine. Any goroutine running for more than 10ms will try to be preempted to leave running time to the other goroutines. 长时间运行的goroutine的抢占。 任何运行时间超过10ms的goroutine都将试图抢占其他goroutine的运行时间。
For more information about asynchronous preemption, I suggest you read my article “Go: Asynchronous Preemption.”
有关异步抢占的更多信息,建议您阅读我的文章“ Go:异步抢占” 。
步伐 (Pace)
sysmon
is smart enough to not consume resources when there is nothing to do. Its cycle time is dynamic and depends on the current activity of the running program.
sysmon
足够聪明,在无事可做时不会消耗资源。 它的循环时间是动态的,并取决于正在运行的程序的当前活动。
The initial pace is set at 20 nanoseconds, meaning the thread is constantly looking to help. Then, after some cycles, if the thread is not doing anything, the sleep between two cycles will double until it reaches 10ms. If your application does not have many system calls or long-running goroutines, the thread should back off to a 10ms delay most of its time, giving a very light overhead to your application.
初始速度设置为20纳秒,这意味着线程一直在寻求帮助。 然后,在某些周期后,如果线程没有执行任何操作,则两个周期之间的睡眠将加倍,直到达到10ms。 如果您的应用程序没有很多系统调用或长时间运行的goroutine,则线程应在大多数时间后退到10ms的延迟,这将给您的应用程序带来非常小的开销。
The thread is also able to detect when it should not run. Here are two cases:
该线程还能够检测何时不应运行。 这是两种情况:
The garbage collector is going to run.
sysmon
will resume when the garbage collector ends.垃圾收集器将运行。 垃圾回收器结束时,
sysmon
将恢复。- All the threads are idle, nothing is running. 所有线程都处于空闲状态,没有任何运行。
In both cases, sysmon
sleeps and does not consume any resources unnecessarily.
在这两种情况下, sysmon
Hibernate,并且不会不必要地消耗任何资源。
翻译自: https://medium.com/@blanchon.vincent/go-sysmon-runtime-monitoring-cff9395060b5
sysmon