Skip to content

Commit

Permalink
Merge pull request #32 from wongak/stack_build_tag
Browse files Browse the repository at this point in the history
add build tag for Go 1.3+. See #31
  • Loading branch information
inconshreveable committed Nov 6, 2014
2 parents 35e462c + a619218 commit 7a18aae
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: go

go:
- 1.0
- 1.1
- 1.2
- 1.3
- release
- tip
33 changes: 14 additions & 19 deletions stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
)

// Call records a single function invocation from a goroutine stack. It is a
Expand Down Expand Up @@ -113,6 +112,20 @@ func (pc Call) Format(s fmt.State, c rune) {
}
}

// Callers returns a Trace for the current goroutine with element 0
// identifying the calling function.
func Callers() Trace {
pcs := poolBuf()
pcs = pcs[:cap(pcs)]
n := runtime.Callers(2, pcs)
cs := make([]Call, n)
for i, pc := range pcs[:n] {
cs[i] = Call(pc)
}
putPoolBuf(pcs)
return cs
}

// name returns the import path qualified name of the function containing the
// call.
func (pc Call) name() string {
Expand Down Expand Up @@ -151,24 +164,6 @@ func (pcs Trace) Format(s fmt.State, c rune) {
s.Write([]byte("]"))
}

var pcStackPool = sync.Pool{
New: func() interface{} { return make([]uintptr, 1000) },
}

// Callers returns a Trace for the current goroutine with element 0
// identifying the calling function.
func Callers() Trace {
pcs := pcStackPool.Get().([]uintptr)
pcs = pcs[:cap(pcs)]
n := runtime.Callers(2, pcs)
cs := make([]Call, n)
for i, pc := range pcs[:n] {
cs[i] = Call(pc)
}
pcStackPool.Put(pcs)
return cs
}

// TrimBelow returns a slice of the Trace with all entries below pc removed.
func (pcs Trace) TrimBelow(pc Call) Trace {
for len(pcs) > 0 && pcs[0] != pc {
Expand Down
19 changes: 19 additions & 0 deletions stack/stack_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build go1.3

package stack

import (
"sync"
)

var pcStackPool = sync.Pool{
New: func() interface{} { return make([]uintptr, 1000) },
}

func poolBuf() []uintptr {
return pcStackPool.Get().([]uintptr)
}

func putPoolBuf(p []uintptr) {
pcStackPool.Put(p)
}
27 changes: 27 additions & 0 deletions stack/stack_pool_chan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build !go1.3

package stack

const (
stackPoolSize = 64
)

var (
pcStackPool = make(chan []uintptr, stackPoolSize)
)

func poolBuf() []uintptr {
select {
case p := <-pcStackPool:
return p
default:
return make([]uintptr, 1000)
}
}

func putPoolBuf(p []uintptr) {
select {
case pcStackPool <- p:
default:
}
}

0 comments on commit 7a18aae

Please sign in to comment.