cpustat

package
v0.0.0-...-b3265a2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: MIT Imports: 16 Imported by: 14

Documentation

Overview

data from /proc/stat about the entire system This data appears to be updated somewhat frequently

Index

Constants

View Source
const (
	CTRL_ATTR_FAMILY_ID   = 1
	CTRL_ATTR_FAMILY_NAME = 2
	CTRL_CMD_GETFAMILY    = 3
)

On older Linux systems, including linux/genetlink.h and taskstats.h doesn't compile. To fix this, we define these three symbols here. Note that they just happen to be sequential, but they are from 3 different enums.

Variables

View Source
var StatsPath = "/proc/stat"

make this a package var so tests or users can change it

Functions

func GetPidList

func GetPidList(list *Pidlist, maxProcsToScan int)

We churn the pidlist constantly, so this is an optimization to reuse the underlying list every time. Replace the new values in the old list, shrinking or growing as necessary. This saves a bit of GC. Note that reading /proc to get the pidlist returns the elements in a consistent order. If we ever get a new source of a pidlist like perf_events or something, make sure it sorts.

func ParsePidList

func ParsePidList(s string) ([]int, error)

ParsePidList take a string of process ids and converts it into a list of int pids

func ParseUserList

func ParseUserList(s string) ([]int, error)

ParseUsrList take a string of Unix usernames and converts it into a slice of int userids

func ProcStatsReader

func ProcStatsReader(pids Pidlist, filter Filters, cur *ProcSampleList, infoMap ProcInfoMap)

ProcStatsReader reads and parses /proc/[pid]/stat for all of pids

func ProcStatsRecord

func ProcStatsRecord(interval uint32, curList, prevList ProcSampleList, sumMap, deltaMap ProcSampleMap)

ProcStatsRecord computes the delta between the Proc elements of two ProcSampleLists These lists do not need to have exactly the same processes in it, but they must both be sorted by Pid. This generally works out because reading the pids from /proc puts them in a consistent order. If we ever get a new source of the pidlist, perf_events or whatever, make sure it sorts.

func ReadFileLines

func ReadFileLines(filename string) ([]string, error)

Read a small file and split on newline

func ReadFloat

func ReadFloat(str string) float64

pull a float64 out of a string

func ReadInt

func ReadInt(str string) int64

pull an int64 out of a string

func ReadSmallFile

func ReadSmallFile(filename string) ([]byte, error)

ReadSmallFile is like os.ReadFile but dangerously optimized for reading files from /proc. The file is not statted first, and the same buffer is used every time.

func ReadSmallFileStat

func ReadSmallFileStat(filename string) ([]byte, os.FileInfo, error)

ReadSmallFileStat is like ReadSmallFile except it also returns a FileInfo from os.Stat

func ReadUInt

func ReadUInt(str string) uint64

pull a uint64 out of a string

func SafeSub

func SafeSub(a, b uint64) uint64

crude protection against rollover. This will miss the last portion of the previous sample before the overflow, but capturing that is complicated because of the various number types involved and their inconsistent documentation.

func SafeSubFloat

func SafeSubFloat(a, b float64) float64

func ScaledSub

func ScaledSub(cur, prev uint64, scale float64) uint64

func StripSpecial

func StripSpecial(r rune) rune

remove grouping characters that confuse the termui parser

func SystemStatsReader

func SystemStatsReader(cur *SystemStats) error

func SystemStatsReaderFromLines

func SystemStatsReaderFromLines(cur *SystemStats, lines []string) error

func TaskStatsLookupPid

func TaskStatsLookupPid(conn *NLConn, sample *ProcSample) error

func TaskStatsReader

func TaskStatsReader(conn *NLConn, pids Pidlist, cur *ProcSampleList)

TaskStatsReader uses conn to build a TaskStatsMap for all pids.

func TaskStatsRecord

func TaskStatsRecord(interval uint32, curList, prevList ProcSampleList, sumMap, deltaMap ProcSampleMap)

TaskStatsRecord computes the delta between Task elements of two ProcSampleLists These lists do not need to have exactly the same processes in it, but they must both be sorted by Pid. This generally works out because reading the pids from /proc puts them in a consistent order. If we ever get a new source of the pidlist, perf_events or whatever, make sure it sorts.

func UpdateProcStatsHist

func UpdateProcStatsHist(histMap ProcStatsHistMap, deltaMap ProcSampleMap)

func UpdateSysStatsHist

func UpdateSysStatsHist(hist *SystemStatsHist, delta *SystemStats)

func UpdateTaskStatsHist

func UpdateTaskStatsHist(histMap TaskStatsHistMap, deltaMap ProcSampleMap)

Types

type Filters

type Filters struct {
	User    []int
	UserStr []string
	Pid     []int
	PidStr  []string
}

func FiltersInit

func FiltersInit(user, pid string) Filters

func (Filters) PidMatch

func (f Filters) PidMatch(pid int) bool

func (Filters) UserMatch

func (f Filters) UserMatch(user int) bool

type NLConn

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

NLConn holds the context necessary to pass around to external callers

func NLInit

func NLInit() *NLConn

NLInit sets up a new taskstats netlink socket All errors are fatal.

func (NLConn) Close

func (s NLConn) Close() error

func (NLConn) Read

func (s NLConn) Read() ([]byte, error)

func (NLConn) String

func (s NLConn) String() string

func (NLConn) Write

func (s NLConn) Write(b []byte) (n int, err error)

type Pidlist

type Pidlist []int

type ProcInfo

type ProcInfo struct {
	FirstSeen  time.Time
	LastSeen   time.Time
	Comm       string   // short name from /proc/pid/stat
	Cmdline    []string // raw parts from /proc/pid/cmdline
	Friendly   string   // our magically transformed name
	Pid        uint64
	Ppid       uint64
	Pgrp       int64
	Session    int64
	Ttynr      int64
	Tpgid      int64
	Flags      uint64
	Starttime  uint64
	Nice       int64
	Rtpriority uint64
	Policy     uint64
	UID        uint32
}

ProcInfo holds properties of a process that don't change very often or can't change

type ProcInfoMap

type ProcInfoMap map[int]*ProcInfo

func (ProcInfoMap) MaybePrune

func (m ProcInfoMap) MaybePrune(chance float64, pids Pidlist, expiry time.Duration)

type ProcSample

type ProcSample struct {
	Pid  int
	Proc ProcStats
	Task TaskStats
}

type ProcSampleList

type ProcSampleList struct {
	Samples []ProcSample
	Len     uint32
}

func NewProcSampleList

func NewProcSampleList(size int) ProcSampleList

type ProcSampleMap

type ProcSampleMap map[int]*ProcSample

type ProcStats

type ProcStats struct {
	CaptureTime time.Time
	Utime       uint64
	Stime       uint64
	Cutime      uint64
	Cstime      uint64
	Numthreads  uint64
	Rss         uint64
	Guesttime   uint64
	Cguesttime  uint64
}

ProcStats holds the fast changing data that comes back from /proc/[pid]/stat These fields are documented in the linux proc(5) man page There are many more of these fields that don't change very often. These are stored in the Cmdline struct.

type ProcStatsHist

type ProcStatsHist struct {
	Utime   *hdrhistogram.Histogram
	Stime   *hdrhistogram.Histogram
	Ustime  *hdrhistogram.Histogram // utime + stime
	Cutime  *hdrhistogram.Histogram
	Cstime  *hdrhistogram.Histogram
	Custime *hdrhistogram.Histogram // cutime + cstime
}

These are somewhat expensive to track, so only maintain a hist for ones we use

func NewProcStatsHist

func NewProcStatsHist() *ProcStatsHist

type ProcStatsHistMap

type ProcStatsHistMap map[int]*ProcStatsHist

type ProcStatsMap

type ProcStatsMap map[int]*ProcStats

type ProcStatsSeekReader

type ProcStatsSeekReader struct {
	PID int
	// contains filtered or unexported fields
}

func (*ProcStatsSeekReader) Close

func (reader *ProcStatsSeekReader) Close() error

func (*ProcStatsSeekReader) Initialize

func (reader *ProcStatsSeekReader) Initialize() error

func (*ProcStatsSeekReader) ReadStats

func (reader *ProcStatsSeekReader) ReadStats(cur *ProcStats) error

type SystemStats

type SystemStats struct {
	CaptureTime  time.Time
	Usr          uint64
	Nice         uint64
	Sys          uint64
	Idle         uint64
	Iowait       uint64
	Irq          uint64
	Softirq      uint64
	Steal        uint64
	Guest        uint64
	GuestNice    uint64
	Ctxt         uint64
	ProcsTotal   uint64
	ProcsRunning uint64
	ProcsBlocked uint64
}

func SystemStatsRecord

func SystemStatsRecord(interval uint32, cur, prev, sum *SystemStats) *SystemStats

type SystemStatsHist

type SystemStatsHist struct {
	Usr          *hdrhistogram.Histogram
	Nice         *hdrhistogram.Histogram
	Sys          *hdrhistogram.Histogram
	Idle         *hdrhistogram.Histogram
	Iowait       *hdrhistogram.Histogram
	ProcsTotal   *hdrhistogram.Histogram
	ProcsRunning *hdrhistogram.Histogram
	ProcsBlocked *hdrhistogram.Histogram
}

func NewSysStatsHist

func NewSysStatsHist() *SystemStatsHist

type SystemStatsSeekReader

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

func (*SystemStatsSeekReader) Close

func (reader *SystemStatsSeekReader) Close() error

func (*SystemStatsSeekReader) Initialize

func (reader *SystemStatsSeekReader) Initialize() error

func (*SystemStatsSeekReader) ReadStats

func (reader *SystemStatsSeekReader) ReadStats(cur *SystemStats) error

type TaskStats

type TaskStats struct {
	Capturetime         time.Time
	Cpudelaycount       uint64 // delay count waiting for CPU, while runnable
	Cpudelaytotal       uint64 // delay time waiting for CPU, while runnable, in ns
	Blkiodelaycount     uint64 // delay count waiting for disk
	Blkiodelaytotal     uint64 // delay time waiting for disk
	Swapindelaycount    uint64 // delay count waiting for swap
	Swapindelaytotal    uint64 // delay time waiting for swap
	Nvcsw               uint64 // voluntary context switches
	Nivcsw              uint64 // involuntary context switches
	Freepagesdelaycount uint64 // delay count waiting for memory reclaim
	Freepagesdelaytotal uint64 // delay time waiting for memory reclaim in unknown units
}

TaskStats is the set of data from linux/taskstats.h that seemed relevant and accurate. There are other things in the kernel struct that are not tracked here, but perhaps should be.

type TaskStatsHist

type TaskStatsHist struct {
	Cpudelay *hdrhistogram.Histogram
	Iowait   *hdrhistogram.Histogram
	Swap     *hdrhistogram.Histogram
}

func NewTaskStatsHist

func NewTaskStatsHist() *TaskStatsHist

type TaskStatsHistMap

type TaskStatsHistMap map[int]*TaskStatsHist

type TaskStatsMap

type TaskStatsMap map[int]*TaskStats

TaskStatsMap maps pid to TaskStats, suually representing a sample of all pids

Jump to

Keyboard shortcuts

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