Skip to content

Commit a619218

Browse files
committed
Callers now go version independent, add poolBuf() and putPoolBuf() funcs
1 parent 7ee94cd commit a619218

File tree

3 files changed

+28
-44
lines changed

3 files changed

+28
-44
lines changed

stack/stack.go

+14
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ func (pc Call) Format(s fmt.State, c rune) {
112112
}
113113
}
114114

115+
// Callers returns a Trace for the current goroutine with element 0
116+
// identifying the calling function.
117+
func Callers() Trace {
118+
pcs := poolBuf()
119+
pcs = pcs[:cap(pcs)]
120+
n := runtime.Callers(2, pcs)
121+
cs := make([]Call, n)
122+
for i, pc := range pcs[:n] {
123+
cs[i] = Call(pc)
124+
}
125+
putPoolBuf(pcs)
126+
return cs
127+
}
128+
115129
// name returns the import path qualified name of the function containing the
116130
// call.
117131
func (pc Call) name() string {

stack/stack_pool.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@ var pcStackPool = sync.Pool{
1010
New: func() interface{} { return make([]uintptr, 1000) },
1111
}
1212

13-
// Callers returns a Trace for the current goroutine with element 0
14-
// identifying the calling function.
15-
func Callers() Trace {
16-
pcs := pcStackPool.Get().([]uintptr)
17-
pcs = pcs[:cap(pcs)]
18-
n := runtime.Callers(2, pcs)
19-
cs := make([]Call, n)
20-
for i, pc := range pcs[:n] {
21-
cs[i] = Call(pc)
22-
}
23-
pcStackPool.Put(pcs)
24-
return cs
13+
func poolBuf() []uintptr {
14+
return pcStackPool.Get().([]uintptr)
15+
}
16+
17+
func putPoolBuf(p []uintptr) {
18+
pcStackPool.Put(p)
2519
}

stack/stack_pool_chan.go

+8-32
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,26 @@
22

33
package stack
44

5-
import (
6-
"runtime"
7-
)
8-
95
const (
106
stackPoolSize = 64
117
)
128

13-
type stackPool struct {
14-
c chan []uintptr
15-
}
16-
17-
func newStackPool() *stackPool {
18-
return &stackPool{c: make(chan []uintptr, stackPoolSize)}
19-
}
9+
var (
10+
pcStackPool = make(chan []uintptr, stackPoolSize)
11+
)
2012

21-
func (p *stackPool) Get() []uintptr {
13+
func poolBuf() []uintptr {
2214
select {
23-
case st := <-p.c:
24-
return st
15+
case p := <-pcStackPool:
16+
return p
2517
default:
2618
return make([]uintptr, 1000)
2719
}
2820
}
2921

30-
func (p *stackPool) Put(st []uintptr) {
22+
func putPoolBuf(p []uintptr) {
3123
select {
32-
case p.c <- st:
24+
case pcStackPool <- p:
3325
default:
3426
}
3527
}
36-
37-
var pcStackPool = newStackPool()
38-
39-
// Callers returns a Trace for the current goroutine with element 0
40-
// identifying the calling function.
41-
func Callers() Trace {
42-
pcs := pcStackPool.Get()
43-
pcs = pcs[:cap(pcs)]
44-
n := runtime.Callers(2, pcs)
45-
cs := make([]Call, n)
46-
for i, pc := range pcs[:n] {
47-
cs[i] = Call(pc)
48-
}
49-
pcStackPool.Put(pcs)
50-
return cs
51-
}

0 commit comments

Comments
 (0)