Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | From 3d9074ead33a5c27dc20bb49457c69c6d2ae6b57 Mon Sep 17 00:00:00 2001 |
| 2 | From: Justin Cormack <justin.cormack@docker.com> |
| 3 | Date: Fri, 23 Jun 2017 17:17:00 -0700 |
| 4 | Subject: [PATCH 3/3] Update memory specs to use int64 not uint64 |
| 5 | |
| 6 | replace #1492 #1494 |
| 7 | fix #1422 |
| 8 | |
| 9 | Since https://github.com/opencontainers/runtime-spec/pull/876 the memory |
| 10 | specifications are now `int64`, as that better matches the visible interface where |
| 11 | `-1` is a valid value. Otherwise finding the correct value was difficult as it |
| 12 | was kernel dependent. |
| 13 | |
| 14 | Signed-off-by: Justin Cormack <justin.cormack@docker.com> |
| 15 | --- |
| 16 | libcontainer/cgroups/fs/memory.go | 36 +++++++++++++++++------------------- |
| 17 | libcontainer/configs/cgroup_linux.go | 10 +++++----- |
| 18 | update.go | 14 +++++++------- |
| 19 | 3 files changed, 29 insertions(+), 31 deletions(-) |
| 20 | |
| 21 | diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go |
| 22 | index da2cc9f8..b739c631 100644 |
| 23 | --- a/src/import/libcontainer/cgroups/fs/memory.go |
| 24 | +++ b/src/import/libcontainer/cgroups/fs/memory.go |
| 25 | @@ -73,14 +73,14 @@ func EnableKernelMemoryAccounting(path string) error { |
| 26 | // until a limit is set on the cgroup and limit cannot be set once the |
| 27 | // cgroup has children, or if there are already tasks in the cgroup. |
| 28 | for _, i := range []int64{1, -1} { |
| 29 | - if err := setKernelMemory(path, uint64(i)); err != nil { |
| 30 | + if err := setKernelMemory(path, i); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | } |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | -func setKernelMemory(path string, kernelMemoryLimit uint64) error { |
| 38 | +func setKernelMemory(path string, kernelMemoryLimit int64) error { |
| 39 | if path == "" { |
| 40 | return fmt.Errorf("no such directory for %s", cgroupKernelMemoryLimit) |
| 41 | } |
| 42 | @@ -88,7 +88,7 @@ func setKernelMemory(path string, kernelMemoryLimit uint64) error { |
| 43 | // kernel memory is not enabled on the system so we should do nothing |
| 44 | return nil |
| 45 | } |
| 46 | - if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatUint(kernelMemoryLimit, 10)), 0700); err != nil { |
| 47 | + if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatInt(kernelMemoryLimit, 10)), 0700); err != nil { |
| 48 | // Check if the error number returned by the syscall is "EBUSY" |
| 49 | // The EBUSY signal is returned on attempts to write to the |
| 50 | // memory.kmem.limit_in_bytes file if the cgroup has children or |
| 51 | @@ -106,14 +106,12 @@ func setKernelMemory(path string, kernelMemoryLimit uint64) error { |
| 52 | } |
| 53 | |
| 54 | func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error { |
| 55 | - ulimited := -1 |
| 56 | - |
| 57 | - // If the memory update is set to uint64(-1) we should also |
| 58 | - // set swap to uint64(-1), it means unlimited memory. |
| 59 | - if cgroup.Resources.Memory == uint64(ulimited) { |
| 60 | - // Only set swap if it's enbled in kernel |
| 61 | + // If the memory update is set to -1 we should also |
| 62 | + // set swap to -1, it means unlimited memory. |
| 63 | + if cgroup.Resources.Memory == -1 { |
| 64 | + // Only set swap if it's enabled in kernel |
| 65 | if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) { |
| 66 | - cgroup.Resources.MemorySwap = uint64(ulimited) |
| 67 | + cgroup.Resources.MemorySwap = -1 |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @@ -128,29 +126,29 @@ func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error { |
| 72 | // When update memory limit, we should adapt the write sequence |
| 73 | // for memory and swap memory, so it won't fail because the new |
| 74 | // value and the old value don't fit kernel's validation. |
| 75 | - if cgroup.Resources.MemorySwap == uint64(ulimited) || memoryUsage.Limit < cgroup.Resources.MemorySwap { |
| 76 | - if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil { |
| 77 | + if cgroup.Resources.MemorySwap == -1 || memoryUsage.Limit < uint64(cgroup.Resources.MemorySwap) { |
| 78 | + if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil { |
| 79 | return err |
| 80 | } |
| 81 | - if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil { |
| 82 | + if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | } else { |
| 86 | - if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil { |
| 87 | + if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | - if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil { |
| 91 | + if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | } |
| 95 | } else { |
| 96 | if cgroup.Resources.Memory != 0 { |
| 97 | - if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil { |
| 98 | + if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil { |
| 99 | return err |
| 100 | } |
| 101 | } |
| 102 | if cgroup.Resources.MemorySwap != 0 { |
| 103 | - if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil { |
| 104 | + if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | } |
| 108 | @@ -171,13 +169,13 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error { |
| 109 | } |
| 110 | |
| 111 | if cgroup.Resources.MemoryReservation != 0 { |
| 112 | - if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(cgroup.Resources.MemoryReservation, 10)); err != nil { |
| 113 | + if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil { |
| 114 | return err |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if cgroup.Resources.KernelMemoryTCP != 0 { |
| 119 | - if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatUint(cgroup.Resources.KernelMemoryTCP, 10)); err != nil { |
| 120 | + if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemoryTCP, 10)); err != nil { |
| 121 | return err |
| 122 | } |
| 123 | } |
| 124 | diff --git a/libcontainer/configs/cgroup_linux.go b/libcontainer/configs/cgroup_linux.go |
| 125 | index 3e0509de..e15a662f 100644 |
| 126 | --- a/src/import/libcontainer/configs/cgroup_linux.go |
| 127 | +++ b/src/import/libcontainer/configs/cgroup_linux.go |
| 128 | @@ -43,19 +43,19 @@ type Resources struct { |
| 129 | Devices []*Device `json:"devices"` |
| 130 | |
| 131 | // Memory limit (in bytes) |
| 132 | - Memory uint64 `json:"memory"` |
| 133 | + Memory int64 `json:"memory"` |
| 134 | |
| 135 | // Memory reservation or soft_limit (in bytes) |
| 136 | - MemoryReservation uint64 `json:"memory_reservation"` |
| 137 | + MemoryReservation int64 `json:"memory_reservation"` |
| 138 | |
| 139 | // Total memory usage (memory + swap); set `-1` to enable unlimited swap |
| 140 | - MemorySwap uint64 `json:"memory_swap"` |
| 141 | + MemorySwap int64 `json:"memory_swap"` |
| 142 | |
| 143 | // Kernel memory limit (in bytes) |
| 144 | - KernelMemory uint64 `json:"kernel_memory"` |
| 145 | + KernelMemory int64 `json:"kernel_memory"` |
| 146 | |
| 147 | // Kernel memory limit for TCP use (in bytes) |
| 148 | - KernelMemoryTCP uint64 `json:"kernel_memory_tcp"` |
| 149 | + KernelMemoryTCP int64 `json:"kernel_memory_tcp"` |
| 150 | |
| 151 | // CPU shares (relative weight vs. other containers) |
| 152 | CpuShares uint64 `json:"cpu_shares"` |
| 153 | diff --git a/update.go b/update.go |
| 154 | index 0ea90d60..133be999 100644 |
| 155 | --- a/src/import/update.go |
| 156 | +++ b/src/import/update.go |
| 157 | @@ -124,11 +124,11 @@ other options are ignored. |
| 158 | |
| 159 | r := specs.LinuxResources{ |
| 160 | Memory: &specs.LinuxMemory{ |
| 161 | - Limit: u64Ptr(0), |
| 162 | - Reservation: u64Ptr(0), |
| 163 | - Swap: u64Ptr(0), |
| 164 | - Kernel: u64Ptr(0), |
| 165 | - KernelTCP: u64Ptr(0), |
| 166 | + Limit: i64Ptr(0), |
| 167 | + Reservation: i64Ptr(0), |
| 168 | + Swap: i64Ptr(0), |
| 169 | + Kernel: i64Ptr(0), |
| 170 | + KernelTCP: i64Ptr(0), |
| 171 | }, |
| 172 | CPU: &specs.LinuxCPU{ |
| 173 | Shares: u64Ptr(0), |
| 174 | @@ -213,7 +213,7 @@ other options are ignored. |
| 175 | } |
| 176 | for _, pair := range []struct { |
| 177 | opt string |
| 178 | - dest *uint64 |
| 179 | + dest *int64 |
| 180 | }{ |
| 181 | {"memory", r.Memory.Limit}, |
| 182 | {"memory-swap", r.Memory.Swap}, |
| 183 | @@ -232,7 +232,7 @@ other options are ignored. |
| 184 | } else { |
| 185 | v = -1 |
| 186 | } |
| 187 | - *pair.dest = uint64(v) |
| 188 | + *pair.dest = v |
| 189 | } |
| 190 | } |
| 191 | r.Pids.Limit = int64(context.Int("pids-limit")) |
| 192 | -- |
| 193 | 2.11.0 |
| 194 | |