balance.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include "balance.h"
  15. /* Drop the dont_move flag on all IRQs for specified CPU */
  16. static int dec_weight(cpu_t *cpu, int value)
  17. {
  18. lub_list_node_t *iter;
  19. if (!cpu)
  20. return -1;
  21. if (value < 0)
  22. return -1;
  23. for (iter = lub_list_iterator_init(cpu->irqs); iter;
  24. iter = lub_list_iterator_next(iter)) {
  25. irq_t *irq;
  26. irq = (irq_t *)lub_list_node__get_data(iter);
  27. if (irq->weight >= value)
  28. irq->weight -= value;
  29. }
  30. return 0;
  31. }
  32. /* Remove IRQ from specified CPU */
  33. static int remove_irq_from_cpu(irq_t *irq, cpu_t *cpu)
  34. {
  35. lub_list_node_t *node;
  36. if (!irq || !cpu)
  37. return -1;
  38. irq->cpu = NULL;
  39. node = lub_list_search(cpu->irqs, irq);
  40. if (!node)
  41. return 0;
  42. lub_list_del(cpu->irqs, node);
  43. lub_list_node_free(node);
  44. return 0;
  45. }
  46. /* Move IRQ to specified CPU. Remove IRQ from the IRQ list
  47. of old CPU. */
  48. static int move_irq_to_cpu(irq_t *irq, cpu_t *cpu)
  49. {
  50. if (!irq || !cpu)
  51. return -1;
  52. if (irq->cpu) {
  53. cpu_t *old_cpu = irq->cpu;
  54. remove_irq_from_cpu(irq, old_cpu);
  55. dec_weight(old_cpu, 1);
  56. }
  57. dec_weight(cpu, 1);
  58. irq->cpu = cpu;
  59. lub_list_add(cpu->irqs, irq);
  60. return 0;
  61. }
  62. /* Search for the best CPU. Best CPU is a CPU with minimal load.
  63. If several CPUs have the same load then the best CPU is a CPU
  64. with minimal number of assigned IRQs */
  65. static cpu_t *choose_cpu(lub_list_t *cpus, cpumask_t cpumask, float threshold)
  66. {
  67. lub_list_node_t *iter;
  68. lub_list_t * min_cpus = NULL;
  69. float min_load = 100.00;
  70. lub_list_node_t *node;
  71. cpu_t *cpu = NULL;
  72. for (iter = lub_list_iterator_init(cpus); iter;
  73. iter = lub_list_iterator_next(iter)) {
  74. cpu = (cpu_t *)lub_list_node__get_data(iter);
  75. if (!cpu_isset(cpu->id, cpumask))
  76. continue;
  77. if (cpu->load >= threshold)
  78. continue;
  79. if ((!min_cpus) || (cpu->load < min_load)) {
  80. min_load = cpu->load;
  81. if (!min_cpus)
  82. min_cpus = lub_list_new(cpu_list_compare_len);
  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_add(min_cpus, cpu);
  88. }
  89. if (cpu->load == min_load)
  90. lub_list_add(min_cpus, cpu);
  91. }
  92. if (!min_cpus)
  93. return NULL;
  94. node = lub_list__get_head(min_cpus);
  95. cpu = (cpu_t *)lub_list_node__get_data(node);
  96. while ((node = lub_list__get_tail(min_cpus))) {
  97. lub_list_del(min_cpus, node);
  98. lub_list_node_free(node);
  99. }
  100. lub_list_free(min_cpus);
  101. return cpu;
  102. }
  103. static int irq_set_affinity(irq_t *irq, cpumask_t cpumask)
  104. {
  105. char path[PATH_MAX];
  106. char buf[NR_CPUS + 1];
  107. FILE *fd;
  108. if (!irq)
  109. return -1;
  110. sprintf(path, "%s/%u/smp_affinity", PROC_IRQ, irq->irq);
  111. if (!(fd = fopen(path, "w")))
  112. return -1;
  113. cpumask_scnprintf(buf, sizeof(buf), cpumask);
  114. fprintf(fd, "%s", buf);
  115. fclose(fd);
  116. /* Check for newly apllied affinity. The affinities for some
  117. IRQ can't be changed. So don't consider such IRQs. The
  118. example is IRQ 0 - timer. */
  119. irq_get_affinity(irq);
  120. if (!cpus_equal(irq->affinity, cpumask)) {
  121. /* Blacklist this IRQ */
  122. irq->blacklisted = 1;
  123. remove_irq_from_cpu(irq, irq->cpu);
  124. printf("Blacklist IRQ %u\n", irq->irq);
  125. }
  126. return 0;
  127. }
  128. /* Find best CPUs for IRQs need to be balanced. */
  129. int balance(lub_list_t *cpus, lub_list_t *balance_irqs, float threshold)
  130. {
  131. lub_list_node_t *iter;
  132. for (iter = lub_list_iterator_init(balance_irqs); iter;
  133. iter = lub_list_iterator_next(iter)) {
  134. irq_t *irq;
  135. cpu_t *cpu;
  136. irq = (irq_t *)lub_list_node__get_data(iter);
  137. /* Try to find local CPU to move IRQ to.
  138. The local CPU is CPU with native NUMA node. */
  139. cpu = choose_cpu(cpus, irq->local_cpus, threshold);
  140. /* If local CPU is not found then try to use
  141. CPU from another NUMA node. It's better then
  142. overloaded CPUs. */
  143. /* Non-local CPUs were disabled. It seems there is
  144. no advantages to use them. The all interactions will
  145. be held by QPI-like interfaces through local CPUs. */
  146. /* if (!cpu) {
  147. cpumask_t complement;
  148. cpus_complement(complement, irq->local_cpus);
  149. cpu = choose_cpu(cpus, complement, threshold);
  150. }
  151. */
  152. if (cpu) {
  153. if (irq->cpu)
  154. printf("Move IRQ %u from CPU%u to CPU%u\n",
  155. irq->irq, irq->cpu->id, cpu->id);
  156. else
  157. printf("Move IRQ %u to CPU%u\n", irq->irq, cpu->id);
  158. move_irq_to_cpu(irq, cpu);
  159. }
  160. }
  161. return 0;
  162. }
  163. int apply_affinity(lub_list_t *balance_irqs)
  164. {
  165. lub_list_node_t *iter;
  166. for (iter = lub_list_iterator_init(balance_irqs); iter;
  167. iter = lub_list_iterator_next(iter)) {
  168. irq_t *irq;
  169. irq = (irq_t *)lub_list_node__get_data(iter);
  170. if (!irq->cpu)
  171. continue;
  172. irq_set_affinity(irq, irq->cpu->cpumask);
  173. }
  174. return 0;
  175. }
  176. /* Count the number of intr-not-null IRQs and minimal IRQ weight */
  177. static int irq_list_info(lub_list_t *irqs, int *min_weight,
  178. unsigned int *irq_num, unsigned int *candidates_num)
  179. {
  180. lub_list_node_t *iter;
  181. if (!irqs)
  182. return -1;
  183. if (min_weight)
  184. *min_weight = -1;
  185. if (irq_num)
  186. *irq_num = 0;
  187. if (candidates_num)
  188. *candidates_num = 0;
  189. for (iter = lub_list_iterator_init(irqs); iter;
  190. iter = lub_list_iterator_next(iter)) {
  191. irq_t *irq = (irq_t *)lub_list_node__get_data(iter);
  192. if (irq->intr == 0)
  193. continue;
  194. if (min_weight) {
  195. if ((*min_weight < 0) || (irq->weight < *min_weight))
  196. *min_weight = irq->weight;
  197. }
  198. if (irq_num)
  199. *irq_num += 1;
  200. if (irq->weight)
  201. continue;
  202. if (candidates_num)
  203. *candidates_num += 1;
  204. }
  205. return 0;
  206. }
  207. /* Search for most overloaded CPU */
  208. static cpu_t * most_overloaded_cpu(lub_list_t *cpus, float threshold)
  209. {
  210. lub_list_node_t *iter;
  211. cpu_t *overloaded_cpu = NULL;
  212. float max_load = 0.0;
  213. /* Search for the most overloaded CPU.
  214. The load must be greater than threshold. */
  215. for (iter = lub_list_iterator_init(cpus); iter;
  216. iter = lub_list_iterator_next(iter)) {
  217. cpu_t *cpu = (cpu_t *)lub_list_node__get_data(iter);
  218. int min_weight = -1;
  219. unsigned int irq_num = 0;
  220. if (cpu->load < threshold)
  221. continue;
  222. if (cpu->load <= max_load)
  223. continue;
  224. /* Don't move last IRQ */
  225. if (lub_list_len(cpu->irqs) <= 1)
  226. continue;
  227. irq_list_info(cpu->irqs, &min_weight, &irq_num, NULL);
  228. /* All IRQs has intr=0 */
  229. if (irq_num == 0)
  230. continue;
  231. if (min_weight > 0)
  232. dec_weight(cpu, min_weight);
  233. /* Ok, it's good CPU to try to free it */
  234. max_load = cpu->load;
  235. overloaded_cpu = cpu;
  236. }
  237. return overloaded_cpu;
  238. }
  239. /* Search for the overloaded CPUs and then choose best IRQ for moving to
  240. another CPU. The best IRQ is IRQ with maximum number of interrupts.
  241. The IRQs with small number of interrupts have very low load or very
  242. high load (in a case of NAPI). */
  243. int choose_irqs_to_move(lub_list_t *cpus, lub_list_t *balance_irqs,
  244. float threshold, birq_choose_strategy_e strategy)
  245. {
  246. lub_list_node_t *iter;
  247. cpu_t *overloaded_cpu = NULL;
  248. irq_t *irq_to_move = NULL;
  249. unsigned long long max_intr = 0;
  250. unsigned long long min_intr = (unsigned long long)(-1);
  251. unsigned int choose = 0;
  252. unsigned int current = 0;
  253. /* Search for overloaded CPUs */
  254. if (!(overloaded_cpu = most_overloaded_cpu(cpus, threshold)))
  255. return 0;
  256. if (strategy == BIRQ_CHOOSE_RND) {
  257. unsigned int candidates = 0;
  258. irq_list_info(overloaded_cpu->irqs, NULL, NULL, &candidates);
  259. if (candidates == 0)
  260. return 0;
  261. choose = rand() % candidates;
  262. }
  263. /* Search for the IRQ (owned by overloaded CPU) with
  264. maximum/minimum number of interrupts. */
  265. for (iter = lub_list_iterator_init(overloaded_cpu->irqs); iter;
  266. iter = lub_list_iterator_next(iter)) {
  267. irq_t *irq = (irq_t *)lub_list_node__get_data(iter);
  268. /* Don't move any IRQs with intr=0. It can be unused IRQ. In
  269. this case the moving is not needed. It can be overloaded
  270. (by NAPI) IRQs. In this case it will be not moved anyway. */
  271. if (irq->intr == 0)
  272. continue;
  273. if (irq->weight)
  274. continue;
  275. if (strategy == BIRQ_CHOOSE_MAX) {
  276. /* Get IRQ with max intr */
  277. if (irq->intr > max_intr) {
  278. max_intr = irq->intr;
  279. irq_to_move = irq;
  280. }
  281. } else if (strategy == BIRQ_CHOOSE_MIN) {
  282. /* Get IRQ with min intr */
  283. if (irq->intr < min_intr) {
  284. min_intr = irq->intr;
  285. irq_to_move = irq;
  286. }
  287. } else if (strategy == BIRQ_CHOOSE_RND) {
  288. if (current == choose) {
  289. irq_to_move = irq;
  290. break;
  291. }
  292. }
  293. current++;
  294. }
  295. if (irq_to_move) {
  296. /* Don't move this IRQ while next iteration. */
  297. irq_to_move->weight = 1;
  298. lub_list_add(balance_irqs, irq_to_move);
  299. }
  300. return 0;
  301. }