irq.c 8.0 KB

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