Browse code

pkg/sysinfo: rm duplicates

The CPU CFS cgroup-aware scheduler is one single kernel feature, not
two, so it does not make sense to have two separate booleans
(CPUCfsQuota and CPUCfsPeriod). Merge these into CPUCfs.

Same for CPU realtime.

For compatibility reasons, /info stays the same for now.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

Kir Kolyshkin authored on 2020/05/23 06:18:06
Showing 7 changed files
... ...
@@ -505,8 +505,8 @@ func verifyPlatformContainerResources(resources *containertypes.Resources, sysIn
505 505
 	if resources.NanoCPUs > 0 && resources.CPUQuota > 0 {
506 506
 		return warnings, fmt.Errorf("Conflicting options: Nano CPUs and CPU Quota cannot both be set")
507 507
 	}
508
-	if resources.NanoCPUs > 0 && (!sysInfo.CPUCfsPeriod || !sysInfo.CPUCfsQuota) {
509
-		return warnings, fmt.Errorf("NanoCPUs can not be set, as your kernel does not support CPU cfs period/quota or the cgroup is not mounted")
508
+	if resources.NanoCPUs > 0 && !sysInfo.CPUCfs {
509
+		return warnings, fmt.Errorf("NanoCPUs can not be set, as your kernel does not support CPU CFS scheduler or the cgroup is not mounted")
510 510
 	}
511 511
 	// The highest precision we could get on Linux is 0.001, by setting
512 512
 	//   cpu.cfs_period_us=1000ms
... ...
@@ -523,17 +523,14 @@ func verifyPlatformContainerResources(resources *containertypes.Resources, sysIn
523 523
 		warnings = append(warnings, "Your kernel does not support CPU shares or the cgroup is not mounted. Shares discarded.")
524 524
 		resources.CPUShares = 0
525 525
 	}
526
-	if resources.CPUPeriod > 0 && !sysInfo.CPUCfsPeriod {
527
-		warnings = append(warnings, "Your kernel does not support CPU cfs period or the cgroup is not mounted. Period discarded.")
526
+	if (resources.CPUPeriod != 0 || resources.CPUQuota != 0) && !sysInfo.CPUCfs {
527
+		warnings = append(warnings, "Your kernel does not support CPU CFS scheduler. CPU period/quota discarded.")
528 528
 		resources.CPUPeriod = 0
529
+		resources.CPUQuota = 0
529 530
 	}
530 531
 	if resources.CPUPeriod != 0 && (resources.CPUPeriod < 1000 || resources.CPUPeriod > 1000000) {
531 532
 		return warnings, fmt.Errorf("CPU cfs period can not be less than 1ms (i.e. 1000) or larger than 1s (i.e. 1000000)")
532 533
 	}
533
-	if resources.CPUQuota > 0 && !sysInfo.CPUCfsQuota {
534
-		warnings = append(warnings, "Your kernel does not support CPU cfs quota or the cgroup is not mounted. Quota discarded.")
535
-		resources.CPUQuota = 0
536
-	}
537 534
 	if resources.CPUQuota > 0 && resources.CPUQuota < 1000 {
538 535
 		return warnings, fmt.Errorf("CPU cfs quota can not be less than 1ms (i.e. 1000)")
539 536
 	}
... ...
@@ -1726,10 +1723,10 @@ func (daemon *Daemon) initCgroupsPath(path string) error {
1726 1726
 
1727 1727
 	path = filepath.Join(mnt, root, path)
1728 1728
 	sysInfo := daemon.RawSysInfo(true)
1729
-	if err := maybeCreateCPURealTimeFile(sysInfo.CPURealtimePeriod, daemon.configStore.CPURealtimePeriod, "cpu.rt_period_us", path); err != nil {
1729
+	if err := maybeCreateCPURealTimeFile(sysInfo.CPURealtime, daemon.configStore.CPURealtimePeriod, "cpu.rt_period_us", path); err != nil {
1730 1730
 		return err
1731 1731
 	}
1732
-	return maybeCreateCPURealTimeFile(sysInfo.CPURealtimeRuntime, daemon.configStore.CPURealtimeRuntime, "cpu.rt_runtime_us", path)
1732
+	return maybeCreateCPURealTimeFile(sysInfo.CPURealtime, daemon.configStore.CPURealtimeRuntime, "cpu.rt_runtime_us", path)
1733 1733
 }
1734 1734
 
1735 1735
 func maybeCreateCPURealTimeFile(sysinfoPresent bool, configValue int64, file string, path string) error {
... ...
@@ -30,8 +30,8 @@ func (daemon *Daemon) fillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo)
30 30
 	v.KernelMemory = sysInfo.KernelMemory
31 31
 	v.KernelMemoryTCP = sysInfo.KernelMemoryTCP
32 32
 	v.OomKillDisable = sysInfo.OomKillDisable
33
-	v.CPUCfsPeriod = sysInfo.CPUCfsPeriod
34
-	v.CPUCfsQuota = sysInfo.CPUCfsQuota
33
+	v.CPUCfsPeriod = sysInfo.CPUCfs
34
+	v.CPUCfsQuota = sysInfo.CPUCfs
35 35
 	v.CPUShares = sysInfo.CPUShares
36 36
 	v.CPUSet = sysInfo.Cpuset
37 37
 	v.PidsLimit = sysInfo.PidsLimit
... ...
@@ -90,10 +90,8 @@ func applyCPUCgroupInfoV2(info *SysInfo, controllers map[string]struct{}, _ stri
90 90
 		return warnings
91 91
 	}
92 92
 	info.CPUShares = true
93
-	info.CPUCfsPeriod = true
94
-	info.CPUCfsQuota = true
95
-	info.CPURealtimePeriod = false
96
-	info.CPURealtimeRuntime = false
93
+	info.CPUCfs = true
94
+	info.CPURealtime = false
97 95
 	return warnings
98 96
 }
99 97
 
... ...
@@ -62,17 +62,11 @@ type cgroupCPUInfo struct {
62 62
 	// Whether CPU shares is supported or not
63 63
 	CPUShares bool
64 64
 
65
-	// Whether CPU CFS(Completely Fair Scheduler) period is supported or not
66
-	CPUCfsPeriod bool
65
+	// Whether CPU CFS (Completely Fair Scheduler) is supported
66
+	CPUCfs bool
67 67
 
68
-	// Whether CPU CFS(Completely Fair Scheduler) quota is supported or not
69
-	CPUCfsQuota bool
70
-
71
-	// Whether CPU real-time period is supported or not
72
-	CPURealtimePeriod bool
73
-
74
-	// Whether CPU real-time runtime is supported or not
75
-	CPURealtimeRuntime bool
68
+	// Whether CPU real-time scheduler is supported
69
+	CPURealtime bool
76 70
 }
77 71
 
78 72
 type cgroupBlkioInfo struct {
... ...
@@ -144,27 +144,17 @@ func applyCPUCgroupInfo(info *SysInfo, cgMounts map[string]string) []string {
144 144
 
145 145
 	info.CPUShares = cgroupEnabled(mountPoint, "cpu.shares")
146 146
 	if !info.CPUShares {
147
-		warnings = append(warnings, "Your kernel does not support cgroup cpu shares")
147
+		warnings = append(warnings, "Your kernel does not support CPU shares")
148 148
 	}
149 149
 
150
-	info.CPUCfsPeriod = cgroupEnabled(mountPoint, "cpu.cfs_period_us")
151
-	if !info.CPUCfsPeriod {
152
-		warnings = append(warnings, "Your kernel does not support cgroup cfs period")
150
+	info.CPUCfs = cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
151
+	if !info.CPUCfs {
152
+		warnings = append(warnings, "Your kernel does not support CPU CFS scheduler")
153 153
 	}
154 154
 
155
-	info.CPUCfsQuota = cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
156
-	if !info.CPUCfsQuota {
157
-		warnings = append(warnings, "Your kernel does not support cgroup cfs quotas")
158
-	}
159
-
160
-	info.CPURealtimePeriod = cgroupEnabled(mountPoint, "cpu.rt_period_us")
161
-	if !info.CPURealtimePeriod {
162
-		warnings = append(warnings, "Your kernel does not support cgroup rt period")
163
-	}
164
-
165
-	info.CPURealtimeRuntime = cgroupEnabled(mountPoint, "cpu.rt_runtime_us")
166
-	if !info.CPURealtimeRuntime {
167
-		warnings = append(warnings, "Your kernel does not support cgroup rt runtime")
155
+	info.CPURealtime = cgroupEnabled(mountPoint, "cpu.rt_period_us")
156
+	if !info.CPURealtime {
157
+		warnings = append(warnings, "Your kernel does not support CPU realtime scheduler")
168 158
 	}
169 159
 
170 160
 	return warnings
... ...
@@ -240,46 +240,41 @@ func TestDecodeHostConfig(t *testing.T) {
240 240
 
241 241
 func TestValidateResources(t *testing.T) {
242 242
 	type resourceTest struct {
243
-		ConfigCPURealtimePeriod   int64
244
-		ConfigCPURealtimeRuntime  int64
245
-		SysInfoCPURealtimePeriod  bool
246
-		SysInfoCPURealtimeRuntime bool
247
-		ErrorExpected             bool
248
-		FailureMsg                string
243
+		ConfigCPURealtimePeriod  int64
244
+		ConfigCPURealtimeRuntime int64
245
+		SysInfoCPURealtime       bool
246
+		ErrorExpected            bool
247
+		FailureMsg               string
249 248
 	}
250 249
 
251 250
 	tests := []resourceTest{
252 251
 		{
253
-			ConfigCPURealtimePeriod:   1000,
254
-			ConfigCPURealtimeRuntime:  1000,
255
-			SysInfoCPURealtimePeriod:  true,
256
-			SysInfoCPURealtimeRuntime: true,
257
-			ErrorExpected:             false,
258
-			FailureMsg:                "Expected valid configuration",
252
+			ConfigCPURealtimePeriod:  1000,
253
+			ConfigCPURealtimeRuntime: 1000,
254
+			SysInfoCPURealtime:       true,
255
+			ErrorExpected:            false,
256
+			FailureMsg:               "Expected valid configuration",
259 257
 		},
260 258
 		{
261
-			ConfigCPURealtimePeriod:   5000,
262
-			ConfigCPURealtimeRuntime:  5000,
263
-			SysInfoCPURealtimePeriod:  false,
264
-			SysInfoCPURealtimeRuntime: true,
265
-			ErrorExpected:             true,
266
-			FailureMsg:                "Expected failure when cpu-rt-period is set but kernel doesn't support it",
259
+			ConfigCPURealtimePeriod:  5000,
260
+			ConfigCPURealtimeRuntime: 5000,
261
+			SysInfoCPURealtime:       false,
262
+			ErrorExpected:            true,
263
+			FailureMsg:               "Expected failure when cpu-rt-period is set but kernel doesn't support it",
267 264
 		},
268 265
 		{
269
-			ConfigCPURealtimePeriod:   5000,
270
-			ConfigCPURealtimeRuntime:  5000,
271
-			SysInfoCPURealtimePeriod:  true,
272
-			SysInfoCPURealtimeRuntime: false,
273
-			ErrorExpected:             true,
274
-			FailureMsg:                "Expected failure when cpu-rt-runtime is set but kernel doesn't support it",
266
+			ConfigCPURealtimePeriod:  5000,
267
+			ConfigCPURealtimeRuntime: 5000,
268
+			SysInfoCPURealtime:       false,
269
+			ErrorExpected:            true,
270
+			FailureMsg:               "Expected failure when cpu-rt-runtime is set but kernel doesn't support it",
275 271
 		},
276 272
 		{
277
-			ConfigCPURealtimePeriod:   5000,
278
-			ConfigCPURealtimeRuntime:  10000,
279
-			SysInfoCPURealtimePeriod:  true,
280
-			SysInfoCPURealtimeRuntime: false,
281
-			ErrorExpected:             true,
282
-			FailureMsg:                "Expected failure when cpu-rt-runtime is greater than cpu-rt-period",
273
+			ConfigCPURealtimePeriod:  5000,
274
+			ConfigCPURealtimeRuntime: 10000,
275
+			SysInfoCPURealtime:       true,
276
+			ErrorExpected:            true,
277
+			FailureMsg:               "Expected failure when cpu-rt-runtime is greater than cpu-rt-period",
283 278
 		},
284 279
 	}
285 280
 
... ...
@@ -289,8 +284,7 @@ func TestValidateResources(t *testing.T) {
289 289
 		hc.Resources.CPURealtimeRuntime = rt.ConfigCPURealtimeRuntime
290 290
 
291 291
 		var si sysinfo.SysInfo
292
-		si.CPURealtimePeriod = rt.SysInfoCPURealtimePeriod
293
-		si.CPURealtimeRuntime = rt.SysInfoCPURealtimeRuntime
292
+		si.CPURealtime = rt.SysInfoCPURealtime
294 293
 
295 294
 		if err := validateResources(&hc, &si); (err != nil) != rt.ErrorExpected {
296 295
 			t.Fatal(rt.FailureMsg, err)
... ...
@@ -85,12 +85,8 @@ func validateResources(hc *container.HostConfig, si *sysinfo.SysInfo) error {
85 85
 		return nil
86 86
 	}
87 87
 
88
-	if hc.Resources.CPURealtimePeriod > 0 && !si.CPURealtimePeriod {
89
-		return fmt.Errorf("Your kernel does not support cgroup cpu real-time period")
90
-	}
91
-
92
-	if hc.Resources.CPURealtimeRuntime > 0 && !si.CPURealtimeRuntime {
93
-		return fmt.Errorf("Your kernel does not support cgroup cpu real-time runtime")
88
+	if (hc.Resources.CPURealtimePeriod != 0 || hc.Resources.CPURealtimeRuntime != 0) && !si.CPURealtime {
89
+		return fmt.Errorf("Your kernel does not support CPU real-time scheduler")
94 90
 	}
95 91
 
96 92
 	if hc.Resources.CPURealtimePeriod != 0 && hc.Resources.CPURealtimeRuntime != 0 && hc.Resources.CPURealtimeRuntime > hc.Resources.CPURealtimePeriod {