github.com/matrixorigin/matrixone@v1.2.0/pkg/util/profile/profile.go (about) 1 // Copyright 2023 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package profile 16 17 import ( 18 "fmt" 19 "io" 20 "runtime/pprof" 21 "runtime/trace" 22 "time" 23 24 "github.com/matrixorigin/matrixone/pkg/common/moerr" 25 ) 26 27 const ( 28 GOROUTINE = "goroutine" 29 HEAP = "heap" 30 ALLOCS = "allocs" 31 THREADCREATE = "threadcreate" 32 BLOCK = "block" 33 MUTEX = "mutex" 34 35 CPU = "cpu" 36 TRACE = "trace" 37 STATUS = "status" 38 ) 39 40 // ProfileGoroutine you can see more infos in https://pkg.go.dev/runtime/pprof#Profile 41 func ProfileGoroutine(w io.Writer, debug int) error { 42 return ProfileRuntime(GOROUTINE, w, debug) 43 } 44 45 func ProfileHeap(w io.Writer, debug int) error { 46 return ProfileRuntime(HEAP, w, debug) 47 } 48 49 func ProfileAllocs(w io.Writer, debug int) error { 50 return ProfileRuntime(ALLOCS, w, debug) 51 } 52 53 func ProfileThreadcreate(w io.Writer, debug int) error { 54 return ProfileRuntime(THREADCREATE, w, debug) 55 } 56 57 func ProfileBlock(w io.Writer, debug int) error { 58 return ProfileRuntime(BLOCK, w, debug) 59 } 60 61 func ProfileMutex(w io.Writer, debug int) error { 62 return ProfileRuntime(MUTEX, w, debug) 63 } 64 65 func ProfileRuntime(name string, w io.Writer, debug int) error { 66 profile := pprof.Lookup(name) 67 if profile == nil { 68 return moerr.GetOkExpectedEOB() 69 } 70 if err := profile.WriteTo(w, debug); err != nil { 71 return err 72 } 73 return nil 74 } 75 76 func ProfileCPU(w io.Writer, d time.Duration) error { 77 err := pprof.StartCPUProfile(w) 78 if err != nil { 79 return err 80 } 81 time.Sleep(d) 82 pprof.StopCPUProfile() 83 return nil 84 } 85 86 func ProfileTrace(w io.Writer, d time.Duration) error { 87 err := trace.Start(w) 88 if err != nil { 89 return err 90 } 91 time.Sleep(d) 92 trace.Stop() 93 return nil 94 } 95 96 const timestampFormatter = "20060102_150405.000000" 97 98 func Time2DatetimeString(t time.Time) string { 99 return t.Format(timestampFormatter) 100 } 101 102 // GetProfileName get formatted filepath 103 // for example: 104 // - pprof/goroutine_${id}_${yyyyDDMM_hhmmss.ns}.pprof 105 // - pprof/heap_${id}_${yyyyDDMM_hhmmss.ns}.pprof 106 // - pprof/cpu_${id}_${yyyyDDMM_hhmmss.ns}.pprof 107 func GetProfileName(typ string, id string, t time.Time) string { 108 return fmt.Sprintf("pprof/%s_%s_%s.pprof", typ, id, Time2DatetimeString(t)) 109 } 110 111 func GetSystemStatusFilePath(id string, t time.Time) string { 112 return fmt.Sprintf("status/%s_%s.ss", id, Time2DatetimeString(t)) 113 }