irq.c 7.7 KB

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