github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/cgroupfs/cpu.go (about) 1 // Copyright 2021 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 cgroupfs 16 17 import ( 18 "fmt" 19 20 "github.com/SagerNet/gvisor/pkg/abi/linux" 21 "github.com/SagerNet/gvisor/pkg/context" 22 "github.com/SagerNet/gvisor/pkg/sentry/fsimpl/kernfs" 23 "github.com/SagerNet/gvisor/pkg/sentry/kernel/auth" 24 ) 25 26 // +stateify savable 27 type cpuController struct { 28 controllerCommon 29 30 // CFS bandwidth control parameters, values in microseconds. 31 cfsPeriod int64 32 cfsQuota int64 33 34 // CPU shares, values should be (num core * 1024). 35 shares int64 36 } 37 38 var _ controller = (*cpuController)(nil) 39 40 func newCPUController(fs *filesystem, defaults map[string]int64) *cpuController { 41 // Default values for controller parameters from Linux. 42 c := &cpuController{ 43 cfsPeriod: 100000, 44 cfsQuota: -1, 45 shares: 1024, 46 } 47 48 if val, ok := defaults["cpu.cfs_period_us"]; ok { 49 c.cfsPeriod = val 50 delete(defaults, "cpu.cfs_period_us") 51 } 52 if val, ok := defaults["cpu.cfs_quota_us"]; ok { 53 c.cfsQuota = val 54 delete(defaults, "cpu.cfs_quota_us") 55 } 56 if val, ok := defaults["cpu.shares"]; ok { 57 c.shares = val 58 delete(defaults, "cpu.shares") 59 } 60 61 c.controllerCommon.init(controllerCPU, fs) 62 return c 63 } 64 65 // AddControlFiles implements controller.AddControlFiles. 66 func (c *cpuController) AddControlFiles(ctx context.Context, creds *auth.Credentials, _ *cgroupInode, contents map[string]kernfs.Inode) { 67 contents["cpu.cfs_period_us"] = c.fs.newStaticControllerFile(ctx, creds, linux.FileMode(0644), fmt.Sprintf("%d\n", c.cfsPeriod)) 68 contents["cpu.cfs_quota_us"] = c.fs.newStaticControllerFile(ctx, creds, linux.FileMode(0644), fmt.Sprintf("%d\n", c.cfsQuota)) 69 contents["cpu.shares"] = c.fs.newStaticControllerFile(ctx, creds, linux.FileMode(0644), fmt.Sprintf("%d\n", c.shares)) 70 }