cpu_predicate.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package guest
  15. import (
  16. "context"
  17. "yunion.io/x/onecloud/pkg/apis"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  20. "yunion.io/x/onecloud/pkg/scheduler/core"
  21. )
  22. // CPUPredicate check the current resources of the CPU is available,
  23. // it returns the maximum available capacity.
  24. type CPUPredicate struct {
  25. predicates.BasePredicate
  26. }
  27. func (f *CPUPredicate) Name() string {
  28. return "host_cpu"
  29. }
  30. func (f *CPUPredicate) Clone() core.FitPredicate {
  31. return &CPUPredicate{}
  32. }
  33. func (f *CPUPredicate) PreExecute(ctx context.Context, u *core.Unit, cs []core.Candidater) (bool, error) {
  34. driver := u.GetHypervisorDriver()
  35. if driver != nil && !driver.DoScheduleCPUFilter() {
  36. return false, nil
  37. }
  38. data := u.SchedData()
  39. if data.Ncpu <= 0 {
  40. return false, nil
  41. }
  42. return true, nil
  43. }
  44. func (f *CPUPredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  45. h := predicates.NewPredicateHelper(f, u, c)
  46. d := u.SchedData()
  47. useRsvd := h.UseReserved()
  48. getter := c.Getter()
  49. archMatch := true
  50. isArmHost := getter.IsArmHost()
  51. isRiscvHost := getter.IsRISCVHost()
  52. if d.Hypervisor != compute.HYPERVISOR_POD {
  53. if apis.IsARM(d.OsArch) {
  54. // process arm64 host
  55. if !isArmHost {
  56. archMatch = false
  57. }
  58. } else if apis.IsRISCV(d.OsArch) {
  59. if !isRiscvHost {
  60. archMatch = false
  61. }
  62. } else {
  63. // process x86_64 host
  64. if isArmHost || isRiscvHost {
  65. archMatch = false
  66. }
  67. }
  68. }
  69. if !archMatch {
  70. h.Exclude2(predicates.ErrHostCpuArchitectureNotMatch, getter.CPUArch(), d.OsArch)
  71. return h.GetResult()
  72. }
  73. reqCPUCount := int64(d.Ncpu + d.ExtraCpuCount)
  74. if getter.KvmCapMaxVcpuCount() > 0 && reqCPUCount > getter.KvmCapMaxVcpuCount() {
  75. h.Exclude2(predicates.ErrHostKvmVcpuMaxNotEnough, getter.KvmCapMaxVcpuCount(), reqCPUCount)
  76. return h.GetResult()
  77. }
  78. freeCPUCount := getter.FreeCPUCount(useRsvd)
  79. if freeCPUCount < reqCPUCount {
  80. totalCPUCount := getter.TotalCPUCount(useRsvd)
  81. h.AppendInsufficientResourceError(reqCPUCount, totalCPUCount, freeCPUCount)
  82. }
  83. h.SetCapacity(freeCPUCount / reqCPUCount)
  84. return h.GetResult()
  85. }