forked from minio/minio
/
cpu.go
143 lines (130 loc) · 3.79 KB
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cpu
import (
"sync"
"time"
)
// rollingAvg holds the rolling average of the cpu load on the minio
// server over its lifetime
var rollingAvg *Load
// cpuMeasureInterval is the interval of time between two
// measurements of CPU load
const cpuLoadMeasureInterval = 5 * time.Second
// triggers the average load computation at server spawn
func init() {
rollingAvg = &Load{
Min: float64(0),
Max: float64(0),
Avg: float64(0),
}
var rollingSum float64
var cycles float64
go func() {
for {
time.Sleep(cpuLoadMeasureInterval)
cycles = cycles + 1
currLoad := GetLoad()
if rollingAvg.Max < currLoad.Max || rollingAvg.Max == 0 {
rollingAvg.Max = currLoad.Max
}
if rollingAvg.Min > currLoad.Min || rollingAvg.Min == 0 {
rollingAvg.Min = currLoad.Min
}
rollingSum = rollingSum + currLoad.Avg
rollingAvg.Avg = rollingSum / cycles
}
}()
}
const (
// cpuLoadWindow is the interval of time for which the
// cpu utilization is measured
cpuLoadWindow = 200 * time.Millisecond
// cpuLoadSampleSize is the number of samples measured
// for calculating cpu utilization
cpuLoadSampleSize = 3
// endOfTime represents the end of time
endOfTime = time.Duration(1<<63 - 1)
)
// Load holds CPU utilization % measured in three intervals of 200ms each
type Load struct {
Avg float64 `json:"avg"`
Max float64 `json:"max"`
Min float64 `json:"min"`
Error string `json:"error,omitempty"`
}
type counter struct{}
// GetHistoricLoad returns the historic CPU utilization of the current process
func GetHistoricLoad() Load {
return *rollingAvg
}
// GetLoad returns the CPU utilization of the current process
// This function works by calcualating the amount of cpu clock
// cycles the current process used in a given time window
//
// This corresponds to the CPU utilization calculation done by
// tools like top. Here, we use the getclocktime with the
// CLOCK_PROCESS_CPUTIME_ID parameter to obtain the total number of
// clock ticks used by the process so far. Then we sleep for
// 200ms and obtain the the total number of clock ticks again. The
// difference between the two counts provides us the number of
// clock ticks used by the process in the 200ms interval.
//
// The ratio of clock ticks used (measured in nanoseconds) to number
// of nanoseconds in 200 milliseconds provides us the CPU usage
// for the process currently
func GetLoad() Load {
vals := make(chan time.Duration, 3)
var wg sync.WaitGroup
for i := 0; i < cpuLoadSampleSize; i++ {
cpuCounter, err := newCounter()
if err != nil {
return Load{
Error: err.Error(),
}
}
wg.Add(1)
go func() {
start := cpuCounter.now()
time.Sleep(cpuLoadWindow)
end := cpuCounter.now()
vals <- end.Sub(start)
wg.Done()
}()
}
wg.Wait()
sum := time.Duration(0)
max := time.Duration(0)
min := (endOfTime)
for i := 0; i < cpuLoadSampleSize; i++ {
val := <-vals
sum = sum + val
if val > max {
max = val
}
if val < min {
min = val
}
}
close(vals)
avg := sum / 3
return Load{
Avg: toFixed4(float64(avg)/float64(200*time.Millisecond)) * 100,
Max: toFixed4(float64(max)/float64(200*time.Millisecond)) * 100,
Min: toFixed4(float64(min)/float64(200*time.Millisecond)) * 100,
Error: "",
}
}