|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package memory |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/elastic/elastic-agent-libs/opt" |
| 24 | + "github.com/elastic/elastic-agent-system-metrics/metric" |
| 25 | + "github.com/elastic/elastic-agent-system-metrics/metric/system/resolve" |
| 26 | +) |
| 27 | + |
| 28 | +// Memory holds os-specifc memory usage data |
| 29 | +// The vast majority of these values are cross-platform |
| 30 | +// However, we're wrapping all them for the sake of safety, and for the more variable swap metrics |
| 31 | +type Memory struct { |
| 32 | + Total opt.Uint `struct:"total,omitempty"` |
| 33 | + Used UsedMemStats `struct:"used,omitempty"` |
| 34 | + |
| 35 | + Free opt.Uint `struct:"free,omitempty"` |
| 36 | + Cached opt.Uint `struct:"cached,omitempty"` |
| 37 | + // "Actual" values are, technically, a linux-only concept |
| 38 | + // For better or worse we've expanded it to include "derived" |
| 39 | + // Memory values on other platforms, which we should |
| 40 | + // probably keep for the sake of backwards compatibility |
| 41 | + // However, because the derived value varies from platform to platform, |
| 42 | + // We may want to more precisely document what these mean. |
| 43 | + Actual ActualMemoryMetrics `struct:"actual,omitempty"` |
| 44 | + |
| 45 | + // Swap metrics |
| 46 | + Swap SwapMetrics `struct:"swap,omitempty"` |
| 47 | +} |
| 48 | + |
| 49 | +// UsedMemStats wraps used.* memory metrics |
| 50 | +type UsedMemStats struct { |
| 51 | + Pct opt.Float `struct:"pct,omitempty"` |
| 52 | + Bytes opt.Uint `struct:"bytes,omitempty"` |
| 53 | +} |
| 54 | + |
| 55 | +// ActualMemoryMetrics wraps the actual.* memory metrics |
| 56 | +type ActualMemoryMetrics struct { |
| 57 | + Free opt.Uint `struct:"free,omitempty"` |
| 58 | + Used UsedMemStats `struct:"used,omitempty"` |
| 59 | +} |
| 60 | + |
| 61 | +// SwapMetrics wraps swap.* memory metrics |
| 62 | +type SwapMetrics struct { |
| 63 | + Total opt.Uint `struct:"total,omitempty"` |
| 64 | + Used UsedMemStats `struct:"used,omitempty"` |
| 65 | + Free opt.Uint `struct:"free,omitempty"` |
| 66 | +} |
| 67 | + |
| 68 | +// Get returns platform-independent memory metrics. |
| 69 | +func Get(procfs resolve.Resolver) (Memory, error) { |
| 70 | + base, err := get(procfs) |
| 71 | + if err != nil { |
| 72 | + return Memory{}, fmt.Errorf("error getting system memory info: %w", err) |
| 73 | + } |
| 74 | + base.fillPercentages() |
| 75 | + return base, nil |
| 76 | +} |
| 77 | + |
| 78 | +// IsZero implements the zeroer interface for structform's folders |
| 79 | +func (used UsedMemStats) IsZero() bool { |
| 80 | + return used.Pct.IsZero() && used.Bytes.IsZero() |
| 81 | +} |
| 82 | + |
| 83 | +// IsZero implements the zeroer interface for structform's folders |
| 84 | +func (swap SwapMetrics) IsZero() bool { |
| 85 | + return swap.Free.IsZero() && swap.Used.IsZero() && swap.Total.IsZero() |
| 86 | +} |
| 87 | + |
| 88 | +func (base *Memory) fillPercentages() { |
| 89 | + // Add percentages |
| 90 | + // In theory, `Used` and `Total` are available everywhere, so assume values are good. |
| 91 | + if base.Total.Exists() && base.Total.ValueOr(0) != 0 { |
| 92 | + percUsed := float64(base.Used.Bytes.ValueOr(0)) / float64(base.Total.ValueOr(1)) |
| 93 | + base.Used.Pct = opt.FloatWith(metric.Round(percUsed)) |
| 94 | + |
| 95 | + actualPercUsed := float64(base.Actual.Used.Bytes.ValueOr(0)) / float64(base.Total.ValueOr(0)) |
| 96 | + base.Actual.Used.Pct = opt.FloatWith(metric.Round(actualPercUsed)) |
| 97 | + } |
| 98 | + |
| 99 | + if base.Swap.Total.ValueOr(0) != 0 && base.Swap.Used.Bytes.Exists() { |
| 100 | + perc := float64(base.Swap.Used.Bytes.ValueOr(0)) / float64(base.Swap.Total.ValueOr(0)) |
| 101 | + base.Swap.Used.Pct = opt.FloatWith(metric.Round(perc)) |
| 102 | + } |
| 103 | +} |
0 commit comments