forked from hkust-adsl/kubernetes-scheduler-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwr_score.go
More file actions
225 lines (187 loc) · 10.4 KB
/
pwr_score.go
File metadata and controls
225 lines (187 loc) · 10.4 KB
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
package plugin
import (
"context"
"fmt"
"math"
"strconv"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
resourcehelper "k8s.io/kubectl/pkg/util/resource"
"k8s.io/kubernetes/pkg/scheduler/framework"
simontype "github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/type"
gpushareutils "github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/type/open-gpu-share/utils"
"github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/utils"
)
type PWRScorePlugin struct {
handle framework.Handle
typicalPods *simontype.TargetPodList
}
// TODO: All the methods and functions should be in place. Now we need to bind the plugin to the scheduler framework, in the right places of the simulator.
// See FGD.
var _ framework.ScorePlugin = &PWRScorePlugin{} // This assignment is used at compile-time to check if the class implements the plugin interface.
// The function below allows to bind this plugin to the simulator.
// NOTE: typical pods should represent the target workload, i.e., pods passed via YAMLs before workload inflation.
// These are required to compute the cluster fragmentation.
func NewPWRScorePlugin(_ runtime.Object, handle framework.Handle, typicalPods *simontype.TargetPodList) (framework.Plugin, error) {
log.Infof("DEBUG FRA, plugin.pwr_score.NewPWRScorePlugin() => Instantiating PWR plugin!\n")
plugin := &PWRScorePlugin{
handle: handle,
typicalPods: typicalPods,
}
allocateGpuIdFunc[plugin.Name()] = allocateGpuIdBasedOnPWRScore
return plugin, nil
}
func (plugin *PWRScorePlugin) Name() string {
return simontype.PWRScorePluginName
}
func (plugin *PWRScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) {
// DEBUG: print the gpu type(s) requested by the pod.
pod_GPU_type := gpushareutils.GetGpuModelFromPodAnnotation(p)
if pod_GPU_type == "" {
if gpushareutils.GetGpuMilliFromPodAnnotation(p) > 0 {
pod_GPU_type = "GENERIC"
} else {
pod_GPU_type = "NONE"
}
}
log.Debugf("DEBUG FRA, plugin.pwr_score.Score() => Scoring node %s w.r.t. pod %s (requested GPU: %s)!\n",
nodeName, p.Name, pod_GPU_type)
// Step 1 - Check if the considered pod does not request any resource -- in this case we return the maximum score (100) and a success status.
// "PodRequestsAndLimits()" returns a dictionary of all defined resources summed up for all containers of the pod.
// If pod overhead is non-nil, the pod overhead is added to the total container resource requests and to the
// total container limits which have a non-zero quantity.
if podReq, _ := resourcehelper.PodRequestsAndLimits(p); len(podReq) == 0 {
log.Debugf("DEBUG FRA, plugin.pwr_score.Score() => the pod does not request any resource!\n")
return framework.MaxNodeScore, framework.NewStatus(framework.Success)
}
// Step 2 - Retrieves the resources of the node specified by nodeName.
nodeResPtr := utils.GetNodeResourceViaHandleAndName(plugin.handle, nodeName)
// Check if "GetNodeResourceViaHandleAndName" failed to retrieve the node's resources, possibly due to the node not being found or some other error.
// In this case, we return the minimum node score and an error status.
if nodeResPtr == nil {
return framework.MinNodeScore, framework.NewStatus(framework.Error, fmt.Sprintf("failed to get nodeRes(%s)\n", nodeName))
}
nodeRes := *nodeResPtr
// Step 3 - Retrieve the resources requested by the pod, and check if the currently considered node is suitable for the pod, i.e.,
// the node has enough resources to accomodate
// the pod, and the GPU type requested by the pod is present on the node.
podRes := utils.GetPodResource(p)
if !utils.IsNodeAccessibleToPod(nodeRes, podRes) {
return framework.MinNodeScore, framework.NewStatus(framework.Error, fmt.Sprintf("Node (%s) %s does not match GPU type request of pod %s\n", nodeName, nodeRes.Repr(), podRes.Repr()))
}
log.Debugf("DEBUG FRA, plugin.pwr_score.Score() => Resources requested from pod: %+v\n", podRes)
log.Debugf("DEBUG FRA, plugin.pwr_score.Score() => Resources offered by node: %+v\n", nodeRes)
// log.Debugf("DEBUG FRA, plugin.pwr_score.Score() => typical pods %+v\n", plugin.typicalPods)
// Step 4 - compute the score of a node w.r.t. the considered pod.
// In this case, the score is calculated based on how much the GPU fragmentation of a node would change IF we hypotetically
// schedule the pod on it -- the more the increase, the worst the score.
score, _ := calculatePWRShareExtendScore(nodeRes, podRes, plugin.typicalPods)
return score, framework.NewStatus(framework.Success)
}
// Here we need to return the struct itself in order to use NormalizeScore.
func (plugin *PWRScorePlugin) ScoreExtensions() framework.ScoreExtensions {
return plugin
}
func (p *PWRScorePlugin) NormalizeScore(ctx context.Context, state *framework.CycleState, pod *v1.Pod, scores framework.NodeScoreList) *framework.Status {
log.Debugf("DEBUG FRA, plugin.pwr_score.NormalizeScore() => Normalizing scores!\n")
// Find the minimum score, as the maximum score is known to be 0
minScore := scores[0].Score
maxScore := minScore
for _, score := range scores {
if score.Score < minScore {
minScore = score.Score
}
if score.Score > maxScore {
maxScore = score.Score
}
}
// Case where all the scores are 0: set them to 100 and return.
if minScore == maxScore {
log.Debugf("DEBUG FRA, plugin.pwr_score.NormalizeScore(): all the scores are equal.\n")
for i, _ := range scores {
scores[i].Score = 100
log.Debugf("DEBUG FRA, plugin.pwr_score.NormalizeScore(): normalized score for node %s: %d\n", scores[i].Name, scores[i].Score)
}
return framework.NewStatus(framework.Success)
}
// Normalize the scores to the range [0, 100].
for i, _ := range scores {
// Normalization formula: normalized_score = (score - minScore) / (0 - minScore) * 100
scores[i].Score = (scores[i].Score - minScore) * 100 / (maxScore - minScore)
log.Debugf("DEBUG FRA, plugin.pwr_score.NormalizeScore(): normalized score for node %s: %d\n", scores[i].Name, scores[i].Score)
}
return framework.NewStatus(framework.Success)
}
// This function computes the score of a node w.r.t. an unscheduled pod. This is done by hypotetically scheduling the pod on the node,
// and then measure how much the node's power consumption increases.
func calculatePWRShareExtendScore(nodeRes simontype.NodeResource, podRes simontype.PodResource, _ *simontype.TargetPodList) (score int64, gpuId string) {
// Compute the node's current power consumption.
old_CPU_energy, old_GPU_energy := nodeRes.GetEnergyConsumptionNode()
old_node_energy := old_CPU_energy + old_GPU_energy
// Case 1 - the pod requests a fraction of the resources of a single GPU.
if podRes.GpuNumber == 1 && podRes.MilliGpu < gpushareutils.MILLI {
// For each GPU in the node, check how the node power consumption would change by hypotetically assigning the considered pod to it.
// NOTE: for now, we are assuming that a GPU consumes max power even if it is minimally used.
score, gpuId = math.MinInt64, ""
// minMilliLeft := int64(gpushareutils.MILLI)
for i := 0; i < len(nodeRes.MilliGpuLeftList); i++ {
// The considered GPU within the node has enough GPU-shared resources to accomodate the pod.
if nodeRes.MilliGpuLeftList[i] >= podRes.MilliGpu {
// Simulate how the available resources on a node would change by scheduling the pod onto a specific node's GPU.
newNodeRes := nodeRes.Copy()
newNodeRes.MilliCpuLeft -= podRes.MilliCpu
newNodeRes.MilliGpuLeftList[i] -= podRes.MilliGpu
// Compute the node's hypotetical increase in power consumption.
new_CPU_energy, new_GPU_energy := newNodeRes.GetEnergyConsumptionNode()
new_node_energy := new_CPU_energy + new_GPU_energy
// Compute the node's score according to the increase in power consumption that we would have by using the i-th GPU.
pwrScore := int64(old_node_energy - new_node_energy)
log.Debugf("DEBUG FRA, plugin.pwr_score.calculatePWRShareExtendScore(): Scoring node %s, GPU %d, with sharing-GPU pod: %d\n",
nodeRes.NodeName, i, pwrScore)
// ### Update the node's best score ### //
// Case 1 - this is the first GPU within the node that can accomodate the pod.
if gpuId == "" {
// minMilliLeft = nodeRes.MilliGpuLeftList[i]
score = pwrScore
gpuId = strconv.Itoa(i)
} else {
// Case 2 - we have found a GPU that is equivalent in terms of power consumption to the best one, but scheduling the pod
// on this GPU ...
/*if (pwrScore == score) && (minMilliLeft < nodeRes.MilliGpuLeftList[i]) {
minMilliLeft = nodeRes.MilliGpuLeftList[i]
gpuId = strconv.Itoa(i)
}*/
// Case 3 - we have found a better GPU than the previous one, i.e., by allocating the pod on this GPU,
// we consume less energy than the previously found solution.
if pwrScore > score {
// minMilliLeft = nodeRes.MilliGpuLeftList[i]
score = pwrScore
gpuId = strconv.Itoa(i)
}
}
}
}
log.Debugf("DEBUG FRA, plugin.pwr_score.calculatePWRShareExtendScore(): Final score for node %s: selected GPU %s, score %d\n",
nodeRes.NodeName, gpuId, score)
return score, gpuId
// Case 2 - the pod requests no (CPU only), or exactly one, or multiple GPUs.
} else {
// Subtract the node's resources that would be taken by the pod once scheduled on it.
newNodeRes, _ := nodeRes.Sub(podRes)
// Compute the node's power consumption, with the updated resource availability.
new_CPU_energy, new_GPU_energy := newNodeRes.GetEnergyConsumptionNode()
new_node_energy := new_CPU_energy + new_GPU_energy
pwrScore := int64(old_node_energy - new_node_energy)
log.Debugf("DEBUG FRA, plugin.pwr_score.calculatePWRShareFragExtendScore(): Scoring node %s with CPU-only or multi-GPU pod: %d\n",
nodeRes.NodeName, pwrScore)
return pwrScore, simontype.AllocateExclusiveGpuId(nodeRes, podRes)
}
}
// This function selects the best GPU(s) found in a given node. It essentially re-executes the allocateGpuIdBasedOnPWRScore function
// executed within Score(), but it considers only the best GPU(s) for a pod found in a node and ignores the computed score.
func allocateGpuIdBasedOnPWRScore(nodeRes simontype.NodeResource, podRes simontype.PodResource, _ simontype.GpuPluginCfg, typicalPods *simontype.TargetPodList) (gpuId string) {
log.Debugf("DEBUG FRA, plugin.pwr_score.allocateGpuIdBasedOnPWRScore() => Scoring node %s w.r.t. pod!\n", nodeRes.NodeName)
_, gpuId = calculatePWRShareExtendScore(nodeRes, podRes, typicalPods)
return gpuId
}