cpu.go (1740B)
1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cgroup2 18 19 import ( 20 "math" 21 "strconv" 22 "strings" 23 ) 24 25 type CPUMax string 26 27 func NewCPUMax(quota *int64, period *uint64) CPUMax { 28 max := "max" 29 if quota != nil { 30 max = strconv.FormatInt(*quota, 10) 31 } 32 return CPUMax(strings.Join([]string{max, strconv.FormatUint(*period, 10)}, " ")) 33 } 34 35 type CPU struct { 36 Weight *uint64 37 Max CPUMax 38 Cpus string 39 Mems string 40 } 41 42 func (c CPUMax) extractQuotaAndPeriod() (int64, uint64) { 43 var ( 44 quota int64 45 period uint64 46 ) 47 values := strings.Split(string(c), " ") 48 if values[0] == "max" { 49 quota = math.MaxInt64 50 } else { 51 quota, _ = strconv.ParseInt(values[0], 10, 64) 52 } 53 period, _ = strconv.ParseUint(values[1], 10, 64) 54 return quota, period 55 } 56 57 func (r *CPU) Values() (o []Value) { 58 if r.Weight != nil { 59 o = append(o, Value{ 60 filename: "cpu.weight", 61 value: *r.Weight, 62 }) 63 } 64 if r.Max != "" { 65 o = append(o, Value{ 66 filename: "cpu.max", 67 value: r.Max, 68 }) 69 } 70 if r.Cpus != "" { 71 o = append(o, Value{ 72 filename: "cpuset.cpus", 73 value: r.Cpus, 74 }) 75 } 76 if r.Mems != "" { 77 o = append(o, Value{ 78 filename: "cpuset.mems", 79 value: r.Mems, 80 }) 81 } 82 return o 83 }