balance.c 8.4 KB

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