@@ -18,6 +18,7 @@ limitations under the License.
1818package updater
1919
2020import (
21+ "math"
2122 "strconv"
2223
2324 "github.com/prometheus/client_golang/prometheus"
@@ -27,14 +28,14 @@ import (
2728const (
2829 metricsNamespace = metrics .TopMetricsNamespace + "updater"
2930
31+ // The metrics will distinguish VPA sizes up to 2^maxVpaSizeLog (~1M)
32+ // Anything above that size will be reported in the top bucket.
3033 maxVpaSizeLog = 20
31- // maxVpaSize = 2 ^ maxVpaSizeLog
32- maxVpaSize = 1024 * 1024
3334)
3435
3536// SizeBasedGauge is a wrapper for incrementally recording values indexed by log2(VPA size)
3637type SizeBasedGauge struct {
37- values map [ int ]int
38+ values [ maxVpaSizeLog ]int
3839 gauge * prometheus.GaugeVec
3940}
4041
@@ -95,17 +96,10 @@ func NewExecutionTimer() *metrics.ExecutionTimer {
9596
9697// newSizeBasedGauge provides a wrapper for counting items in a loop
9798func newSizeBasedGauge (gauge * prometheus.GaugeVec ) * SizeBasedGauge {
98- obj := SizeBasedGauge {
99- values : make ( map [ int ]int ) ,
99+ return & SizeBasedGauge {
100+ values : [ maxVpaSizeLog ]int {} ,
100101 gauge : gauge ,
101102 }
102-
103- // initialize with empty data so we can clean stale gauge values in Observe
104- for i := 0 ; i <= maxVpaSizeLog ; i ++ {
105- obj .values [i ] = 0
106- }
107-
108- return & obj
109103}
110104
111105// NewControlledPodsCounter returns a wrapper for counting Pods controlled by Updater
@@ -129,16 +123,15 @@ func NewVpasWithEvictedPodsCounter() *SizeBasedGauge {
129123}
130124
131125func getVpaSizeLog2 (vpaSize int ) int {
132- if vpaSize >= maxVpaSize {
133- return maxVpaSizeLog
126+ if vpaSize == 0 {
127+ return 0
134128 }
135129
136- log2 := 0
137- for vpaSize > 1 {
138- log2 ++
139- vpaSize >>= 1
130+ ret := int (math .Log2 (float64 (vpaSize )))
131+ if ret > maxVpaSizeLog {
132+ return maxVpaSizeLog
140133 }
141- return log2
134+ return ret
142135}
143136
144137// AddEvictedPod increases the counter of pods evicted by Updater, by given VPA size
0 commit comments