Skip to content

Commit 877818c

Browse files
committed
WIP: prevalidate and postvalidate
Signed-off-by: Francesco Romani <fromani@redhat.com>
1 parent cc52c5f commit 877818c

File tree

1 file changed

+24
-4
lines changed
  • pkg/performanceprofile/profilecreator/autosize

1 file changed

+24
-4
lines changed

pkg/performanceprofile/profilecreator/autosize/autosize.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
)
3939

4040
var (
41+
ErrInvalidParameters = errors.New("invalid parameters")
4142
ErrUnderallocatedControlPlane = errors.New("not enough CPUs for control plane")
4243
ErrOverallocatedControlPlane = errors.New("too many CPUs for control plane")
4344
ErrInconsistentAllocation = errors.New("inconsistent CPus allocation")
@@ -54,6 +55,7 @@ func DefaultEnv() Env {
5455
}
5556

5657
type Params struct {
58+
DeviceCount int
5759
OfflinedCPUCount int
5860
UserLevelNetworking bool
5961
MachineData *profilecreator.GHWHandler
@@ -63,7 +65,7 @@ type Params struct {
6365
}
6466

6567
func (p Params) String() string {
66-
return fmt.Sprintf("cpus=%d offline=%v SMTLevel=%v", p.totalCPUs, p.OfflinedCPUCount, p.smtLevel)
68+
return fmt.Sprintf("cpus=%d offline=%v SMTLevel=%v devices=%d (userNetworking=%v)", p.totalCPUs, p.OfflinedCPUCount, p.smtLevel, p.DeviceCount, p.UserLevelNetworking)
6769
}
6870

6971
func setupMachineData(p *Params) error {
@@ -143,9 +145,23 @@ func (vals Values) String() string {
143145
return fmt.Sprintf("reserved=%v/isolated=%v", vals.ReservedCPUCount, vals.IsolatedCPUCount)
144146
}
145147

148+
func CheckParameters(params Params) error {
149+
if params.DeviceCount < 0 {
150+
return ErrInvalidParameters
151+
}
152+
if params.OfflinedCPUCount < 0 {
153+
return ErrInvalidParameters
154+
}
155+
// are we offlining everything? we need at least 1 physical core to do any work, including staying alive
156+
if params.OfflinedCPUCount > (params.totalCPUs - params.smtLevel) {
157+
return ErrInvalidParameters
158+
}
159+
return nil
160+
}
161+
146162
// gonum doesn't support bounds yet so we have to make this an explicit step
147163
// https://github.com/gonum/gonum/issues/1725
148-
func Validate(params Params, vals Values) error {
164+
func CheckValues(params Params, vals Values) error {
149165
Tc := params.TotalCPUs()
150166
if vals.ReservedCPUCount < params.SMTLevel() {
151167
return ErrUnderallocatedControlPlane
@@ -187,7 +203,11 @@ func objective(p Params, x []float64) float64 {
187203
}
188204

189205
func Compute(env Env, params Params) (Values, Score, error) {
190-
err := setupMachineData(&params)
206+
err := CheckParameters(params)
207+
if err != nil {
208+
return params.DefaultAllocation(), Score{}, err
209+
}
210+
err = setupMachineData(&params)
191211
if err != nil {
192212
env.Log.Printf("Optimization failed: %v", err)
193213
return params.DefaultAllocation(), Score{}, err
@@ -220,7 +240,7 @@ func Compute(env Env, params Params) (Values, Score, error) {
220240
}
221241
env.Log.Printf("Optimization result: %s", opt.String())
222242

223-
if err := Validate(params, opt); err != nil {
243+
if err := CheckValues(params, opt); err != nil {
224244
env.Log.Printf("Optimization invalid: %v", err)
225245
return params.DefaultAllocation(), Score{}, err
226246
}

0 commit comments

Comments
 (0)