irq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* irq.c
  2. * Parse IRQ-related files.
  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 "lub/list.h"
  12. #include "irq.h"
  13. #include "pxm.h"
  14. #define STR(str) ( str ? str : "" )
  15. int irq_list_compare(const void *first, const void *second)
  16. {
  17. const irq_t *f = (const irq_t *)first;
  18. const irq_t *s = (const irq_t *)second;
  19. return (f->irq - s->irq);
  20. }
  21. static irq_t * irq_new(int num)
  22. {
  23. irq_t *new;
  24. if (!(new = malloc(sizeof(*new))))
  25. return NULL;
  26. new->irq = num;
  27. new->type = NULL;
  28. new->desc = NULL;
  29. new->refresh = 1;
  30. new->old_intr = 0;
  31. new->intr = 0;
  32. new->cpu = NULL;
  33. new->weight = 0;
  34. cpus_init(new->local_cpus);
  35. cpus_init(new->affinity);
  36. cpus_setall(new->local_cpus);
  37. cpus_clear(new->affinity);
  38. new->blacklisted = 0;
  39. return new;
  40. }
  41. static void irq_free(irq_t *irq)
  42. {
  43. free(irq->type);
  44. free(irq->desc);
  45. cpus_free(irq->local_cpus);
  46. cpus_free(irq->affinity);
  47. free(irq);
  48. }
  49. irq_t * irq_list_search(lub_list_t *irqs, unsigned int num)
  50. {
  51. lub_list_node_t *node;
  52. irq_t search;
  53. search.irq = num;
  54. node = lub_list_search(irqs, &search);
  55. if (!node)
  56. return NULL;
  57. return (irq_t *)lub_list_node__get_data(node);
  58. }
  59. static irq_t * irq_list_add(lub_list_t *irqs, unsigned int num)
  60. {
  61. lub_list_node_t *node;
  62. irq_t *new;
  63. irq_t search;
  64. search.irq = num;
  65. node = lub_list_search(irqs, &search);
  66. if (node) /* IRQ already exists. May be renew some fields later */
  67. return (irq_t *)lub_list_node__get_data(node);
  68. if (!(new = irq_new(num)))
  69. return NULL;
  70. lub_list_add(irqs, new);
  71. return new;
  72. }
  73. int irq_list_free(lub_list_t *irqs)
  74. {
  75. lub_list_node_t *iter;
  76. while ((iter = lub_list__get_head(irqs))) {
  77. irq_t *irq;
  78. irq = (irq_t *)lub_list_node__get_data(iter);
  79. irq_free(irq);
  80. cpus_free(irq->local_cpus);
  81. cpus_free(irq->affinity);
  82. lub_list_del(irqs, iter);
  83. lub_list_node_free(iter);
  84. }
  85. lub_list_free(irqs);
  86. return 0;
  87. }
  88. /* Show IRQ information */
  89. static void irq_show(irq_t *irq)
  90. {
  91. char buf[NR_CPUS + 1];
  92. char buf2[NR_CPUS + 1];
  93. if (cpus_full(irq->local_cpus))
  94. snprintf(buf, sizeof(buf), "*");
  95. else
  96. cpumask_scnprintf(buf, sizeof(buf), irq->local_cpus);
  97. buf[sizeof(buf) - 1] = '\0';
  98. cpumask_scnprintf(buf2, sizeof(buf2), irq->affinity);
  99. buf2[sizeof(buf2) - 1] = '\0';
  100. printf("IRQ %3d [%s] [%s] [%s] %s %llu %llu\n", irq->irq, buf, buf2, STR(irq->type), STR(irq->desc), irq->old_intr, irq->intr);
  101. }
  102. /* Show IRQ list */
  103. int irq_list_show(lub_list_t *irqs)
  104. {
  105. lub_list_node_t *iter;
  106. for (iter = lub_list_iterator_init(irqs); iter;
  107. iter = lub_list_iterator_next(iter)) {
  108. irq_t *irq;
  109. irq = (irq_t *)lub_list_node__get_data(iter);
  110. irq_show(irq);
  111. }
  112. return 0;
  113. }
  114. static int parse_local_cpus(lub_list_t *irqs, const char *sysfs_path,
  115. unsigned int num, lub_list_t *pxms)
  116. {
  117. char path[PATH_MAX];
  118. FILE *fd;
  119. char *str = NULL;
  120. size_t sz;
  121. cpumask_t local_cpus;
  122. cpus_init(local_cpus);
  123. irq_t *irq = NULL;
  124. cpumask_t cpumask;
  125. cpus_init(cpumask);
  126. irq = irq_list_search(irqs, num);
  127. if (!irq)
  128. return -1;
  129. /* Find proximity in config file. */
  130. if (!pxm_search(pxms, sysfs_path, &cpumask)) {
  131. cpus_copy(irq->local_cpus, cpumask);
  132. return 0;
  133. }
  134. snprintf(path, sizeof(path),
  135. "%s/%s/local_cpus", SYSFS_PCI_PATH, sysfs_path);
  136. path[sizeof(path) - 1] = '\0';
  137. if (!(fd = fopen(path, "r")))
  138. return -1;
  139. if (getline(&str, &sz, fd) < 0) {
  140. fclose(fd);
  141. return -1;
  142. }
  143. fclose(fd);
  144. cpumask_parse_user(str, strlen(str), local_cpus);
  145. cpus_and(irq->local_cpus, irq->local_cpus, local_cpus);
  146. cpus_free(local_cpus);
  147. cpus_free(cpumask);
  148. free(str);
  149. return 0;
  150. }
  151. static int parse_sysfs(lub_list_t *irqs, lub_list_t *pxms)
  152. {
  153. DIR *dir;
  154. DIR *msi;
  155. struct dirent *dent;
  156. struct dirent *ment;
  157. FILE *fd;
  158. char path[PATH_MAX];
  159. int num;
  160. /* Now we can parse PCI devices only */
  161. /* Get info from /sys/bus/pci/devices */
  162. dir = opendir(SYSFS_PCI_PATH);
  163. if (!dir)
  164. return -1;
  165. while((dent = readdir(dir))) {
  166. if (!strcmp(dent->d_name, ".") ||
  167. !strcmp(dent->d_name, ".."))
  168. continue;
  169. /* Search for MSI IRQs. Since linux-3.2 */
  170. snprintf(path, sizeof(path),
  171. "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
  172. path[sizeof(path) - 1] = '\0';
  173. if ((msi = opendir(path))) {
  174. while((ment = readdir(msi))) {
  175. if (!strcmp(ment->d_name, ".") ||
  176. !strcmp(ment->d_name, ".."))
  177. continue;
  178. num = strtol(ment->d_name, NULL, 10);
  179. if (!num)
  180. continue;
  181. parse_local_cpus(irqs, dent->d_name, num, pxms);
  182. }
  183. closedir(msi);
  184. continue;
  185. }
  186. /* Try to get IRQ number from irq file */
  187. snprintf(path, sizeof(path),
  188. "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
  189. path[sizeof(path) - 1] = '\0';
  190. if (!(fd = fopen(path, "r")))
  191. continue;
  192. if (fscanf(fd, "%d", &num) < 0) {
  193. fclose(fd);
  194. continue;
  195. }
  196. fclose(fd);
  197. if (!num)
  198. continue;
  199. parse_local_cpus(irqs, dent->d_name, num, pxms);
  200. }
  201. closedir(dir);
  202. return 0;
  203. }
  204. int irq_get_affinity(irq_t *irq)
  205. {
  206. char path[PATH_MAX];
  207. FILE *fd;
  208. char *str = NULL;
  209. size_t sz;
  210. if (!irq)
  211. return -1;
  212. snprintf(path, sizeof(path),
  213. "%s/%u/smp_affinity", PROC_IRQ, irq->irq);
  214. path[sizeof(path) - 1] = '\0';
  215. if (!(fd = fopen(path, "r")))
  216. return -1;
  217. if (getline(&str, &sz, fd) < 0) {
  218. fclose(fd);
  219. return -1;
  220. }
  221. fclose(fd);
  222. cpumask_parse_user(str, strlen(str), irq->affinity);
  223. free(str);
  224. return 0;
  225. }
  226. /* Parse /proc/interrupts to get actual IRQ list */
  227. int scan_irqs(lub_list_t *irqs, lub_list_t *balance_irqs, lub_list_t *pxms)
  228. {
  229. FILE *fd;
  230. unsigned int num;
  231. char *str = NULL;
  232. size_t sz;
  233. irq_t *irq;
  234. lub_list_node_t *iter;
  235. int new_irq_num = 0;
  236. if (!(fd = fopen(PROC_INTERRUPTS, "r")))
  237. return -1;
  238. while(getline(&str, &sz, fd) >= 0) {
  239. char *endptr, *tok;
  240. int new = 0;
  241. num = strtoul(str, &endptr, 10);
  242. if (endptr == str)
  243. continue;
  244. /* Search for IRQ within list of known IRQs */
  245. if (!(irq = irq_list_search(irqs, num))) {
  246. new = 1;
  247. new_irq_num++;
  248. irq = irq_list_add(irqs, num);
  249. /* By default all CPUs are local for IRQ. Real local
  250. * CPUs will be find while sysfs scan.
  251. */
  252. cpus_setall(irq->local_cpus);
  253. }
  254. /* Set refresh flag because IRQ was found.
  255. * It's used to find out disappeared IRQs.
  256. */
  257. irq->refresh = 1;
  258. /* Doesn't refresh info for blacklisted IRQs */
  259. if (irq->blacklisted)
  260. continue;
  261. /* Find IRQ type - first non-digital and non-space */
  262. while (*endptr && !isalpha(*endptr))
  263. endptr++;
  264. tok = endptr; /* It will be IRQ type */
  265. while (*endptr && !isblank(*endptr))
  266. endptr++;
  267. free(irq->type);
  268. irq->type = strndup(tok, endptr - tok);
  269. /* Find IRQ devices list */
  270. while (*endptr && !isalpha(*endptr))
  271. endptr++;
  272. tok = endptr; /* It will be device list */
  273. while (*endptr && !iscntrl(*endptr))
  274. endptr++;
  275. free(irq->desc);
  276. irq->desc = strndup(tok, endptr - tok);
  277. /* Always get current smp affinity. It's necessary due to
  278. * problems with arch/driver. The affinity can be old (didn't
  279. * switched to new state).
  280. */
  281. irq_get_affinity(irq);
  282. /* Print info about new IRQ. */
  283. if (new)
  284. printf("Add IRQ %3d %s\n", irq->irq, STR(irq->desc));
  285. /* If affinity uses more than one CPU then consider IRQ as new one.
  286. * It's not normal state for really non-new IRQs.
  287. */
  288. if (cpus_weight(irq->affinity) <= 1)
  289. continue;
  290. /* Don't balance IRQs with 0 number of interrupts */
  291. if (irq->intr == 0)
  292. continue;
  293. /* Add IRQs to list of IRQs to balance. */
  294. lub_list_add(balance_irqs, irq);
  295. }
  296. free(str);
  297. fclose(fd);
  298. /* Remove disappeared IRQs */
  299. iter = lub_list_iterator_init(irqs);
  300. while(iter) {
  301. irq_t *irq;
  302. lub_list_node_t *old_iter;
  303. irq = (irq_t *)lub_list_node__get_data(iter);
  304. old_iter = iter;
  305. iter = lub_list_iterator_next(iter);
  306. if (!irq->refresh) {
  307. lub_list_del(irqs, old_iter);
  308. printf("Remove IRQ %3d %s\n", irq->irq, STR(irq->desc));
  309. irq_free(irq);
  310. } else {
  311. /* Drop refresh flag for next iteration */
  312. irq->refresh = 0;
  313. }
  314. }
  315. /* No new IRQs were found. It doesn't need to scan sysfs. */
  316. if (new_irq_num == 0)
  317. return 0;
  318. /* Add IRQ info from sysfs */
  319. printf("New IRQs: %d. Scanning sysfs...\n", new_irq_num);
  320. parse_sysfs(irqs, pxms);
  321. return 0;
  322. }