Demystifying the Go Scheduler

DEMYSTIFYING 
THE GO 
SCHEDULER 
MATTHEW DALE
Go Concurrency Crash Course 
The atomic unit of concurrent work in Go is the goroutine 
Started by invoking a function with the go keyword 
! 
Invoked function runs concurrently with the current thread 
of execution 
Goroutine synchronization is accomplished with channels 
Message passing construct 
Buffered or unbuffered
The Go Scheduler 
Attempts to efficiently schedule 
goroutines on available resources 
The scheduler will pick up a new 
goroutine when the current goroutine… 
1. Finishes 
2. Makes a blocking system call 
E.g. reading a file 
3. Makes a blocking Go runtime call 
E.g. reading from a channel 
4. Invokes another function (only 
happens sometimes)* 
Taken from http://morsmachine.dk/go-scheduler 
M - Machine (OS thread) 
P - Context (Go scheduler) 
*As of Go 1.2 ( https://golang.org/doc/go1.2#preemption ) G - Goroutine
If the current goroutine is blocked on a system call… 
The OS thread processing the goroutine idles waiting for the 
system call to return 
The context that was scheduling gorotuines on the blocked thread 
is moved to a new thread (or a new one is created if none are 
available) Taken from http://morsmachine.dk/go-scheduler
HOW DOES GO ASK FOR 
RESOURCES ABSTRACTED BY THE 
OPERATING SYSTEM?
The syscall Package 
All calls that can block an OS thread go through 
the syscall package (except cgo calls) 
Responsible for informing the Go runtime that a 
potentially blocking system call is about to happen
src/pkg/os/file.go, line 91 
src/pkg/os/file_unix.go, line 186 
Let’s follow a call to file.Read…
src/package/zsyscall_linux_amd64.go, line 831 (generated) 
src/pkg/syscall/asm_linux_amd64.s, line 44
src/pkg/runtime/proc.c, line 1502
Notes on Host Setup 
Modify /etc/security/limits.conf 
Go will quickly use up the default allotment of 
file descriptors on most Linux distros 
The default Go HTTP server will panic if 
listener.Accept() ever returns an error, killing your 
service 
Example configuration 
ec2-user soft nofile 100000 
ec2-user hard nofile 100000 
root soft nofile 100000 
root hard nofile 100000
FILE I/O DEMO!
BUT HOW DOES THAT WORK WITH 
NETWORK I/O? 
! 
WOULDN’T THAT JUST CREATE A 
BILLION THREADS?
The netpoller 
Runs in its own thread and polls the OS’s asynchronous 
I/O network interface for data 
Goroutines asking for network I/O block waiting for the 
netpoller to give them data, not for a system event 
Go treats Unix sockets and network connection file 
descriptors the same, so Unix socket I/O will not block 
OS threads 
See http://morsmachine.dk/netpoller for a great, 
concise description of the netpoller
RESOURCE STARVATION 
OR 
WHY DID MY GO SERVICE 
STOP RESPONDING?
WEB SERVICE 
PERFORMANCE DEMO!
NOT SO GOOD…
Which Problems Are 
Essential vs Accidental*? 
Essential 
A computer can do a finite 
amount of work per unit time 
If there is more work than 
resources, then work must be 
delegated, queued or dropped 
Accidental 
Sometimes you and the Go 
scheduler disagree about what 
is the most important work to 
do 
*Essential vs accidental complexity is a problem decomposition 
strategy proposed in Frederick Brooks Jr.’s book The Mythical 
Man-Month
Simplify vs Complexify 
Simplify - synchronous instead of concurrent 
Still requires determining how to do CPU load 
balancing 
Pushes performance problems to the front, letting the 
caller tune on their side 
Complexify - refactor into micro services and add queues 
Lots of additional code just to solve a “simple” 
problem
Think bite-sized 
Refactor long-running, non-blocking 
workloads to add more 
function calls 
The Go scheduler will 
sometimes preempt the 
running goroutine when it 
makes a non-inlineable 
function call 
Explicitly call runtime.Gosched 
Causes the current running 
goroutine to yield to the 
scheduler
Think micro services 
Keep heterogeneous 
workloads in different Go 
processes 
One service handles 
quick or I/O heavy tasks 
One service handles 
long-running 
OS handles resource 
scheduling
runtime.LockOSThread 
Locks the current goroutine to the current thread 
and doesn’t allow any other goroutines to run on 
that thread 
Call runtime.UnlockOSThread to unlock the 
thread.
Extra Go Scheduler Topics 
There are a lot of topics relevant to the Go 
scheduler but beyond the scope of this 
presentation, including… 
Tailoring GOMAXPROCS for your workload 
How cgo calls interact with the scheduler
Sources and Suggested 
Reading 
http://morsmachine.dk/go-scheduler 
http://morsmachine.dk/netpoller 
http://dave.cheney.net/2014/06/07/five-things-that-make- 
go-fast 
http://golang.org/pkg/runtime/ 
Code examples used in this presentation can be 
found at https://github.com/matthewdale/ 
GoSchedulerDemo
QUESTIONS?
1 of 24

Recommended

Route Redistribution by
Route RedistributionRoute Redistribution
Route RedistributionNetwax Lab
1.3K views14 slides
An Overview of Border Gateway Protocol (BGP) by
An Overview of Border Gateway Protocol (BGP)An Overview of Border Gateway Protocol (BGP)
An Overview of Border Gateway Protocol (BGP)Jasim Alam
4.1K views27 slides
bgp(border gateway protocol) by
bgp(border gateway protocol)bgp(border gateway protocol)
bgp(border gateway protocol)Noor Ul Hudda Memon
1.1K views41 slides
CSI-503 - 13. Scheduler and Dispatcher by
CSI-503 - 13. Scheduler and Dispatcher CSI-503 - 13. Scheduler and Dispatcher
CSI-503 - 13. Scheduler and Dispatcher ghayour abbas
269 views5 slides
Migrating from OSPF to IS-IS by Philip Smith by
Migrating from OSPF to IS-IS by Philip SmithMigrating from OSPF to IS-IS by Philip Smith
Migrating from OSPF to IS-IS by Philip SmithMyNOG
3.1K views33 slides
Routing Protocol EIGRP by
Routing Protocol EIGRPRouting Protocol EIGRP
Routing Protocol EIGRPDmitry Figol
30K views49 slides

More Related Content

What's hot

Coordination and Agreement .ppt by
Coordination and Agreement .pptCoordination and Agreement .ppt
Coordination and Agreement .pptSOURAVKUMAR723356
117 views93 slides
OTV PPT by NETWORKERS HOME by
OTV PPT by NETWORKERS HOMEOTV PPT by NETWORKERS HOME
OTV PPT by NETWORKERS HOMEnetworkershome
4.7K views85 slides
DVWA(Damn Vulnerabilities Web Application) by
DVWA(Damn Vulnerabilities Web Application)DVWA(Damn Vulnerabilities Web Application)
DVWA(Damn Vulnerabilities Web Application)Soham Kansodaria
7K views26 slides
Bgp by
BgpBgp
BgpFebrian ‎
5.2K views93 slides
OSPF Configuration by
OSPF ConfigurationOSPF Configuration
OSPF Configuration NetProtocol Xpert
1.1K views12 slides
Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF) by
Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF)Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF)
Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF)anilinvns
3K views24 slides

What's hot(20)

OTV PPT by NETWORKERS HOME by networkershome
OTV PPT by NETWORKERS HOMEOTV PPT by NETWORKERS HOME
OTV PPT by NETWORKERS HOME
networkershome4.7K views
DVWA(Damn Vulnerabilities Web Application) by Soham Kansodaria
DVWA(Damn Vulnerabilities Web Application)DVWA(Damn Vulnerabilities Web Application)
DVWA(Damn Vulnerabilities Web Application)
Soham Kansodaria7K views
Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF) by anilinvns
Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF)Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF)
Day 3 ENHANCED IGRP (EIGRP) AND OPEN SHORTEST PATH FIRST (OSPF)
anilinvns3K views
EIGRP (Enhanced Interior Gateway Routing Protocol) by NetProtocol Xpert
EIGRP (Enhanced Interior Gateway Routing Protocol)EIGRP (Enhanced Interior Gateway Routing Protocol)
EIGRP (Enhanced Interior Gateway Routing Protocol)
NetProtocol Xpert2.2K views
Cs8591 Computer Networks - UNIT V by pkaviya
Cs8591 Computer Networks - UNIT VCs8591 Computer Networks - UNIT V
Cs8591 Computer Networks - UNIT V
pkaviya772 views
JAVA THREADS.pdf by Mohit Kumar
JAVA THREADS.pdfJAVA THREADS.pdf
JAVA THREADS.pdf
Mohit Kumar321 views
Protocolo OSPF de Múltiples áreas by Xavier Beltrán
Protocolo OSPF de Múltiples áreasProtocolo OSPF de Múltiples áreas
Protocolo OSPF de Múltiples áreas
Xavier Beltrán83 views
Browsing Linux Kernel Source by Motaz Saad
Browsing Linux Kernel SourceBrowsing Linux Kernel Source
Browsing Linux Kernel Source
Motaz Saad7K views
HTTP HOST header attacks by DefconRussia
HTTP HOST header attacksHTTP HOST header attacks
HTTP HOST header attacks
DefconRussia19.2K views
Enable DPDK and SR-IOV for containerized virtual network functions with zun by heut2008
Enable DPDK and SR-IOV for containerized virtual network functions with zunEnable DPDK and SR-IOV for containerized virtual network functions with zun
Enable DPDK and SR-IOV for containerized virtual network functions with zun
heut20082.5K views
Using BGP To Manage Dual Internet Connections by Rowell Dionicio
Using BGP To Manage Dual Internet ConnectionsUsing BGP To Manage Dual Internet Connections
Using BGP To Manage Dual Internet Connections
Rowell Dionicio31.2K views
routing basics - (static-default-dynamic) by Shanza Sohail
routing basics - (static-default-dynamic)routing basics - (static-default-dynamic)
routing basics - (static-default-dynamic)
Shanza Sohail435 views
BGP (border gateway routing protocol) by Netwax Lab
BGP (border gateway routing protocol)BGP (border gateway routing protocol)
BGP (border gateway routing protocol)
Netwax Lab860 views

Similar to Demystifying the Go Scheduler

concurrency by
concurrencyconcurrency
concurrencyJonathan Wagoner
65 views31 slides
The Windows Scheduler by
The Windows SchedulerThe Windows Scheduler
The Windows SchedulerPeter Shirley-Quirk
6K views15 slides
The Pillars Of Concurrency by
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrencyaviade
355 views19 slides
Multithreading 101 by
Multithreading 101Multithreading 101
Multithreading 101Tim Penhey
2.6K views45 slides
5 - ch3.pptx by
5 - ch3.pptx5 - ch3.pptx
5 - ch3.pptxTemp35704
7 views83 slides
Tuning Java Servers by
Tuning Java Servers Tuning Java Servers
Tuning Java Servers Srinath Perera
1.6K views49 slides

Similar to Demystifying the Go Scheduler(20)

The Pillars Of Concurrency by aviade
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrency
aviade355 views
Multithreading 101 by Tim Penhey
Multithreading 101Multithreading 101
Multithreading 101
Tim Penhey2.6K views
A Scalable I/O Manager for GHC by Johan Tibell
A Scalable I/O Manager for GHCA Scalable I/O Manager for GHC
A Scalable I/O Manager for GHC
Johan Tibell2.2K views
Slot02 concurrency1 by Viên Mai
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
Viên Mai92 views
Gophers Riding Elephants: Writing PostgreSQL tools in Go by AJ Bahnken
Gophers Riding Elephants: Writing PostgreSQL tools in GoGophers Riding Elephants: Writing PostgreSQL tools in Go
Gophers Riding Elephants: Writing PostgreSQL tools in Go
AJ Bahnken480 views
Asynchronous programming intro by cc liu
Asynchronous programming introAsynchronous programming intro
Asynchronous programming intro
cc liu6.6K views
node.js 실무 - node js in practice by Jesang Yoon by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoonnode.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoon
Jesang Yoon508 views
.NET Multithreading/Multitasking by Sasha Kravchuk
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
Sasha Kravchuk965 views
Concurrent Programming with Ruby and Tuple Spaces by luccastera
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
luccastera5.4K views
Import golang; struct microservice by Giulio De Donato
Import golang; struct microserviceImport golang; struct microservice
Import golang; struct microservice
Giulio De Donato4.2K views
Looming Marvelous - Virtual Threads in Java Javaland.pdf by jexp
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
jexp118 views
Fundamental concurrent programming by Dimas Prawira
Fundamental concurrent programmingFundamental concurrent programming
Fundamental concurrent programming
Dimas Prawira393 views
Let's Talk Locks! by C4Media
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
C4Media459 views

Recently uploaded

How Workforce Management Software Empowers SMEs | TraQSuite by
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteTraQSuite
6 views3 slides
ADDO_2022_CICID_Tom_Halpin.pdf by
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdfTomHalpin9
5 views33 slides
Dapr Unleashed: Accelerating Microservice Development by
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice DevelopmentMiroslav Janeski
15 views29 slides
What is API by
What is APIWhat is API
What is APIartembondar5
13 views15 slides
Quality Engineer: A Day in the Life by
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the LifeJohn Valentino
7 views18 slides
The Era of Large Language Models.pptx by
The Era of Large Language Models.pptxThe Era of Large Language Models.pptx
The Era of Large Language Models.pptxAbdulVahedShaik
7 views9 slides

Recently uploaded(20)

How Workforce Management Software Empowers SMEs | TraQSuite by TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuite
TraQSuite6 views
ADDO_2022_CICID_Tom_Halpin.pdf by TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski15 views
Quality Engineer: A Day in the Life by John Valentino
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the Life
John Valentino7 views
Transport Management System - Shipment & Container Tracking by Freightoscope
Transport Management System - Shipment & Container TrackingTransport Management System - Shipment & Container Tracking
Transport Management System - Shipment & Container Tracking
Freightoscope 5 views
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation by HCLSoftware
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook AutomationDRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
HCLSoftware6 views
tecnologia18.docx by nosi6702
tecnologia18.docxtecnologia18.docx
tecnologia18.docx
nosi67025 views
Understanding HTML terminology by artembondar5
Understanding HTML terminologyUnderstanding HTML terminology
Understanding HTML terminology
artembondar57 views
JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254558 views
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke35 views
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile... by Stefan Wolpers
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...
Stefan Wolpers42 views
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P... by NimaTorabi2
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
NimaTorabi216 views
Electronic AWB - Electronic Air Waybill by Freightoscope
Electronic AWB - Electronic Air Waybill Electronic AWB - Electronic Air Waybill
Electronic AWB - Electronic Air Waybill
Freightoscope 5 views
Top-5-production-devconMunich-2023-v2.pptx by Tier1 app
Top-5-production-devconMunich-2023-v2.pptxTop-5-production-devconMunich-2023-v2.pptx
Top-5-production-devconMunich-2023-v2.pptx
Tier1 app8 views
How to build dyanmic dashboards and ensure they always work by Wiiisdom
How to build dyanmic dashboards and ensure they always workHow to build dyanmic dashboards and ensure they always work
How to build dyanmic dashboards and ensure they always work
Wiiisdom14 views

Demystifying the Go Scheduler

  • 1. DEMYSTIFYING THE GO SCHEDULER MATTHEW DALE
  • 2. Go Concurrency Crash Course The atomic unit of concurrent work in Go is the goroutine Started by invoking a function with the go keyword ! Invoked function runs concurrently with the current thread of execution Goroutine synchronization is accomplished with channels Message passing construct Buffered or unbuffered
  • 3. The Go Scheduler Attempts to efficiently schedule goroutines on available resources The scheduler will pick up a new goroutine when the current goroutine… 1. Finishes 2. Makes a blocking system call E.g. reading a file 3. Makes a blocking Go runtime call E.g. reading from a channel 4. Invokes another function (only happens sometimes)* Taken from http://morsmachine.dk/go-scheduler M - Machine (OS thread) P - Context (Go scheduler) *As of Go 1.2 ( https://golang.org/doc/go1.2#preemption ) G - Goroutine
  • 4. If the current goroutine is blocked on a system call… The OS thread processing the goroutine idles waiting for the system call to return The context that was scheduling gorotuines on the blocked thread is moved to a new thread (or a new one is created if none are available) Taken from http://morsmachine.dk/go-scheduler
  • 5. HOW DOES GO ASK FOR RESOURCES ABSTRACTED BY THE OPERATING SYSTEM?
  • 6. The syscall Package All calls that can block an OS thread go through the syscall package (except cgo calls) Responsible for informing the Go runtime that a potentially blocking system call is about to happen
  • 7. src/pkg/os/file.go, line 91 src/pkg/os/file_unix.go, line 186 Let’s follow a call to file.Read…
  • 8. src/package/zsyscall_linux_amd64.go, line 831 (generated) src/pkg/syscall/asm_linux_amd64.s, line 44
  • 10. Notes on Host Setup Modify /etc/security/limits.conf Go will quickly use up the default allotment of file descriptors on most Linux distros The default Go HTTP server will panic if listener.Accept() ever returns an error, killing your service Example configuration ec2-user soft nofile 100000 ec2-user hard nofile 100000 root soft nofile 100000 root hard nofile 100000
  • 12. BUT HOW DOES THAT WORK WITH NETWORK I/O? ! WOULDN’T THAT JUST CREATE A BILLION THREADS?
  • 13. The netpoller Runs in its own thread and polls the OS’s asynchronous I/O network interface for data Goroutines asking for network I/O block waiting for the netpoller to give them data, not for a system event Go treats Unix sockets and network connection file descriptors the same, so Unix socket I/O will not block OS threads See http://morsmachine.dk/netpoller for a great, concise description of the netpoller
  • 14. RESOURCE STARVATION OR WHY DID MY GO SERVICE STOP RESPONDING?
  • 17. Which Problems Are Essential vs Accidental*? Essential A computer can do a finite amount of work per unit time If there is more work than resources, then work must be delegated, queued or dropped Accidental Sometimes you and the Go scheduler disagree about what is the most important work to do *Essential vs accidental complexity is a problem decomposition strategy proposed in Frederick Brooks Jr.’s book The Mythical Man-Month
  • 18. Simplify vs Complexify Simplify - synchronous instead of concurrent Still requires determining how to do CPU load balancing Pushes performance problems to the front, letting the caller tune on their side Complexify - refactor into micro services and add queues Lots of additional code just to solve a “simple” problem
  • 19. Think bite-sized Refactor long-running, non-blocking workloads to add more function calls The Go scheduler will sometimes preempt the running goroutine when it makes a non-inlineable function call Explicitly call runtime.Gosched Causes the current running goroutine to yield to the scheduler
  • 20. Think micro services Keep heterogeneous workloads in different Go processes One service handles quick or I/O heavy tasks One service handles long-running OS handles resource scheduling
  • 21. runtime.LockOSThread Locks the current goroutine to the current thread and doesn’t allow any other goroutines to run on that thread Call runtime.UnlockOSThread to unlock the thread.
  • 22. Extra Go Scheduler Topics There are a lot of topics relevant to the Go scheduler but beyond the scope of this presentation, including… Tailoring GOMAXPROCS for your workload How cgo calls interact with the scheduler
  • 23. Sources and Suggested Reading http://morsmachine.dk/go-scheduler http://morsmachine.dk/netpoller http://dave.cheney.net/2014/06/07/five-things-that-make- go-fast http://golang.org/pkg/runtime/ Code examples used in this presentation can be found at https://github.com/matthewdale/ GoSchedulerDemo