38
38
ctx context.Context
39
39
cancel context.CancelFunc
40
40
wg sync.WaitGroup
41
- cfgMaxProcs int
42
41
lastMaxProcs int
43
42
lastMemoryLimit uint64
44
43
)
@@ -116,10 +115,7 @@ func refreshCgroupCPU() error {
116
115
117
116
// Get CPU quota from cgroup.
118
117
cpuPeriod , cpuQuota , err := cgroup .GetCPUPeriodAndQuota ()
119
- if err != nil {
120
- return err
121
- }
122
- if cpuPeriod > 0 && cpuQuota > 0 {
118
+ if err == nil && cpuPeriod > 0 && cpuQuota > 0 {
123
119
ratio := float64 (cpuQuota ) / float64 (cpuPeriod )
124
120
if ratio < float64 (quota ) {
125
121
quota = int (math .Ceil (ratio ))
@@ -130,26 +126,25 @@ func refreshCgroupCPU() error {
130
126
log .Info ("set the maxprocs" , zap .Int ("quota" , quota ))
131
127
metrics .MaxProcs .Set (float64 (quota ))
132
128
lastMaxProcs = quota
133
- } else if lastMaxProcs == 0 {
134
- log .Info ("set the maxprocs" , zap .Int ("cfgMaxProcs" , cfgMaxProcs ))
135
- metrics .MaxProcs .Set (float64 (cfgMaxProcs ))
136
- lastMaxProcs = cfgMaxProcs
137
129
}
138
- return nil
130
+
131
+ return err
139
132
}
140
133
141
134
func refreshCgroupMemory () error {
142
- memLimit , err := cgroup .GetMemoryLimit ()
143
- if err != nil {
144
- return err
145
- }
135
+ // Get the total memory limit from `procfs`
146
136
vmem , err := mem .VirtualMemory ()
147
137
if err != nil {
148
138
return err
149
139
}
150
- if memLimit > vmem .Total {
151
- memLimit = vmem .Total
140
+ memLimit := vmem .Total
141
+
142
+ // Try to update the limit from cgroup.
143
+ cgroupMemLimit , err := cgroup .GetMemoryLimit ()
144
+ if err == nil && cgroupMemLimit < memLimit {
145
+ memLimit = cgroupMemLimit
152
146
}
147
+
153
148
if memLimit != lastMemoryLimit {
154
149
log .Info ("set the memory limit" , zap .Uint64 ("memLimit" , memLimit ))
155
150
metrics .MemoryLimit .Set (float64 (memLimit ))
0 commit comments