balance.c 8.5 KB

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