Skip to content

Commit

Permalink
Tweak memory calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
rcowham committed May 22, 2024
1 parent 8c14aa4 commit 4fc9f81
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,19 @@ type labelStruct struct {

func (p4m *P4DMetrics) getMemoryUsage() uint64 {
// An approximation for process memory usage - https://pkg.go.dev/runtime/metrics#pkg-examples
const myMetric = "/memory/classes/total:bytes"
// https://www.datadoghq.com/blog/go-memory-metrics/ - says (total-released)
const metricTotal = "/memory/classes/total:bytes"
const metricReleased = "/memory/classes/heap/released:bytes"

sample := make([]metrics.Sample, 1)
sample[0].Name = myMetric
sample := make([]metrics.Sample, 2)
sample[0].Name = metricTotal
sample[1].Name = metricReleased
metrics.Read(sample)
// Check if the metric is actually supported.
if sample[0].Value.Kind() == metrics.KindBad {
if sample[0].Value.Kind() == metrics.KindBad || sample[1].Value.Kind() == metrics.KindBad {
return 0
}
freeBytes := sample[0].Value.Uint64()
freeBytes := sample[0].Value.Uint64() - sample[1].Value.Uint64()
return freeBytes
}

Expand Down

0 comments on commit 4fc9f81

Please sign in to comment.