Skip to content

Commit

Permalink
Fetch process resource stats as best-effort (ava-labs#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored May 23, 2023
1 parent 9e6bf96 commit ffde992
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ func (n *Node) initVdrs() validators.Set {
// Initialize [n.resourceManager].
func (n *Node) initResourceManager(reg prometheus.Registerer) error {
resourceManager, err := resource.NewManager(
n.Log,
n.Config.DatabaseConfig.Path,
n.Config.SystemTrackerFrequency,
n.Config.SystemTrackerCPUHalflife,
Expand Down
37 changes: 33 additions & 4 deletions utils/resource/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/process"

"go.uber.org/zap"

"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/storage"
)

Expand Down Expand Up @@ -65,6 +69,7 @@ type Manager interface {
}

type manager struct {
log logging.Logger
processMetrics *metrics

processesLock sync.Mutex
Expand All @@ -84,6 +89,7 @@ type manager struct {
}

func NewManager(
log logging.Logger,
diskPath string,
frequency,
cpuHalflife,
Expand All @@ -96,6 +102,7 @@ func NewManager(
}

m := &manager{
log: log,
processMetrics: processMetrics,
processes: make(map[int]*proc),
onClose: make(chan struct{}),
Expand Down Expand Up @@ -133,7 +140,10 @@ func (m *manager) TrackProcess(pid int) {
return
}

process := &proc{p: p}
process := &proc{
p: p,
log: m.log,
}

m.processesLock.Lock()
m.processes[pid] = process
Expand Down Expand Up @@ -167,6 +177,12 @@ func (m *manager) update(diskPath string, frequency, cpuHalflife, diskHalflife t
currentScaledWriteUsage := newDiskWeight * currentWriteUsage

availableBytes, getBytesErr := storage.AvailableBytes(diskPath)
if getBytesErr != nil {
m.log.Debug("failed to lookup resource",
zap.String("resource", "system disk"),
zap.Error(getBytesErr),
)
}

m.usageLock.Lock()
m.cpuUsage = oldCPUWeight*m.cpuUsage + currentScaledCPUUsage
Expand Down Expand Up @@ -218,7 +234,8 @@ func (m *manager) getActiveUsage(secondsSinceLastUpdate float64) (float64, float
}

type proc struct {
p *process.Process
p *process.Process
log logging.Logger

initialized bool

Expand All @@ -242,12 +259,24 @@ func (p *proc) getActiveUsage(secondsSinceLastUpdate float64) (float64, float64,
// assume that the utilization is 0.
times, err := p.p.Times()
if err != nil {
return 0, 0, 0
p.log.Debug("failed to lookup resource",
zap.String("resource", "process CPU"),
zap.Int32("pid", p.p.Pid),
zap.Error(err),
)
times = &cpu.TimesStat{}
}

// Note: IOCounters is not implemented on macos and therefore always returns
// an error on macos.
io, err := p.p.IOCounters()
if err != nil {
return 0, 0, 0
p.log.Debug("failed to lookup resource",
zap.String("resource", "process IO"),
zap.Int32("pid", p.p.Pid),
zap.Error(err),
)
io = &process.IOCountersStat{}
}

var (
Expand Down
10 changes: 9 additions & 1 deletion vms/registry/vm_getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/filesystem"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/resource"
"github.com/ava-labs/avalanchego/vms"
)
Expand Down Expand Up @@ -146,7 +147,14 @@ func initVMGetterTest(t *testing.T) *vmGetterTestResources {
mockReader := filesystem.NewMockReader(ctrl)
mockManager := vms.NewMockManager(ctrl)
mockRegistry := prometheus.NewRegistry()
mockCPUTracker, err := resource.NewManager(" ", time.Hour, time.Hour, time.Hour, mockRegistry)
mockCPUTracker, err := resource.NewManager(
logging.NoLog{},
"",
time.Hour,
time.Hour,
time.Hour,
mockRegistry,
)
require.NoError(t, err)

getter := NewVMGetter(
Expand Down

0 comments on commit ffde992

Please sign in to comment.