process

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Overview

Package process provides methods for gathering information about active processes

Index

Examples

Constants

View Source
const (
	STATE_RUNNING   = "R"
	STATE_SLEEPING  = "S"
	STATE_DISK_WAIT = "D"
	STATE_ZOMBIE    = "Z"
	STATE_STOPPED   = "T"
	STATE_DEAD      = "X"
	STATE_WAKEKILL  = "K"
	STATE_WAKING    = "W"
	STATE_PARKED    = "P"
)

Process state flags

View Source
const (
	PRIO_CLASS_NONE        = 0
	PRIO_CLASS_REAL_TIME   = 1
	PRIO_CLASS_BEST_EFFORT = 2
	PRIO_CLASS_IDLE        = 3
)

Variables

This section is empty.

Functions

func CalculateCPUUsage

func CalculateCPUUsage(s1, s2 ProcSample, duration time.Duration) float64

CalculateCPUUsage calculates CPU usage

Example
pid := 1345
duration := time.Second * 15

sample1, _ := GetSample(pid)
time.Sleep(duration)
sample2, _ := GetSample(pid)

fmt.Printf("CPU Usage: %g%%\n", CalculateCPUUsage(sample1, sample2, duration))
Output:

func GetCPUPriority

func GetCPUPriority(pid int) (int, int, error)

GetCPUPriority returns process CPU scheduling priority (PR, NI, error)

Example
pid := os.Getpid()
pr, ni, err := GetCPUPriority(pid)

if err != nil {
	return
}

fmt.Printf("PR: %d | NI: %d\n", pr, ni)
Output:

func GetIOPriority

func GetIOPriority(pid int) (int, int, error)

GetIOPriority returns process IO scheduling priority (class, classdata, error)

Example
pid := os.Getpid()
class, classdata, err := GetIOPriority(pid)

if err != nil {
	return
}

fmt.Printf("Class: %d | Classdata: %d\n", class, classdata)
Output:

func SetCPUPriority

func SetCPUPriority(pid, niceness int) error

SetCPUPriority sets process CPU scheduling priority

Example
pid := os.Getpid()
err := SetCPUPriority(pid, -20)

if err != nil {
	return
}

pr, ni, err := GetCPUPriority(pid)

if err != nil {
	return
}

fmt.Printf("PR: %d | NI: %d\n", pr, ni)
Output:

func SetIOPriority

func SetIOPriority(pid, class, classdata int) error

SetIOPriority sets process IO scheduling priority

Example
pid := os.Getpid()
err := SetIOPriority(pid, PRIO_CLASS_REAL_TIME, 5)

if err != nil {
	return
}

class, classdata, err := GetIOPriority(pid)

if err != nil {
	return
}

fmt.Printf("Class: %d | Classdata: %d\n", class, classdata)
Output:

Types

type MemInfo

type MemInfo struct {
	VmPeak uint64 `json:"peak"` // Peak virtual memory size
	VmSize uint64 `json:"size"` // Virtual memory size
	VmLck  uint64 `json:"lck"`  // Locked memory size
	VmPin  uint64 `json:"pin"`  // Pinned memory size (since Linux 3.2)
	VmHWM  uint64 `json:"hwm"`  // Peak resident set size ("high water mark")
	VmRSS  uint64 `json:"rss"`  // Resident set size
	VmData uint64 `json:"data"` // Size of data
	VmStk  uint64 `json:"stk"`  // Size of stack
	VmExe  uint64 `json:"exe"`  // Size of text segments
	VmLib  uint64 `json:"lib"`  // Shared library code size
	VmPTE  uint64 `json:"pte"`  // Page table entries size (since Linux 2.6.10)
	VmSwap uint64 `json:"swap"` // Swap size
}

MemInfo contains process memory usage stats

func GetMemInfo

func GetMemInfo(pid int) (*MemInfo, error)

GetMemInfo returns info about process memory usage

Example
info, err := GetMemInfo(1000)

if err != nil {
	return
}

fmt.Printf("%v\n", info)
Output:

type MountInfo

type MountInfo struct {
	MountID        uint16   `json:"mount_id"`        // Unique identifier of the mount (may be reused after umount)
	ParentID       uint16   `json:"parent_id"`       // ID of parent (or of self for the top of the mount tree)
	StDevMajor     uint16   `json:"stdev_major"`     // Major value of st_dev for files on filesystem
	StDevMinor     uint16   `json:"stdev_minor"`     // Minor value of st_dev for files on filesystem
	Root           string   `json:"root"`            // Root of the mount within the filesystem
	MountPoint     string   `json:"mount_point"`     // Mount point relative to the process's root
	MountOptions   []string `json:"mount_options"`   // Per mount options
	OptionalFields []string `json:"optional_fields"` // Zero or more fields of the form "tag[:value]"
	FSType         string   `json:"fs_type"`         // Name of filesystem of the form "type[.subtype]"
	MountSource    string   `json:"mount_source"`    // Filesystem specific information or "none"
	SuperOptions   []string `json:"super_options"`   // Per super block options
}

MountInfo contains information about mounts https://www.kernel.org/doc/Documentation/filesystems/proc.txt

func GetMountInfo

func GetMountInfo(pid int) ([]*MountInfo, error)

GetMountInfo returns info about process mounts

Example
info, err := GetMountInfo(1000)

if err != nil {
	return
}

fmt.Printf("%v\n", info)
Output:

type ProcInfo

type ProcInfo struct {
	PID        int    `json:"pid"`         // The process ID
	Comm       string `json:"comm"`        // The filename of the executable, in parentheses
	State      string `json:"state"`       // Process state
	PPID       int    `json:"ppid"`        // The PID of the parent of this process
	Session    int    `json:"session"`     // The session ID of the process
	TTYNR      int    `json:"tty_nr"`      // The controlling terminal of the process
	TPGid      int    `json:"tpgid"`       // The ID of the foreground process group of the controlling terminal of the process
	UTime      uint64 `json:"utime"`       // Amount of time that this process has been scheduled in user mode, measured in clock ticks
	STime      uint64 `json:"stime"`       // Amount of time that this process has been scheduled in kernel mode, measured in clock ticks
	CUTime     uint64 `json:"cutime"`      // Amount of time that this process's waited-for children have been scheduled in user mode, measured in clock ticks
	CSTime     uint64 `json:"cstime"`      // Amount of time that this process's waited-for children have been scheduled in kernel mode, measured in clock ticks
	Priority   int    `json:"priority"`    // Priority
	Nice       int    `json:"nice"`        // The nice value
	NumThreads int    `json:"num_threads"` // Number of threads in this process
}

ProcInfo contains partial info from /proc/[PID]/stat

func GetInfo

func GetInfo(pid int) (*ProcInfo, error)

GetInfo returns process info from procfs

func (*ProcInfo) ToSample

func (pi *ProcInfo) ToSample() ProcSample

ToSample converts ProcInfo to ProcSample for CPU usage calculation

type ProcSample

type ProcSample uint

ProcSample contains value for usage calculation

func GetSample

func GetSample(pid int) (ProcSample, error)

GetSample returns ProcSample for CPU usage calculation

type ProcessInfo

type ProcessInfo struct {
	Command  string         // Full command
	User     string         // Username
	PID      int            // PID
	Parent   int            // Parent process PID
	Childs   []*ProcessInfo // Slice with child processes
	IsThread bool           // True if process is thread
}

ProcessInfo contains basic info about process

func GetList

func GetList() ([]*ProcessInfo, error)

GetList returns slice with all active processes on the system

Example
processes, err := GetList()

if err != nil {
	return
}

// processes is slice with info about all active processes
fmt.Printf("%v\n", processes)
Output:

func GetTree

func GetTree(pid ...int) (*ProcessInfo, error)

GetTree returns root process with all subprocesses on the system

Example
process, err := GetTree()

if err != nil {
	return
}

// process is a top process in the tree
fmt.Printf("%v\n", process.PID)
Output:

Jump to

Keyboard shortcuts

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