balance.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /* Drop the dont_move flag on all IRQs for specified CPU */
  15. static int drop_dont_move_flag(cpu_t *cpu)
  16. {
  17. lub_list_node_t *iter;
  18. if (!cpu)
  19. return -1;
  20. for (iter = lub_list_iterator_init(cpu->irqs); iter;
  21. iter = lub_list_iterator_next(iter)) {
  22. irq_t *irq;
  23. irq = (irq_t *)lub_list_node__get_data(iter);
  24. irq->dont_move = 0;
  25. }
  26. return 0;
  27. }
  28. /* Move IRQ to specified CPU. Remove IRQ from the IRQ list
  29. of old CPU. */
  30. static int move_irq_to_cpu(irq_t *irq, cpu_t *cpu)
  31. {
  32. if (!irq || !cpu)
  33. return -1;
  34. if (irq->cpu) {
  35. cpu_t *old_cpu = irq->cpu;
  36. lub_list_node_t *node;
  37. node = lub_list_search(old_cpu->irqs, irq);
  38. if (node) {
  39. lub_list_del(old_cpu->irqs, node);
  40. lub_list_node_free(node);
  41. }
  42. drop_dont_move_flag(old_cpu);
  43. }
  44. drop_dont_move_flag(cpu);
  45. irq->cpu = cpu;
  46. lub_list_add(cpu->irqs, irq);
  47. return 0;
  48. }
  49. /* Search for the best CPU. Best CPU is a CPU with minimal load.
  50. If several CPUs have the same load then the best CPU is a CPU
  51. with minimal number of assigned IRQs */
  52. static cpu_t *choose_cpu(lub_list_t *cpus, cpumask_t cpumask, float threshold)
  53. {
  54. lub_list_node_t *iter;
  55. lub_list_t * min_cpus = NULL;
  56. float min_load = 100.00;
  57. lub_list_node_t *node;
  58. cpu_t *cpu = NULL;
  59. for (iter = lub_list_iterator_init(cpus); iter;
  60. iter = lub_list_iterator_next(iter)) {
  61. cpu = (cpu_t *)lub_list_node__get_data(iter);
  62. if (!cpu_isset(cpu->id, cpumask))
  63. continue;
  64. if (cpu->load >= threshold)
  65. continue;
  66. if ((!min_cpus) || (cpu->load < min_load)) {
  67. min_load = cpu->load;
  68. if (!min_cpus)
  69. min_cpus = lub_list_new(cpu_list_compare_len);
  70. while ((node = lub_list__get_tail(min_cpus))) {
  71. lub_list_del(min_cpus, node);
  72. lub_list_node_free(node);
  73. }
  74. lub_list_add(min_cpus, cpu);
  75. }
  76. if (cpu->load == min_load)
  77. lub_list_add(min_cpus, cpu);
  78. }
  79. if (!min_cpus)
  80. return NULL;
  81. node = lub_list__get_head(min_cpus);
  82. cpu = (cpu_t *)lub_list_node__get_data(node);
  83. while ((node = lub_list__get_tail(min_cpus))) {
  84. lub_list_del(min_cpus, node);
  85. lub_list_node_free(node);
  86. }
  87. lub_list_free(min_cpus);
  88. return cpu;
  89. }
  90. /* Find best CPUs for IRQs need to be balanced. */
  91. int balance(lub_list_t *cpus, lub_list_t *balance_irqs, float threshold)
  92. {
  93. lub_list_node_t *iter;
  94. for (iter = lub_list_iterator_init(balance_irqs); iter;
  95. iter = lub_list_iterator_next(iter)) {
  96. irq_t *irq;
  97. cpu_t *cpu;
  98. irq = (irq_t *)lub_list_node__get_data(iter);
  99. /* Try to find local CPU to move IRQ to.
  100. The local CPU is CPU with native NUMA node. */
  101. cpu = choose_cpu(cpus, irq->local_cpus, threshold);
  102. /* If local CPU is not found then try to use
  103. CPU from another NUMA node. It's better then
  104. overloaded CPUs. */
  105. /* Non-local CPUs were disabled. It seems there is
  106. no advantages to use them. The all interactions will
  107. be held by QPI-like interfaces through local CPUs. */
  108. /* if (!cpu) {
  109. cpumask_t complement;
  110. cpus_complement(complement, irq->local_cpus);
  111. cpu = choose_cpu(cpus, complement, threshold);
  112. }
  113. */
  114. if (cpu) {
  115. if (irq->cpu)
  116. printf("Move IRQ %u from CPU%u to CPU%u\n",
  117. irq->irq, irq->cpu->id, cpu->id);
  118. else
  119. printf("Move IRQ %u to CPU%u\n", irq->irq, cpu->id);
  120. move_irq_to_cpu(irq, cpu);
  121. }
  122. }
  123. return 0;
  124. }
  125. int apply_affinity(lub_list_t *balance_irqs)
  126. {
  127. lub_list_node_t *iter;
  128. for (iter = lub_list_iterator_init(balance_irqs); iter;
  129. iter = lub_list_iterator_next(iter)) {
  130. irq_t *irq;
  131. irq = (irq_t *)lub_list_node__get_data(iter);
  132. if (!irq->cpu)
  133. continue;
  134. irq_set_affinity(irq, irq->cpu->cpumask);
  135. }
  136. return 0;
  137. }
  138. /* Search for the overloaded CPUs and then choose best IRQ for moving to
  139. another CPU. The best IRQ is IRQ with maximum number of interrupts.
  140. The IRQs with small number of interrupts have very low load or very
  141. high load (in a case of NAPI). */
  142. int choose_irqs_to_move(lub_list_t *cpus, lub_list_t *balance_irqs, float threshold)
  143. {
  144. lub_list_node_t *iter;
  145. cpu_t *overloaded_cpu = NULL;
  146. irq_t *irq_to_move = NULL;
  147. float max_load = 0.0;
  148. unsigned long long max_intr = 0;
  149. /* Search for the most overloaded CPU.
  150. The load must be greater than threshold. */
  151. for (iter = lub_list_iterator_init(cpus); iter;
  152. iter = lub_list_iterator_next(iter)) {
  153. cpu_t *cpu = (cpu_t *)lub_list_node__get_data(iter);
  154. if (cpu->load < threshold)
  155. continue;
  156. if (cpu->load > max_load) {
  157. max_load = cpu->load;
  158. overloaded_cpu = cpu;
  159. }
  160. }
  161. /* Can't find overloaded CPUs */
  162. if (!overloaded_cpu)
  163. return 0;
  164. /* Search for the IRQ (owned by overloaded CPU) with
  165. maximum number of interrupts. */
  166. if (lub_list_len(overloaded_cpu->irqs) <= 1)
  167. return 0;
  168. for (iter = lub_list_iterator_init(overloaded_cpu->irqs); iter;
  169. iter = lub_list_iterator_next(iter)) {
  170. irq_t *irq = (irq_t *)lub_list_node__get_data(iter);
  171. /* Don't move any IRQs with intr=0. It can be unused IRQ. In
  172. this case the moving is not needed. It can be overloaded
  173. (by NAPI) IRQs. In this case it will be not moved anyway. */
  174. if (irq->intr == 0)
  175. continue;
  176. if (irq->dont_move)
  177. continue;
  178. if (irq->intr >= max_intr) {
  179. max_intr = irq->intr;
  180. irq_to_move = irq;
  181. }
  182. }
  183. if (irq_to_move) {
  184. /* Don't move this IRQ while next iteration. */
  185. irq_to_move->dont_move = 1;
  186. lub_list_add(balance_irqs, irq_to_move);
  187. }
  188. return 0;
  189. }