What is sysmon and what does it do?
Question 159HardGo 1.22 to 1.25
sysmon is a runtime thread that runs without a P, so it keeps working even when every P is busy or stuck. It wakes up adaptively, every 20 µs to 10 ms, backing off when the program is idle. Its jobs:
- Retake Ps from syscalls: if a P has been in
_Psyscallfor more than one tick and there is work waiting, it hands the P to another M. - Preempt long-running goroutines: a G that has run for more than about 10 ms (
forcePreemptNS) is flagged and sent an async preemption signal. - Poll the network if no one has polled for about 10 ms, and inject the ready goroutines.
- Force GC if none has run for 2 minutes (
forcegcperiod). - Wake the scavenger, which returns memory to the OS, and help with timers.
What the interviewer wants to hear: you understand that fairness in Go comes from sysmon plus async preemption, not from time slices handed out by the scheduler itself. This also explains why the scheduler "tick" is about 10 ms and why latency-sensitive loops can still see jitter at that scale.
More on Goroutines & the Scheduler
- Q157What happens to the scheduler when a goroutine makes a blocking syscall?
- Q158What is the netpoller and how does it make network I/O look blocking yet scale?
- Q160What does runtime.Gosched do and when (if ever) should you use it?
- Q161What does runtime.LockOSThread do and when is it needed?
- Q162What is a goroutine leak? Give common causes.
- Q163How do you detect and debug goroutine leaks in production and tests?