top

package
v0.0.0-...-886287a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package top wraps Linux 'top' command.

Index

Constants

This section is empty.

Variables

View Source
var DefaultExecPath = "/usr/bin/top"

DefaultExecPath is the default 'top' command path.

View Source
var Headers = []string{
	"PID",
	"USER",
	"PR",
	"NI",
	"VIRT",
	"RES",
	"SHR",
	"S",
	"%CPU",
	"%MEM",
	"TIME+",
	"COMMAND",
}

Headers is the headers in 'top' output.

View Source
var RowSchema = schema.RawData{
	IsYAML: false,
	Columns: []schema.Column{
		{Name: "PID", Godoc: "pid of the process", Kind: reflect.Int64},
		{Name: "USER", Godoc: "user name", Kind: reflect.String},
		{Name: "PR", Godoc: "priority", Kind: reflect.String},
		{Name: "NI", Godoc: "nice value of the task", Kind: reflect.String},
		{Name: "VIRT", Godoc: "total amount  of virtual memory used by the task (in KiB)", Kind: reflect.String},
		{Name: "RES", Godoc: "non-swapped physical memory a task is using (in KiB)", Kind: reflect.String},
		{Name: "SHR", Godoc: "amount of shared memory available to a task, not all of which is typically resident (in KiB)", Kind: reflect.String},
		{Name: "S", Godoc: "process status", Kind: reflect.String},
		{Name: "CPUPercent", Godoc: "%CPU", Kind: reflect.Float64},
		{Name: "MEMPercent", Godoc: "%MEM", Kind: reflect.Float64},
		{Name: "TIME", Godoc: "CPU time (TIME+)", Kind: reflect.String},
		{Name: "COMMAND", Godoc: "command", Kind: reflect.String},
	},
	ColumnsToParse: map[string]schema.RawDataType{
		"S":    schema.TypeStatus,
		"VIRT": schema.TypeBytes,
		"RES":  schema.TypeBytes,
		"SHR":  schema.TypeBytes,
	},
}

RowSchema represents a row in 'top' command output. Reference http://man7.org/linux/man-pages/man1/top.1.html.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Exec is the 'top' command path.
	// Defaults to '/usr/bin/top'.
	Exec string

	// Limit limits the iteration of 'top' commands to run before exit.
	// If 1, 'top' prints out the current processes and exits.
	// It's '-n' flag.
	Limit int

	// IntervalSecond is the delay time between updates.
	// Default is 1 second.
	// It's '-d' flag.
	IntervalSecond float64

	// PID specifies the PID to monitor.
	// It's '-p' flag.
	PID int64

	// Writer stores 'top' command outputs.
	Writer io.Writer
	// contains filtered or unexported fields
}

Config configures 'top' command runs.

func (*Config) Flags

func (cfg *Config) Flags() (fs []string)

Flags returns the 'top' command flags.

func (*Config) StartStream

func (cfg *Config) StartStream() (*Stream, error)

StartStream starts 'top' command stream.

type Row

type Row struct {
	// PID is pid of the process.
	PID int64 `column:"pid"`
	// USER is user name.
	USER string `column:"user"`
	// PR is priority.
	PR string `column:"pr"`
	// NI is nice value of the task.
	NI string `column:"ni"`
	// VIRT is total amount  of virtual memory used by the task (in KiB).
	VIRT            string `column:"virt"`
	VIRTBytesN      uint64 `column:"virt_bytes_n"`
	VIRTParsedBytes string `column:"virt_parsed_bytes"`
	// RES is non-swapped physical memory a task is using (in KiB).
	RES            string `column:"res"`
	RESBytesN      uint64 `column:"res_bytes_n"`
	RESParsedBytes string `column:"res_parsed_bytes"`
	// SHR is amount of shared memory available to a task, not all of which is typically resident (in KiB).
	SHR            string `column:"shr"`
	SHRBytesN      uint64 `column:"shr_bytes_n"`
	SHRParsedBytes string `column:"shr_parsed_bytes"`
	// S is process status.
	S             string `column:"s"`
	SParsedStatus string `column:"s_parsed_status"`
	// CPUPercent is %CPU.
	CPUPercent float64 `column:"cpupercent"`
	// MEMPercent is %MEM.
	MEMPercent float64 `column:"mempercent"`
	// TIME is CPU time (TIME+).
	TIME string `column:"time"`
	// COMMAND is command.
	COMMAND string `column:"command"`
}

Row represents a row in 'top' command output.

func Get

func Get(topPath string, pid int64) ([]Row, error)

Get returns all entries in 'top' command. If pid<1, it reads all processes in 'top' command. This is one-time command.

func Parse

func Parse(s string) ([]Row, error)

Parse parses 'top' command output and returns the rows.

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

Stream provides top command output stream.

func (*Stream) ErrChan

func (str *Stream) ErrChan() <-chan error

ErrChan returns the error from stream.

func (*Stream) Latest

func (str *Stream) Latest() map[int64]Row

Latest returns the latest top command outputs.

func (*Stream) Stop

func (str *Stream) Stop() error

Stop kills the 'top' process and waits for it to exit.

func (*Stream) Wait

func (str *Stream) Wait() error

Wait just waits for the 'top' process to exit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL