Introduction to Go scheduler
-
Upload
bin-wang -
Category
Technology
-
view
431 -
download
4
Transcript of Introduction to Go scheduler
![Page 1: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/1.jpg)
Introduction to Go Scheduler
![Page 2: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/2.jpg)
Introduction o Go• Very easy to use lots of light weight
processes (Go routines) in the same time.
• Use the “go” keyword.
![Page 3: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/3.jpg)
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.
![Page 4: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/4.jpg)
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
![Page 5: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/5.jpg)
Source Code
• src/pkg/runtime/proc.c
• src/pkg/runtime/runtime.h
• src/pkg/runtime/asm_386.s
![Page 6: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/6.jpg)
Basic Structures
• M: OS threads.
• P: Context to run Go routines.
• G: Go routine.
![Page 7: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/7.jpg)
Basic StructuresM0 M1 M2
P P
G0 G0
G
G
G
G
G
G
![Page 8: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/8.jpg)
The Init of Scheduler
M0
G0
![Page 9: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/9.jpg)
M0
G0
![Page 10: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/10.jpg)
M0
G0
P P(idle)
runtime·schedinit
![Page 11: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/11.jpg)
runtime·mainmain·main
runtime.newproc
M0
G0
P P(idle)G
main
runtime·mstart(run the
scheduler to execute G)
![Page 12: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/12.jpg)
Init of Another Go Routine
• newproc
• newproc1
M0
G0
P P(idle)G
main
G
![Page 13: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/13.jpg)
Init of Another Go Routine
• newproc
• newproc1
• wakep
• startm(nil, true)
• newm
• mstart
M0
G0
P PG
main
G0
M1
G
![Page 14: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/14.jpg)
When to Schedule
• block system call
After Go 1.2:
• call function
• use a channel
![Page 15: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/15.jpg)
System Call• .entersyscallblock
M0
G0
P PG
main
G0
M1
G(syscall)
G
![Page 16: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/16.jpg)
System Call• .entersyscallblock
• handoffp M0
G0
PG
main
M1
G(syscall)
P(idle)
G0
M2
G
![Page 17: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/17.jpg)
System Call• .entersyscallblock
• handoffp
• startm(p
M0
G0
PG
main
M1
G(syscall)
P
G0
M2
G
![Page 18: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/18.jpg)
How to Change Current Go Routine
![Page 19: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/19.jpg)
A Demo
![Page 20: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/20.jpg)
A Demo
Run with flags:
• GODEBUG=schedtrace=1000,scheddetail=1
• GOMAXPROCS=4
![Page 21: Introduction to Go scheduler](https://reader033.fdocuments.in/reader033/viewer/2022042608/55d54f3fbb61ebb6488b45dc/html5/thumbnails/21.jpg)
Q & A