github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/process/system.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 process 18 19 import ( 20 "fmt" 21 "strconv" 22 "strings" 23 24 "k8s.io/apimachinery/pkg/util/sets" 25 ) 26 27 // CPUSetParse constructs an integer cpu set from a Linux CPU list formatted string. 28 // See: http://man7.org/linux/man-pages/man7/cpuset.7.html#FORMATS 29 func CPUSetParse(s string) (sets.Int, error) { 30 if s == "" { 31 return sets.Int{}, nil 32 } 33 34 b := sets.Int{} 35 ranges := strings.Split(s, ",") 36 37 for _, r := range ranges { 38 boundaries := strings.Split(r, "-") 39 if len(boundaries) == 1 { 40 elem, err := strconv.Atoi(boundaries[0]) 41 if err != nil { 42 return sets.Int{}, fmt.Errorf("parse index 0 of 1 boundaries failed: %w", err) 43 } 44 45 b.Insert(elem) 46 } else if len(boundaries) == 2 { 47 start, err := strconv.Atoi(boundaries[0]) 48 if err != nil { 49 return sets.Int{}, fmt.Errorf("parse index 0 of 2 boundaries failed: %w", err) 50 } 51 52 end, err := strconv.Atoi(boundaries[1]) 53 if err != nil { 54 return sets.Int{}, fmt.Errorf("parse index 1 of 2 boundaries failed: %w", err) 55 } 56 57 for e := start; e <= end; e++ { 58 b.Insert(e) 59 } 60 } 61 } 62 63 return b, nil 64 }