cpunumapin.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. "yunion.io/x/onecloud/pkg/apis/scheduler"
  17. "yunion.io/x/onecloud/pkg/scheduler/algorithm/priorities"
  18. "yunion.io/x/onecloud/pkg/scheduler/core"
  19. )
  20. type CpuNumaPinPriority struct {
  21. priorities.BasePriority
  22. }
  23. func (p *CpuNumaPinPriority) Name() string {
  24. return "cpu_numa_pin"
  25. }
  26. func (p *CpuNumaPinPriority) Clone() core.Priority {
  27. return &CpuNumaPinPriority{}
  28. }
  29. func (p *CpuNumaPinPriority) Map(u *core.Unit, c core.Candidater) (core.HostPriority, error) {
  30. h := priorities.NewPriorityHelper(p, u, c)
  31. getter := c.Getter()
  32. if getter.Host().EnableNumaAllocate && getter.NumaAllocateEnabled() && len(u.SchedData().CpuNumaPin) == 0 {
  33. cpuNumaFree := getter.GetFreeCpuNuma()
  34. reqCpuCount := u.SchedInfo.Ncpu
  35. reqMemSize := u.SchedInfo.Memory
  36. nodeCount := 1
  37. for ; nodeCount <= len(cpuNumaFree); nodeCount *= 2 {
  38. if scheduler.NodesFreeCpuEnough(nodeCount, reqCpuCount, cpuNumaFree) &&
  39. scheduler.NodesFreeMemSizeEnough(nodeCount, int(reqMemSize), cpuNumaFree) {
  40. break
  41. }
  42. }
  43. if nodeCount > 0 && nodeCount <= len(cpuNumaFree) {
  44. score := 100 / nodeCount
  45. h.SetScore(int(score))
  46. }
  47. }
  48. return h.GetResult()
  49. }