balance.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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,
  135. float load_limit, cpumask_t *exclude_cpus)
  136. {
  137. lub_list_node_t *iter;
  138. for (iter = lub_list_iterator_init(balance_irqs); iter;
  139. iter = lub_list_iterator_next(iter)) {
  140. irq_t *irq;
  141. cpu_t *cpu;
  142. cpumask_t possible_cpus;
  143. irq = (irq_t *)lub_list_node__get_data(iter);
  144. /* Try to find local CPU to move IRQ to.
  145. The local CPU is CPU with native NUMA node. */
  146. /* Possible CPUs is local CPUs minus exclude-CPUs.
  147. possible_cpus = local_cpus & ~exclude_cpus */
  148. cpus_init(possible_cpus);
  149. cpus_copy(possible_cpus, *exclude_cpus);
  150. cpus_complement(possible_cpus, possible_cpus);
  151. cpus_and(possible_cpus, possible_cpus, irq->local_cpus);
  152. cpu = choose_cpu(cpus, &possible_cpus, load_limit);
  153. cpus_free(possible_cpus);
  154. /* If local CPU is not found then try to use
  155. CPU from another NUMA node. It's better then
  156. overloaded CPUs. */
  157. /* Non-local CPUs were disabled. It seems there is
  158. no advantages to use them. The all interactions will
  159. be held by QPI-like interfaces through local CPUs. */
  160. /* if (!cpu) {
  161. cpumask_t complement;
  162. cpus_init(complement);
  163. cpus_complement(complement, irq->local_cpus);
  164. cpu = choose_cpu(cpus, &complement, load_limit);
  165. cpus_free(complement);
  166. }
  167. */
  168. if (cpu) {
  169. if (irq->cpu)
  170. printf("Move IRQ %u from CPU%u to CPU%u\n",
  171. irq->irq, irq->cpu->id, cpu->id);
  172. else
  173. printf("Move IRQ %u to CPU%u\n", irq->irq, cpu->id);
  174. move_irq_to_cpu(irq, cpu);
  175. }
  176. }
  177. return 0;
  178. }
  179. int apply_affinity(lub_list_t *balance_irqs)
  180. {
  181. lub_list_node_t *iter;
  182. for (iter = lub_list_iterator_init(balance_irqs); iter;
  183. iter = lub_list_iterator_next(iter)) {
  184. irq_t *irq;
  185. irq = (irq_t *)lub_list_node__get_data(iter);
  186. if (!irq->cpu)
  187. continue;
  188. irq_set_affinity(irq, &(irq->cpu->cpumask));
  189. }
  190. return 0;
  191. }
  192. /* Count the number of intr-not-null IRQs and minimal IRQ weight */
  193. static int irq_list_info(lub_list_t *irqs, int *min_weight,
  194. unsigned int *irq_num, unsigned int *candidates_num)
  195. {
  196. lub_list_node_t *iter;
  197. if (!irqs)
  198. return -1;
  199. if (min_weight)
  200. *min_weight = -1;
  201. if (irq_num)
  202. *irq_num = 0;
  203. if (candidates_num)
  204. *candidates_num = 0;
  205. for (iter = lub_list_iterator_init(irqs); iter;
  206. iter = lub_list_iterator_next(iter)) {
  207. irq_t *irq = (irq_t *)lub_list_node__get_data(iter);
  208. if (irq->intr == 0)
  209. continue;
  210. if (min_weight) {
  211. if ((*min_weight < 0) || (irq->weight < *min_weight))
  212. *min_weight = irq->weight;
  213. }
  214. if (irq_num)
  215. *irq_num += 1;
  216. if (irq->weight)
  217. continue;
  218. if (candidates_num)
  219. *candidates_num += 1;
  220. }
  221. return 0;
  222. }
  223. /* Search for most overloaded CPU */
  224. static cpu_t * most_overloaded_cpu(lub_list_t *cpus, float threshold)
  225. {
  226. lub_list_node_t *iter;
  227. cpu_t *overloaded_cpu = NULL;
  228. float max_load = 0.0;
  229. /* Search for the most overloaded CPU.
  230. The load must be greater than threshold. */
  231. for (iter = lub_list_iterator_init(cpus); iter;
  232. iter = lub_list_iterator_next(iter)) {
  233. cpu_t *cpu = (cpu_t *)lub_list_node__get_data(iter);
  234. int min_weight = -1;
  235. unsigned int irq_num = 0;
  236. if (cpu->load < threshold)
  237. continue;
  238. if (cpu->load <= max_load)
  239. continue;
  240. /* Don't move last IRQ */
  241. if (lub_list_len(cpu->irqs) <= 1)
  242. continue;
  243. irq_list_info(cpu->irqs, &min_weight, &irq_num, NULL);
  244. /* All IRQs has intr=0 */
  245. if (irq_num == 0)
  246. continue;
  247. if (min_weight > 0)
  248. dec_weight(cpu, min_weight);
  249. /* Ok, it's good CPU to try to free it */
  250. max_load = cpu->load;
  251. overloaded_cpu = cpu;
  252. }
  253. return overloaded_cpu;
  254. }
  255. /* Search for the overloaded CPUs and then choose best IRQ for moving to
  256. another CPU. The best IRQ is IRQ with maximum number of interrupts.
  257. The IRQs with small number of interrupts have very low load or very
  258. high load (in a case of NAPI). */
  259. int choose_irqs_to_move(lub_list_t *cpus, lub_list_t *balance_irqs,
  260. float threshold, birq_choose_strategy_e strategy,
  261. cpumask_t *exclude_cpus)
  262. {
  263. lub_list_node_t *iter;
  264. cpu_t *overloaded_cpu = NULL;
  265. irq_t *irq_to_move = NULL;
  266. unsigned long long max_intr = 0;
  267. unsigned long long min_intr = (unsigned long long)(-1);
  268. unsigned int choose = 0;
  269. unsigned int current = 0;
  270. /* Stage 1: Try to move active IRQs from excluded-CPUs */
  271. if (!cpus_empty(*exclude_cpus)) {
  272. /* Iterate CPU list and find excluded ones */
  273. for (iter = lub_list_iterator_init(cpus); iter;
  274. iter = lub_list_iterator_next(iter)) {
  275. lub_list_node_t *iter2;
  276. cpu_t *cpu = (cpu_t *)lub_list_node__get_data(iter);
  277. if (!cpu_isset(cpu->id, *exclude_cpus))
  278. continue;
  279. /* Move all active IRQs to another CPUs */
  280. for (iter2 = lub_list_iterator_init(cpu->irqs); iter2;
  281. iter2 = lub_list_iterator_next(iter2)) {
  282. irq_t *irq = (irq_t *)lub_list_node__get_data(iter2);
  283. if (irq->intr == 0)
  284. continue;
  285. lub_list_add(balance_irqs, irq);
  286. }
  287. }
  288. }
  289. /* Stage 2: Move IRQs from overloaded CPUs */
  290. /* Search for overloaded CPUs */
  291. if (!(overloaded_cpu = most_overloaded_cpu(cpus, threshold)))
  292. return 0;
  293. if (strategy == BIRQ_CHOOSE_RND) {
  294. unsigned int candidates = 0;
  295. irq_list_info(overloaded_cpu->irqs, NULL, NULL, &candidates);
  296. if (candidates == 0)
  297. return 0;
  298. choose = rand() % candidates;
  299. }
  300. /* Search for the IRQ (owned by overloaded CPU) with
  301. maximum/minimum number of interrupts. */
  302. for (iter = lub_list_iterator_init(overloaded_cpu->irqs); iter;
  303. iter = lub_list_iterator_next(iter)) {
  304. irq_t *irq = (irq_t *)lub_list_node__get_data(iter);
  305. /* Don't move any IRQs with intr=0. It can be unused IRQ. In
  306. this case the moving is not needed. It can be overloaded
  307. (by NAPI) IRQs. In this case it will be not moved anyway. */
  308. if (irq->intr == 0)
  309. continue;
  310. if (irq->weight)
  311. continue;
  312. if (strategy == BIRQ_CHOOSE_MAX) {
  313. /* Get IRQ with max intr */
  314. if (irq->intr > max_intr) {
  315. max_intr = irq->intr;
  316. irq_to_move = irq;
  317. }
  318. } else if (strategy == BIRQ_CHOOSE_MIN) {
  319. /* Get IRQ with min intr */
  320. if (irq->intr < min_intr) {
  321. min_intr = irq->intr;
  322. irq_to_move = irq;
  323. }
  324. } else if (strategy == BIRQ_CHOOSE_RND) {
  325. if (current == choose) {
  326. irq_to_move = irq;
  327. break;
  328. }
  329. }
  330. current++;
  331. }
  332. if (irq_to_move) {
  333. /* Don't move this IRQ while next iteration. */
  334. irq_to_move->weight = 1;
  335. lub_list_add(balance_irqs, irq_to_move);
  336. }
  337. return 0;
  338. }