resource

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package resource は走行中のホスト/プロセスのリソース使用状況を計測する。

alp / slow query log / pprof は「アプリと DB の中で何が遅いか」は教えてくれるが、 どのプロセスがどのホストの CPU / メモリを食い切っているかは分からない。 走行中に ssh して top を眺めるのをやめ、他の計測と同じ収集として残せるようにする。

取得は /proc の直読み。外部コマンドに依存しない。

Index

Constants

View Source
const DefaultInterval = 5 * time.Second

DefaultInterval はサンプリング間隔。 常駐して走行中に動くものなので、粗くしてオーバーヘッドを抑える。 PPROTEIN_RESOURCE_INTERVAL(秒)で変えられる。

View Source
const DefaultTopN = 12

DefaultTopN は要約に出すプロセスの件数。

Variables

View Source
var ProcRoot = "/proc"

ProcRoot は /proc の位置。テストで差し替えられるようにしてある。

Functions

func NewHandler

func NewHandler() http.Handler

NewHandler は /debug/resource のハンドラを返す。

GET /debug/resource?seconds=90[&interval=5]

seconds の間サンプリングし、Report の JSON を返す。 pprotein の collector が Duration を seconds として付けてくるので、 他の計測と同じ収集ライフサイクル(開始トリガー / Duration)にそのまま乗る。

Types

type CPUSummary

type CPUSummary struct {
	User, System, Iowait, Steal, Irq MinMaxAvg
	Busy                             MinMaxAvg
}

CPUSummary はホスト全体の CPU 内訳(%)。Busy は idle 以外の合計。

type CPUTimes

type CPUTimes struct {
	User, Nice, System, Idle, Iowait, Irq, SoftIrq, Steal uint64
}

CPUTimes は /proc/stat の cpu 行(累積 jiffies)。

func (CPUTimes) Sub

func (c CPUTimes) Sub(o CPUTimes) CPUTimes

func (CPUTimes) Total

func (c CPUTimes) Total() uint64

type DiskStat

type DiskStat struct {
	ReadSectors  uint64
	WriteSectors uint64
	IoMs         uint64
}

DiskStat は /proc/diskstats(累積)。Sectors は 512B 単位。

type DiskSummary

type DiskSummary struct {
	Name      string
	ReadKBps  MinMaxAvg
	WriteKBps MinMaxAvg
	UtilPct   MinMaxAvg
}

type MemSummary

type MemSummary struct {
	TotalKB  uint64
	UsedKB   MinMaxAvg // MemTotal - MemAvailable
	SwapUsed MinMaxAvg
}

type MinMaxAvg

type MinMaxAvg struct {
	Min, Max, Avg float64
	// contains filtered or unexported fields
}

type Process

type Process struct {
	Pid     int
	Command string
	CPU     uint64 // utime + stime(累積 jiffies)
	RssKB   uint64
}

Process は1プロセスの累積 CPU jiffies と RSS。

type ProcessSummary

type ProcessSummary struct {
	Pid     int
	Command string
	CPU     MinMaxAvg
	RssKB   MinMaxAvg
}

ProcessSummary はプロセス単位の CPU(コア1本を100%とした値)と RSS。

type Processor

type Processor struct {
	// TopN は要約に出すプロセスの件数。0 なら DefaultTopN。
	TopN int
}

Processor は収集した Report を要約テキストに変換する(pprotein 本体側)。 alp / pt-query-digest と違って外部コマンドを呼ばず、Go の中で集計する。

func (*Processor) Cacheable

func (p *Processor) Cacheable() bool

func (*Processor) Process

func (p *Processor) Process(snapshot *collect.Snapshot) (io.ReadCloser, error)

type Report

type Report struct {
	Host       string
	StartedAt  time.Time
	FinishedAt time.Time
	IntervalMs int64
	Samples    []Sample
}

Report は1回の走行分の計測結果。snapshot の本体としてこの JSON が保存される。

func Collect

func Collect(duration, interval time.Duration) (*Report, error)

Collect は duration の間 interval おきにサンプリングする。 最低2サンプル取らないと差分が計算できないので、必ず両端を含める。

type Sample

type Sample struct {
	At             time.Time
	CPU            CPUTimes
	NumCPU         int
	Load1          float64
	MemTotalKB     uint64
	MemAvailableKB uint64
	SwapTotalKB    uint64
	SwapFreeKB     uint64
	Disks          map[string]DiskStat
	Processes      []Process
}

Sample は1時点のスナップショット。CPU とディスクは累積値なので、 集計時に隣り合うサンプルの差分を取る。

func Sampling

func Sampling() (*Sample, error)

Sampling は1時点のリソース使用状況を読む。

type Summary

type Summary struct {
	Host       string
	StartedAt  time.Time
	FinishedAt time.Time
	NumCPU     int
	Samples    int

	CPU  CPUSummary
	Load MinMaxAvg
	Mem  MemSummary

	Disks     []DiskSummary
	Processes []ProcessSummary
}

Summary は Report を集計した結果。CLI やグラフからも使う。

func Summarize

func Summarize(r *Report, topN int) *Summary

Summarize は隣り合うサンプルの差分から走行中の平均・ピークを出す。

func (*Summary) Text

func (s *Summary) Text() string

Text は要約を人間とエージェントが読めるテキストにする。

Jump to

Keyboard shortcuts

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