memory_predicate.go 2.8 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. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/apis/scheduler"
  20. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  21. "yunion.io/x/onecloud/pkg/scheduler/core"
  22. )
  23. // MemoryPredicate filter current resources free memory capacity is meet,
  24. // if it is satisfied to return the size of the memory that
  25. // can carry the scheduling request.
  26. type MemoryPredicate struct {
  27. predicates.BasePredicate
  28. }
  29. func (p *MemoryPredicate) Name() string {
  30. return "host_memory"
  31. }
  32. func (p *MemoryPredicate) Clone() core.FitPredicate {
  33. return &MemoryPredicate{}
  34. }
  35. func (p *MemoryPredicate) PreExecute(ctx context.Context, u *core.Unit, cs []core.Candidater) (bool, error) {
  36. driver := u.GetHypervisorDriver()
  37. if driver != nil && !driver.DoScheduleMemoryFilter() {
  38. return false, nil
  39. }
  40. data := u.SchedData()
  41. if data.Memory <= 0 {
  42. return false, nil
  43. }
  44. return true, nil
  45. }
  46. func (p *MemoryPredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  47. h := predicates.NewPredicateHelper(p, u, c)
  48. d := u.SchedData()
  49. useRsvd := h.UseReserved()
  50. getter := c.Getter()
  51. freeMemSize := getter.FreeMemorySize(useRsvd)
  52. reqMemSize := int64(d.Memory)
  53. if freeMemSize < reqMemSize {
  54. totalMemSize := getter.TotalMemorySize(useRsvd)
  55. h.AppendInsufficientResourceError(reqMemSize, totalMemSize, freeMemSize)
  56. }
  57. if cpuNumaFree := getter.GetFreeCpuNuma(); cpuNumaFree != nil {
  58. allcateEnough := false
  59. if d.CpuNumaPin != nil {
  60. nodeCount := len(d.CpuNumaPin)
  61. if scheduler.NodesFreeCpuEnough(nodeCount, d.Ncpu, cpuNumaFree) &&
  62. scheduler.NodesFreeMemSizeEnough(nodeCount, int(reqMemSize), cpuNumaFree) {
  63. allcateEnough = true
  64. }
  65. } else {
  66. for nodeCount := 1; nodeCount <= len(cpuNumaFree); nodeCount *= 2 {
  67. if !scheduler.NodesFreeCpuEnough(nodeCount, d.Ncpu, cpuNumaFree) {
  68. continue
  69. }
  70. if !scheduler.NodesFreeMemSizeEnough(nodeCount, int(reqMemSize), cpuNumaFree) {
  71. continue
  72. }
  73. allcateEnough = true
  74. }
  75. }
  76. if !allcateEnough {
  77. h.AppendPredicateFailMsg(
  78. fmt.Sprintf("cpu numa free %s can't alloc with req mem %v req cpu %v ",
  79. jsonutils.Marshal(cpuNumaFree).String(), d.Memory, d.Ncpu),
  80. )
  81. }
  82. }
  83. h.SetCapacity(freeMemSize / reqMemSize)
  84. return h.GetResult()
  85. }