-
Notifications
You must be signed in to change notification settings - Fork 542
/
scheduling_throughput.go
189 lines (170 loc) · 6.15 KB
/
scheduling_throughput.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
/*
Copyright 2018 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 common
import (
"fmt"
"math"
"sort"
"time"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/perf-tests/clusterloader2/pkg/errors"
"k8s.io/perf-tests/clusterloader2/pkg/measurement"
measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util"
"k8s.io/perf-tests/clusterloader2/pkg/util"
)
const (
schedulingThroughputMeasurementName = "SchedulingThroughput"
defaultSchedulingThroughputInterval = 5 * time.Second
)
func init() {
if err := measurement.Register(schedulingThroughputMeasurementName, createSchedulingThroughputMeasurement); err != nil {
klog.Fatalf("Cannot register %s: %v", schedulingThroughputMeasurementName, err)
}
}
func createSchedulingThroughputMeasurement() measurement.Measurement {
return &schedulingThroughputMeasurement{}
}
type schedulingThroughputMeasurement struct {
schedulingThroughputs []float64
isRunning bool
stopCh chan struct{}
}
// Execute supports two actions:
// - start - starts the pods scheduling observation.
// Pods can be specified by field and/or label selectors.
// If namespace is not passed by parameter, all-namespace scope is assumed.
// - gather - creates summary for observed values.
func (s *schedulingThroughputMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) {
action, err := util.GetString(config.Params, "action")
if err != nil {
return nil, err
}
switch action {
case "start":
if s.isRunning {
klog.V(3).Infof("%s: measurement already running", s)
return nil, nil
}
selector := util.NewObjectSelector()
if err := selector.Parse(config.Params); err != nil {
return nil, err
}
measurmentInterval, err := util.GetDurationOrDefault(config.Params, "measurmentInterval", defaultSchedulingThroughputInterval)
if err != nil {
return nil, err
}
s.stopCh = make(chan struct{})
return nil, s.start(config.ClusterFramework.GetClientSets().GetClient(), selector, measurmentInterval)
case "gather":
threshold, err := util.GetFloat64OrDefault(config.Params, "threshold", 0)
if err != nil {
klog.Warningf("error while getting threshold param: %v", err)
}
enableViolations, err := util.GetBoolOrDefault(config.Params, "enableViolations", true)
if err != nil {
klog.Warningf("error while getting enableViolations param: %v", err)
}
summary, err := s.gather(threshold)
if err != nil {
if !errors.IsMetricViolationError(err) {
klog.Errorf("%s gathering error: %v", config.Identifier, err)
return nil, err
}
if !enableViolations {
err = nil
}
}
return summary, err
default:
return nil, fmt.Errorf("unknown action %v", action)
}
}
// Dispose cleans up after the measurement.
func (s *schedulingThroughputMeasurement) Dispose() {
s.stop()
}
// String returns a string representation of the measurement.
func (*schedulingThroughputMeasurement) String() string {
return schedulingThroughputMeasurementName
}
func (s *schedulingThroughputMeasurement) start(clientSet clientset.Interface, selector *util.ObjectSelector, measurmentInterval time.Duration) error {
ps, err := measurementutil.NewPodStore(clientSet, selector)
if err != nil {
return fmt.Errorf("pod store creation error: %v", err)
}
s.isRunning = true
klog.V(2).Infof("%s: starting collecting throughput data", s)
go func() {
defer ps.Stop()
lastScheduledCount := 0
for {
select {
case <-s.stopCh:
return
case <-time.After(measurmentInterval):
pods, err := ps.List()
if err != nil {
// List in NewPodStore never returns error.
// TODO(mborsz): Even if this is a case now, it doesn't need to be true in future. Refactor this.
panic(fmt.Errorf("unexpected error on PodStore.List: %w", err))
}
podsStatus := measurementutil.ComputePodsStartupStatus(pods, 0, nil /* updatePodPredicate */)
throughput := float64(podsStatus.Scheduled-lastScheduledCount) / float64(measurmentInterval/time.Second)
s.schedulingThroughputs = append(s.schedulingThroughputs, throughput)
lastScheduledCount = podsStatus.Scheduled
klog.V(3).Infof("%v: %s: %d pods scheduled", s, selector.String(), lastScheduledCount)
}
}
}()
return nil
}
func (s *schedulingThroughputMeasurement) gather(threshold float64) ([]measurement.Summary, error) {
if !s.isRunning {
klog.Errorf("%s: measurement is not running", s)
return nil, fmt.Errorf("measurement is not running")
}
s.stop()
klog.V(2).Infof("%s: gathering data", s)
throughputSummary := &schedulingThroughput{}
if length := len(s.schedulingThroughputs); length > 0 {
sort.Float64s(s.schedulingThroughputs)
throughputSummary.Perc50 = s.schedulingThroughputs[int(math.Ceil(float64(length*50)/100))-1]
throughputSummary.Perc90 = s.schedulingThroughputs[int(math.Ceil(float64(length*90)/100))-1]
throughputSummary.Perc99 = s.schedulingThroughputs[int(math.Ceil(float64(length*99)/100))-1]
throughputSummary.Max = s.schedulingThroughputs[length-1]
}
content, err := util.PrettyPrintJSON(throughputSummary)
if err != nil {
return nil, err
}
summary := measurement.CreateSummary(schedulingThroughputMeasurementName, "json", content)
if threshold > 0 && throughputSummary.Max < threshold {
err = errors.NewMetricViolationError(
"scheduler throughput",
fmt.Sprintf("actual throughput %f lower than threshold %f", throughputSummary.Max, threshold))
}
return []measurement.Summary{summary}, err
}
func (s *schedulingThroughputMeasurement) stop() {
if s.isRunning {
close(s.stopCh)
s.isRunning = false
}
}
type schedulingThroughput struct {
Perc50 float64 `json:"perc50"`
Perc90 float64 `json:"perc90"`
Perc99 float64 `json:"perc99"`
Max float64 `json:"max"`
}