github.com/okex/exchain@v1.8.0/libs/tendermint/state/execution_exchain.go (about) 1 package state 2 3 import ( 4 "log" 5 "os" 6 "path" 7 "runtime" 8 "runtime/pprof" 9 "strconv" 10 "time" 11 ) 12 13 const ( 14 FlagApplyBlockPprofTime = "applyblock-pprof-time" 15 ) 16 17 var ( 18 IgnoreSmbCheck bool = false 19 20 ApplyBlockPprofTime = -1 21 HomeDir = "" 22 tmpPprofName = "tmp.cpu.pprof" 23 ) 24 25 func SetIgnoreSmbCheck(check bool) { 26 IgnoreSmbCheck = check 27 } 28 29 func PprofStart() (*os.File, time.Time) { 30 startTime := time.Now() 31 p := getFilePath("") 32 if _, err := os.Stat(p); os.IsNotExist(err) { 33 err := os.MkdirAll(p, 0755) 34 if err != nil { 35 log.Fatal(err) 36 } 37 } 38 39 f, err := os.OpenFile(path.Join(p, tmpPprofName), os.O_RDWR|os.O_CREATE, 0644) 40 if err != nil { 41 log.Fatal(err) 42 } 43 runtime.SetCPUProfileRate(300) 44 pprof.StartCPUProfile(f) 45 return f, startTime 46 } 47 48 func PprofEnd(height int, f *os.File, startTime time.Time) { 49 pprof.StopCPUProfile() 50 f.Close() 51 sec := time.Since(startTime).Milliseconds() 52 if int(sec) >= ApplyBlockPprofTime { 53 pprofName := "pprof." + strconv.Itoa(height) + "-" + strconv.Itoa(int(sec)) + "ms.bin" 54 newDir := getFilePath(pprofName) 55 os.Rename(getFilePath(tmpPprofName), newDir) 56 } else { 57 os.Remove(getFilePath(tmpPprofName)) 58 } 59 60 } 61 62 func getFilePath(fileName string) string { 63 return path.Join(HomeDir, "pprof", fileName) 64 }