balance.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* balance.c
  2. * Balance IRQs.
  3. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <limits.h>
  10. #include <ctype.h>
  11. #include "statistics.h"
  12. #include "cpu.h"
  13. #include "irq.h"
  14. /* Move IRQ to specified CPU. Remove IRQ from the IRQ list
  15. of old CPU. */
  16. static int move_irq_to_cpu(irq_t *irq, cpu_t *cpu)
  17. {
  18. if (!irq || !cpu)
  19. return -1;
  20. if (irq->cpu) {
  21. cpu_t *old_cpu = irq->cpu;
  22. lub_list_node_t *node;
  23. node = lub_list_search(old_cpu->irqs, irq);
  24. if (node) {
  25. lub_list_del(old_cpu->irqs, node);
  26. lub_list_node_free(node);
  27. }
  28. }
  29. lub_list_add(cpu->irqs, irq);
  30. irq->cpu = cpu;
  31. return 0;
  32. }
  33. /* Search for the best CPU. Best CPU is a CPU with minimal load.
  34. If several CPUs have the same load then the best CPU is a CPU
  35. with minimal number of assigned IRQs */
  36. static cpu_t *choose_cpu(lub_list_t *cpus, cpumask_t cpumask, float threshold)
  37. {
  38. lub_list_node_t *iter;
  39. lub_list_t * min_cpus = NULL;
  40. float min_load = 100.00;
  41. lub_list_node_t *node;
  42. cpu_t *cpu = NULL;
  43. for (iter = lub_list_iterator_init(cpus); iter;
  44. iter = lub_list_iterator_next(iter)) {
  45. cpu = (cpu_t *)lub_list_node__get_data(iter);
  46. if (!cpu_isset(cpu->id, cpumask))
  47. continue;
  48. if (cpu->load >= threshold)
  49. continue;
  50. if ((!min_cpus) || (cpu->load < min_load)) {
  51. min_load = cpu->load;
  52. if (!min_cpus)
  53. min_cpus = lub_list_new(cpu_list_compare_len);
  54. while ((node = lub_list__get_tail(min_cpus))) {
  55. lub_list_del(min_cpus, node);
  56. lub_list_node_free(node);
  57. }
  58. lub_list_add(min_cpus, cpu);
  59. }
  60. if (cpu->load == min_load)
  61. lub_list_add(min_cpus, cpu);
  62. }
  63. if (!min_cpus)
  64. return NULL;
  65. node = lub_list__get_head(min_cpus);
  66. cpu = (cpu_t *)lub_list_node__get_data(node);
  67. while ((node = lub_list__get_tail(min_cpus))) {
  68. lub_list_del(min_cpus, node);
  69. lub_list_node_free(node);
  70. }
  71. lub_list_free(min_cpus);
  72. return cpu;
  73. }
  74. /* Find best CPUs for IRQs need to be balanced. */
  75. int balance(lub_list_t *cpus, lub_list_t *balance_irqs, float threshold)
  76. {
  77. lub_list_node_t *iter;
  78. for (iter = lub_list_iterator_init(balance_irqs); iter;
  79. iter = lub_list_iterator_next(iter)) {
  80. irq_t *irq;
  81. cpu_t *cpu;
  82. irq = (irq_t *)lub_list_node__get_data(iter);
  83. /* Try to find local CPU to move IRQ to.
  84. The local CPU is CPU with native NUMA node. */
  85. cpu = choose_cpu(cpus, irq->local_cpus, threshold);
  86. /* If local CPU is not found then try to use
  87. CPU from another NUMA node. It's better then
  88. overloaded CPUs. */
  89. if (!cpu) {
  90. cpumask_t complement;
  91. cpus_complement(complement, irq->local_cpus);
  92. cpu = choose_cpu(cpus, complement, threshold);
  93. }
  94. if (cpu) {
  95. move_irq_to_cpu(irq, cpu);
  96. printf("Move IRQ %u to CPU%u\n", irq->irq, cpu->id);
  97. }
  98. }
  99. return 0;
  100. }
  101. int apply_affinity(lub_list_t *balance_irqs)
  102. {
  103. lub_list_node_t *iter;
  104. for (iter = lub_list_iterator_init(balance_irqs); iter;
  105. iter = lub_list_iterator_next(iter)) {
  106. irq_t *irq;
  107. irq = (irq_t *)lub_list_node__get_data(iter);
  108. if (!irq->cpu)
  109. continue;
  110. irq_set_affinity(irq, irq->cpu->cpumask);
  111. }
  112. return 0;
  113. }
  114. /* Search for the overloaded CPUs and then choose best IRQ for moving to
  115. another CPU. The best IRQ is IRQ with maximum number of interrupts.
  116. The IRQs with small number of interrupts have very low load or very
  117. high load (in a case of NAPI). */
  118. int choose_irqs_to_move(lub_list_t *cpus, lub_list_t *balance_irqs, float threshold)
  119. {
  120. lub_list_node_t *iter;
  121. cpu_t *overloaded_cpu = NULL;
  122. irq_t *irq_to_move = NULL;
  123. float max_load = 0.0;
  124. unsigned long long max_intr = 0;
  125. /* Search for the most overloaded CPU.
  126. The load must be greater than threshold. */
  127. for (iter = lub_list_iterator_init(cpus); iter;
  128. iter = lub_list_iterator_next(iter)) {
  129. cpu_t *cpu = (cpu_t *)lub_list_node__get_data(iter);
  130. if (cpu->load < threshold)
  131. continue;
  132. if (cpu->load > max_load) {
  133. max_load = cpu->load;
  134. overloaded_cpu = cpu;
  135. }
  136. }
  137. /* Can't find overloaded CPUs */
  138. if (!overloaded_cpu)
  139. return 0;
  140. /* Search for the IRQ (owned by overloaded CPU) with
  141. maximum number of interrupts. */
  142. if (lub_list_len(overloaded_cpu->irqs) <= 1)
  143. return 0;
  144. for (iter = lub_list_iterator_init(overloaded_cpu->irqs); iter;
  145. iter = lub_list_iterator_next(iter)) {
  146. irq_t *irq = (irq_t *)lub_list_node__get_data(iter);
  147. if (irq->intr >= max_intr) {
  148. max_intr = irq->intr;
  149. irq_to_move = irq;
  150. }
  151. }
  152. if (irq_to_move)
  153. lub_list_add(balance_irqs, irq_to_move);
  154. return 0;
  155. }