SlideShare a Scribd company logo
1 of 24
Download to read offline
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?

More Related Content

What's hot

関数型・オブジェクト指向 宗教戦争に疲れたなたに送るGo言語入門
関数型・オブジェクト指向宗教戦争に疲れたなたに送るGo言語入門関数型・オブジェクト指向宗教戦争に疲れたなたに送るGo言語入門
関数型・オブジェクト指向 宗教戦争に疲れたなたに送るGo言語入門Tadahiro Ishisaka
 
CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善
CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善
CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善Ito Takayuki
 
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例UnityTechnologiesJapan002
 
NextGen Server/Client Architecture - gRPC + Unity + C#
NextGen Server/Client Architecture - gRPC + Unity + C#NextGen Server/Client Architecture - gRPC + Unity + C#
NextGen Server/Client Architecture - gRPC + Unity + C#Yoshifumi Kawai
 
大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装kidach1
 
30分でわかるマイクロサービスアーキテクチャ 第2版
30分でわかるマイクロサービスアーキテクチャ 第2版30分でわかるマイクロサービスアーキテクチャ 第2版
30分でわかるマイクロサービスアーキテクチャ 第2版Naoki (Neo) SATO
 
ソフトウェア設計のすすめ
ソフトウェア設計のすすめソフトウェア設計のすすめ
ソフトウェア設計のすすめYoshimura Soichiro
 
A Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in DepthA Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in DepthYoshifumi Kawai
 
WebRTC と Native とそれから、それから。
WebRTC と Native とそれから、それから。 WebRTC と Native とそれから、それから。
WebRTC と Native とそれから、それから。 tnoho
 
ドメイン駆動設計の正しい歩き方
ドメイン駆動設計の正しい歩き方ドメイン駆動設計の正しい歩き方
ドメイン駆動設計の正しい歩き方増田 亨
 
[KubeCon NA 2020] containerd: Rootless Containers 2020
[KubeCon NA 2020] containerd: Rootless Containers 2020[KubeCon NA 2020] containerd: Rootless Containers 2020
[KubeCon NA 2020] containerd: Rootless Containers 2020Akihiro Suda
 
Observableで非同期処理
Observableで非同期処理Observableで非同期処理
Observableで非同期処理torisoup
 
DSIRNLP #3 LZ4 の速さの秘密に迫ってみる
DSIRNLP #3 LZ4 の速さの秘密に迫ってみるDSIRNLP #3 LZ4 の速さの秘密に迫ってみる
DSIRNLP #3 LZ4 の速さの秘密に迫ってみるAtsushi KOMIYA
 
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPFA Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPFoholiab
 
Raspberry PiのUSB OTGを試す
Raspberry PiのUSB OTGを試すRaspberry PiのUSB OTGを試す
Raspberry PiのUSB OTGを試すKenichiro MATOHARA
 
Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)
Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)
Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)Hiro H.
 
Spanner移行について本気出して考えてみた
Spanner移行について本気出して考えてみたSpanner移行について本気出して考えてみた
Spanner移行について本気出して考えてみたtechgamecollege
 
初心者向けMongoDBのキホン!
初心者向けMongoDBのキホン!初心者向けMongoDBのキホン!
初心者向けMongoDBのキホン!Tetsutaro Watanabe
 
第27回ロボティクスシンポジアスライド
第27回ロボティクスシンポジアスライド第27回ロボティクスシンポジアスライド
第27回ロボティクスシンポジアスライドRyuichi Ueda
 
Web開発者が始める .NET MAUI Blazor App
Web開発者が始める .NET MAUI Blazor AppWeb開発者が始める .NET MAUI Blazor App
Web開発者が始める .NET MAUI Blazor AppTomomitsuKusaba
 

What's hot (20)

関数型・オブジェクト指向 宗教戦争に疲れたなたに送るGo言語入門
関数型・オブジェクト指向宗教戦争に疲れたなたに送るGo言語入門関数型・オブジェクト指向宗教戦争に疲れたなたに送るGo言語入門
関数型・オブジェクト指向 宗教戦争に疲れたなたに送るGo言語入門
 
CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善
CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善
CircleCIのinfrastructureを支えるTerraformのCI/CDパイプラインの改善
 
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例
 
NextGen Server/Client Architecture - gRPC + Unity + C#
NextGen Server/Client Architecture - gRPC + Unity + C#NextGen Server/Client Architecture - gRPC + Unity + C#
NextGen Server/Client Architecture - gRPC + Unity + C#
 
大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装
 
30分でわかるマイクロサービスアーキテクチャ 第2版
30分でわかるマイクロサービスアーキテクチャ 第2版30分でわかるマイクロサービスアーキテクチャ 第2版
30分でわかるマイクロサービスアーキテクチャ 第2版
 
ソフトウェア設計のすすめ
ソフトウェア設計のすすめソフトウェア設計のすすめ
ソフトウェア設計のすすめ
 
A Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in DepthA Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in Depth
 
WebRTC と Native とそれから、それから。
WebRTC と Native とそれから、それから。 WebRTC と Native とそれから、それから。
WebRTC と Native とそれから、それから。
 
ドメイン駆動設計の正しい歩き方
ドメイン駆動設計の正しい歩き方ドメイン駆動設計の正しい歩き方
ドメイン駆動設計の正しい歩き方
 
[KubeCon NA 2020] containerd: Rootless Containers 2020
[KubeCon NA 2020] containerd: Rootless Containers 2020[KubeCon NA 2020] containerd: Rootless Containers 2020
[KubeCon NA 2020] containerd: Rootless Containers 2020
 
Observableで非同期処理
Observableで非同期処理Observableで非同期処理
Observableで非同期処理
 
DSIRNLP #3 LZ4 の速さの秘密に迫ってみる
DSIRNLP #3 LZ4 の速さの秘密に迫ってみるDSIRNLP #3 LZ4 の速さの秘密に迫ってみる
DSIRNLP #3 LZ4 の速さの秘密に迫ってみる
 
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPFA Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
 
Raspberry PiのUSB OTGを試す
Raspberry PiのUSB OTGを試すRaspberry PiのUSB OTGを試す
Raspberry PiのUSB OTGを試す
 
Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)
Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)
Linuxにて複数のコマンドを並列実行(同時実行数の制限付き)
 
Spanner移行について本気出して考えてみた
Spanner移行について本気出して考えてみたSpanner移行について本気出して考えてみた
Spanner移行について本気出して考えてみた
 
初心者向けMongoDBのキホン!
初心者向けMongoDBのキホン!初心者向けMongoDBのキホン!
初心者向けMongoDBのキホン!
 
第27回ロボティクスシンポジアスライド
第27回ロボティクスシンポジアスライド第27回ロボティクスシンポジアスライド
第27回ロボティクスシンポジアスライド
 
Web開発者が始める .NET MAUI Blazor App
Web開発者が始める .NET MAUI Blazor AppWeb開発者が始める .NET MAUI Blazor App
Web開発者が始める .NET MAUI Blazor App
 

Similar to Demystifying the Go Scheduler

Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrencyaviade
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101Tim Penhey
 
5 - ch3.pptx
5 - ch3.pptx5 - ch3.pptx
5 - ch3.pptxTemp35704
 
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncationLM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncationMani Deepak Choudhry
 
A Scalable I/O Manager for GHC
A Scalable I/O Manager for GHCA Scalable I/O Manager for GHC
A Scalable I/O Manager for GHCJohan Tibell
 
Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1Viên Mai
 
Gophers Riding Elephants: Writing PostgreSQL tools in Go
Gophers Riding Elephants: Writing PostgreSQL tools in GoGophers Riding Elephants: Writing PostgreSQL tools in Go
Gophers Riding Elephants: Writing PostgreSQL tools in GoAJ Bahnken
 
Asynchronous programming intro
Asynchronous programming introAsynchronous programming intro
Asynchronous programming introcc liu
 
node.js 실무 - node js in practice 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 YoonJesang Yoon
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spacesluccastera
 
Import golang; struct microservice
Import golang; struct microserviceImport golang; struct microservice
Import golang; struct microserviceGiulio De Donato
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Akshay Nagpurkar
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Akshay Nagpurkar
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfjexp
 

Similar to Demystifying the Go Scheduler (20)

Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
concurrency
concurrencyconcurrency
concurrency
 
The Windows Scheduler
The Windows SchedulerThe Windows Scheduler
The Windows Scheduler
 
The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrency
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
5 - ch3.pptx
5 - ch3.pptx5 - ch3.pptx
5 - ch3.pptx
 
Tuning Java Servers
Tuning Java Servers Tuning Java Servers
Tuning Java Servers
 
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncationLM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
 
A Scalable I/O Manager for GHC
A Scalable I/O Manager for GHCA Scalable I/O Manager for GHC
A Scalable I/O Manager for GHC
 
Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
 
Gophers Riding Elephants: Writing PostgreSQL tools in Go
Gophers Riding Elephants: Writing PostgreSQL tools in GoGophers Riding Elephants: Writing PostgreSQL tools in Go
Gophers Riding Elephants: Writing PostgreSQL tools in Go
 
Asynchronous programming intro
Asynchronous programming introAsynchronous programming intro
Asynchronous programming intro
 
node.js 실무 - node js in practice 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
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
 
Import golang; struct microservice
Import golang; struct microserviceImport golang; struct microservice
Import golang; struct microservice
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
 
MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
 

Recently uploaded

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

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