Skip to content

Commit e387b1e

Browse files
committed
fix the issue that CPU quota is 0 if it's not limited by cgroup
Signed-off-by: Yang Keao <[email protected]>
1 parent 3ab8c7f commit e387b1e

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

pkg/util/cgmon/cgmon.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ var (
3838
ctx context.Context
3939
cancel context.CancelFunc
4040
wg sync.WaitGroup
41-
cfgMaxProcs int
4241
lastMaxProcs int
4342
lastMemoryLimit uint64
4443
)
@@ -116,10 +115,7 @@ func refreshCgroupCPU() error {
116115

117116
// Get CPU quota from cgroup.
118117
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 {
123119
ratio := float64(cpuQuota) / float64(cpuPeriod)
124120
if ratio < float64(quota) {
125121
quota = int(math.Ceil(ratio))
@@ -130,26 +126,25 @@ func refreshCgroupCPU() error {
130126
log.Info("set the maxprocs", zap.Int("quota", quota))
131127
metrics.MaxProcs.Set(float64(quota))
132128
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
137129
}
138-
return nil
130+
131+
return err
139132
}
140133

141134
func refreshCgroupMemory() error {
142-
memLimit, err := cgroup.GetMemoryLimit()
143-
if err != nil {
144-
return err
145-
}
135+
// Get the total memory limit from `procfs`
146136
vmem, err := mem.VirtualMemory()
147137
if err != nil {
148138
return err
149139
}
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
152146
}
147+
153148
if memLimit != lastMemoryLimit {
154149
log.Info("set the memory limit", zap.Uint64("memLimit", memLimit))
155150
metrics.MemoryLimit.Set(float64(memLimit))

0 commit comments

Comments
 (0)