|
| 1 | +// Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package cpuset |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | +) |
| 19 | + |
| 20 | +// Builder will allow building of cpuset fields |
| 21 | +// http://man7.org/linux/man-pages/man7/cpuset.7.html |
| 22 | +type Builder struct { |
| 23 | + cpus []int |
| 24 | + cpuRanges []_range |
| 25 | + |
| 26 | + mems []int |
| 27 | + memRanges []_range |
| 28 | +} |
| 29 | + |
| 30 | +type _range struct { |
| 31 | + min, max int |
| 32 | +} |
| 33 | + |
| 34 | +// CPUSet represents the linux CPUSet which is a series of configurable values |
| 35 | +// that allow processes to run on a specific CPUs and those CPUs are then bound |
| 36 | +// to the memory nodes specified. |
| 37 | +type CPUSet struct { |
| 38 | + cpus string |
| 39 | + mems string |
| 40 | +} |
| 41 | + |
| 42 | +// CPUs returns the cpuset.cpus string |
| 43 | +func (c CPUSet) CPUs() string { |
| 44 | + return c.cpus |
| 45 | +} |
| 46 | + |
| 47 | +// Mems returns the cpuset.mems string |
| 48 | +func (c CPUSet) Mems() string { |
| 49 | + return c.mems |
| 50 | +} |
| 51 | + |
| 52 | +// AddCPU will add the physical CPU number that the process is allowed to run |
| 53 | +// on. |
| 54 | +func (b Builder) AddCPU(cpu int) Builder { |
| 55 | + b.cpus = append(b.cpus, cpu) |
| 56 | + return b |
| 57 | +} |
| 58 | + |
| 59 | +// AddCPURange adds a range of physical CPU numbers that the process is allowed |
| 60 | +// to run on |
| 61 | +func (b Builder) AddCPURange(min, max int) Builder { |
| 62 | + b.cpuRanges = append(b.cpuRanges, _range{ |
| 63 | + min: min, |
| 64 | + max: max, |
| 65 | + }) |
| 66 | + |
| 67 | + return b |
| 68 | +} |
| 69 | + |
| 70 | +// AddMem adds a memory node which limit where the cpus can allocate memory |
| 71 | +func (b Builder) AddMem(mem int) Builder { |
| 72 | + b.mems = append(b.mems, mem) |
| 73 | + return b |
| 74 | +} |
| 75 | + |
| 76 | +// AddMemRange adds a range of memory nodes by utilizing the minimum node to |
| 77 | +// use to the maximum node to use. |
| 78 | +func (b Builder) AddMemRange(min, max int) Builder { |
| 79 | + b.memRanges = append(b.memRanges, _range{ |
| 80 | + min: min, |
| 81 | + max: max, |
| 82 | + }) |
| 83 | + |
| 84 | + return b |
| 85 | +} |
| 86 | + |
| 87 | +// Build constructs a new CPUSet |
| 88 | +func (b Builder) Build() CPUSet { |
| 89 | + cpus := stringify(b.cpus, b.cpuRanges) |
| 90 | + mems := stringify(b.mems, b.memRanges) |
| 91 | + |
| 92 | + return CPUSet{ |
| 93 | + cpus: cpus, |
| 94 | + mems: mems, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func stringify(elems []int, ranges []_range) string { |
| 99 | + str := "" |
| 100 | + for _, elem := range elems { |
| 101 | + sep := "," |
| 102 | + if len(str) == 0 { |
| 103 | + sep = "" |
| 104 | + } |
| 105 | + |
| 106 | + str += fmt.Sprintf("%s%d", sep, elem) |
| 107 | + } |
| 108 | + |
| 109 | + for _, r := range ranges { |
| 110 | + sep := "," |
| 111 | + if len(str) == 0 { |
| 112 | + sep = "" |
| 113 | + } |
| 114 | + |
| 115 | + str += fmt.Sprintf("%s%d-%d", sep, r.min, r.max) |
| 116 | + } |
| 117 | + |
| 118 | + return str |
| 119 | +} |
0 commit comments