github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xruntime/rt.go (about) 1 package xruntime 2 3 import ( 4 "fmt" 5 "runtime" 6 "runtime/debug" 7 "strings" 8 "sync" 9 ) 10 11 // CallerName return current caller name 12 func CallerName() string { 13 pc, _, _, ok := runtime.Caller(1) 14 if !ok { 15 return "???" 16 } 17 name := runtime.FuncForPC(pc).Name() 18 items := strings.Split(name, "/") 19 if len(items) > 0 { 20 return items[len(items)-1] 21 } 22 return name 23 } 24 25 func Caller() string { 26 _, file, line, ok := runtime.Caller(1) 27 if !ok { 28 return "???" 29 } 30 return fmt.Sprintf("%s:%d", file, line) 31 } 32 33 func BuildPath() string { 34 info, ok := debug.ReadBuildInfo() 35 if !ok { 36 return "" 37 } 38 return info.Path 39 } 40 41 // Stack get stacktrace with pretty format. 42 func Stack(pretty bool) []byte { 43 stack := debug.Stack() 44 // todo: clean no need data and format it. 45 return stack 46 } 47 48 var gogc int 49 var gogcOnce sync.Once 50 51 // SetGoGc sets the garbage collection target percentage 52 // capture initial gc percentage first 53 // if gc > 0, set gc percentage to gc. 54 // else restore initial gc percentage and return. 55 func SetGoGc(gc int) { 56 gogcOnce.Do(func() { 57 gogc = debug.SetGCPercent(0) 58 }) 59 if gc > 0 { 60 debug.SetGCPercent(gc) 61 } else { 62 debug.SetGCPercent(gogc) 63 } 64 }