generated from kubernetes/kubernetes-template-project
/
cache.go
331 lines (286 loc) · 9.95 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2023 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sampler
import (
"container/ring"
"context"
"math"
"sync"
"time"
"github.com/containerd/containerd"
"sigs.k8s.io/usage-metrics-collector/pkg/api/samplerserverv1alpha1"
commonlog "sigs.k8s.io/usage-metrics-collector/pkg/log"
)
var (
log = commonlog.Log.WithName("kube-metrics-node-sampler")
)
// sampleCache continuously reads metric samples from containerd into a buffer and caches them.
type sampleCache struct {
samplerserverv1alpha1.Buffer
// Reader is used to read container metrics.
// +optional
metricsReader metricsReader
readerConfig samplerserverv1alpha1.Reader
// containerSamples stores the samples read from containerd
samples *ring.Ring
samplesMutex sync.Mutex
once sync.Once
// useContainerMonitor use container monitor for metrics
UseContainerMonitor bool
ContainerdClient *containerd.Client
}
// Start starts the cache reading from /sys/fs/cgroup
func (s *sampleCache) Start(ctx context.Context) error {
log.Info("starting sampler")
s.init()
frequency := time.Minute / time.Duration(s.PollsPerMinute)
ticker := time.NewTicker(frequency)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
// stop scraping
log.Info("stopping cgroup sampler")
return nil
case <-ticker.C:
_ = s.fetchSample()
}
}
}
// getAllSamples returns all cached samples keyed by the ContainerID
func (s *sampleCache) getAllSamples() (allSampleInstants, int) {
s.init()
log := log.WithName("get-all-samples")
// Get the raw sample data
var count int
var samples []sampleInstants
func() {
s.samplesMutex.Lock()
defer s.samplesMutex.Unlock()
s.samples.Next().Do(func(i interface{}) {
if i == nil { // haven't populated this yet
return
}
count++
samples = append(samples, i.(sampleInstants))
})
}()
all := allSampleInstants{
containers: map[ContainerKey]sampleInstantSlice{},
node: map[samplerserverv1alpha1.NodeAggregationLevel]sampleInstantSlice{},
}
for i := range samples {
sample := samples[i]
// Index by container
for k, v := range sample.containers {
if !v.HasCPUData {
// sample is missing normalized CPU information, skip it rather than returning 0 values
continue
}
if v.CPUCoresNanoSec > uint64(s.metricsReader.MaxCPUCoresNanoSec) {
// filter samples outside the acceptable range
continue
}
if int64(v.CPUCoresNanoSec) < s.metricsReader.MinCPUCoresNanoSec {
// filter samples outside the acceptable range
continue
}
all.containers[k] = append(all.containers[k], v)
}
for level, values := range sample.node {
if !values.HasCPUData {
// sample is missing normalized CPU information, skip it rather than returning 0 values
continue
}
if values.CPUCoresNanoSec > uint64(s.metricsReader.MaxCPUCoresNanoSec) {
// filter samples outside the acceptable range
continue
}
if int64(values.CPUCoresNanoSec) < s.metricsReader.MinCPUCoresNanoSec {
// filter samples outside the acceptable range
continue
}
all.node[level] = append(all.node[level], values)
}
}
log.V(3).Info("returning samples", "count", len(all.containers))
return all, count
}
// fetchSample fetches a new Sample from containerd
func (s *sampleCache) fetchSample() error {
log := log.WithName("fetch-sample")
var cpuMetrics cpuMetrics
var memoryMetrics memoryMetrics
var err error
if s.UseContainerMonitor {
cpuMetrics, memoryMetrics, err = s.getContainerCPUAndMemoryCM()
} else {
cpuMetrics, memoryMetrics, err = s.getContainerCPUAndMemory()
}
if err != nil {
log.Error(err, "failed to get cpu and memory metrics")
return err
}
results := sampleInstants{
containers: map[ContainerKey]sampleInstant{},
node: map[samplerserverv1alpha1.NodeAggregationLevel]sampleInstant{},
}
for key, cpu := range cpuMetrics {
memory := memoryMetrics[key]
sample := s.containerToSample(key, cpu, memory)
log.V(5).Info("got sample", "sample", sample, "container", key, "found")
results.containers[key] = sample
}
// node level
nodeCPUMetrics := map[samplerserverv1alpha1.NodeAggregationLevel]containerCPUMetrics{}
for level, files := range s.metricsReader.nodeCPUFiles {
metrics, err := s.metricsReader.GetLevelCPUMetrics(files)
if err != nil {
return err
}
nodeCPUMetrics[level] = metrics
}
nodeMemoryMetrics := map[samplerserverv1alpha1.NodeAggregationLevel]containerMemoryMetrics{}
for level, files := range s.metricsReader.nodeMemoryFiles {
metrics, err := s.metricsReader.GetLevelMemoryMetrics(files)
if err != nil {
return err
}
nodeMemoryMetrics[level] = metrics
}
// assemble node metrics
results.node = s.nodeToSample(nodeCPUMetrics, nodeMemoryMetrics)
s.AddSample(results)
return nil
}
func (s *sampleCache) getContainerCPUAndMemory() (cpuMetrics, memoryMetrics, error) {
cpuMetrics, err := s.metricsReader.GetContainerCPUMetrics()
if err != nil {
log.Error(err, "failed to get cpu metrics")
return nil, nil, err
}
if len(cpuMetrics) == 0 {
log.Info("no cacheable results for cpu metrics", "paths", s.metricsReader.CPUPaths)
return nil, nil, err
}
memoryMetrics, err := s.metricsReader.GetContainerMemoryMetrics()
if err != nil {
log.Error(err, "failed to get memory metrics")
return nil, nil, err
}
if len(memoryMetrics) == 0 {
log.Info("no cacheable results for memory metrics", "paths", s.metricsReader.MemoryPaths)
return nil, nil, err
}
return cpuMetrics, memoryMetrics, nil
}
// AddSample adds a sample read from containerd.
// This function is public so that tests can add testdata to a Cache for integration testing.
func (s *sampleCache) AddSample(results sampleInstants) {
s.samplesMutex.Lock()
defer s.samplesMutex.Unlock()
log.V(5).Info("caching samples", "container-count", len(results.containers))
s.samples = s.samples.Next() // increment to the next element
s.samples.Value = results
}
// containerToSample returns a sampleInstant for the container read from containerd
func (s *sampleCache) containerToSample(
id ContainerKey,
cpu containerCPUMetrics,
memory containerMemoryMetrics,
) sampleInstant {
last := s.lastSampleForContainer(id)
sample := s.metricToSample(last, cpu, memory)
return sample
}
func (s *sampleCache) nodeToSample(
cpu map[samplerserverv1alpha1.NodeAggregationLevel]containerCPUMetrics,
memory map[samplerserverv1alpha1.NodeAggregationLevel]containerMemoryMetrics,
) map[samplerserverv1alpha1.NodeAggregationLevel]sampleInstant {
last := s.lastSampleForNode()
samples := map[samplerserverv1alpha1.NodeAggregationLevel]sampleInstant{}
for level := range cpu { // assume cpu and memory have the same aggregation levels
samples[level] = s.metricToSample(last[level], cpu[level], memory[level])
}
return samples
}
// lastSampleForContainer returns the last Sample read for a container
func (s *sampleCache) lastSampleForContainer(id ContainerKey) sampleInstant {
s.samplesMutex.Lock()
defer s.samplesMutex.Unlock()
if s.samples.Value == nil {
return sampleInstant{}
}
last := s.samples.Value.(sampleInstants)
if result, ok := last.containers[id]; ok {
return result
}
return sampleInstant{}
}
func (s *sampleCache) lastSampleForNode() map[samplerserverv1alpha1.NodeAggregationLevel]sampleInstant {
s.samplesMutex.Lock()
defer s.samplesMutex.Unlock()
if s.samples.Value == nil {
return map[samplerserverv1alpha1.NodeAggregationLevel]sampleInstant{}
}
last := s.samples.Value.(sampleInstants)
return last.node
}
// metricToSample parses the metric into a sample, deriving values from the last sample
func (s *sampleCache) metricToSample(
last sampleInstant,
cpu containerCPUMetrics,
memory containerMemoryMetrics) sampleInstant {
sample := sampleInstant{
Time: cpu.usage.Time,
CumulativeCPUUsec: cpu.usage.UsageNanoSec,
CumulativeCPUThrottlingUsec: cpu.throttling.ThrottledNanoSec,
CumulativeCPUPeriods: cpu.throttling.TotalPeriods,
CumulativeCPUThrottledPeriods: cpu.throttling.ThrottledPeriods,
MemoryBytes: memory.RSS + memory.Cache,
CumulativeMemoryOOMKill: memory.OOMKills,
CumulativeMemoryOOM: memory.OOMs,
}
if last.Time.IsZero() {
// only compute rate if the last sample was set
return sample
}
// this should be roughly equal to the polling period, but we don't know for sure
sec := getSeconds(last, sample)
sample.HasCPUData = true
sample.CPUCoresNanoSec = normalizeSeconds(last.CumulativeCPUUsec, sample.CumulativeCPUUsec, sec)
sample.CPUThrottledUSec = normalizeSeconds(last.CumulativeCPUThrottlingUsec, sample.CumulativeCPUThrottlingUsec, sec)
deltaPeriods := float64(sample.CumulativeCPUPeriods) - float64(last.CumulativeCPUPeriods)
if deltaPeriods != 0 {
// Avoid posting a NaN if no scheduling periods have elapsed
sample.CPUPercentPeriodsThrottled = (float64(sample.CumulativeCPUThrottledPeriods) - float64(last.CumulativeCPUThrottledPeriods)) / deltaPeriods
}
return sample
}
// getSeconds returns the number of seconds between 2 samples
func getSeconds(old, new sampleInstant) float64 {
return new.Time.Sub(old.Time).Seconds()
}
// normalizeSeconds takes the delta of values between 2 samples, and normalizes
// the value by dividing by the number of seconds between samples.
func normalizeSeconds(old, new uint64, sec float64) uint64 {
return uint64(math.Max(float64(new-old)/sec, 0.))
}
// init intializes the cache before it is started
func (s *sampleCache) init() {
s.once.Do(func() {
s.metricsReader.Reader = s.readerConfig
s.samples = ring.New(s.Size)
})
}