|
38 | 38 | ctx context.Context
|
39 | 39 | cancel context.CancelFunc
|
40 | 40 | wg sync.WaitGroup
|
41 |
| - cfgMaxProcs int |
42 |
| - lastMaxProcs int |
| 41 | + lastCPU int |
43 | 42 | lastMemoryLimit uint64
|
44 | 43 | )
|
45 | 44 |
|
@@ -115,45 +114,44 @@ func refreshCgroupCPU() error {
|
115 | 114 | quota := runtime.NumCPU()
|
116 | 115 |
|
117 | 116 | // Get CPU quota from cgroup.
|
118 |
| - cpuPeriod, cpuQuota, err := cgroup.GetCPUPeriodAndQuota() |
119 |
| - if err != nil { |
120 |
| - return err |
121 |
| - } |
122 |
| - if cpuPeriod > 0 && cpuQuota > 0 { |
| 117 | + cpuPeriod, cpuQuota, err := getCgroupCPUPeriodAndQuota() |
| 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))
|
126 | 122 | }
|
127 | 123 | }
|
128 | 124 |
|
129 |
| - if quota != lastMaxProcs { |
| 125 | + if quota != lastCPU { |
130 | 126 | log.Info("set the maxprocs", zap.Int("quota", quota))
|
131 | 127 | metrics.MaxProcs.Set(float64(quota))
|
132 |
| - 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 |
| 128 | + lastCPU = quota |
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 := getCgroupMemoryLimit() |
| 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))
|
156 | 151 | lastMemoryLimit = memLimit
|
157 | 152 | }
|
158 |
| - return nil |
| 153 | + return err |
159 | 154 | }
|
| 155 | + |
| 156 | +var getCgroupCPUPeriodAndQuota = cgroup.GetCPUPeriodAndQuota |
| 157 | +var getCgroupMemoryLimit = cgroup.GetMemoryLimit |
0 commit comments