Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(errors/gerror): 缓存堆栈信息 #4071

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions errors/gerror/gerror_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package gerror

import (
"bytes"
"strconv"
"sync"
)

var cacheStackTrace = &stackTrace{
stackBufs: make([][]byte, 0),
pcMap: make(map[uintptr]int),
}

var bytesBufferPool = &sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}

func getBytesBuffer() *bytes.Buffer {
return bytesBufferPool.Get().(*bytes.Buffer)
}

func putBytesBuffer(b *bytes.Buffer) {
b.Reset()
bytesBufferPool.Put(b)
}

type stackObjectFlag int32

const (
// consts.StackFilterKeyForGoFrame = "github.com/gogf/gf/"
stackObjectFlagStackFilterKeyForGoFrame stackObjectFlag = 1
// stackFilterKeyLocal = "/errors/gerror/gerror"
stackObjectFlagStackFilterKeyLocal stackObjectFlag = 2
)

type stackObject struct {
// stackBufs[bufId]
stackBufId int32
flag stackObjectFlag
}

type stackTrace struct {
mu sync.RWMutex
stackBufs [][]byte
stackObjects []stackObject
// map[pc]stackBufs.idx
pcMap map[uintptr]int
//pcMap sync.Map

Check failure on line 50 in errors/gerror/gerror_cache.go

View workflow job for this annotation

GitHub Actions / golangci-lint (stable)

commentFormatting: put a space between `//` and comment text (gocritic)
}

func (st *stackTrace) addAndGetBytesPtr(pc uintptr, buf []byte, flag stackObjectFlag) *[]byte {
st.mu.Lock()
defer st.mu.Unlock()

v, ok := st.pcMap[pc]
if ok {
return &st.stackBufs[v]
}

st.stackBufs = append(st.stackBufs, buf)
id := len(st.stackBufs)
st.stackObjects = append(st.stackObjects, stackObject{
stackBufId: int32(id - 1),
flag: stackObjectFlag(flag),
})
st.pcMap[pc] = id - 1
return &st.stackBufs[id-1]
}

func (st *stackTrace) getBytesPtr(pc uintptr) (*[]byte, stackObjectFlag) {
st.mu.RLock()
defer st.mu.RUnlock()

idx, ok := st.pcMap[pc]
if ok {
flag := st.stackObjects[idx].flag
return &st.stackBufs[idx], flag
}
return nil, -1
}

func init() {
n := 50
lineIdPrefix = make([][]byte, n+1)
for i := 0; i <= n; i++ {
// 1).
// 2).
// ...
// 50).
lineIdPrefix[i] = []byte("\n\t" + strconv.Itoa(i+1) + "). ")
}

idStrs = make([]string, n+1)
for i := 0; i <= n; i++ {
idStrs[i] = strconv.Itoa(i)
}
}

func getLineIdPrefix(i int) []byte {
if i < len(lineIdPrefix) {
return lineIdPrefix[i]
}
return []byte("\n\t" + strconv.Itoa(i+1) + "). ")
}

var (
lineIdPrefix [][]byte
idStrs []string
)

func getIdStr(id int) string {
if id < len(idStrs) {
return idStrs[id]
}
return strconv.Itoa(id)
}
7 changes: 5 additions & 2 deletions errors/gerror/gerror_error_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ func (err *Error) Format(s fmt.State, verb rune) {
_, _ = io.WriteString(s, err.Error())
}
case s.Flag('+'):
var buffer = getBytesBuffer()
defer putBytesBuffer(buffer)
if verb == 's' {
_, _ = io.WriteString(s, err.Stack())
err.stackWithBuffer(buffer, "")
} else {
_, _ = io.WriteString(s, err.Error()+"\n"+err.Stack())
err.stackWithBuffer(buffer, err.Error())
}
_, _ = buffer.WriteTo(s)
default:
_, _ = io.WriteString(s, err.Error())
}
Expand Down
Loading
Loading