github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/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 "github.com/nicocha30/gvisor-ligolo/pkg/atomicbitops" 19 "github.com/nicocha30/gvisor-ligolo/pkg/context" 20 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/fsimpl/kernfs" 21 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/kernel" 22 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/kernel/auth" 23 ) 24 25 // +stateify savable 26 type cpuController struct { 27 controllerCommon 28 controllerStateless 29 controllerNoResource 30 31 // CFS bandwidth control parameters, values in microseconds. 32 cfsPeriod atomicbitops.Int64 33 cfsQuota atomicbitops.Int64 34 35 // CPU shares, values should be (num core * 1024). 36 shares atomicbitops.Int64 37 } 38 39 var _ controller = (*cpuController)(nil) 40 41 func newCPUController(fs *filesystem, defaults map[string]int64) *cpuController { 42 // Default values for controller parameters from Linux. 43 c := &cpuController{ 44 cfsPeriod: atomicbitops.FromInt64(100000), 45 cfsQuota: atomicbitops.FromInt64(-1), 46 shares: atomicbitops.FromInt64(1024), 47 } 48 49 if val, ok := defaults["cpu.cfs_period_us"]; ok { 50 c.cfsPeriod = atomicbitops.FromInt64(val) 51 delete(defaults, "cpu.cfs_period_us") 52 } 53 if val, ok := defaults["cpu.cfs_quota_us"]; ok { 54 c.cfsQuota = atomicbitops.FromInt64(val) 55 delete(defaults, "cpu.cfs_quota_us") 56 } 57 if val, ok := defaults["cpu.shares"]; ok { 58 c.shares = atomicbitops.FromInt64(val) 59 delete(defaults, "cpu.shares") 60 } 61 62 c.controllerCommon.init(kernel.CgroupControllerCPU, fs) 63 return c 64 } 65 66 // Clone implements controller.Clone. 67 func (c *cpuController) Clone() controller { 68 new := &cpuController{ 69 cfsPeriod: atomicbitops.FromInt64(c.cfsPeriod.Load()), 70 cfsQuota: atomicbitops.FromInt64(c.cfsQuota.Load()), 71 shares: atomicbitops.FromInt64(c.shares.Load()), 72 } 73 new.controllerCommon.cloneFromParent(c) 74 return new 75 } 76 77 // AddControlFiles implements controller.AddControlFiles. 78 func (c *cpuController) AddControlFiles(ctx context.Context, creds *auth.Credentials, _ *cgroupInode, contents map[string]kernfs.Inode) { 79 contents["cpu.cfs_period_us"] = c.fs.newStubControllerFile(ctx, creds, &c.cfsPeriod, true) 80 contents["cpu.cfs_quota_us"] = c.fs.newStubControllerFile(ctx, creds, &c.cfsQuota, true) 81 contents["cpu.shares"] = c.fs.newStubControllerFile(ctx, creds, &c.shares, true) 82 }