github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/cgroupfs/cpuacct.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  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    22  	"github.com/SagerNet/gvisor/pkg/context"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/fsimpl/kernfs"
    24  	"github.com/SagerNet/gvisor/pkg/sentry/kernel/auth"
    25  	"github.com/SagerNet/gvisor/pkg/sentry/usage"
    26  )
    27  
    28  // +stateify savable
    29  type cpuacctController struct {
    30  	controllerCommon
    31  }
    32  
    33  var _ controller = (*cpuacctController)(nil)
    34  
    35  func newCPUAcctController(fs *filesystem) *cpuacctController {
    36  	c := &cpuacctController{}
    37  	c.controllerCommon.init(controllerCPUAcct, fs)
    38  	return c
    39  }
    40  
    41  // AddControlFiles implements controller.AddControlFiles.
    42  func (c *cpuacctController) AddControlFiles(ctx context.Context, creds *auth.Credentials, cg *cgroupInode, contents map[string]kernfs.Inode) {
    43  	cpuacctCG := &cpuacctCgroup{cg}
    44  	contents["cpuacct.stat"] = c.fs.newControllerFile(ctx, creds, &cpuacctStatData{cpuacctCG})
    45  	contents["cpuacct.usage"] = c.fs.newControllerFile(ctx, creds, &cpuacctUsageData{cpuacctCG})
    46  	contents["cpuacct.usage_user"] = c.fs.newControllerFile(ctx, creds, &cpuacctUsageUserData{cpuacctCG})
    47  	contents["cpuacct.usage_sys"] = c.fs.newControllerFile(ctx, creds, &cpuacctUsageSysData{cpuacctCG})
    48  }
    49  
    50  // +stateify savable
    51  type cpuacctCgroup struct {
    52  	*cgroupInode
    53  }
    54  
    55  func (c *cpuacctCgroup) collectCPUStats() usage.CPUStats {
    56  	var cs usage.CPUStats
    57  	c.fs.tasksMu.RLock()
    58  	// Note: This isn't very accurate, since the tasks are potentially
    59  	// still running as we accumulate their stats.
    60  	for t := range c.ts {
    61  		cs.Accumulate(t.CPUStats())
    62  	}
    63  	c.fs.tasksMu.RUnlock()
    64  	return cs
    65  }
    66  
    67  // +stateify savable
    68  type cpuacctStatData struct {
    69  	*cpuacctCgroup
    70  }
    71  
    72  // Generate implements vfs.DynamicBytesSource.Generate.
    73  func (d *cpuacctStatData) Generate(ctx context.Context, buf *bytes.Buffer) error {
    74  	cs := d.collectCPUStats()
    75  	fmt.Fprintf(buf, "user %d\n", linux.ClockTFromDuration(cs.UserTime))
    76  	fmt.Fprintf(buf, "system %d\n", linux.ClockTFromDuration(cs.SysTime))
    77  	return nil
    78  }
    79  
    80  // +stateify savable
    81  type cpuacctUsageData struct {
    82  	*cpuacctCgroup
    83  }
    84  
    85  // Generate implements vfs.DynamicBytesSource.Generate.
    86  func (d *cpuacctUsageData) Generate(ctx context.Context, buf *bytes.Buffer) error {
    87  	cs := d.collectCPUStats()
    88  	fmt.Fprintf(buf, "%d\n", cs.UserTime.Nanoseconds()+cs.SysTime.Nanoseconds())
    89  	return nil
    90  }
    91  
    92  // +stateify savable
    93  type cpuacctUsageUserData struct {
    94  	*cpuacctCgroup
    95  }
    96  
    97  // Generate implements vfs.DynamicBytesSource.Generate.
    98  func (d *cpuacctUsageUserData) Generate(ctx context.Context, buf *bytes.Buffer) error {
    99  	cs := d.collectCPUStats()
   100  	fmt.Fprintf(buf, "%d\n", cs.UserTime.Nanoseconds())
   101  	return nil
   102  }
   103  
   104  // +stateify savable
   105  type cpuacctUsageSysData struct {
   106  	*cpuacctCgroup
   107  }
   108  
   109  // Generate implements vfs.DynamicBytesSource.Generate.
   110  func (d *cpuacctUsageSysData) Generate(ctx context.Context, buf *bytes.Buffer) error {
   111  	cs := d.collectCPUStats()
   112  	fmt.Fprintf(buf, "%d\n", cs.SysTime.Nanoseconds())
   113  	return nil
   114  }