本文整理汇总了Golang中runtime/pprof.Lookup函数的典型用法代码示例。如果您正苦于以下问题:Golang Lookup函数的具体用法?Golang Lookup怎么用?Golang Lookup使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Lookup函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Handler
// Handler returns an http.HandlerFunc that returns pprof profiles
// and additional metrics.
// The handler must be accessible through the "/debug/_gom" route
// in order for gom to display the stats from the debugged program.
// See the godoc examples for usage.
func Handler() http.HandlerFunc {
// TODO(jbd): enable block profile.
return func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Query().Get("view") {
case "profile":
name := r.URL.Query().Get("name")
if name == "profile" {
httppprof.Profile(w, r)
return
}
httppprof.Handler(name).ServeHTTP(w, r)
return
case "symbol":
httppprof.Symbol(w, r)
return
}
n := &stats{
Goroutine: pprof.Lookup("goroutine").Count(),
Thread: pprof.Lookup("threadcreate").Count(),
Block: pprof.Lookup("block").Count(),
Timestamp: time.Now().Unix(),
}
err := json.NewEncoder(w).Encode(n)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, err)
}
}
}
示例2: init
func init() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP)
go func() {
for s := range ch {
switch s {
case syscall.SIGHUP:
straceFile := "/tmp/costest.stack.trace"
log.Debugf("receive signal HUP, output stack trace to %s", straceFile)
f, err := os.OpenFile(straceFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Error("create stack trace file error:", err)
continue
}
fmt.Fprint(f, "\n\nGoroutines\n\n")
pprof.Lookup("goroutine").WriteTo(f, 2)
fmt.Fprint(f, "\n\nHeap\n\n")
pprof.Lookup("heap").WriteTo(f, 1)
fmt.Fprint(f, "\n\nThreadCreate\n\n")
pprof.Lookup("threadcreate").WriteTo(f, 1)
fmt.Fprint(f, "\n\nBlock\n\n")
pprof.Lookup("block").WriteTo(f, 1)
f.Close()
}
}
}()
}
示例3: ShouldNotBeRunningGoroutines
// ShouldNotBeRunningGoroutines takes in the name of the current module as
// `actual` and returns a blank string if no other goroutines are running
// in that module, besides testing gorutines.
// If there are other goroutines running, it will output the full stacktrace.
// It does this by parsing the full stack trace of all currently running
// goroutines and seeing if any of them are within this module and are not
// testing goroutines.
func ShouldNotBeRunningGoroutines(actual interface{}, _ ...interface{}) string {
// this function has to take an interface{} type so that you can use it with
// GoConvey's `So(...)` function.
module := actual.(string)
var b bytes.Buffer
// passes 1 as the debug parameter so there are function names and line numbers
pprof.Lookup("goroutine").WriteTo(&b, 1)
scanner := bufio.NewScanner(&b)
// each line of this stack trace is one path in one goroutine that is running
for scanner.Scan() {
t := scanner.Text()
// now we wanna check when this line we are looking at shows a goroutine
// that is running a file in this module that is not a test
runningInModule := strings.Contains(t, module)
runningTest := strings.Contains(t, "test")
runningExternal := strings.Contains(t, "Godeps") || strings.Contains(t, "vendor")
runningOtherFileInModule := runningInModule && !runningTest && !runningExternal
if runningOtherFileInModule {
// if we find that it is in fact running another goroutine from this
// package then output the full stacktrace, with debug level 2 to show
// more information
pprof.Lookup("goroutine").WriteTo(&b, 2)
return "Was running other goroutines: " + t + b.String()
}
}
return ""
}
示例4: debug
func debug() {
log.Println("Running debug report...")
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Println("MEMORY STATS")
log.Printf("%d,%d,%d,%d\n", m.HeapSys, m.HeapAlloc, m.HeapIdle, m.HeapReleased)
log.Println("NUM CPU:", runtime.NumCPU())
//profiling
f, err := os.Create("memprofileup.out")
defer f.Close()
fg, err := os.Create("goprof.out")
fb, err := os.Create("blockprof.out")
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
pprof.Lookup("goroutine").WriteTo(fg, 0)
pprof.Lookup("block").WriteTo(fb, 0)
f.Close()
fg.Close()
fb.Close()
time.Sleep(1 * time.Second)
panic("Debugging: Dump the stacks:")
}
示例5: DumpOnSignal
func DumpOnSignal(signals ...os.Signal) {
c := make(chan os.Signal, 1)
signal.Notify(c, signals...)
for _ = range c {
log.Printf("dump: goroutine...")
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
log.Printf("dump: heap...")
pprof.Lookup("heap").WriteTo(os.Stderr, 1)
}
}
示例6: handle
func handle(conn net.Conn, msg []byte) error {
switch msg[0] {
case signal.StackTrace:
buf := make([]byte, 1<<16)
n := runtime.Stack(buf, true)
_, err := conn.Write(buf[:n])
return err
case signal.GC:
runtime.GC()
_, err := conn.Write([]byte("ok"))
return err
case signal.MemStats:
var s runtime.MemStats
runtime.ReadMemStats(&s)
fmt.Fprintf(conn, "alloc: %v bytes\n", s.Alloc)
fmt.Fprintf(conn, "total-alloc: %v bytes\n", s.TotalAlloc)
fmt.Fprintf(conn, "sys: %v bytes\n", s.Sys)
fmt.Fprintf(conn, "lookups: %v\n", s.Lookups)
fmt.Fprintf(conn, "mallocs: %v\n", s.Mallocs)
fmt.Fprintf(conn, "frees: %v\n", s.Frees)
fmt.Fprintf(conn, "heap-alloc: %v bytes\n", s.HeapAlloc)
fmt.Fprintf(conn, "heap-sys: %v bytes\n", s.HeapSys)
fmt.Fprintf(conn, "heap-idle: %v bytes\n", s.HeapIdle)
fmt.Fprintf(conn, "heap-in-use: %v bytes\n", s.HeapInuse)
fmt.Fprintf(conn, "heap-released: %v bytes\n", s.HeapReleased)
fmt.Fprintf(conn, "heap-objects: %v\n", s.HeapObjects)
fmt.Fprintf(conn, "stack-in-use: %v bytes\n", s.StackInuse)
fmt.Fprintf(conn, "stack-sys: %v bytes\n", s.StackSys)
fmt.Fprintf(conn, "next-gc: when heap-alloc >= %v bytes\n", s.NextGC)
fmt.Fprintf(conn, "last-gc: %v ns\n", s.LastGC)
fmt.Fprintf(conn, "gc-pause: %v ns\n", s.PauseTotalNs)
fmt.Fprintf(conn, "num-gc: %v\n", s.NumGC)
fmt.Fprintf(conn, "enable-gc: %v\n", s.EnableGC)
fmt.Fprintf(conn, "debug-gc: %v\n", s.DebugGC)
case signal.Version:
fmt.Fprintf(conn, "%v\n", runtime.Version())
case signal.HeapProfile:
pprof.Lookup("heap").WriteTo(conn, 0)
case signal.CPUProfile:
if err := pprof.StartCPUProfile(conn); err != nil {
return nil
}
time.Sleep(30 * time.Second)
pprof.StopCPUProfile()
case signal.Vitals:
fmt.Fprintf(conn, "goroutines: %v\n", runtime.NumGoroutine())
fmt.Fprintf(conn, "OS threads: %v\n", pprof.Lookup("threadcreate").Count())
fmt.Fprintf(conn, "GOMAXPROCS: %v\n", runtime.GOMAXPROCS(0))
fmt.Fprintf(conn, "num CPU: %v\n", runtime.NumCPU())
}
return nil
}
示例7: init
func init() {
http.HandleFunc("/debug/pprofstats", func(w http.ResponseWriter, r *http.Request) {
n := &stats{
Goroutine: pprof.Lookup("goroutine").Count(),
Thread: pprof.Lookup("threadcreate").Count(),
Block: pprof.Lookup("block").Count(),
Timestamp: time.Now().Unix(),
}
err := json.NewEncoder(w).Encode(n)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, err)
}
})
}
示例8: after
// after runs after all testing.
func after() {
if *cpuProfile != "" {
pprof.StopCPUProfile() // flushes profile to disk
}
if *traceFile != "" {
trace.Stop() // flushes trace to disk
}
if *memProfile != "" {
f, err := os.Create(toOutputDir(*memProfile))
if err != nil {
fmt.Fprintf(os.Stderr, "testing: %s\n", err)
os.Exit(2)
}
runtime.GC() // materialize all statistics
if err = pprof.WriteHeapProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *memProfile, err)
os.Exit(2)
}
f.Close()
}
if *blockProfile != "" && *blockProfileRate >= 0 {
f, err := os.Create(toOutputDir(*blockProfile))
if err != nil {
fmt.Fprintf(os.Stderr, "testing: %s\n", err)
os.Exit(2)
}
if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *blockProfile, err)
os.Exit(2)
}
f.Close()
}
if *mutexProfile != "" && *mutexProfileFraction >= 0 {
f, err := os.Create(toOutputDir(*mutexProfile))
if err != nil {
fmt.Fprintf(os.Stderr, "testing: %s\n", err)
os.Exit(2)
}
if err = pprof.Lookup("mutex").WriteTo(f, 0); err != nil {
fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *blockProfile, err)
os.Exit(2)
}
f.Close()
}
if cover.Mode != "" {
coverReport()
}
}
示例9: Stats
// Stats handler returns an http.HandlerFunc that returns stats
// about the number of current goroutines, threads, etc.
// Stats handler must be accessible through "/debug/pprofstats" route
// in order for gom to display the stats from the debugged program.
func Stats() http.HandlerFunc {
// TODO(jbd): enable block profile.
return func(w http.ResponseWriter, r *http.Request) {
n := &stats{
Goroutine: pprof.Lookup("goroutine").Count(),
Thread: pprof.Lookup("threadcreate").Count(),
Block: pprof.Lookup("block").Count(),
Timestamp: time.Now().Unix(),
}
err := json.NewEncoder(w).Encode(n)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, err)
}
}
}
示例10: heap
func heap() {
f, err := os.Create("profile")
if err != nil {
fmt.Printf("%s\n", err)
return
}
defer f.Close()
//==================================
var p [1024]*people
for i := 0; i < len(p); i++ {
p[i] = &people{}
p[i].age[1023] = 2
}
doP(p[:])
for i := 0; i < len(p); i++ {
p[i] = nil
}
var p2 [4 * 1024]*people
for i := 0; i < len(p2); i++ {
p2[i] = &people{}
p2[i].age[1023] = 1
}
time.Sleep(3 * time.Second)
//==================================
profile := pprof.Lookup("heap")
if profile != nil {
profile.WriteTo(f, 1)
}
}
示例11: DumpOnSignal
func DumpOnSignal() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR2)
for _ = range c {
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
}
}
示例12: TestStartStop
func TestStartStop(t *testing.T) {
assert := assert.New(t)
startGoroutineNum := runtime.NumGoroutine()
for i := 0; i < 10; i++ {
qa.Root(t, func(root string) {
configFile := TestConfig(root)
app := carbon.New(configFile)
assert.NoError(app.ParseConfig())
assert.NoError(app.Start())
app.Stop()
})
}
endGoroutineNum := runtime.NumGoroutine()
// GC worker etc
if !assert.InDelta(startGoroutineNum, endGoroutineNum, 3) {
p := pprof.Lookup("goroutine")
p.WriteTo(os.Stdout, 1)
}
}
示例13: profileStartup
func profileStartup() {
if *profileHeap != "" {
f, err := os.Create(*profileHeap)
if err != nil {
log.Fatal(err)
}
go func() {
for {
time.Sleep(time.Second * 5)
l := <-profileLock
f.Seek(0, os.SEEK_SET)
f.Truncate(0)
pprof.Lookup("heap").WriteTo(f, 0)
profileLock <- l
}
}()
}
if *profileCPU != "" {
f, err := os.Create(*profileCPU)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
}
示例14: main
func main() {
// Extract the command line arguments
relayPort, clusterId, rsaKey := parseFlags()
// Check for CPU profiling
if *cpuProfile != "" {
prof, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(prof)
defer pprof.StopCPUProfile()
}
// Check for lock contention profiling
if *blockProfile != "" {
prof, err := os.Create(*blockProfile)
if err != nil {
log.Fatal(err)
}
runtime.SetBlockProfileRate(1)
defer pprof.Lookup("block").WriteTo(prof, 0)
}
// Create and boot a new carrier
log.Printf("main: booting iris overlay...")
overlay := iris.New(clusterId, rsaKey)
if peers, err := overlay.Boot(); err != nil {
log.Fatalf("main: failed to boot iris overlay: %v.", err)
} else {
log.Printf("main: iris overlay converged with %v remote connections.", peers)
}
// Create and boot a new relay
log.Printf("main: booting relay service...")
rel, err := relay.New(relayPort, overlay)
if err != nil {
log.Fatalf("main: failed to create relay service: %v.", err)
}
if err := rel.Boot(); err != nil {
log.Fatalf("main: failed to boot relay: %v.", err)
}
// Capture termination signals
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
// Report success
log.Printf("main: iris successfully booted, listening on port %d.", relayPort)
// Wait for termination request, clean up and exit
<-quit
log.Printf("main: terminating relay service...")
if err := rel.Terminate(); err != nil {
log.Printf("main: failed to terminate relay service: %v.", err)
}
log.Printf("main: terminating carrier...")
if err := overlay.Shutdown(); err != nil {
log.Printf("main: failed to shutdown iris overlay: %v.", err)
}
log.Printf("main: iris terminated.")
}
示例15: Setup
func Setup(r pork.Router) {
r.RespondWithFunc("/debug/goroutine", func(w pork.ResponseWriter, r *http.Request) {
p := pprof.Lookup("goroutine")
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
p.WriteTo(w, 2)
})
}