irq.c 8.0 KB

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