irq.c 7.9 KB

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