Skip to content

Commit 429a538

Browse files
Merge pull request #1495 from justincormack/memory-int64
Update spec to master, switch to int64 for memory limits
2 parents ff00fb1 + 3d9074e commit 429a538

File tree

7 files changed

+36
-68
lines changed

7 files changed

+36
-68
lines changed

libcontainer/cgroups/fs/memory.go

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

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

108108
func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
109-
ulimited := -1
110-
111-
// If the memory update is set to uint64(-1) we should also
112-
// set swap to uint64(-1), it means unlimited memory.
113-
if cgroup.Resources.Memory == uint64(ulimited) {
114-
// Only set swap if it's enbled in kernel
109+
// If the memory update is set to -1 we should also
110+
// set swap to -1, it means unlimited memory.
111+
if cgroup.Resources.Memory == -1 {
112+
// Only set swap if it's enabled in kernel
115113
if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) {
116-
cgroup.Resources.MemorySwap = uint64(ulimited)
114+
cgroup.Resources.MemorySwap = -1
117115
}
118116
}
119117

@@ -128,29 +126,29 @@ func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
128126
// When update memory limit, we should adapt the write sequence
129127
// for memory and swap memory, so it won't fail because the new
130128
// value and the old value don't fit kernel's validation.
131-
if cgroup.Resources.MemorySwap == uint64(ulimited) || memoryUsage.Limit < cgroup.Resources.MemorySwap {
132-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
129+
if cgroup.Resources.MemorySwap == -1 || memoryUsage.Limit < uint64(cgroup.Resources.MemorySwap) {
130+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
133131
return err
134132
}
135-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
133+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
136134
return err
137135
}
138136
} else {
139-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
137+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
140138
return err
141139
}
142-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
140+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
143141
return err
144142
}
145143
}
146144
} else {
147145
if cgroup.Resources.Memory != 0 {
148-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
146+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
149147
return err
150148
}
151149
}
152150
if cgroup.Resources.MemorySwap != 0 {
153-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
151+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
154152
return err
155153
}
156154
}
@@ -171,13 +169,13 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
171169
}
172170

173171
if cgroup.Resources.MemoryReservation != 0 {
174-
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(cgroup.Resources.MemoryReservation, 10)); err != nil {
172+
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
175173
return err
176174
}
177175
}
178176

179177
if cgroup.Resources.KernelMemoryTCP != 0 {
180-
if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatUint(cgroup.Resources.KernelMemoryTCP, 10)); err != nil {
178+
if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemoryTCP, 10)); err != nil {
181179
return err
182180
}
183181
}

libcontainer/configs/cgroup_linux.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ type Resources struct {
4343
Devices []*Device `json:"devices"`
4444

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

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

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

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

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

6060
// CPU shares (relative weight vs. other containers)
6161
CpuShares uint64 `json:"cpu_shares"`

libcontainer/specconv/example.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package specconv
22

33
import (
44
"os"
5-
"runtime"
65
"strings"
76

87
"github.com/opencontainers/runtime-spec/specs-go"
@@ -15,10 +14,6 @@ func sPtr(s string) *string { return &s }
1514
func Example() *specs.Spec {
1615
return &specs.Spec{
1716
Version: specs.Version,
18-
Platform: specs.Platform{
19-
OS: runtime.GOOS,
20-
Arch: runtime.GOARCH,
21-
},
2217
Root: specs.Root{
2318
Path: "rootfs",
2419
Readonly: true,

spec.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io/ioutil"
99
"os"
10-
"runtime"
1110

1211
"github.com/opencontainers/runc/libcontainer/configs"
1312
"github.com/opencontainers/runc/libcontainer/specconv"
@@ -131,9 +130,6 @@ func loadSpec(cPath string) (spec *specs.Spec, err error) {
131130
if err = json.NewDecoder(cf).Decode(&spec); err != nil {
132131
return nil, err
133132
}
134-
if err = validatePlatform(&spec.Platform); err != nil {
135-
return nil, err
136-
}
137133
return spec, validateProcessSpec(spec.Process)
138134
}
139135

@@ -148,13 +144,3 @@ func createLibContainerRlimit(rlimit specs.LinuxRlimit) (configs.Rlimit, error)
148144
Soft: rlimit.Soft,
149145
}, nil
150146
}
151-
152-
func validatePlatform(platform *specs.Platform) error {
153-
if platform.OS != runtime.GOOS {
154-
return fmt.Errorf("target os %s mismatch with current os %s", platform.OS, runtime.GOOS)
155-
}
156-
if platform.Arch != runtime.GOARCH {
157-
return fmt.Errorf("target arch %s mismatch with current arch %s", platform.Arch, runtime.GOARCH)
158-
}
159-
return nil
160-
}

update.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ other options are ignored.
124124

125125
r := specs.LinuxResources{
126126
Memory: &specs.LinuxMemory{
127-
Limit: u64Ptr(0),
128-
Reservation: u64Ptr(0),
129-
Swap: u64Ptr(0),
130-
Kernel: u64Ptr(0),
131-
KernelTCP: u64Ptr(0),
127+
Limit: i64Ptr(0),
128+
Reservation: i64Ptr(0),
129+
Swap: i64Ptr(0),
130+
Kernel: i64Ptr(0),
131+
KernelTCP: i64Ptr(0),
132132
},
133133
CPU: &specs.LinuxCPU{
134134
Shares: u64Ptr(0),
@@ -213,7 +213,7 @@ other options are ignored.
213213
}
214214
for _, pair := range []struct {
215215
opt string
216-
dest *uint64
216+
dest *int64
217217
}{
218218
{"memory", r.Memory.Limit},
219219
{"memory-swap", r.Memory.Swap},
@@ -232,7 +232,7 @@ other options are ignored.
232232
} else {
233233
v = -1
234234
}
235-
*pair.dest = uint64(v)
235+
*pair.dest = v
236236
}
237237
}
238238
r.Pids.Limit = int64(context.Int("pids-limit"))

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# OCI runtime-spec. When updating this, make sure you use a version tag rather
22
# than a commit ID so it's much more obvious what version of the spec we are
33
# using.
4-
github.com/opencontainers/runtime-spec 239c4e44f2a612ed85f6db9c66247aa33f437e91
4+
github.com/opencontainers/runtime-spec 198f23f827eea397d4331d7eb048d9d4c7ff7bee
55
# Core libcontainer functionality.
66
github.com/mrunalp/fileutils ed869b029674c0e9ce4c0dfa781405c2d9946d08
77
github.com/opencontainers/selinux v1.0.0-rc1

vendor/github.com/opencontainers/runtime-spec/specs-go/config.go

Lines changed: 6 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)