+ All Categories
Home > Technology > Introduction to Go scheduler

Introduction to Go scheduler

Date post: 20-Aug-2015
Category:
Upload: bin-wang
View: 429 times
Download: 4 times
Share this document with a friend
Popular Tags:
21
Introduction to Go Scheduler
Transcript
Page 1: Introduction to Go scheduler

Introduction to Go Scheduler

Page 2: 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” keyword.

Page 3: Introduction to Go scheduler

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

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

Source Code

• src/pkg/runtime/proc.c

• src/pkg/runtime/runtime.h

• src/pkg/runtime/asm_386.s

Page 6: Introduction to Go scheduler

Basic Structures

• M: OS threads.

• P: Context to run Go routines.

• G: Go routine.

Page 7: Introduction to Go scheduler

Basic StructuresM0 M1 M2

P P

G0 G0

G

G

G

G

G

G

Page 8: Introduction to Go scheduler

The Init of Scheduler

M0

G0

Page 9: Introduction to Go scheduler

M0

G0

Page 10: Introduction to Go scheduler

M0

G0

P P(idle)

runtime·schedinit

Page 11: Introduction to Go scheduler

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

Init of Another Go Routine

• newproc

• newproc1

M0

G0

P P(idle)G

main

G

Page 13: Introduction to Go scheduler

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

When to Schedule

• block system call

After Go 1.2:

• call function

• use a channel

Page 15: Introduction to Go scheduler

System Call• .entersyscallblock

M0

G0

P PG

main

G0

M1

G(syscall)

G

Page 16: Introduction to Go scheduler

System Call• .entersyscallblock

• handoffp M0

G0

PG

main

M1

G(syscall)

P(idle)

G0

M2

G

Page 17: Introduction to Go scheduler

System Call• .entersyscallblock

• handoffp

• startm(p

M0

G0

PG

main

M1

G(syscall)

P

G0

M2

G

Page 18: Introduction to Go scheduler

How to Change Current Go Routine

Page 19: Introduction to Go scheduler

A Demo

Page 20: Introduction to Go scheduler

A Demo

Run with flags:

• GODEBUG=schedtrace=1000,scheddetail=1

• GOMAXPROCS=4

Page 21: Introduction to Go scheduler

Q & A


Recommended