4 ms·
Understanding the Go Scheduler
- 90s_dev 1y agoI heard that the scheduler is a huge obstacle to many potential optimizations, is that true?
- NAHWheatCracker 1y agoIn some ways, yes. If you want to optimize at that level you ought to use another language. I'm not a low level optimization guy, but I've had occasions where I wanted control over which threads my goroutines are running on or prioritizing important goroutines. It's a trade off for making things less complex, which is standard for Go. I suppose there's always hope that the Go developers can change things.
- silisili 1y agoYou can kinda work around this though. runtime package has a LockOSThread that pins a goroutine to its current thread and prevents others from using it. If you model it in a way where you have one goroutine per os thread that receives and does work, it gets you close. But in many cases that means rearching the entire code base, as it's not a style I typically reach for.
- naikrovek 1y agoThat sounds a lot like just using another language.
- silisili 1y agoIt's really not that bad. If you have a codebase in Go you can speed up, it's fine. That said, if you're greenfielding and see this as a limitation to begin with, picking another language is probably the right way.
- jerf 1y agoIf you need it here or there, no. I've got a use case where I need a single locked thread for a particular syscall's functionality. It's not like it leaks out into the rest of the program and everything else has to change to accomodate it. If you need it pervasively, Go may not be the correct choice. Then again, the list of languages that is not a correct choice in that case is quite long. That's a minority case. An important one, but a minority one.
- jasonthorsness 1y agoIt's always a sign of good design when something as complex as the scheduler described "just works" with the simple abstraction of the goroutine. What a great article. "1/61 of the time, check the global run queue." Stuff like this is a little odd; I would have thought this would be a variable dependent on the number of physical cores.
- 01HNNWZ0MV43FF 1y agoThat's so funny. I just saw `61` in the Tokio code with a comment "copied this from Go"
- __turbobrew__ 1y agoMake sure you set GOMAXPROCS when the runtime is cgroup limited. I once profiled a slow go program running on a node with 168 cores, but cpu.max was 2 cores for the cgroup. The runtime defaults to set GOMAXPROCS to the number of visible cores which was 168 in this case. Over half the runtime was the scheduler bouncing goroutines between 168 processes despite cpu.max being 2 CPU. The JRE is smart enough to figure out if it is running in a resource limited cgroup and make sane decisions based upon that, but golang has no such thing.
- xyzzy_plugh 1y agoRelevant proposal to make GOMAXPROCS cgroup-aware: https://github.com/golang/go/issues/73193 https://github.com/golang/go/issues/73193
- robinhoodexe 1y agoLooks like it was just merged btw.
- yencabulator 1y agoThis should be automatic these days (for the basic scenarios). https://github.com/golang/go/blob/a1a151496503cafa5e4c672e0e9c1e1bb3516702/src/runtime/cgroup_linux.go https://github.com/golang/go/blob/a1a151496503cafa5e4c672e0e...
- jasonthorsness 1y agouh isn't that change 3 hours old?
- yencabulator 1y agoOh heh yes it is. I just remembered the original discussion from 2019 (https://github.com/golang/go/issues/33803 https://github.com/golang/go/issues/33803) and grepped the source tree for cgroup to see if that got done or not, but didn't check when it got done. As said in 2019, import https://github.com/uber-go/automaxprocs https://github.com/uber-go/automaxprocs to get the functionality ASAP.
- kortex 1y agoFantastic writeup! Visualizations are great, the writeup is thorough but readable.
- weiwenhao 1y agoYour write-up is so detailed that I even feel like I could implement a complete golang scheduler myself
- davidw 1y agoI'd be interested in seeing a comparison of this and the BEAM/Erlang/Elixir scheduler by someone paying attention to the details.