Share
Embed
Transcript
63 slides
-
2 Who Am I? • Software Engineer at Rookout •
Go, Java, Ruby • C#, Python, JavaScript, C++ • Amateur sewist -
What is Stack? • Section of memory ◦ One stack
per thread • Works like a stack (LIFO) • Stores function parameters, local variables, return addresses 3 -
What is Stack? 4 … func runFirst() { a :=
1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } Stack -
What is Stack? 5 … 1 a Local variables Stack
func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? 6 … 1 a Local variables Stack
1 b Function parameters 2 c func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? 7 … 1 a Local variables Stack
1 b Function parameters 2 c 0x12345 Return address func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? 8 … 1 a Local variables Stack
1 b Function parameters 2 c 0x12345 Return address func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } 0 d Local variables -
What is Stack? 9 … 1 a Local variables Stack
1 b Function parameters 2 c 0x12345 Return address 0 d Local variables func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? 10 … 1 a Local variables Stack
1 b Function parameters 2 c 0x12345 Return address func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? • Grows down - last thing we
pushed onto the stack will be at the lowest address • RSP - “variable” that holds the last address of the stack 11 -
What is Stack? 12 … 1 0x1238 Local variables Stack
1 0x1230 Function parameters 2 0x1228 0x12345 func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } Return address 0x1220 -
What is Stack? 13 … 1 0x1238 Local variables Stack
1 0x1230 Function parameters 2 0x1228 0x12345 func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } Return address 0x1220 rsp -
What is Stack? 14 … 1 0x1238 Local variables Stack
1 0x1230 Function parameters 2 0x1228 rsp Bigger rsp → smaller stack -
What’s So Special About Go? 15 • Threads ≠ Goroutines
• Threads - 1-10MB • Goroutines - 4K -
When Is morestack Called? 17 • The compiler adds a
prologue and epilogue to every function • Prologue checks if more stack is needed • Epilogue calls `morestack` -
needsMorestack() 19 • Not a real function • A condition
that compares: ◦ Available stack ◦ Stack in use • How does that look like? -
g struct 20 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... rsp: 0x1090 -
g struct 21 Stack 0x1000 0x1008 0x1090 0x1088 0x1080 0x1010
... x x x rsp: 0x1078 g.stack.lo g.stack.hi -
g struct 22 Stack 0x1000 0x1008 0x1090 0x1088 0x1080 0x1010
... x x x x x x x x x x x x x x x rsp: 0x1010 x g.stack.lo g.stack.hi -
g struct 23 Stack 0x1000 0x1008 0x1090 0x1088 0x1080 0x1010
... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x -
g struct 24 Stack 0x1000 0x1008 0x1090 0x1088 0x1080 0x1010
... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x -
`morestack` needs 0x10 stack 26 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 27 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 28 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 29 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 30 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 31 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 32 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 33 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 34 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 35 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 36 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0x1000 x g.stack.lo g.stack.hi x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 37 Stack 0x1000 0x1008 0x1090 0x1088
0x1080 0x1010 ... x x x x x x x x x x x x x x x rsp: 0xff0 x g.stack.lo g.stack.hi x x x x 0xff8 0xff0 -
`morestack` needs 0x10 stack 38 38 Stack g.stack.lo g.stack.hi 0x1000
0x1008 0x1090 0x1088 0x1080 0x1010 ... rsp: 0x1090 g.stack.lo+0x10 -
`morestack` needs 0x10 stack 39 Stack g.stack.lo g.stack.hi 0x1000 0x1008
0x1088 0x1080 0x1010 ... rsp: 0x1010 x x x x x x x x x x x x x x x x g.stack.lo+0x10 -
`morestack` needs 0x10 stack 40 Stack g.stack.lo g.stack.hi 0x1000 0x1008
0x1088 0x1080 0x1010 ... rsp: 0x1010 x x x x x x x x x x x x x x x x g.stack.lo+0x10 -
`morestack` needs 0x10 stack 41 Stack g.stack.lo g.stack.hi 0x1000 0x1008
0x1088 0x1080 0x1010 ... rsp: 0x1010 x x x x x x x x x x x x x x x x g.stack.lo+0x10 -
`morestack` needs 0x10 stack 42 Stack g.stack.lo g.stack.hi 0x1000 0x1008
0x1088 0x1080 0x1010 ... rsp: 0x1000 x x x x x x x x x x x x x x x x g.stack.lo+0x10 x x -
Stackguard 44 • Used to check if we have enough
stack • Always between stack.lo and stack.hi • Will leave enough memory for calls to functions like `morestack` -
Stack Usage 48 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1088 0x1080
0x1010 ... rsp: 0x1020 x x x x x x x x x x x x x x g.stackguard0 -
Stack Usage 49 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1088 0x1080
0x1010 ... rsp: 0x1020 x x x x x x x x x x x x x x g.stackguard0 -
Stack Usage 50 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1088 0x1080
0x1010 ... rsp: 0x1020 x x x x x x x x x x x x x x g.stackguard0 -
Stack Usage 51 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1088 0x1080
0x1010 ... rsp: 0xf20 x x x x x x x x x x x x x x g.stackguard0 x x x x x x x x x x x x x x x x -
What is Stack? 52 … func runFirst() { a :=
1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } Stack -
What is Stack? 53 … 1 a Local variables Stack
func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? 54 … 1 a Local variables Stack
1 b Function parameters 2 c func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? 55 … 1 a Local variables Stack
1 b Function parameters 2 c func runFirst() { a := 1 runSecond(a, 2) return } func runSecond(b, c int) { var d int ... } -
What is Stack? 56 … 1 a Local variables Stack
1 b Function parameters 2 c func runFirst() { a := 1 if shouldRunSecond { runSecond(a, 2) } return } -
Stack Usage 57 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1088 0x1080
0x1010 ... rsp: 0x1020 x x x x x x x x x x x x x x g.stackguard0 -
Stack Usage 58 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1088 0x1080
0x1010 ... rsp: 0x1020 x x x x x x x x x x x x x x g.stackguard0 -
Stack Usage 59 Stack g.stack.lo g.stack.hi 0x1000 0x1008 0x1088 0x1080
0x1010 ... rsp: 0x1020 x x x x x x x x x x x x x x g.stackguard0 -
Stack Shrink • `shrinkstack` • Called by GC • Shrink
stack if less than a quarter is being used 62