github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/top.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"text/tabwriter"
     8  
     9  	"github.com/containers/libpod/cmd/podman/cliconfig"
    10  	"github.com/containers/libpod/pkg/adapter"
    11  	"github.com/containers/libpod/pkg/util"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  func getDescriptorString() string {
    17  	descriptors, err := util.GetContainerPidInformationDescriptors()
    18  	if err == nil {
    19  		return fmt.Sprintf(`
    20    Format Descriptors:
    21      %s`, strings.Join(descriptors, ","))
    22  	}
    23  	return ""
    24  }
    25  
    26  var (
    27  	topCommand     cliconfig.TopValues
    28  	topDescription = fmt.Sprintf(`Similar to system "top" command.
    29  
    30    Specify format descriptors to alter the output.
    31  
    32    Running "podman top -l pid pcpu seccomp" will print the process ID, the CPU percentage and the seccomp mode of each process of the latest container.
    33  %s`, getDescriptorString())
    34  
    35  	_topCommand = &cobra.Command{
    36  		Use:   "top [flags] CONTAINER [FORMAT-DESCRIPTORS|ARGS]",
    37  		Short: "Display the running processes of a container",
    38  		Long:  topDescription,
    39  		RunE: func(cmd *cobra.Command, args []string) error {
    40  			topCommand.InputArgs = args
    41  			topCommand.GlobalFlags = MainGlobalOpts
    42  			topCommand.Remote = remoteclient
    43  			return topCmd(&topCommand)
    44  		},
    45  		Args: cobra.ArbitraryArgs,
    46  		Example: `podman top ctrID
    47  podman top --latest
    48  podman top ctrID pid seccomp args %C
    49  podman top ctrID -eo user,pid,comm`,
    50  	}
    51  )
    52  
    53  func init() {
    54  	topCommand.Command = _topCommand
    55  	topCommand.SetHelpTemplate(HelpTemplate())
    56  	topCommand.SetUsageTemplate(UsageTemplate())
    57  	flags := topCommand.Flags()
    58  	flags.SetInterspersed(false)
    59  	flags.BoolVar(&topCommand.ListDescriptors, "list-descriptors", false, "")
    60  	markFlagHidden(flags, "list-descriptors")
    61  	flags.BoolVarP(&topCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
    62  	markFlagHiddenForRemoteClient("latest", flags)
    63  }
    64  
    65  func topCmd(c *cliconfig.TopValues) error {
    66  	var err error
    67  	args := c.InputArgs
    68  
    69  	if c.ListDescriptors {
    70  		descriptors, err := util.GetContainerPidInformationDescriptors()
    71  		if err != nil {
    72  			return err
    73  		}
    74  		fmt.Println(strings.Join(descriptors, "\n"))
    75  		return nil
    76  	}
    77  
    78  	if len(args) < 1 && !c.Latest {
    79  		return errors.Errorf("you must provide the name or id of a running container")
    80  	}
    81  
    82  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    83  	if err != nil {
    84  		return errors.Wrapf(err, "error creating libpod runtime")
    85  	}
    86  	defer runtime.DeferredShutdown(false)
    87  
    88  	psOutput, err := runtime.Top(c)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
    93  	for _, proc := range psOutput {
    94  		if _, err := fmt.Fprintln(w, proc); err != nil {
    95  			return err
    96  		}
    97  	}
    98  	return w.Flush()
    99  }