Skip to content

Commit 8430cc4

Browse files
committed
Use uint64 for resources to keep consistency with runtime-spec
Signed-off-by: Qiang Huang <[email protected]>
1 parent d270940 commit 8430cc4

File tree

8 files changed

+63
-62
lines changed

8 files changed

+63
-62
lines changed

libcontainer/cgroups/fs/apply_raw.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ func removePath(p string, err error) error {
346346
return nil
347347
}
348348

349-
func CheckCpushares(path string, c int64) error {
350-
var cpuShares int64
349+
func CheckCpushares(path string, c uint64) error {
350+
var cpuShares uint64
351351

352352
if c == 0 {
353353
return nil

libcontainer/cgroups/fs/cpu.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (s *CpuGroup) ApplyDir(path string, cgroup *configs.Cgroup, pid int) error
5555

5656
func (s *CpuGroup) SetRtSched(path string, cgroup *configs.Cgroup) error {
5757
if cgroup.Resources.CpuRtPeriod != 0 {
58-
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatInt(cgroup.Resources.CpuRtPeriod, 10)); err != nil {
58+
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatUint(cgroup.Resources.CpuRtPeriod, 10)); err != nil {
5959
return err
6060
}
6161
}
@@ -69,12 +69,12 @@ func (s *CpuGroup) SetRtSched(path string, cgroup *configs.Cgroup) error {
6969

7070
func (s *CpuGroup) Set(path string, cgroup *configs.Cgroup) error {
7171
if cgroup.Resources.CpuShares != 0 {
72-
if err := writeFile(path, "cpu.shares", strconv.FormatInt(cgroup.Resources.CpuShares, 10)); err != nil {
72+
if err := writeFile(path, "cpu.shares", strconv.FormatUint(cgroup.Resources.CpuShares, 10)); err != nil {
7373
return err
7474
}
7575
}
7676
if cgroup.Resources.CpuPeriod != 0 {
77-
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(cgroup.Resources.CpuPeriod, 10)); err != nil {
77+
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatUint(cgroup.Resources.CpuPeriod, 10)); err != nil {
7878
return err
7979
}
8080
}

libcontainer/cgroups/fs/memory.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,22 @@ func EnableKernelMemoryAccounting(path string) error {
7171
// until a limit is set on the cgroup and limit cannot be set once the
7272
// cgroup has children, or if there are already tasks in the cgroup.
7373
for _, i := range []int64{1, -1} {
74-
if err := setKernelMemory(path, i); err != nil {
74+
if err := setKernelMemory(path, uint64(i)); err != nil {
7575
return err
7676
}
7777
}
7878
return nil
7979
}
8080

81-
func setKernelMemory(path string, kernelMemoryLimit int64) error {
81+
func setKernelMemory(path string, kernelMemoryLimit uint64) error {
8282
if path == "" {
8383
return fmt.Errorf("no such directory for %s", cgroupKernelMemoryLimit)
8484
}
8585
if !cgroups.PathExists(filepath.Join(path, cgroupKernelMemoryLimit)) {
8686
// kernel memory is not enabled on the system so we should do nothing
8787
return nil
8888
}
89-
if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatInt(kernelMemoryLimit, 10)), 0700); err != nil {
89+
if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatUint(kernelMemoryLimit, 10)), 0700); err != nil {
9090
// Check if the error number returned by the syscall is "EBUSY"
9191
// The EBUSY signal is returned on attempts to write to the
9292
// memory.kmem.limit_in_bytes file if the cgroup has children or
@@ -104,12 +104,14 @@ func setKernelMemory(path string, kernelMemoryLimit int64) error {
104104
}
105105

106106
func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
107-
// If the memory update is set to -1 we should also set swap to -1
108-
// -1 means unlimited memory
109-
if cgroup.Resources.Memory == -1 {
107+
ulimited := -1
108+
109+
// If the memory update is set to uint64(-1) we should also
110+
// set swap to uint64(-1), it means unlimited memory.
111+
if cgroup.Resources.Memory == uint64(ulimited) {
110112
// Only set swap if it's enbled in kernel
111113
if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) {
112-
cgroup.Resources.MemorySwap = -1
114+
cgroup.Resources.MemorySwap = uint64(ulimited)
113115
}
114116
}
115117

@@ -124,29 +126,29 @@ func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
124126
// When update memory limit, we should adapt the write sequence
125127
// for memory and swap memory, so it won't fail because the new
126128
// value and the old value don't fit kernel's validation.
127-
if cgroup.Resources.MemorySwap == -1 || memoryUsage.Limit < uint64(cgroup.Resources.MemorySwap) {
128-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
129+
if cgroup.Resources.MemorySwap == uint64(ulimited) || memoryUsage.Limit < cgroup.Resources.MemorySwap {
130+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
129131
return err
130132
}
131-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
133+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
132134
return err
133135
}
134136
} else {
135-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
137+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
136138
return err
137139
}
138-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
140+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
139141
return err
140142
}
141143
}
142144
} else {
143145
if cgroup.Resources.Memory != 0 {
144-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
146+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
145147
return err
146148
}
147149
}
148150
if cgroup.Resources.MemorySwap != 0 {
149-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
151+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
150152
return err
151153
}
152154
}
@@ -167,13 +169,13 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
167169
}
168170

169171
if cgroup.Resources.MemoryReservation != 0 {
170-
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
172+
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(cgroup.Resources.MemoryReservation, 10)); err != nil {
171173
return err
172174
}
173175
}
174176

175177
if cgroup.Resources.KernelMemoryTCP != 0 {
176-
if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemoryTCP, 10)); err != nil {
178+
if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatUint(cgroup.Resources.KernelMemoryTCP, 10)); err != nil {
177179
return err
178180
}
179181
}
@@ -184,12 +186,12 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
184186
}
185187
if cgroup.Resources.MemorySwappiness == nil || int64(*cgroup.Resources.MemorySwappiness) == -1 {
186188
return nil
187-
} else if int64(*cgroup.Resources.MemorySwappiness) >= 0 && int64(*cgroup.Resources.MemorySwappiness) <= 100 {
188-
if err := writeFile(path, "memory.swappiness", strconv.FormatInt(*cgroup.Resources.MemorySwappiness, 10)); err != nil {
189+
} else if *cgroup.Resources.MemorySwappiness <= 100 {
190+
if err := writeFile(path, "memory.swappiness", strconv.FormatUint(*cgroup.Resources.MemorySwappiness, 10)); err != nil {
189191
return err
190192
}
191193
} else {
192-
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", int64(*cgroup.Resources.MemorySwappiness))
194+
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", *cgroup.Resources.MemorySwappiness)
193195
}
194196

195197
return nil
@@ -251,7 +253,7 @@ func memoryAssigned(cgroup *configs.Cgroup) bool {
251253
cgroup.Resources.KernelMemory > 0 ||
252254
cgroup.Resources.KernelMemoryTCP > 0 ||
253255
cgroup.Resources.OomKillDisable ||
254-
(cgroup.Resources.MemorySwappiness != nil && *cgroup.Resources.MemorySwappiness != -1)
256+
(cgroup.Resources.MemorySwappiness != nil && int64(*cgroup.Resources.MemorySwappiness) != -1)
255257
}
256258

257259
func getMemoryData(path, name string) (cgroups.MemoryData, error) {

libcontainer/cgroups/fs/memory_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) {
235235
defer helper.cleanup()
236236

237237
swappinessBefore := 60 //default is 60
238-
swappinessAfter := int64(0)
238+
swappinessAfter := uint64(0)
239239

240240
helper.writeFileContents(map[string]string{
241241
"memory.swappiness": strconv.Itoa(swappinessBefore),
@@ -251,7 +251,7 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) {
251251
if err != nil {
252252
t.Fatalf("Failed to parse memory.swappiness - %s", err)
253253
}
254-
if int64(value) != swappinessAfter {
254+
if value != swappinessAfter {
255255
t.Fatalf("Got the wrong value (%d), set memory.swappiness = %d failed.", value, swappinessAfter)
256256
}
257257
}

libcontainer/cgroups/systemd/apply_systemd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,19 @@ func (m *Manager) Apply(pid int) error {
260260

261261
if c.Resources.Memory != 0 {
262262
properties = append(properties,
263-
newProp("MemoryLimit", uint64(c.Resources.Memory)))
263+
newProp("MemoryLimit", c.Resources.Memory))
264264
}
265265

266266
if c.Resources.CpuShares != 0 {
267267
properties = append(properties,
268-
newProp("CPUShares", uint64(c.Resources.CpuShares)))
268+
newProp("CPUShares", c.Resources.CpuShares))
269269
}
270270

271271
// cpu.cfs_quota_us and cpu.cfs_period_us are controlled by systemd.
272272
if c.Resources.CpuQuota != 0 && c.Resources.CpuPeriod != 0 {
273-
cpuQuotaPerSecUSec := c.Resources.CpuQuota * 1000000 / c.Resources.CpuPeriod
273+
cpuQuotaPerSecUSec := uint64(c.Resources.CpuQuota*1000000) / c.Resources.CpuPeriod
274274
properties = append(properties,
275-
newProp("CPUQuotaPerSecUSec", uint64(cpuQuotaPerSecUSec)))
275+
newProp("CPUQuotaPerSecUSec", cpuQuotaPerSecUSec))
276276
}
277277

278278
if c.Resources.BlkioWeight != 0 {

libcontainer/configs/cgroup_unix.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,34 @@ type Resources struct {
4545
Devices []*Device `json:"devices"`
4646

4747
// Memory limit (in bytes)
48-
Memory int64 `json:"memory"`
48+
Memory uint64 `json:"memory"`
4949

5050
// Memory reservation or soft_limit (in bytes)
51-
MemoryReservation int64 `json:"memory_reservation"`
51+
MemoryReservation uint64 `json:"memory_reservation"`
5252

5353
// Total memory usage (memory + swap); set `-1` to enable unlimited swap
54-
MemorySwap int64 `json:"memory_swap"`
54+
MemorySwap uint64 `json:"memory_swap"`
5555

5656
// Kernel memory limit (in bytes)
57-
KernelMemory int64 `json:"kernel_memory"`
57+
KernelMemory uint64 `json:"kernel_memory"`
5858

5959
// Kernel memory limit for TCP use (in bytes)
60-
KernelMemoryTCP int64 `json:"kernel_memory_tcp"`
60+
KernelMemoryTCP uint64 `json:"kernel_memory_tcp"`
6161

6262
// CPU shares (relative weight vs. other containers)
63-
CpuShares int64 `json:"cpu_shares"`
63+
CpuShares uint64 `json:"cpu_shares"`
6464

6565
// CPU hardcap limit (in usecs). Allowed cpu time in a given period.
6666
CpuQuota int64 `json:"cpu_quota"`
6767

6868
// CPU period to be used for hardcapping (in usecs). 0 to use system default.
69-
CpuPeriod int64 `json:"cpu_period"`
69+
CpuPeriod uint64 `json:"cpu_period"`
7070

7171
// How many time CPU will use in realtime scheduling (in usecs).
7272
CpuRtRuntime int64 `json:"cpu_rt_quota"`
7373

7474
// CPU period to be used for realtime scheduling (in usecs).
75-
CpuRtPeriod int64 `json:"cpu_rt_period"`
75+
CpuRtPeriod uint64 `json:"cpu_rt_period"`
7676

7777
// CPU to use
7878
CpusetCpus string `json:"cpuset_cpus"`
@@ -114,7 +114,7 @@ type Resources struct {
114114
OomKillDisable bool `json:"oom_kill_disable"`
115115

116116
// Tuning swappiness behaviour per cgroup
117-
MemorySwappiness *int64 `json:"memory_swappiness"`
117+
MemorySwappiness *uint64 `json:"memory_swappiness"`
118118

119119
// Set priority of network traffic for container
120120
NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"`

libcontainer/specconv/spec_linux.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,40 +344,39 @@ func createCgroupConfig(name string, useSystemdCgroup bool, spec *specs.Spec) (*
344344
c.Resources.Devices = append(c.Resources.Devices, allowedDevices...)
345345
if r.Memory != nil {
346346
if r.Memory.Limit != nil {
347-
c.Resources.Memory = int64(*r.Memory.Limit)
347+
c.Resources.Memory = *r.Memory.Limit
348348
}
349349
if r.Memory.Reservation != nil {
350-
c.Resources.MemoryReservation = int64(*r.Memory.Reservation)
350+
c.Resources.MemoryReservation = *r.Memory.Reservation
351351
}
352352
if r.Memory.Swap != nil {
353-
c.Resources.MemorySwap = int64(*r.Memory.Swap)
353+
c.Resources.MemorySwap = *r.Memory.Swap
354354
}
355355
if r.Memory.Kernel != nil {
356-
c.Resources.KernelMemory = int64(*r.Memory.Kernel)
356+
c.Resources.KernelMemory = *r.Memory.Kernel
357357
}
358358
if r.Memory.KernelTCP != nil {
359-
c.Resources.KernelMemoryTCP = int64(*r.Memory.KernelTCP)
359+
c.Resources.KernelMemoryTCP = *r.Memory.KernelTCP
360360
}
361361
if r.Memory.Swappiness != nil {
362-
swappiness := int64(*r.Memory.Swappiness)
363-
c.Resources.MemorySwappiness = &swappiness
362+
c.Resources.MemorySwappiness = r.Memory.Swappiness
364363
}
365364
}
366365
if r.CPU != nil {
367366
if r.CPU.Shares != nil {
368-
c.Resources.CpuShares = int64(*r.CPU.Shares)
367+
c.Resources.CpuShares = *r.CPU.Shares
369368
}
370369
if r.CPU.Quota != nil {
371-
c.Resources.CpuQuota = int64(*r.CPU.Quota)
370+
c.Resources.CpuQuota = *r.CPU.Quota
372371
}
373372
if r.CPU.Period != nil {
374-
c.Resources.CpuPeriod = int64(*r.CPU.Period)
373+
c.Resources.CpuPeriod = *r.CPU.Period
375374
}
376375
if r.CPU.RealtimeRuntime != nil {
377-
c.Resources.CpuRtRuntime = int64(*r.CPU.RealtimeRuntime)
376+
c.Resources.CpuRtRuntime = *r.CPU.RealtimeRuntime
378377
}
379378
if r.CPU.RealtimePeriod != nil {
380-
c.Resources.CpuRtPeriod = int64(*r.CPU.RealtimePeriod)
379+
c.Resources.CpuRtPeriod = *r.CPU.RealtimePeriod
381380
}
382381
if r.CPU.Cpus != "" {
383382
c.Resources.CpusetCpus = r.CPU.Cpus

update.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,18 @@ other options are ignored.
232232

233233
// Update the value
234234
config.Cgroups.Resources.BlkioWeight = *r.BlockIO.Weight
235-
config.Cgroups.Resources.CpuPeriod = int64(*r.CPU.Period)
236-
config.Cgroups.Resources.CpuQuota = int64(*r.CPU.Quota)
237-
config.Cgroups.Resources.CpuShares = int64(*r.CPU.Shares)
238-
config.Cgroups.Resources.CpuRtPeriod = int64(*r.CPU.RealtimePeriod)
239-
config.Cgroups.Resources.CpuRtRuntime = int64(*r.CPU.RealtimeRuntime)
235+
config.Cgroups.Resources.CpuPeriod = *r.CPU.Period
236+
config.Cgroups.Resources.CpuQuota = *r.CPU.Quota
237+
config.Cgroups.Resources.CpuShares = *r.CPU.Shares
238+
config.Cgroups.Resources.CpuRtPeriod = *r.CPU.RealtimePeriod
239+
config.Cgroups.Resources.CpuRtRuntime = *r.CPU.RealtimeRuntime
240240
config.Cgroups.Resources.CpusetCpus = r.CPU.Cpus
241241
config.Cgroups.Resources.CpusetMems = r.CPU.Mems
242-
config.Cgroups.Resources.KernelMemory = int64(*r.Memory.Kernel)
243-
config.Cgroups.Resources.KernelMemoryTCP = int64(*r.Memory.KernelTCP)
244-
config.Cgroups.Resources.Memory = int64(*r.Memory.Limit)
245-
config.Cgroups.Resources.MemoryReservation = int64(*r.Memory.Reservation)
246-
config.Cgroups.Resources.MemorySwap = int64(*r.Memory.Swap)
242+
config.Cgroups.Resources.KernelMemory = *r.Memory.Kernel
243+
config.Cgroups.Resources.KernelMemoryTCP = *r.Memory.KernelTCP
244+
config.Cgroups.Resources.Memory = *r.Memory.Limit
245+
config.Cgroups.Resources.MemoryReservation = *r.Memory.Reservation
246+
config.Cgroups.Resources.MemorySwap = *r.Memory.Swap
247247

248248
return container.Set(config)
249249
},

0 commit comments

Comments
 (0)