Mais conteúdo relacionado Semelhante a Why Go Scales (20) Why Go Scales21. var mysql = require('mysql');
var con = mysql.createConnection({
...
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
... //some more code
22. var mysql = require('mysql');
var con = mysql.createConnection({
...
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
... //some more code
1
2
3
4
5
6
37. go build -gcflags '-N -l'
go tool objdump -s stacksize.Add goway
TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:20 0x1087050 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x1087055 488b4c2408 MOVQ 0x8(SP), CX
stack.go:20 0x108705a 4801c8 ADDQ CX, AX
stack.go:20 0x108705d 4889442418 MOVQ AX, 0x18(SP)
stack.go:20 0x1087062 c3 RET
38. go build -gcflags '-N -l'
go tool objdump -s stacksize.Add goway
TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:20 0x1087050 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x1087055 488b4c2408 MOVQ 0x8(SP), CX
stack.go:20 0x108705a 4801c8 ADDQ CX, AX
stack.go:20 0x108705d 4889442418 MOVQ AX, 0x18(SP)
stack.go:20 0x1087062 c3 RET
func Add(a, b int) int {
return a + b
}
39. go build -gcflags '-N -l'
go tool objdump -s stacksize.Add goway
TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:20 0x1087050 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x1087055 488b4c2408 MOVQ 0x8(SP), CX
stack.go:20 0x108705a 4801c8 ADDQ CX, AX
stack.go:20 0x108705d 4889442418 MOVQ AX, 0x18(SP)
stack.go:20 0x1087062 c3 RET
func Add(a, b int) int {
return a + b
}
40. go build -gcflags '-N -l'
go tool objdump -s stacksize.Add goway
TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:20 0x1087050 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x1087055 488b4c2408 MOVQ 0x8(SP), CX
stack.go:20 0x108705a 4801c8 ADDQ CX, AX
stack.go:20 0x108705d 4889442418 MOVQ AX, 0x18(SP)
stack.go:20 0x1087062 c3 RET
func Add(a, b int) int {
return a + b
}
41. go build -gcflags '-N -l'
go tool objdump -s stacksize.Add goway
TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:20 0x1087050 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x1087055 488b4c2408 MOVQ 0x8(SP), CX
stack.go:20 0x108705a 4801c8 ADDQ CX, AX
stack.go:20 0x108705d 4889442418 MOVQ AX, 0x18(SP)
stack.go:20 0x1087062 c3 RET
func Add(a, b int) int {
return a + b
}
42. go build -gcflags '-N -l'
go tool objdump -s stacksize.Add goway
TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:20 0x1087050 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x1087055 488b4c2408 MOVQ 0x8(SP), CX
stack.go:20 0x108705a 4801c8 ADDQ CX, AX
stack.go:20 0x108705d 4889442418 MOVQ AX, 0x18(SP)
stack.go:20 0x1087062 c3 RET
func Add(a, b int) int {
return a + b
}
44. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
45. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
46. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
47. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
48. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
49. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
50. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
51. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
52. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
53. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
56. func Add(a, b int) int {
var bytes [100]byte
return a + b + int(bytes[0])
}
57. func Add(a, b int) int {
var bytes [105]byte
return a + b + int(bytes[0])
}
61. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
Main
goroutine
62. func TestScheduler() {
numOfGoRoutines := 2
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
Main
goroutine
Create several goroutinesgoroutine
63. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
Main
goroutine
goroutine
Main signals goroutines to stop
64. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for !done {
progress[id]++
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
done = true
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
Main
goroutine
goroutine
While not done: progress++
65. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
Main
goroutine
goroutine
How much progress will each
goroutine make before it stopped?
66. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
Main
goroutine
goroutine
Are they scheduled equally?
68. Go routine started: 1
Go routine started: 0
All done. Progress: [576524 578794]
Total Time 4.84726ms
69. Go routine started: 2
Go routine started: 1
Go routine started: 0
All done. Progress: [1281232 1537252 711224]
Total Time 19.286207ms
78. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
No switching point
79. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
time.Sleep(0)
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
80. Go routine started: 3
Go routine started: 2
Go routine started: 1
Go routine started: 0
All done. Progress: [782164 773498 742771 794478]
Total Time 42.244354ms
81. func TestScheduler() {
numOfGoRoutines := 1
progress := make([]int, numOfGoRoutines)
done := int64(0)
goRoutineStarted := sync.WaitGroup{}
goRoutineStarted.Add(numOfGoRoutines)
goRoutineFinished := sync.WaitGroup{}
goRoutineFinished.Add(numOfGoRoutines)
for i := 0; i < numOfGoRoutines; i++ {
go func(id int) {
fmt.Println("Go routine started:", id)
goRoutineStarted.Done()
for atomic.LoadInt64(&done) == 0 {
progress[id]++
doSomething()
}
goRoutineFinished.Done()
}(i)
}
goRoutineStarted.Wait()
atomic.StoreInt64(&done, 1)
goRoutineFinished.Wait()
fmt.Println("All done. Progress: ", progress)
}
func doSomething() {
doNothing()
}
func doNothing() {
}
84. TEXT goway/stacksize.Add(SB) /Users/eyalp/gocode/src/goway/stacksize/stack.go
stack.go:18 0x1087050 65488b0c25a0080000 GS MOVQ GS:0x8a0, CX
stack.go:18 0x1087059 483b6110 CMPQ 0x10(CX), SP
stack.go:18 0x108705d 7635 JBE 0x1087094
stack.go:18 0x108705f 4883ec08 SUBQ $0x8, SP
stack.go:18 0x1087063 48892c24 MOVQ BP, 0(SP)
stack.go:18 0x1087067 488d2c24 LEAQ 0(SP), BP
stack.go:18 0x108706b 48c744242000000000 MOVQ $0x0, 0x20(SP)
stack.go:19 0x1087074 e827000000 CALL goway/stacksize.log(SB)
stack.go:20 0x1087079 488b442410 MOVQ 0x10(SP), AX
stack.go:20 0x108707e 488b4c2418 MOVQ 0x18(SP), CX
stack.go:20 0x1087083 4801c8 ADDQ CX, AX
stack.go:20 0x1087086 4889442420 MOVQ AX, 0x20(SP)
stack.go:20 0x108708b 488b2c24 MOVQ 0(SP), BP
stack.go:20 0x108708f 4883c408 ADDQ $0x8, SP
stack.go:20 0x1087093 c3 RET
stack.go:18 0x1087094 e8872efcff CALL runtime.morestack_noctxt(SB)
stack.go:18 0x1087099 ebb5 JMP goway/stacksize.Add(SB)
94. File I/ONetwork I/O
Thread Poolepoll kqueue IOCP
File I/O
Syscalls
Network I/O
Thread Cache
Netpoller
epoll kqueue IOCP
Node.JS Go