github.com/songzhibin97/gkit@v1.2.13/watching/config.go (about) 1 package watching 2 3 import ( 4 "os" 5 "sync" 6 "time" 7 ) 8 9 type configs struct { 10 UseGoProcAsCPUCore bool // use the go max procs number as the CPU core number when it's true 11 UseCGroup bool // use the CGroup to calc cpu/memory when it's true 12 13 // overwrite the system level memory limitation when > 0. 14 memoryLimit uint64 15 cpuCore float64 16 17 *ShrinkThrConfigs 18 19 *DumpConfigs 20 21 LogLevel int 22 Logger *os.File 23 24 // interval for dump loop, default 5s 25 CollectInterval time.Duration 26 intervalResetting chan struct{} 27 28 // the cooldown time after every type of dump 29 // interval for cooldown,default 1m 30 // the cpu/mem/goroutine have different cooldowns of their own 31 CoolDown time.Duration 32 33 // if current cpu usage percent is greater than CPUMaxPercent, 34 // Watching would not dump all types profile, cuz this 35 // move may result of the system crash. 36 CPUMaxPercent int 37 38 // if write lock is held mean Watching's 39 // configuration is being modified. 40 L *sync.RWMutex 41 42 logConfigs *logConfigs 43 GroupConfigs *groupConfigs 44 45 MemConfigs *typeConfig 46 GCHeapConfigs *typeConfig 47 CpuConfigs *typeConfig 48 ThreadConfigs *typeConfig 49 50 // profile reporter 51 rptConfigs *ReporterConfigs 52 } 53 54 // DumpConfigs contains configuration about dump file. 55 type DumpConfigs struct { 56 // full path to put the profile files, default /tmp 57 DumpPath string 58 // default dump to binary profile, set to true if you want a text profile 59 DumpProfileType dumpProfileType 60 // only dump top 10 if set to false, otherwise dump all, only effective when in_text = true 61 DumpFullStack bool 62 } 63 64 // ShrinkThrConfigs contains the configuration about shrink thread 65 type ShrinkThrConfigs struct { 66 // shrink the thread number when it exceeds the max threshold that specified in Threshold 67 Enable bool 68 Threshold int 69 Delay time.Duration // start to shrink thread after the delay time. 70 } 71 72 type logConfigs struct { 73 RotateEnable bool 74 SplitLoggerSize int64 // SplitLoggerSize The size of the log split 75 } 76 77 type typeConfig struct { 78 Enable bool 79 // mem/cpu/gcheap trigger minimum in percent, goroutine/thread trigger minimum in number 80 TriggerMin int 81 82 // mem/cpu/gcheap trigger abs in percent, goroutine/thread trigger abs in number 83 TriggerAbs int 84 85 // mem/cpu/gcheap/goroutine/thread trigger diff in percent 86 TriggerDiff int 87 } 88 89 type gcHeapConfigs struct { 90 // enable the heap dumper, should dump if one of the following requirements is matched 91 // 1. GC heap usage > GCHeapTriggerPercentMin && GC heap usage diff > GCHeapTriggerPercentDiff 92 // 2. GC heap usage > GCHeapTriggerPercentAbs 93 Enable bool 94 GCHeapTriggerPercentMin int // GC heap trigger minimum in percent 95 GCHeapTriggerPercentDiff int // GC heap trigger diff in percent 96 GCHeapTriggerPercentAbs int // GC heap trigger absolute in percent 97 } 98 99 type groupConfigs struct { 100 // enable the goroutine dumper, should dump if one of the following requirements is matched 101 // 1. goroutine_num > GoroutineTriggerNumMin && goroutine_num < GoroutineTriggerNumMax && goroutine diff percent > GoroutineTriggerPercentDiff 102 // 2. goroutine_num > GoroutineTriggerNumAbsNum && goroutine_num < GoroutineTriggerNumMax 103 *typeConfig 104 GoroutineTriggerNumMax int // goroutine trigger max in number 105 } 106 107 type memConfigs struct { 108 // enable the heap dumper, should dump if one of the following requirements is matched 109 // 1. memory usage > MemTriggerPercentMin && memory usage diff > MemTriggerPercentDiff 110 // 2. memory usage > MemTriggerPercentAbs 111 Enable bool 112 MemTriggerPercentMin int // mem trigger minimum in percent 113 MemTriggerPercentDiff int // mem trigger diff in percent 114 MemTriggerPercentAbs int // mem trigger absolute in percent 115 } 116 117 type cpuConfigs struct { 118 // enable the cpu dumper, should dump if one of the following requirements is matched 119 // 1. cpu usage > CPUTriggerMin && cpu usage diff > CPUTriggerDiff 120 // 2. cpu usage > CPUTriggerAbs 121 Enable bool 122 CPUTriggerPercentMin int // cpu trigger min in percent 123 CPUTriggerPercentDiff int // cpu trigger diff in percent 124 CPUTriggerPercentAbs int // cpu trigger abs in percent 125 } 126 127 type threadConfigs struct { 128 Enable bool 129 ThreadTriggerPercentMin int // thread trigger min in number 130 ThreadTriggerPercentDiff int // thread trigger diff in percent 131 ThreadTriggerPercentAbs int // thread trigger abs in number 132 } 133 134 type ReporterConfigs struct { 135 reporter ProfileReporter 136 active int32 // switch 137 } 138 139 // defaultReporterConfigs returns ReporterConfigs。 140 func defaultReporterConfigs() *ReporterConfigs { 141 opts := &ReporterConfigs{} 142 143 return opts 144 } 145 146 func defaultLogConfigs() *logConfigs { 147 return &logConfigs{ 148 RotateEnable: true, 149 SplitLoggerSize: defaultShardLoggerSize, 150 } 151 } 152 153 func defaultGroupConfigs() *groupConfigs { 154 return &groupConfigs{ 155 typeConfig: &typeConfig{ 156 Enable: false, 157 TriggerMin: defaultGoroutineTriggerAbs, 158 TriggerAbs: defaultGoroutineTriggerDiff, 159 TriggerDiff: defaultGoroutineTriggerMin, 160 }, 161 GoroutineTriggerNumMax: 0, 162 } 163 } 164 165 func defaultGCHeapOptions() *typeConfig { 166 return &typeConfig{ 167 Enable: false, 168 TriggerMin: defaultGCHeapTriggerAbs, 169 TriggerAbs: defaultGCHeapTriggerDiff, 170 TriggerDiff: defaultGCHeapTriggerMin, 171 } 172 } 173 174 func defaultMemConfigs() *typeConfig { 175 return &typeConfig{ 176 Enable: false, 177 TriggerMin: defaultMemTriggerAbs, 178 TriggerAbs: defaultMemTriggerDiff, 179 TriggerDiff: defaultMemTriggerMin, 180 } 181 } 182 183 func defaultCPUConfigs() *typeConfig { 184 return &typeConfig{ 185 Enable: false, 186 TriggerMin: defaultCPUTriggerAbs, 187 TriggerAbs: defaultCPUTriggerDiff, 188 TriggerDiff: defaultCPUTriggerMin, 189 } 190 } 191 192 func defaultThreadConfig() *typeConfig { 193 return &typeConfig{ 194 Enable: false, 195 TriggerMin: defaultThreadTriggerAbs, 196 TriggerAbs: defaultThreadTriggerDiff, 197 TriggerDiff: defaultThreadTriggerMin, 198 } 199 } 200 201 func defaultConfig() *configs { 202 return &configs{ 203 logConfigs: defaultLogConfigs(), 204 GroupConfigs: defaultGroupConfigs(), 205 MemConfigs: defaultMemConfigs(), 206 GCHeapConfigs: defaultGCHeapOptions(), 207 CpuConfigs: defaultCPUConfigs(), 208 ThreadConfigs: defaultThreadConfig(), 209 LogLevel: LogLevelDebug, 210 Logger: os.Stdout, 211 CollectInterval: defaultInterval, 212 intervalResetting: make(chan struct{}, 1), 213 CoolDown: defaultCooldown, 214 DumpConfigs: &DumpConfigs{ 215 DumpPath: defaultDumpPath, 216 DumpProfileType: defaultDumpProfileType, 217 DumpFullStack: false, 218 }, 219 ShrinkThrConfigs: &ShrinkThrConfigs{ 220 Enable: false, 221 }, 222 L: &sync.RWMutex{}, 223 rptConfigs: defaultReporterConfigs(), 224 } 225 } 226 227 // GetShrinkThreadConfigs return a copy of ShrinkThrConfigs. 228 func (c *configs) GetShrinkThreadConfigs() ShrinkThrConfigs { 229 c.L.RLock() 230 defer c.L.RUnlock() 231 return *c.ShrinkThrConfigs 232 } 233 234 // GetMemConfigs return a copy of memConfigs. 235 func (c *configs) GetMemConfigs() typeConfig { 236 c.L.RLock() 237 defer c.L.RUnlock() 238 return *c.MemConfigs 239 } 240 241 // GetCPUConfigs return a copy of cpuConfigs 242 func (c *configs) GetCPUConfigs() typeConfig { 243 c.L.RLock() 244 defer c.L.RUnlock() 245 return *c.CpuConfigs 246 } 247 248 // GetGroupConfigs return a copy of grOptions 249 func (c *configs) GetGroupConfigs() groupConfigs { 250 c.L.RLock() 251 defer c.L.RUnlock() 252 return *c.GroupConfigs 253 } 254 255 // GetThreadConfigs return a copy of threadConfigs 256 func (c *configs) GetThreadConfigs() typeConfig { 257 c.L.RLock() 258 defer c.L.RUnlock() 259 return *c.ThreadConfigs 260 } 261 262 // GetGcHeapConfigs return a copy of gcHeapConfigs 263 func (c *configs) GetGcHeapConfigs() typeConfig { 264 c.L.RLock() 265 defer c.L.RUnlock() 266 return *c.GCHeapConfigs 267 } 268 269 // GetReporterConfigs returns a copy of ReporterConfigs. 270 func (c *configs) GetReporterConfigs() ReporterConfigs { 271 c.L.RLock() 272 defer c.L.RUnlock() 273 return *c.rptConfigs 274 }