-
Notifications
You must be signed in to change notification settings - Fork 89
/
provisioner_calculate.go
291 lines (257 loc) · 13.3 KB
/
provisioner_calculate.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
/*
Copyright 2022 The Katalyst 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 malachite
// for those metrics need extra calculation logic,
// we will put them in a separate file here
import (
"time"
"github.com/kubewharf/katalyst-core/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/provisioner/malachite/types"
"github.com/kubewharf/katalyst-core/pkg/util/metric"
)
// processContainerMemBandwidth handles memory bandwidth (read/write) rate in a period while,
// and it will need the previously collected data to do this
func (m *MalachiteMetricsProvisioner) processContainerMemBandwidth(podUID, containerName string, cgStats *types.MalachiteCgroupInfo, lastUpdateTimeInSec float64) {
var (
lastOCRReadDRAMsMetric, _ = m.metricStore.GetContainerMetric(podUID, containerName, consts.MetricOCRReadDRAMsContainer)
lastIMCWritesMetric, _ = m.metricStore.GetContainerMetric(podUID, containerName, consts.MetricIMCWriteContainer)
lastStoreAllInsMetric, _ = m.metricStore.GetContainerMetric(podUID, containerName, consts.MetricStoreAllInsContainer)
lastStoreInsMetric, _ = m.metricStore.GetContainerMetric(podUID, containerName, consts.MetricStoreInsContainer)
// those value are uint64 type from source
lastOCRReadDRAMs = uint64(lastOCRReadDRAMsMetric.Value)
lastIMCWrites = uint64(lastIMCWritesMetric.Value)
lastStoreAllIns = uint64(lastStoreAllInsMetric.Value)
lastStoreIns = uint64(lastStoreInsMetric.Value)
)
var (
curOCRReadDRAMs, curIMCWrites, curStoreAllIns, curStoreIns uint64
curUpdateTimeInSec float64
)
if cgStats.CgroupType == "V1" {
curOCRReadDRAMs = cgStats.V1.Cpu.OcrReadDrams
curIMCWrites = cgStats.V1.Cpu.ImcWrites
curStoreAllIns = cgStats.V1.Cpu.StoreAllIns
curStoreIns = cgStats.V1.Cpu.StoreIns
curUpdateTimeInSec = float64(cgStats.V1.Cpu.UpdateTime)
} else if cgStats.CgroupType == "V2" {
curOCRReadDRAMs = cgStats.V2.Cpu.OcrReadDrams
curIMCWrites = cgStats.V2.Cpu.ImcWrites
curStoreAllIns = cgStats.V2.Cpu.StoreAllIns
curStoreIns = cgStats.V2.Cpu.StoreIns
curUpdateTimeInSec = float64(cgStats.V2.Cpu.UpdateTime)
} else {
return
}
// read bandwidth
m.setContainerRateMetric(podUID, containerName, consts.MetricMemBandwidthReadContainer,
func() float64 {
// read megabyte
return float64(uint64CounterDelta(lastOCRReadDRAMs, curOCRReadDRAMs)) * 64 / (1024 * 1024)
},
int64(lastUpdateTimeInSec), int64(curUpdateTimeInSec))
// write bandwidth
m.setContainerRateMetric(podUID, containerName, consts.MetricMemBandwidthWriteContainer,
func() float64 {
storeAllInsInc := uint64CounterDelta(lastStoreAllIns, curStoreAllIns)
if storeAllInsInc == 0 {
return 0
}
storeInsInc := uint64CounterDelta(lastStoreIns, curStoreIns)
imcWritesInc := uint64CounterDelta(lastIMCWrites, curIMCWrites)
// write megabyte
return float64(storeInsInc) / float64(storeAllInsInc) / (1024 * 1024) * float64(imcWritesInc) * 64
},
int64(lastUpdateTimeInSec), int64(curUpdateTimeInSec))
}
// processContainerCPURelevantRate is used to calculate some container cpu-relevant rates.
// this would be executed before setting the latest values into metricStore.
func (m *MalachiteMetricsProvisioner) processContainerCPURelevantRate(podUID, containerName string, cgStats *types.MalachiteCgroupInfo, lastUpdateTimeInSec float64) {
lastMetricValueFn := func(metricName string) float64 {
lastMetric, _ := m.metricStore.GetContainerMetric(podUID, containerName, metricName)
return lastMetric.Value
}
var (
lastCPUIns = uint64(lastMetricValueFn(consts.MetricCPUInstructionsContainer))
lastCPUCycles = uint64(lastMetricValueFn(consts.MetricCPUCyclesContainer))
lastCPUNRTht = uint64(lastMetricValueFn(consts.MetricCPUNrThrottledContainer))
lastCPUNRPeriod = uint64(lastMetricValueFn(consts.MetricCPUNrPeriodContainer))
lastThrottleTime = uint64(lastMetricValueFn(consts.MetricCPUThrottledTimeContainer))
lastL3CacheMiss = uint64(lastMetricValueFn(consts.MetricCPUL3CacheMissContainer))
curCPUIns, curCPUCycles, curCPUNRTht, curCPUNRPeriod, curCPUThrottleTime, curL3CacheMiss uint64
curUpdateTime int64
)
if cgStats.CgroupType == "V1" {
curCPUIns = cgStats.V1.Cpu.Instructions
curCPUCycles = cgStats.V1.Cpu.Cycles
curCPUNRTht = cgStats.V1.Cpu.CPUNrThrottled
curCPUNRPeriod = cgStats.V1.Cpu.CPUNrPeriods
curCPUThrottleTime = cgStats.V1.Cpu.CPUThrottledTime / 1000
if cgStats.V1.Cpu.L3Misses > 0 {
curL3CacheMiss = cgStats.V1.Cpu.L3Misses
} else if cgStats.V1.Cpu.OcrReadDrams > 0 {
curL3CacheMiss = cgStats.V1.Cpu.OcrReadDrams
}
curUpdateTime = cgStats.V1.Cpu.UpdateTime
} else if cgStats.CgroupType == "V2" {
curCPUIns = cgStats.V2.Cpu.Instructions
curCPUCycles = cgStats.V2.Cpu.Cycles
curCPUNRTht = cgStats.V2.Cpu.CPUStats.NrThrottled
curCPUNRPeriod = cgStats.V2.Cpu.CPUStats.NrPeriods
curCPUThrottleTime = cgStats.V2.Cpu.CPUStats.ThrottledUsec
if cgStats.V2.Cpu.L3Misses > 0 {
curL3CacheMiss = cgStats.V2.Cpu.L3Misses
} else if cgStats.V2.Cpu.OcrReadDrams > 0 {
curL3CacheMiss = cgStats.V2.Cpu.OcrReadDrams
}
curUpdateTime = cgStats.V2.Cpu.UpdateTime
} else {
return
}
m.setContainerRateMetric(podUID, containerName, consts.MetricCPUInstructionsRateContainer, func() float64 {
return float64(uint64CounterDelta(lastCPUIns, curCPUIns))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricCPUCyclesRateContainer, func() float64 {
return float64(uint64CounterDelta(lastCPUCycles, curCPUCycles))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricCPUNrThrottledRateContainer, func() float64 {
return float64(uint64CounterDelta(lastCPUNRTht, curCPUNRTht))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricCPUNrPeriodRateContainer, func() float64 {
return float64(uint64CounterDelta(lastCPUNRPeriod, curCPUNRPeriod))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricCPUThrottledTimeRateContainer, func() float64 {
return float64(uint64CounterDelta(lastThrottleTime, curCPUThrottleTime))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricCPUL3CacheMissRateContainer, func() float64 {
return float64(uint64CounterDelta(lastL3CacheMiss, curL3CacheMiss))
}, int64(lastUpdateTimeInSec), curUpdateTime)
}
func (m *MalachiteMetricsProvisioner) processContainerMemRelevantRate(podUID, containerName string, cgStats *types.MalachiteCgroupInfo, lastUpdateTimeInSec float64) {
lastMetricValueFn := func(metricName string) float64 {
lastMetric, _ := m.metricStore.GetContainerMetric(podUID, containerName, metricName)
return lastMetric.Value
}
var (
lastPGFault = uint64(lastMetricValueFn(consts.MetricMemPgfaultContainer))
lastPGMajFault = uint64(lastMetricValueFn(consts.MetricMemPgmajfaultContainer))
lastOOMCnt = uint64(lastMetricValueFn(consts.MetricMemOomContainer))
curPGFault, curPGMajFault, curOOMCnt uint64
curUpdateTime int64
)
if cgStats.CgroupType == "V1" {
curPGFault = cgStats.V1.Memory.Pgfault
curPGMajFault = cgStats.V1.Memory.Pgmajfault
curOOMCnt = cgStats.V1.Memory.BpfMemStat.OomCnt
curUpdateTime = cgStats.V1.Memory.UpdateTime
} else if cgStats.CgroupType == "V2" {
curPGFault = cgStats.V2.Memory.MemStats.Pgmajfault
curPGMajFault = cgStats.V2.Memory.MemStats.Pgmajfault
curOOMCnt = cgStats.V2.Memory.BpfMemStat.OomCnt
curUpdateTime = cgStats.V2.Memory.UpdateTime
} else {
return
}
m.setContainerRateMetric(podUID, containerName, consts.MetricMemPgfaultRateContainer, func() float64 {
return float64(uint64CounterDelta(lastPGFault, curPGFault))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricMemPgmajfaultRateContainer, func() float64 {
return float64(uint64CounterDelta(lastPGMajFault, curPGMajFault))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricMemOomRateContainer, func() float64 {
return float64(uint64CounterDelta(lastOOMCnt, curOOMCnt))
}, int64(lastUpdateTimeInSec), curUpdateTime)
}
func (m *MalachiteMetricsProvisioner) processContainerNetRelevantRate(podUID, containerName string, cgStats *types.MalachiteCgroupInfo, lastUpdateTimeInSec float64) {
lastMetricValueFn := func(metricName string) float64 {
lastMetric, _ := m.metricStore.GetContainerMetric(podUID, containerName, metricName)
return lastMetric.Value
}
var (
lastNetTCPRx = uint64(lastMetricValueFn(consts.MetricNetTcpRecvPacketsContainer))
lastNetTCPTx = uint64(lastMetricValueFn(consts.MetricNetTcpSendPacketsContainer))
lastNetTCPRxBytes = uint64(lastMetricValueFn(consts.MetricNetTcpRecvBytesContainer))
lastNetTCPTxBytes = uint64(lastMetricValueFn(consts.MetricNetTcpSendBytesContainer))
netData *types.NetClsCgData
)
if cgStats.V1 != nil {
netData = cgStats.V1.NetCls
} else if cgStats.V2 != nil {
netData = cgStats.V2.NetCls
} else {
return
}
curUpdateTime := netData.UpdateTime
_curUpdateTime := time.Unix(curUpdateTime, 0)
updateTimeDiff := float64(curUpdateTime) - lastUpdateTimeInSec
if updateTimeDiff > 0 {
m.setContainerRateMetric(podUID, containerName, consts.MetricNetTcpSendBPSContainer, func() float64 {
return float64(uint64CounterDelta(lastNetTCPTxBytes, netData.BpfNetData.NetTCPTxBytes))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricNetTcpRecvBPSContainer, func() float64 {
return float64(uint64CounterDelta(lastNetTCPRxBytes, netData.BpfNetData.NetTCPRxBytes))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricNetTcpSendPpsContainer, func() float64 {
return float64(uint64CounterDelta(lastNetTCPTx, netData.BpfNetData.NetTCPTx))
}, int64(lastUpdateTimeInSec), curUpdateTime)
m.setContainerRateMetric(podUID, containerName, consts.MetricNetTcpRecvPpsContainer, func() float64 {
return float64(uint64CounterDelta(lastNetTCPRx, netData.BpfNetData.NetTCPRx))
}, int64(lastUpdateTimeInSec), curUpdateTime)
} else {
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricNetTcpSendBPSContainer, metric.MetricData{
Value: float64(uint64CounterDelta(netData.OldBpfNetData.NetTCPTxBytes, netData.BpfNetData.NetTCPTxBytes)) / defaultMetricUpdateInterval,
Time: &_curUpdateTime,
})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricNetTcpRecvBPSContainer, metric.MetricData{
Value: float64(uint64CounterDelta(netData.OldBpfNetData.NetTCPRxBytes, netData.BpfNetData.NetTCPRxBytes)) / defaultMetricUpdateInterval,
Time: &_curUpdateTime,
})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricNetTcpSendPpsContainer, metric.MetricData{
Value: float64(uint64CounterDelta(netData.OldBpfNetData.NetTCPTx, netData.BpfNetData.NetTCPTx)) / defaultMetricUpdateInterval,
Time: &_curUpdateTime,
})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricNetTcpRecvPpsContainer, metric.MetricData{
Value: float64(uint64CounterDelta(netData.OldBpfNetData.NetTCPRx, netData.BpfNetData.NetTCPRx)) / defaultMetricUpdateInterval,
Time: &_curUpdateTime,
})
}
}
// setContainerRateMetric is used to set rate metric in container level.
// This method will check if the metric is really updated, and decide weather to update metric in metricStore.
// The method could help avoid lots of meaningless "zero" value.
func (m *MalachiteMetricsProvisioner) setContainerRateMetric(podUID, containerName, targetMetricName string, deltaValueFunc func() float64, lastUpdateTime, curUpdateTime int64) {
timeDeltaInSec := curUpdateTime - lastUpdateTime
if lastUpdateTime == 0 || timeDeltaInSec <= 0 {
// Return directly when the following situations happen:
// 1. lastUpdateTime == 0, which means no previous data.
// 2. timeDeltaInSec == 0, which means the metric is not updated,
// this is originated from the sampling lag between katalyst-core and malachite(data source)
// 3. timeDeltaInSec < 0, this is illegal and unlikely to happen.
return
}
// TODO this will duplicate "updateTime" a lot.
// But to my knowledge, the cost could be acceptable.
updateTime := time.Unix(curUpdateTime, 0)
m.metricStore.SetContainerMetric(podUID, containerName, targetMetricName,
metric.MetricData{Value: deltaValueFunc() / float64(timeDeltaInSec), Time: &updateTime})
}
// uint64CounterDelta calculate the delta between two uint64 counters
// Sometimes the counter value would go beyond the MaxUint64. In that case,
// negative counter delta would happen, and the data is not incorrect.
func uint64CounterDelta(previous, current uint64) uint64 {
if current >= previous {
return current - previous
}
// Return 0 when previous > current, because we may not be able to make sure
// the upper bound for each counter.
return 0
}