/
info.go
executable file
·314 lines (262 loc) · 8.49 KB
/
info.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
// Copyright 2022 Hyeon-Jun Jang, SSLab, Konkuk University
//
// 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 kumonitor
import (
"time"
"k8s.io/klog"
)
const miliCPU = 10000000
const miliGPU = 10
// const miliRX = 80000 // miliNetworkBits = 10KB
type ResourceName string
type AcctUsageAndTime struct {
timeStamp uint64
acctUsage uint64
}
type ResourceInfo struct {
name ResourceName
path string
usagePath string
miliScale int
price float64
/* Limit */
initLimit float64
limit float64
nextLimit float64
/* Usage */
acctUsageAndTime []AcctUsageAndTime
usage float64
avgUsage float64 // Weighted Average : (7*ri.avgUsage + ri.usage) / 8
dynamicWeight float64 // Dynamic Weight for this resource : price / {avgUsage / sum of avgUsage}
}
func (ri *ResourceInfo) Init(name ResourceName, scale int, price float64) {
ri.name, ri.miliScale, ri.price = name, scale, price
ri.limit, ri.usage, ri.avgUsage, ri.avgUsage = 0, 0, 0, 0
ri.acctUsageAndTime = append(ri.acctUsageAndTime, AcctUsageAndTime{timeStamp: uint64(time.Now().UnixNano()), acctUsage: 0})
}
func (ri *ResourceInfo) Limit() float64 { return ri.limit }
func (ri *ResourceInfo) Usage() float64 { return ri.usage }
func (ri *ResourceInfo) AvgUsage() float64 { return ri.avgUsage }
func (ri *ResourceInfo) DynamicWeight() float64 { return ri.dynamicWeight }
func (ri *ResourceInfo) Price() float64 { return ri.price }
func (ri *ResourceInfo) SetLimit(limit float64) {
// klog.V(5).Info("Set ", ri.name, ": ", limit)
switch ri.name {
case "CPU":
setFileUint(uint64(limit)*1000, ri.path, "/cpu.cfs_quota_us")
ri.limit = limit
return
case "GPU":
setFileUint(uint64(limit)*10, ri.path, "/gpu_limit")
setFileUint(uint64(limit)*10, ri.path, "/gpu_request")
UpdateGemini()
ri.limit = limit
return
}
}
/*
Func Name : (ri *ResourceInfo) updateUsage() bool
Objective :
1) Get AcctUsage From ri.usagePath
2) Update usage, avgUsage, acctUsage
3) Return completed(=true) when the filepath doesn't exist
*/
func (ri *ResourceInfo) updateUsage() bool {
timeStamp := uint64(time.Now().UnixNano())
var acctUsage uint64
var completed bool
switch ri.name {
case "CPU":
acctUsage, completed = ReadCPUStat(ri.path)
if completed {
return true
}
case "GPU":
acctUsage, completed = GetFileUint(ri.usagePath)
if completed {
return true
}
}
prev := ri.acctUsageAndTime[len(ri.acctUsageAndTime)-1]
ri.usage = float64(acctUsage-prev.acctUsage) * 100. / float64(timeStamp-prev.timeStamp)
ri.avgUsage = (7*ri.avgUsage + ri.usage) / 8
ri.acctUsageAndTime = append(ri.acctUsageAndTime,
AcctUsageAndTime{timeStamp: timeStamp, acctUsage: acctUsage})
return false
}
type PodStatus string
const (
PodInitializing PodStatus = "initializing"
PodNotReady PodStatus = "not ready"
PodRunning PodStatus = "running"
PodCompleted PodStatus = "completed"
)
// Pod Info are managed by KuScale
type PodInfo struct {
PodName string
ID string
dockerID string
imageName string
status PodStatus
reservedToken uint64
totalToken float64
expectedToken float64
availableToken float64
lastUpdatedTime int64
lastElaspedTime float64
TokenQueue float64
TokenReservation float64
UpdatedCount int64 // Update Count from KuScale
RNs []ResourceName
RIs map[ResourceName]*ResourceInfo
}
func (pi *PodInfo) CPU() *ResourceInfo {
return pi.RIs["CPU"]
}
func (pi *PodInfo) GPU() *ResourceInfo {
return pi.RIs["GPU"]
}
func NewPodInfo(podName string, RNs []ResourceName) *PodInfo {
klog.V(5).Infof("Makeing New Pod Info of %s", podName)
podInfo := PodInfo{
PodName: podName,
status: PodInitializing,
}
podInfo.RNs = RNs
podInfo.RIs = make(map[ResourceName]*ResourceInfo)
for _, name := range podInfo.RNs {
ri := ResourceInfo{name: name}
switch name {
case "CPU":
ri.Init(name, miliCPU, 1)
case "GPU":
ri.Init(name, miliGPU, 3)
}
podInfo.RIs[name] = &ri
}
klog.V(5).Infof("Made New Pod Info of %s", podName)
return &podInfo
}
/*
Func Name : (pi *PodInfo) UpdatePodUsage()
Objective :
1) Monitoring the pods in RunningPodMap
2) Check and Remove Completed Pods
*/
func (pi *PodInfo) UpdatePodUsage() {
// startTime := kuprofiler.StartTime()
// defer kuprofiler.Record("UpdatePodUsage", startTime)
for _, ri := range pi.RIs {
completed := ri.updateUsage()
if completed {
klog.V(10).Info(pi.PodName, " may be finished because the filepath doesn't exist")
pi.status = PodCompleted
}
}
klog.V(4).Info(pi.PodName, "'s usages are ", int64(pi.RIs["CPU"].Usage()), int64(pi.RIs["GPU"].Usage()))
}
/*
Func Name : (pi *PodInfo) UpdateTokenQueue()
Objective :
1) Update TokenQueue
*/
func (pi *PodInfo) UpdateTokenQueue() {
TokenQueue := pi.TokenQueue + pi.TokenReservation*pi.lastElaspedTime
for _, ri := range pi.RIs {
limit, price := ri.Limit(), ri.Price()
TokenQueue = TokenQueue - price*limit*pi.lastElaspedTime
}
if TokenQueue < 0 {
TokenQueue = 0
} else if TokenQueue >= pi.TokenReservation {
// TODO : Update Max
TokenQueue = pi.TokenReservation
}
pi.TokenQueue = TokenQueue
klog.V(10).Info(pi.PodName, " 's TokenQueue is updated to ", pi.TokenQueue, " with Token Reservation : ", pi.TokenReservation)
}
/*
Func Name : (pi *PodInfo) UpdateDynamicWeight(staticV float64)
Objective :
1) Update DynamicWeight
*/
func (pi *PodInfo) UpdateDynamicWeight(staticV float64) {
if staticV > 0 {
for _, ri := range pi.RIs {
ri.dynamicWeight = ri.price * staticV
}
return
}
// Get Sum of AvgUsage
sumAvgUsage := 0.
for _, ri := range pi.RIs {
sumAvgUsage += ri.AvgUsage() + 1
}
// Weight
W := 15.
AVGDIFF := 0.
for _, ri := range pi.RIs {
AVGDIFF += (ri.AvgUsage() + 1.) / (ri.Usage() + 1.)
}
W = AVGDIFF * W
klog.V(10).Info("Update W : ", W)
// Update Dynamic Weight
for rn, ri := range pi.RIs {
avgUsage, price := ri.AvgUsage()+1, ri.Price()
ri.dynamicWeight = W * price * sumAvgUsage / avgUsage
klog.V(10).Info("Update DynamicWeight for Pod: ", pi.PodName, ", ", rn, "'s Dynamic Weight : ", ri.dynamicWeight,
" avg : ", ri.avgUsage)
}
}
func (pi *PodInfo) SetInitLimit() {
for _, ri := range pi.RIs {
ri.SetLimit(10)
}
}
func (pi *PodInfo) setNextLimit() {
for _, ri := range pi.RIs {
if ri.nextLimit < 10 {
ri.nextLimit = 10
}
ri.SetLimit(ri.nextLimit)
}
pi.UpdatedCount = pi.UpdatedCount + 1
pi.lastUpdatedTime = time.Now().UnixNano()
klog.V(4).Info(pi.PodName, "'s limits are set to : ", int64(pi.CPU().nextLimit), int64(pi.GPU().nextLimit))
}
func (pi *PodInfo) getNextLimit(remainedTimePerSecond float64) {
k := 0.
pi.availableToken = k*pi.TokenQueue + pi.TokenReservation*remainedTimePerSecond
/*** Caclulate Next Limit wihtout Any Conditions ***/
cpuRI, gpuRI := pi.RIs["CPU"], pi.RIs["GPU"]
cpuNextLimit := cpuRI.usage + cpuRI.price*pi.availableToken/(2*cpuRI.dynamicWeight)
gpuNextLimit := gpuRI.usage + gpuRI.price*pi.availableToken/(2*gpuRI.dynamicWeight)
tokenCondition := pi.availableToken - (cpuRI.price*cpuNextLimit+gpuRI.price*gpuNextLimit)*remainedTimePerSecond
if tokenCondition >= 0 {
klog.V(10).Info(pi.PodName, "'s Next Reseravation :", int64(cpuNextLimit), " , ", int64(gpuNextLimit), " Token Enough : tokenCondition : ", int64(tokenCondition))
pi.CPU().nextLimit = cpuNextLimit
pi.GPU().nextLimit = gpuNextLimit
return
}
/*** Caclulate Next Limit wiht Token Conditions ***/
up := pi.availableToken/remainedTimePerSecond - cpuRI.price*cpuRI.usage - gpuRI.price*gpuRI.usage
below := gpuRI.dynamicWeight*cpuRI.price*cpuRI.price + cpuRI.dynamicWeight*gpuRI.price*gpuRI.price
cpuNextLimit = cpuRI.usage + cpuRI.price*gpuRI.dynamicWeight*up/below
gpuNextLimit = gpuRI.usage + gpuRI.price*cpuRI.dynamicWeight*up/below
tokenCondition = pi.availableToken - (cpuRI.price*cpuNextLimit+gpuRI.price*gpuNextLimit)*remainedTimePerSecond
klog.V(10).Info(pi.PodName, "'s Next Reseravation :", int64(cpuNextLimit), " , ", int64(gpuNextLimit), " Token Enough : tokenCondition : ", int64(tokenCondition))
pi.CPU().nextLimit = cpuNextLimit
pi.GPU().nextLimit = gpuNextLimit
return
}