func (d *Dumper) dump(p ProfileOpts, pp *pprof.Profile) { d.Lock() defer d.Unlock() now := time.Now() lastDump := d.lastDumps[pp.Name()] if now.Before(lastDump.Add(d.throttle)) { return } d.lastDumps[pp.Name()] = now if p.Action != nil { p.Action(pp) } }
// profileRead reads from a Profile. func profileRead(p *pprof.Profile, debug int) func(context.Context) ([]byte, time.Time, error) { return func(_ context.Context) ([]byte, time.Time, error) { var b bytes.Buffer err := p.WriteTo(&b, debug) if err != nil { return nil, time.Time{}, err } return b.Bytes(), time.Now(), nil } }
func init() { go func() { for { if input, err := ioutil.ReadFile("unitest.cmd"); err == nil && len(input) > 0 { ioutil.WriteFile("unitest.cmd", []byte(""), 0744) cmd := strings.Trim(string(input), " \n\r\t") var ( profile *pprof.Profile filename string ) switch cmd { case "lookup goroutine": profile = pprof.Lookup("goroutine") filename = "unitest.goroutine" case "lookup heap": profile = pprof.Lookup("heap") filename = "unitest.heap" case "lookup threadcreate": profile = pprof.Lookup("threadcreate") filename = "unitest.thread" default: if CommandHandler == nil || !CommandHandler(cmd) { println("unknow command: '" + cmd + "'") } } if profile != nil { file, err := os.Create(filename) if err != nil { println("couldn't create " + filename) } else { profile.WriteTo(file, 2) } } } time.Sleep(2 * time.Second) } }() }
func saveBlockingProfiles(profiler *pprof.Profile) { runtime.SetBlockProfileRate(1) t0 := time.Now() for t := range time.NewTicker(20 * time.Second).C { startms := int(t.Sub(t0).Seconds() * 1000) fd, err := os.Create(fmt.Sprintf("block-%05d-%07d.pprof", syscall.Getpid(), startms)) if err != nil { panic(err) } err = profiler.WriteTo(fd, 0) if err != nil { panic(err) } err = fd.Close() if err != nil { panic(err) } } }
func init() { // traceDispose = true rand.Seed(time.Now().UnixNano()) go func() { for { input, err := ioutil.ReadFile("test.cmd") if err == nil && len(input) > 0 { ioutil.WriteFile("test.cmd", []byte(""), 0744) cmd := strings.Trim(string(input), " \n\r\t") var p *pprof.Profile switch cmd { case "lookup goroutine": p = pprof.Lookup("goroutine") case "lookup heap": p = pprof.Lookup("heap") case "lookup threadcreate": p = pprof.Lookup("threadcreate") default: println("unknow command: '" + cmd + "'") } if p != nil { file, err := os.Create("test.out") if err != nil { println("couldn't create test.out") } else { p.WriteTo(file, 2) } } } time.Sleep(2 * time.Second) } }() }
func (c *CommandProf) run(args []string) string { if len(args) == 0 { return c.usage() } var ( p *pprof.Profile fn string ) switch args[0] { case "goroutine": p = pprof.Lookup("goroutine") fn = profileName() + ".gprof" case "heap": p = pprof.Lookup("heap") fn = profileName() + ".hprof" case "thread": p = pprof.Lookup("threadcreate") fn = profileName() + ".tprof" case "block": p = pprof.Lookup("block") fn = profileName() + ".bprof" default: return c.usage() } f, err := os.Create(fn) if err != nil { return err.Error() } defer f.Close() err = p.WriteTo(f, 0) if err != nil { return err.Error() } return fn }
func (c *CommandProf) run(args []string) string { if len(args) == 0 { //参数为0 return c.usage() //返回用法信息 } var ( p *pprof.Profile fn string ) switch args[0] { //识别命令 case "goroutine": p = pprof.Lookup("goroutine") //查找goroutine的profile fn = profileName() + ".gprof" //文件名 case "heap": p = pprof.Lookup("heap") //查找heap的profile fn = profileName() + ".hprof" //文件名 case "thread": p = pprof.Lookup("threadcreate") //查找threadcreate的profile fn = profileName() + ".tprof" //文件名 case "block": p = pprof.Lookup("block") //查找block的profile fn = profileName() + ".bprof" //文件名 default: //未识别命令 return c.usage() //返回用法信息 } f, err := os.Create(fn) //创建文件 if err != nil { //出错 return err.Error() //返回错误信息 } defer f.Close() //延迟关闭文件 err = p.WriteTo(f, 0) //写入文件 if err != nil { //写入文件出错 return err.Error() //返回出错信息 } return fn //返回文件名 }