github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/proc/stat.go (about)

     1  // Copyright 2018 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 proc
    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/fs/proc/seqfile"
    24  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    25  )
    26  
    27  // LINT.IfChange
    28  
    29  // statData backs /proc/stat.
    30  //
    31  // +stateify savable
    32  type statData struct {
    33  	// k is the owning Kernel.
    34  	k *kernel.Kernel
    35  }
    36  
    37  // NeedsUpdate implements seqfile.SeqSource.NeedsUpdate.
    38  func (*statData) NeedsUpdate(generation int64) bool {
    39  	return true
    40  }
    41  
    42  // cpuStats contains the breakdown of CPU time for /proc/stat.
    43  type cpuStats struct {
    44  	// user is time spent in userspace tasks with non-positive niceness.
    45  	user uint64
    46  
    47  	// nice is time spent in userspace tasks with positive niceness.
    48  	nice uint64
    49  
    50  	// system is time spent in non-interrupt kernel context.
    51  	system uint64
    52  
    53  	// idle is time spent idle.
    54  	idle uint64
    55  
    56  	// ioWait is time spent waiting for IO.
    57  	ioWait uint64
    58  
    59  	// irq is time spent in interrupt context.
    60  	irq uint64
    61  
    62  	// softirq is time spent in software interrupt context.
    63  	softirq uint64
    64  
    65  	// steal is involuntary wait time.
    66  	steal uint64
    67  
    68  	// guest is time spent in guests with non-positive niceness.
    69  	guest uint64
    70  
    71  	// guestNice is time spent in guests with positive niceness.
    72  	guestNice uint64
    73  }
    74  
    75  // String implements fmt.Stringer.
    76  func (c cpuStats) String() string {
    77  	return fmt.Sprintf("%d %d %d %d %d %d %d %d %d %d", c.user, c.nice, c.system, c.idle, c.ioWait, c.irq, c.softirq, c.steal, c.guest, c.guestNice)
    78  }
    79  
    80  // ReadSeqFileData implements seqfile.SeqSource.ReadSeqFileData.
    81  func (s *statData) ReadSeqFileData(ctx context.Context, h seqfile.SeqHandle) ([]seqfile.SeqData, int64) {
    82  	if h != nil {
    83  		return nil, 0
    84  	}
    85  
    86  	var buf bytes.Buffer
    87  
    88  	// TODO(b/37226836): We currently export only zero CPU stats. We could
    89  	// at least provide some aggregate stats.
    90  	var cpu cpuStats
    91  	fmt.Fprintf(&buf, "cpu  %s\n", cpu)
    92  
    93  	for c, max := uint(0), s.k.ApplicationCores(); c < max; c++ {
    94  		fmt.Fprintf(&buf, "cpu%d %s\n", c, cpu)
    95  	}
    96  
    97  	// The total number of interrupts is dependent on the CPUs and PCI
    98  	// devices on the system. See arch_probe_nr_irqs.
    99  	//
   100  	// Since we don't report real interrupt stats, just choose an arbitrary
   101  	// value from a representative VM.
   102  	const numInterrupts = 256
   103  
   104  	// The Kernel doesn't handle real interrupts, so report all zeroes.
   105  	// TODO(b/37226836): We could count page faults as #PF.
   106  	fmt.Fprintf(&buf, "intr 0") // total
   107  	for i := 0; i < numInterrupts; i++ {
   108  		fmt.Fprintf(&buf, " 0")
   109  	}
   110  	fmt.Fprintf(&buf, "\n")
   111  
   112  	// Total number of context switches.
   113  	// TODO(b/37226836): Count this.
   114  	fmt.Fprintf(&buf, "ctxt 0\n")
   115  
   116  	// CLOCK_REALTIME timestamp from boot, in seconds.
   117  	fmt.Fprintf(&buf, "btime %d\n", s.k.Timekeeper().BootTime().Seconds())
   118  
   119  	// Total number of clones.
   120  	// TODO(b/37226836): Count this.
   121  	fmt.Fprintf(&buf, "processes 0\n")
   122  
   123  	// Number of runnable tasks.
   124  	// TODO(b/37226836): Count this.
   125  	fmt.Fprintf(&buf, "procs_running 0\n")
   126  
   127  	// Number of tasks waiting on IO.
   128  	// TODO(b/37226836): Count this.
   129  	fmt.Fprintf(&buf, "procs_blocked 0\n")
   130  
   131  	// Number of each softirq handled.
   132  	fmt.Fprintf(&buf, "softirq 0") // total
   133  	for i := 0; i < linux.NumSoftIRQ; i++ {
   134  		fmt.Fprintf(&buf, " 0")
   135  	}
   136  	fmt.Fprintf(&buf, "\n")
   137  
   138  	return []seqfile.SeqData{
   139  		{
   140  			Buf:    buf.Bytes(),
   141  			Handle: (*statData)(nil),
   142  		},
   143  	}, 0
   144  }
   145  
   146  // LINT.ThenChange(../../fsimpl/proc/task_files.go)