MCPcopy
hub / github.com/CodisLabs/codis / AutoGOMAXPROCS

Function AutoGOMAXPROCS

cmd/proxy/main.go:242–282  ·  view source on GitHub ↗
(min, max int)

Source from the content-addressed store, hash-verified

240}
241
242func AutoGOMAXPROCS(min, max int) {
243 for {
244 var ncpu = runtime.GOMAXPROCS(0)
245 var less, more int
246 var usage [10]float64
247 for i := 0; i < len(usage) && more == 0; i++ {
248 u, _, err := utils.CPUUsage(time.Second)
249 if err != nil {
250 log.WarnErrorf(err, "get cpu usage failed")
251 time.Sleep(time.Second * 30)
252 continue
253 }
254 u /= float64(ncpu)
255 switch {
256 case u < 0.30 && ncpu > min:
257 less++
258 case u > 0.70 && ncpu < max:
259 more++
260 }
261 usage[i] = u
262 }
263 var nn = ncpu
264 switch {
265 case more != 0:
266 nn = ncpu + ((max - ncpu + 3) / 4)
267 case less == len(usage):
268 nn = ncpu - 1
269 }
270 if nn != ncpu {
271 runtime.GOMAXPROCS(nn)
272 var b bytes.Buffer
273 for i, u := range usage {
274 if i != 0 {
275 fmt.Fprintf(&b, ", ")
276 }
277 fmt.Fprintf(&b, "%.3f", u)
278 }
279 log.Warnf("ncpu = %d -> %d, usage = [%s]", ncpu, nn, b.Bytes())
280 }
281 }
282}
283
284func AutoOnlineWithDashboard(p *proxy.Proxy, dashboard string) {
285 for i := 0; i < 10; i++ {

Callers 1

mainFunction · 0.85

Calls 3

WarnErrorfMethod · 0.80
WarnfMethod · 0.80
SleepMethod · 0.65

Tested by

no test coverage detected