github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/runsc/cmd/usage.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 cmd
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  
    21  	"github.com/google/subcommands"
    22  	"github.com/metacubex/gvisor/runsc/cmd/util"
    23  	"github.com/metacubex/gvisor/runsc/config"
    24  	"github.com/metacubex/gvisor/runsc/container"
    25  	"github.com/metacubex/gvisor/runsc/flag"
    26  )
    27  
    28  // Usage implements subcommands.Command for the "usage" command.
    29  type Usage struct {
    30  	full bool
    31  	fd   bool
    32  }
    33  
    34  // Name implements subcommands.Command.Name.
    35  func (*Usage) Name() string {
    36  	return "usage"
    37  }
    38  
    39  // Synopsis implements subcommands.Command.Synopsis.
    40  func (*Usage) Synopsis() string {
    41  	return "Usage shows application memory usage across various categories in bytes."
    42  }
    43  
    44  // Usage implements subcommands.Command.Usage.
    45  func (*Usage) Usage() string {
    46  	return `usage [flags] <container id> - print memory usages to standard output.
    47  `
    48  }
    49  
    50  // SetFlags implements subcommands.Command.SetFlags.
    51  func (u *Usage) SetFlags(f *flag.FlagSet) {
    52  	f.BoolVar(&u.full, "full", false, "enumerate all usage by categories")
    53  	f.BoolVar(&u.fd, "fd", false, "retrieves a subset of usage through the established usage FD")
    54  }
    55  
    56  // Execute implements subcommands.Command.Execute.
    57  func (u *Usage) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    58  	if f.NArg() < 1 {
    59  		f.Usage()
    60  		return subcommands.ExitUsageError
    61  	}
    62  
    63  	id := f.Arg(0)
    64  	conf := args[0].(*config.Config)
    65  
    66  	cont, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true})
    67  	if err != nil {
    68  		util.Fatalf("loading container: %v", err)
    69  	}
    70  
    71  	if u.fd {
    72  		m, err := cont.Sandbox.UsageFD()
    73  		if err != nil {
    74  			util.Fatalf("usagefd failed: %v", err)
    75  		}
    76  
    77  		mapped, unknown, total, err := m.Fetch()
    78  		if err != nil {
    79  			util.Fatalf("Fetch memory usage failed: %v", err)
    80  		}
    81  
    82  		util.Infof("Mapped %v, Unknown %v, Total %v\n", mapped, unknown, total)
    83  	} else {
    84  		m, err := cont.Sandbox.Usage(u.full)
    85  		if err != nil {
    86  			util.Fatalf("usage failed: %v", err)
    87  		}
    88  		encoder := json.NewEncoder(&util.Writer{})
    89  		encoder.SetIndent("", "  ")
    90  		if err := encoder.Encode(m); err != nil {
    91  			util.Fatalf("Encode MemoryUsage failed: %v", err)
    92  		}
    93  	}
    94  	return subcommands.ExitSuccess
    95  }