Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Introduction to Go 
Scheduler
Introduction o Go 
• Very easy to use lots of light weight processes 
(Go routines) in the same time. 
• Use the “go” keyw...
Why Not Use System 
Scheduler 
• Processes in an application don’t need too many 
context. 
• It’s difficult for OS to han...
What Will Include 
• Basic structures 
• The init of the scheduler 
• The init of a Go routine 
• The schedule that happen...
Source Code 
• src/pkg/runtime/proc.c 
• src/pkg/runtime/runtime.h 
• src/pkg/runtime/asm_386.s
Basic Structures 
• M: OS threads. 
• P: Context to run Go routines. 
• G: Go routine.
Basic Structures 
M0 M1 M2 
P G 
P 
G0 G 
G0 
G 
G 
G 
G
The Init of Scheduler 
M0 
G0
M0 
G0
runtime·schedinit 
M0 
P P(idle) 
G0
runtime·main 
main·main 
runtime.newproc 
runtime·mstart 
M0 
P P(idle) 
G0 
G 
main 
(run the 
scheduler to 
execute G)
Init of Another Go Routine 
• newproc 
• newproc1 
M0 
P P(idle) 
G0 
G 
main 
G
Init of Another Go Routine 
• newproc 
• newproc1 
• wakep 
• startm(nil, true) 
• newm 
• mstart 
M0 
M1 
P P 
G0 
G 
mai...
When to Schedule 
• block system call 
After Go 1.2: 
• call function 
• use a channel
System Call 
• .entersyscallblock 
M0 
M1 
P P 
G0 
G 
main 
G0 
G(sysc 
all) 
G
System Call 
• .entersyscallblock 
• handoffp M0 
P 
G0 
G 
main 
M1 
G(sysc 
all) 
M2 
P(idle) 
G0 
G
System Call 
• .entersyscallblock 
• handoffp 
• startm(p 
M0 
P 
G0 
G 
main 
M1 
G(sysc 
all) 
M2 
P 
G0 
G
How to Change Current Go 
Routine
A Demo
A Demo 
Run with flags: 
• GODEBUG=schedtrace=1000,scheddetail=1 
• GOMAXPROCS=4
Q & A
Upcoming SlideShare
Loading in …5
×

Introduction to Go scheduler

2,305 views

Published on

Introduction to Golang Scheduler

Published in: Technology
  • Be the first to comment

Introduction to Go scheduler

  1. 1. Introduction to Go Scheduler
  2. 2. Introduction o Go • Very easy to use lots of light weight processes (Go routines) in the same time. • Use the “go” keyword.
  3. 3. Why Not Use System Scheduler • Processes in an application don’t need too many context. • It’s difficult for OS to handle too many threads or processes. • System scheduler is too overhead.
  4. 4. What Will Include • Basic structures • The init of the scheduler • The init of a Go routine • The schedule that happens in the system call • How to change current running Go routine
  5. 5. Source Code • src/pkg/runtime/proc.c • src/pkg/runtime/runtime.h • src/pkg/runtime/asm_386.s
  6. 6. Basic Structures • M: OS threads. • P: Context to run Go routines. • G: Go routine.
  7. 7. Basic Structures M0 M1 M2 P G P G0 G G0 G G G G
  8. 8. The Init of Scheduler M0 G0
  9. 9. M0 G0
  10. 10. runtime·schedinit M0 P P(idle) G0
  11. 11. runtime·main main·main runtime.newproc runtime·mstart M0 P P(idle) G0 G main (run the scheduler to execute G)
  12. 12. Init of Another Go Routine • newproc • newproc1 M0 P P(idle) G0 G main G
  13. 13. Init of Another Go Routine • newproc • newproc1 • wakep • startm(nil, true) • newm • mstart M0 M1 P P G0 G main G0 G
  14. 14. When to Schedule • block system call After Go 1.2: • call function • use a channel
  15. 15. System Call • .entersyscallblock M0 M1 P P G0 G main G0 G(sysc all) G
  16. 16. System Call • .entersyscallblock • handoffp M0 P G0 G main M1 G(sysc all) M2 P(idle) G0 G
  17. 17. System Call • .entersyscallblock • handoffp • startm(p M0 P G0 G main M1 G(sysc all) M2 P G0 G
  18. 18. How to Change Current Go Routine
  19. 19. A Demo
  20. 20. A Demo Run with flags: • GODEBUG=schedtrace=1000,scheddetail=1 • GOMAXPROCS=4
  21. 21. Q & A

×