|
| 1 | +// Copyright 2018 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/google/subcommands" |
| 23 | + specs "github.com/opencontainers/runtime-spec/specs-go" |
| 24 | + "gvisor.dev/gvisor/runsc/cmd/util" |
| 25 | + "gvisor.dev/gvisor/runsc/config" |
| 26 | + "gvisor.dev/gvisor/runsc/container" |
| 27 | + "gvisor.dev/gvisor/runsc/flag" |
| 28 | +) |
| 29 | + |
| 30 | +func i64Ptr(i int64) *int64 { return &i } |
| 31 | +func u64Ptr(i uint64) *uint64 { return &i } |
| 32 | +func u16Ptr(i uint16) *uint16 { return &i } |
| 33 | +func boolPtr(b bool) *bool { return &b } |
| 34 | + |
| 35 | +// Update implements subcommands.Command for the "update" command. |
| 36 | +type Update struct { |
| 37 | + resources string |
| 38 | + |
| 39 | + cpuPeriod uint64 |
| 40 | + cpuQuota int64 |
| 41 | + cpuBurst uint64 |
| 42 | + cpuShares uint64 |
| 43 | + cpuRtPeriod uint64 |
| 44 | + cpuRtRuntime int64 |
| 45 | + cpusetCpus string // can this be string or this has to be list of smth? |
| 46 | + cpusetMems string |
| 47 | + cpuIdle int64 |
| 48 | + |
| 49 | + memory int64 |
| 50 | + memoryReservation int64 |
| 51 | + memorySwap int64 |
| 52 | + |
| 53 | + blkioWeight int |
| 54 | + |
| 55 | + pidsLimit int64 |
| 56 | + |
| 57 | + l3CacheSchema string |
| 58 | + memBwSchema string |
| 59 | +} |
| 60 | + |
| 61 | +// Name implements subcommands.Command.Name. |
| 62 | +func (*Update) Name() string { |
| 63 | + return "update" |
| 64 | +} |
| 65 | + |
| 66 | +// Synopsis implements subcommands.Command.Synopsis. |
| 67 | +func (*Update) Synopsis() string { |
| 68 | + return "update container resource constraints" |
| 69 | +} |
| 70 | + |
| 71 | +// Usage implements subcommands.Command.Usage. |
| 72 | +func (*Update) Usage() string { |
| 73 | + return `update [flags] <container id> - update container resource constraints |
| 74 | +` |
| 75 | +} |
| 76 | + |
| 77 | +// SetFlags implements subcommands.Command.SetFlags. |
| 78 | +func (u *Update) SetFlags(f *flag.FlagSet) { |
| 79 | + f.StringVar(&u.resources, "resources", "", `path to the file containing the resources to update or '-' to read from the standard input |
| 80 | +
|
| 81 | +The accepted format is as follow (unchanged values can be omitted): |
| 82 | +
|
| 83 | +{ |
| 84 | + "memory": { |
| 85 | + "limit": 0, |
| 86 | + "reservation": 0, |
| 87 | + "swap": 0, |
| 88 | + "checkBeforeUpdate": true |
| 89 | + }, |
| 90 | + "cpu": { |
| 91 | + "shares": 0, |
| 92 | + "quota": 0, |
| 93 | + "burst": 0, |
| 94 | + "period": 0, |
| 95 | + "realtimeRuntime": 0, |
| 96 | + "realtimePeriod": 0, |
| 97 | + "cpus": "", |
| 98 | + "mems": "", |
| 99 | + "idle": 0 |
| 100 | + }, |
| 101 | + "blockIO": { |
| 102 | + "weight": 0 |
| 103 | + } |
| 104 | +} |
| 105 | +
|
| 106 | +Note: if data is to be read from a file or the standard input, all |
| 107 | +other options are ignored. |
| 108 | +`) |
| 109 | + |
| 110 | + f.Uint64Var(&u.cpuPeriod, "cpu-period", 0, "CPU CFS period to be used for hardcapping (in usecs). 0 to use system default") |
| 111 | + f.Int64Var(&u.cpuQuota, "cpu-quota", 0, "CPU CFS hardcap limit (in usecs). Allowed cpu time in a given period") |
| 112 | + f.Uint64Var(&u.cpuBurst, "cpu-burst", 0, "CPU CFS hardcap burst limit (in usecs). Allowed accumulated cpu time additionally for burst a given period") |
| 113 | + f.Uint64Var(&u.cpuShares, "cpu-share", 0, "CPU shares (relative weight vs. other containers)") |
| 114 | + f.Uint64Var(&u.cpuRtPeriod, "cpu-rt-period", 0, "CPU realtime period to be used for hardcapping (in usecs). 0 to use system default") |
| 115 | + f.Int64Var(&u.cpuRtRuntime, "cpu-rt-runtime", 0, "CPU realtime hardcap limit (in usecs). Allowed cpu time in a given period") |
| 116 | + f.StringVar(&u.cpusetCpus, "cpuset-cpus", "", "CPU(s) to use") |
| 117 | + f.StringVar(&u.cpusetMems, "cpuset-mems", "", "Memory node(s) to use") |
| 118 | + f.Int64Var(&u.cpuIdle, "cpu-idle", 0, "set cgroup SCHED_IDLE or not, 0: default behavior, 1: SCHED_IDLE") |
| 119 | + |
| 120 | + f.Int64Var(&u.memory, "memory", 0, "Memory limit (in bytes)") |
| 121 | + f.Int64Var(&u.memoryReservation, "memory-reservation", 0, "Memory reservation or soft_limit (in bytes)") |
| 122 | + f.Int64Var(&u.memorySwap, "memory-swap", 0, "Total memory usage (memory + swap); set '-1' to enable unlimited swap") |
| 123 | + |
| 124 | + f.IntVar(&u.blkioWeight, "blkio-weight", 0, "Specifies per cgroup weight, range is from 10 to 1000") |
| 125 | + |
| 126 | + f.Int64Var(&u.pidsLimit, "pids-limit", 0, "Maximum number of pids allowed in the container") |
| 127 | + |
| 128 | + f.StringVar(&u.l3CacheSchema, "l3-cache-schema", "", "The string of Intel RDT/CAT L3 cache schema") |
| 129 | + f.StringVar(&u.memBwSchema, "mem-bw-schema", "", "The string of Intel RDT/MBA memory bandwidth schema") |
| 130 | +} |
| 131 | + |
| 132 | +// Execute implements subcommands.Command.Execute. |
| 133 | +func (u *Update) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { |
| 134 | + if f.NArg() != 1 { |
| 135 | + f.Usage() |
| 136 | + return subcommands.ExitUsageError |
| 137 | + } |
| 138 | + |
| 139 | + id := f.Arg(0) |
| 140 | + conf := args[0].(*config.Config) |
| 141 | + |
| 142 | + c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{}) |
| 143 | + if err != nil { |
| 144 | + util.Fatalf("loading container %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + r := specs.LinuxResources{ |
| 148 | + Memory: &specs.LinuxMemory{ |
| 149 | + Limit: i64Ptr(0), |
| 150 | + Reservation: i64Ptr(0), |
| 151 | + Swap: i64Ptr(0), |
| 152 | + CheckBeforeUpdate: boolPtr(false), |
| 153 | + }, |
| 154 | + CPU: &specs.LinuxCPU{ |
| 155 | + Shares: u64Ptr(0), |
| 156 | + Quota: i64Ptr(0), |
| 157 | + Burst: u64Ptr(0), |
| 158 | + Period: u64Ptr(0), |
| 159 | + RealtimeRuntime: i64Ptr(0), |
| 160 | + RealtimePeriod: u64Ptr(0), |
| 161 | + Cpus: "", |
| 162 | + Mems: "", |
| 163 | + }, |
| 164 | + BlockIO: &specs.LinuxBlockIO{ |
| 165 | + Weight: u16Ptr(0), |
| 166 | + }, |
| 167 | + Pids: &specs.LinuxPids{ |
| 168 | + Limit: 0, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + if in := u.resources; in != "" { |
| 173 | + var ( |
| 174 | + f *os.File |
| 175 | + err error |
| 176 | + ) |
| 177 | + switch in { |
| 178 | + case "-": |
| 179 | + f = os.Stdin |
| 180 | + default: |
| 181 | + f, err = os.Open(in) |
| 182 | + if err != nil { |
| 183 | + return util.Errorf("opening %q: %v", in, err) |
| 184 | + } |
| 185 | + defer f.Close() |
| 186 | + } |
| 187 | + err = json.NewDecoder(f).Decode(&r) |
| 188 | + if err != nil { |
| 189 | + return util.Errorf("decoding %q: %v", in, err) |
| 190 | + } |
| 191 | + } else { |
| 192 | + r.Memory.Limit = i64Ptr(u.memory) |
| 193 | + r.Memory.Reservation = i64Ptr(u.memoryReservation) |
| 194 | + r.Memory.Swap = i64Ptr(u.memorySwap) |
| 195 | + |
| 196 | + r.CPU.Shares = u64Ptr(u.cpuShares) |
| 197 | + r.CPU.Quota = i64Ptr(u.cpuQuota) |
| 198 | + r.CPU.Burst = u64Ptr(u.cpuBurst) |
| 199 | + r.CPU.Period = u64Ptr(u.cpuPeriod) |
| 200 | + r.CPU.RealtimeRuntime = i64Ptr(u.cpuRtRuntime) |
| 201 | + r.CPU.RealtimePeriod = u64Ptr(u.cpuRtPeriod) |
| 202 | + r.CPU.Cpus = u.cpusetCpus |
| 203 | + r.CPU.Mems = u.cpusetMems |
| 204 | + |
| 205 | + r.BlockIO.Weight = u16Ptr(uint16(u.blkioWeight)) |
| 206 | + |
| 207 | + r.Pids.Limit = u.pidsLimit |
| 208 | + } |
| 209 | + |
| 210 | + if err = c.Set(&r); err != nil { |
| 211 | + return util.Errorf("setting resources: %v", err) |
| 212 | + } |
| 213 | + |
| 214 | + return subcommands.ExitSuccess |
| 215 | +} |
0 commit comments