Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitor process memory usage #914

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions utils/hwstats/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/frostbyte73/core"
"github.com/prometheus/procfs"
"go.uber.org/atomic"
"golang.org/x/sys/unix"

"github.com/livekit/protocol/logger"
)
Expand All @@ -37,11 +38,18 @@ type CPUStats struct {
platform platformCPUMonitor

idleCallback func(idle float64)
procCallback func(idle float64, usage map[int]float64)
procCallback func(*ProcStats)
warningThrottle core.Throttle
closeChan chan struct{}
}

type ProcStats struct {
CpuIdle float64
Cpu map[int]float64
MemoryTotal int
Memory map[int]int
}

func NewCPUStats(idleUpdateCallback func(idle float64)) (*CPUStats, error) {
p, err := newPlatformCPUMonitor()
if err != nil {
Expand All @@ -60,7 +68,7 @@ func NewCPUStats(idleUpdateCallback func(idle float64)) (*CPUStats, error) {
return c, nil
}

func NewProcCPUStats(procUpdateCallback func(idle float64, usage map[int]float64)) (*CPUStats, error) {
func NewProcMonitor(onUpdate func(*ProcStats)) (*CPUStats, error) {
p, err := newPlatformCPUMonitor()
if err != nil {
return nil, err
Expand All @@ -69,11 +77,11 @@ func NewProcCPUStats(procUpdateCallback func(idle float64, usage map[int]float64
c := &CPUStats{
platform: p,
warningThrottle: core.NewThrottle(time.Minute),
procCallback: procUpdateCallback,
procCallback: onUpdate,
closeChan: make(chan struct{}),
}

go c.monitorProcCPULoad()
go c.monitorProcesses()

return c, nil
}
Expand Down Expand Up @@ -119,8 +127,12 @@ func (c *CPUStats) monitorCPULoad() {
}
}

func (c *CPUStats) monitorProcCPULoad() {
func (c *CPUStats) monitorProcesses() {
numCPU := c.platform.numCPU()
pageSize := unix.Getpagesize()
if pageSize == 0 {
pageSize = 4096
}

fs, err := procfs.NewFS(procfs.DefaultMountPoint)
if err != nil {
Expand Down Expand Up @@ -179,8 +191,13 @@ func (c *CPUStats) monitorProcCPULoad() {
total.CPUTotal.User + total.CPUTotal.Nice + total.CPUTotal.System +
total.CPUTotal.IRQ + total.CPUTotal.SoftIRQ + total.CPUTotal.Steal

usage := make(map[int]float64)
podUsage := 0.0
stats := &ProcStats{
CpuIdle: numCPU,
Cpu: make(map[int]float64),
MemoryTotal: 0,
Memory: make(map[int]int),
}

for pid, stat := range procStats {
// process usage as percent of total host cpu
procPercentUsage := float64(stat.UTime + stat.STime - prevStats[pid].UTime - prevStats[pid].STime)
Expand All @@ -193,16 +210,19 @@ func (c *CPUStats) monitorProcCPULoad() {
pid = ppids[pid]
}

procUsage := hostCPU * procPercentUsage / 100 / (totalHostTime - prevTotalTime)
usage[pid] += procUsage
podUsage += procUsage
cpu := hostCPU * procPercentUsage / 100 / (totalHostTime - prevTotalTime)
stats.Cpu[pid] += cpu
stats.CpuIdle -= cpu

memory := stat.RSS * pageSize
stats.Memory[pid] += memory
stats.MemoryTotal += memory
}

idle := numCPU - podUsage
c.idleCPUs.Store(idle)
c.idleCPUs.Store(stats.CpuIdle)

if c.procCallback != nil {
c.procCallback(idle, usage)
c.procCallback(stats)
}

prevTotalTime = totalHostTime
Expand Down
Loading