cpu

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StateRunning         = 0 // Was running, got preempted
	StateInterruptible   = 1 // Voluntary sleep (mutex, sleep())
	StateUninterruptible = 2 // I/O wait (D state)
)

Task state constants (must match BPF)

Variables

This section is empty.

Functions

This section is empty.

Types

type CPUUsageCollector

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

func NewCPUUsageCollector

func NewCPUUsageCollector(objs *cpuObjects) (*CPUUsageCollector, error)

func (*CPUUsageCollector) Close

func (c *CPUUsageCollector) Close() error

func (*CPUUsageCollector) ExportMetrics

func (c *CPUUsageCollector) ExportMetrics(ctx context.Context, systemWide bool, exporters ...metrics.Exporter) error

ExportMetrics exports metrics using the provided exporters.

func (*CPUUsageCollector) GetAllMetrics

func (c *CPUUsageCollector) GetAllMetrics() map[uint32]*PidMetrics

GetAllMetrics returns all PID metrics

func (*CPUUsageCollector) GetMetrics

func (c *CPUUsageCollector) GetMetrics(pid uint32) *PidMetrics

GetMetrics returns the metrics for a specific PID

func (*CPUUsageCollector) GetSnapshot

func (c *CPUUsageCollector) GetSnapshot(systemWide bool) *metrics.MetricsSnapshot

GetSnapshot returns a metrics snapshot for export.

func (*CPUUsageCollector) PrintMetrics

func (c *CPUUsageCollector) PrintMetrics(systemWide bool, perPID bool)

PrintMetrics prints PMU metrics systemWide: true for system-wide mode, false for targeted PID mode perPID: true to show per-PID breakdown in system-wide mode

func (*CPUUsageCollector) ReadCPUUsage

func (c *CPUUsageCollector) ReadCPUUsage() error

type CpuStat

type CpuStat struct {
	CPU       uint32
	Busy      uint64
	Total     uint64
	Timestamp uint64
}

type HardwarePerfEvents

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

HardwarePerfEvents manages hardware performance counter events for all CPUs

func NewHardwarePerfEvents

func NewHardwarePerfEvents(cpus []int) (*HardwarePerfEvents, error)

NewHardwarePerfEvents opens hardware perf events on all specified CPUs. Returns nil (not error) if hardware counters are unavailable.

func (*HardwarePerfEvents) AttachToMaps

func (h *HardwarePerfEvents) AttachToMaps(
	cyclesMap *ebpf.Map,
	instructionsMap *ebpf.Map,
	cacheMissesMap *ebpf.Map,
) error

AttachToMaps attaches the perf event file descriptors to the eBPF PERF_EVENT_ARRAY maps

func (*HardwarePerfEvents) Close

func (h *HardwarePerfEvents) Close() error

Close closes all perf event file descriptors

func (*HardwarePerfEvents) EnableInBPF

func (h *HardwarePerfEvents) EnableInBPF(enabledMap *ebpf.Map) error

EnableInBPF sets the hw_counters_enabled flag in the eBPF program

type PMUMonitor

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

PMUMonitor handles PMU hardware counter monitoring

func NewPMUMonitor

func NewPMUMonitor(pid int, systemWide bool, cpus []uint) (*PMUMonitor, error)

NewPMUMonitor creates a new PMU monitor

func (*PMUMonitor) Close

func (m *PMUMonitor) Close()

Close releases all resources associated with the monitor

func (*PMUMonitor) ExportMetrics

func (m *PMUMonitor) ExportMetrics(ctx context.Context, systemWide bool, exporters ...metrics.Exporter) error

ExportMetrics exports metrics using the provided exporters.

func (*PMUMonitor) GetMetricsSnapshot

func (m *PMUMonitor) GetMetricsSnapshot(systemWide bool) *metrics.MetricsSnapshot

GetMetricsSnapshot returns a snapshot of the current metrics.

func (*PMUMonitor) PrintMetrics

func (m *PMUMonitor) PrintMetrics(systemWide, perPID bool)

PrintMetrics prints collected metrics

type PidMetrics

type PidMetrics struct {
	OnCPUHist         *hdrhistogram.Histogram // Time on CPU per switch
	RunqLatencyHist   *hdrhistogram.Histogram // Time waiting in runqueue
	TotalCycles       uint64
	TotalInstructions uint64
	TotalCacheMisses  uint64
	SampleCount       uint64
	// Task state counters
	PreemptedCount uint64 // Switched out while running (preempted)
	VoluntaryCount uint64 // Voluntary sleep (interruptible)
	IOWaitCount    uint64 // I/O wait (uninterruptible/D state)
}

PidMetrics holds accumulated metrics for a PID

type PidStat

type PidStat struct {
	TGID uint32

	DeltaNs       uint64 // On-CPU time
	RunqLatencyNs uint64 // Runqueue wait time (wakeup to switch)
	Cycles        uint64
	Instructions  uint64
	CacheMisses   uint64
	Timestamp     uint64
	PrevState     uint8 // Why switched out: 0=running, 1=sleep, 2=io
	Preempt       uint8 // Was preempted?
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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