Skip to content

Commit 4de62a2

Browse files
author
wziww
committed
调整标题大小
1 parent 740417c commit 4de62a2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

runtime_stack.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
## Stack Dump
1+
# Stack Dump
22

33
> 引言:在工程中经常需要在异常发生的时候打印相应的日志,或利用成熟的第三方日志库,或自己进行简单的实现,在相应日志输出的时候,常常会有不同的级别,当遇到 error 或者 panic 之类的情况的时候,通常需要输出相应堆栈信息来辅助我们进行问题的定位以及解决,那么,关于堆栈这块的信息我们该怎么获取,获取的时候需要注意什么,以及堆栈的获取会对程序造成什么影响,会带来多大的性能损耗?日志库的使用或者进行相关配置我们该怎么选择?本章的目的就是深入一下 golang 中 stack dump 的细节
44
5-
### STACK APIs
5+
## STACK APIs
66
- debug.Stack
77
- debug.PrintStack
88
- runtime.Stack
99
- runtime.Callers
1010
- runtime.Caller
1111

12-
#### debug.Stack & debug.PrintStack
12+
## debug.Stack & debug.PrintStack
1313
```go
1414
package main
1515

@@ -210,7 +210,7 @@ const _TracebackMaxFrames = 100
210210
```
211211
最大帧数为固定值 100,意味着在 stack dump 时需要追溯至多 100 个栈帧的信息,O(N) 复杂度。
212212

213-
#### runtime.Caller & runtime.Callers
213+
## runtime.Caller & runtime.Callers
214214
runtime/extern.go
215215
```go
216216
// Caller reports file and line number information about function invocations on
@@ -267,7 +267,7 @@ func callers(skip int, pcbuf []uintptr) int {
267267
```
268268
`callers` 函数大部分逻辑和之前的 `runtime.Stack`函数中 `traceback` 大同小异,唯一不同的就是
269269
`gentraceback` 调用的入参 `max` 参数是人为设置的,并且进针对当前 `goroutine` 进行
270-
#### 总结
270+
## 总结
271271
- stack dump 操作是否会有性能损耗,损耗在哪儿(是
272272
- 与调用方式有关,如果是通过类似 `runtime.Stack` 方法打印所有堆栈信息的, 会触发 `STW` 操作,是一个代价比较大的操作
273273
- 与需要追溯的栈帧数量有关
@@ -278,5 +278,5 @@ func callers(skip int, pcbuf []uintptr) int {
278278
-`runtime.Callers``runtime.CallersFrames` 相结合代替 `runtime.Stack``debug.Stack`,避免使用默认的 `stack dump` 配置,根据自己的业务情况选择合适的栈帧数量(如不同级别的日志打印不同数量的栈帧信息),知名的开源日志库 `github.com/uber-go/zap` 正是使用这种思路
279279

280280
----
281-
##### 参考:
281+
### 参考:
282282
[zap stacktrace 实现](https://homes.cs.washington.edu/~bornholt/post/memory-models.html)

0 commit comments

Comments
 (0)