/
quota.go
285 lines (255 loc) · 7.44 KB
/
quota.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
/*
Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
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 k8s
import (
"fmt"
"os"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
pfResources "github.com/PaddlePaddle/PaddleFlow/pkg/common/resources"
)
const (
DefaultCpuRequest = 1
DefaultMemRequest = 1073741824 // 1Gi=1024*1024*1024B=1073741824B
MaxGPUIndex = 16
GPUIndexResources = "gpuDeviceIDX"
)
var (
GPUCorePodKey = "GPU_CORE_POD"
GPUMemPodKey = "GPU_MEM_POD"
GPUIdxKey = "GPU_IDX"
GPURNamePrefix = "/gpu"
GPUXName = "cgpu"
GPUXCoreName = fmt.Sprintf("%s_core", GPUXName)
)
func init() {
gpuCorePod := os.Getenv("PF_GPU_CORE_POD")
if gpuCorePod != "" {
GPUCorePodKey = gpuCorePod
}
gpuMemPod := os.Getenv("PF_GPU_MEM_POD")
if gpuMemPod != "" {
GPUMemPodKey = gpuMemPod
}
gpuIDX := os.Getenv("PF_GPU_IDX")
if gpuIDX != "" {
GPUIdxKey = gpuIDX
}
gpuNamePrefix := os.Getenv("PF_GPU_NAME_PREFIX")
if gpuNamePrefix != "" {
GPURNamePrefix = gpuNamePrefix
}
fmt.Printf("CORE_POD key: %s, IDX key: %s, GPU Name prefix: %s\n", GPUCorePodKey, GPUIdxKey, GPURNamePrefix)
}
func SharedGPUIDX(pod *v1.Pod) int64 {
log.Debugf("pod %s/%s shared gpu idx: %v", pod.Namespace, pod.Name, pod.Annotations)
var deviceIDX int64 = -1
annotations := pod.Annotations
if annotations == nil {
return deviceIDX
}
corePodStr, findCore := annotations[GPUCorePodKey]
idxStr, find := annotations[GPUIdxKey]
if !findCore || !find {
return deviceIDX
}
corePodNum, err := strconv.Atoi(corePodStr)
if err != nil {
return deviceIDX
}
idxs := strings.Split(idxStr, ",")
log.Debugf("split idxs: %v", idxs)
if corePodNum/50 == len(idxs) {
// is shared gpu
deviceIDX = 0
var deviceID int
for _, idx := range idxs {
deviceID, err = strconv.Atoi(idx)
if err != nil {
log.Warnf("convert str[%s] to int failed, err: %v", idx, err)
return -1
}
deviceIDX += GPUDeviceIDX(deviceID)
}
}
return deviceIDX
}
func IsGPUX(rName string) bool {
return strings.HasSuffix(rName, GPUXName) || strings.HasPrefix(rName, GPURNamePrefix)
}
func getGPUX(rName string, rValue int64) (int64, bool) {
if strings.HasPrefix(rName, GPURNamePrefix) {
return rValue, true
}
if strings.HasSuffix(rName, GPUXName) {
return rValue / 100, true
}
return 0, false
}
func SubWithGPUX(total *pfResources.Resource, rr map[string]int64) map[string]interface{} {
if total == nil || rr == nil {
return make(map[string]interface{})
}
log.Debugf("total: %v, used: %v", total, rr)
var isShared = false
var sharedDevices []int
gpuIDX, find := rr[GPUIndexResources]
if find {
isShared = true
sharedDevices = GPUSharedDevices(gpuIDX)
}
log.Debugf("gpuIDX: %v, shared devices: %v", gpuIDX, sharedDevices)
// get gpu count and name
var gpuTotalCount int64
var gpuxName string
scalarResources := total.ScalarResources("")
for rName, rValue := range scalarResources {
value, ok := getGPUX(rName, int64(rValue))
if ok {
gpuxName = rName
gpuTotalCount = value
break
}
}
// get used gpu and gpu core
var gpux, gpuxCore int64
for rName, rValue := range rr {
// check used shared gpu
if strings.HasSuffix(rName, GPUXName) {
gpux = rValue
}
if strings.HasSuffix(rName, GPUXCoreName) {
gpuxCore = rValue
}
// check used gpu
if strings.HasPrefix(rName, GPURNamePrefix) {
log.Debugf("used %s , value: %v", rName, rValue)
gpuTotalCount -= rValue
}
}
log.Debugf("gpuIDX: %v, used gpu %d, and used gpu core %d", gpuIDX, gpux, gpuxCore)
// calculate idle gpu
var idlegpux, idleSharedGPUX int64
gpuxResource := map[string]interface{}{}
if isShared {
totalShared := int64(len(sharedDevices))
totalUsed := gpuxCore/50 - gpux
sharedUsed := 2*gpux - gpuxCore/50
idlegpux = gpuTotalCount - totalShared - totalUsed
idleSharedGPUX = totalShared*2 - sharedUsed
gpuxResource = map[string]interface{}{
"100": idlegpux,
"50": idleSharedGPUX,
}
log.Debugf("%s, idle gpu %d, idle shared gpu %d", gpuxName, idlegpux, idleSharedGPUX)
} else {
idlegpux = gpuTotalCount - gpux
gpuxResource["100"] = idlegpux
}
cpu := resource.NewMilliQuantity(int64(total.CPU())-rr[pfResources.ResCPU], resource.DecimalSI)
memory := resource.NewQuantity(int64(total.Memory())-rr[pfResources.ResMemory], resource.BinarySI)
return map[string]interface{}{
pfResources.ResCPU: cpu.String(),
pfResources.ResMemory: memory.String(),
gpuxName: gpuxResource,
}
}
func SubQuota(r *pfResources.Resource, pod *v1.Pod) error {
for _, container := range pod.Spec.Containers {
containerQuota := NewResource(container.Resources.Requests)
r.Sub(containerQuota)
}
return nil
}
// CalcPodResources calculate pod minimum resource
func CalcPodResources(pod *v1.Pod) *pfResources.Resource {
podRes := pfResources.EmptyResource()
if pod == nil {
return podRes
}
for _, c := range pod.Spec.Containers {
res := NewResource(c.Resources.Requests)
podRes.Add(res)
}
return podRes
}
func NewResource(rl v1.ResourceList) *pfResources.Resource {
r := pfResources.EmptyResource()
for rName, rQuant := range rl {
switch rName {
case v1.ResourceCPU:
r.SetResources(pfResources.ResCPU, rQuant.MilliValue())
case v1.ResourceMemory:
r.SetResources(pfResources.ResMemory, rQuant.Value())
case v1.ResourceEphemeralStorage:
r.SetResources(pfResources.ResStorage, rQuant.Value())
default:
if IsScalarResourceName(rName) {
r.SetResources(string(rName), rQuant.Value())
}
}
}
return r
}
// NewResourceList create a new resource object from resource list
func NewResourceList(r *pfResources.Resource) v1.ResourceList {
resourceList := v1.ResourceList{}
if r == nil {
return resourceList
}
for resourceName, RQuant := range r.Resource() {
rName := v1.ResourceName("")
var quantity *resource.Quantity
switch resourceName {
case pfResources.ResCPU:
quantity = resource.NewMilliQuantity(int64(RQuant), resource.DecimalSI)
rName = v1.ResourceCPU
case pfResources.ResMemory:
quantity = resource.NewQuantity(int64(RQuant), resource.BinarySI)
rName = v1.ResourceMemory
default:
quantity = resource.NewQuantity(int64(RQuant), resource.BinarySI)
rName = v1.ResourceName(resourceName)
}
resourceList[rName] = *quantity
}
return resourceList
}
func NewMinResourceList() v1.ResourceList {
resourceList := v1.ResourceList{}
resourceList[v1.ResourceCPU] = *resource.NewQuantity(DefaultCpuRequest, resource.DecimalSI)
resourceList[v1.ResourceMemory] = *resource.NewQuantity(DefaultMemRequest, resource.BinarySI)
return resourceList
}
// GPUDeviceIDX process virtual gpu
func GPUDeviceIDX(idx int) int64 {
if idx < 0 || idx > MaxGPUIndex {
return 0
}
return 1 << (idx * 2)
}
// GPUSharedDevices get shared gpu device index
func GPUSharedDevices(deviceIDX int64) []int {
var result []int
for idx := 0; idx <= MaxGPUIndex; idx++ {
value := deviceIDX & 0b11
if value > 0 {
result = append(result, idx)
}
deviceIDX = deviceIDX >> 2
}
return result
}