irq.c 8.1 KB

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