Browse code

Use CpuMaximum instead of CpuPercent for more precision

Signed-off-by: Darren Stahl <darst@microsoft.com>

Darren Stahl authored on 2017/05/10 10:25:44
Showing 2 changed files
... ...
@@ -120,7 +120,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (*specs.Spec, error) {
120 120
 
121 121
 	// In s.Windows.Resources
122 122
 	cpuShares := uint16(c.HostConfig.CPUShares)
123
-	cpuPercent := uint8(c.HostConfig.CPUPercent)
123
+	cpuMaximum := uint16(c.HostConfig.CPUPercent) * 100
124 124
 	cpuCount := uint64(c.HostConfig.CPUCount)
125 125
 	if c.HostConfig.NanoCPUs > 0 {
126 126
 		if isHyperV {
... ...
@@ -128,21 +128,24 @@ func (daemon *Daemon) createSpec(c *container.Container) (*specs.Spec, error) {
128 128
 			leftoverNanoCPUs := c.HostConfig.NanoCPUs % 1e9
129 129
 			if leftoverNanoCPUs != 0 {
130 130
 				cpuCount++
131
-				cpuPercent = uint8(c.HostConfig.NanoCPUs * 100 / int64(cpuCount) / 1e9)
131
+				cpuMaximum = uint16(c.HostConfig.NanoCPUs / int64(cpuCount) / (1e9 / 10000))
132
+				if cpuMaximum < 1 {
133
+					// The requested NanoCPUs is so small that we rounded to 0, use 1 instead
134
+					cpuMaximum = 1
135
+				}
132 136
 			}
133 137
 		} else {
134
-			cpuPercent = uint8(c.HostConfig.NanoCPUs * 100 / int64(sysinfo.NumCPU()) / 1e9)
135
-
136
-			if cpuPercent < 1 {
138
+			cpuMaximum = uint16(c.HostConfig.NanoCPUs / int64(sysinfo.NumCPU()) / (1e9 / 10000))
139
+			if cpuMaximum < 1 {
137 140
 				// The requested NanoCPUs is so small that we rounded to 0, use 1 instead
138
-				cpuPercent = 1
141
+				cpuMaximum = 1
139 142
 			}
140 143
 		}
141 144
 	}
142 145
 	memoryLimit := uint64(c.HostConfig.Memory)
143 146
 	s.Windows.Resources = &specs.WindowsResources{
144 147
 		CPU: &specs.WindowsCPUResources{
145
-			Percent: &cpuPercent,
148
+			Maximum: &cpuMaximum,
146 149
 			Shares:  &cpuShares,
147 150
 			Count:   &cpuCount,
148 151
 		},
... ...
@@ -128,8 +128,8 @@ func (clnt *client) Create(containerID string, checkpoint string, checkpointDir
128 128
 			if spec.Windows.Resources.CPU.Shares != nil {
129 129
 				configuration.ProcessorWeight = uint64(*spec.Windows.Resources.CPU.Shares)
130 130
 			}
131
-			if spec.Windows.Resources.CPU.Percent != nil {
132
-				configuration.ProcessorMaximum = int64(*spec.Windows.Resources.CPU.Percent) * 100 // ProcessorMaximum is a value between 1 and 10000
131
+			if spec.Windows.Resources.CPU.Maximum != nil {
132
+				configuration.ProcessorMaximum = int64(*spec.Windows.Resources.CPU.Maximum)
133 133
 			}
134 134
 		}
135 135
 		if spec.Windows.Resources.Memory != nil {