irq_parse.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* irq_parse.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. #define STR(str) ( str ? str : "" )
  14. int irq_list_compare(const void *first, const void *second)
  15. {
  16. const irq_t *f = (const irq_t *)first;
  17. const irq_t *s = (const irq_t *)second;
  18. return (f->irq - s->irq);
  19. }
  20. static irq_t * irq_new(int num)
  21. {
  22. irq_t *new;
  23. if (!(new = malloc(sizeof(*new))))
  24. return NULL;
  25. new->irq = num;
  26. new->type = NULL;
  27. new->desc = NULL;
  28. new->refresh = 1;
  29. new->old_intr = 0;
  30. new->intr = 0;
  31. new->cpu = NULL;
  32. new->dont_move = 0;
  33. cpus_setall(new->local_cpus);
  34. cpus_clear(new->affinity);
  35. new->blacklisted = 0;
  36. return new;
  37. }
  38. static void irq_free(irq_t *irq)
  39. {
  40. free(irq->type);
  41. free(irq->desc);
  42. free(irq);
  43. }
  44. irq_t * irq_list_search(lub_list_t *irqs, unsigned int num)
  45. {
  46. lub_list_node_t *node;
  47. irq_t search;
  48. search.irq = num;
  49. node = lub_list_search(irqs, &search);
  50. if (!node)
  51. return NULL;
  52. return (irq_t *)lub_list_node__get_data(node);
  53. }
  54. static irq_t * irq_list_add(lub_list_t *irqs, unsigned int num)
  55. {
  56. lub_list_node_t *node;
  57. irq_t *new;
  58. irq_t search;
  59. search.irq = num;
  60. node = lub_list_search(irqs, &search);
  61. if (node) /* IRQ already exists. May be renew some fields later */
  62. return (irq_t *)lub_list_node__get_data(node);
  63. if (!(new = irq_new(num)))
  64. return NULL;
  65. lub_list_add(irqs, new);
  66. return new;
  67. }
  68. int irq_list_free(lub_list_t *irqs)
  69. {
  70. lub_list_node_t *iter;
  71. while ((iter = lub_list__get_head(irqs))) {
  72. irq_t *irq;
  73. irq = (irq_t *)lub_list_node__get_data(iter);
  74. irq_free(irq);
  75. lub_list_del(irqs, iter);
  76. lub_list_node_free(iter);
  77. }
  78. lub_list_free(irqs);
  79. return 0;
  80. }
  81. /* Show IRQ information */
  82. static void irq_show(irq_t *irq)
  83. {
  84. char buf[NR_CPUS + 1];
  85. if (cpus_full(irq->local_cpus))
  86. snprintf(buf, sizeof(buf), "*");
  87. else
  88. cpumask_scnprintf(buf, sizeof(buf), irq->local_cpus);
  89. printf("IRQ %3d %s [%s] %s\n", irq->irq, buf, STR(irq->type), STR(irq->desc));
  90. }
  91. /* Show IRQ list */
  92. int irq_list_show(lub_list_t *irqs)
  93. {
  94. lub_list_node_t *iter;
  95. for (iter = lub_list_iterator_init(irqs); iter;
  96. iter = lub_list_iterator_next(iter)) {
  97. irq_t *irq;
  98. irq = (irq_t *)lub_list_node__get_data(iter);
  99. irq_show(irq);
  100. }
  101. return 0;
  102. }
  103. static int parse_local_cpus(lub_list_t *irqs, const char *sysfs_path,
  104. unsigned int num)
  105. {
  106. char path[PATH_MAX];
  107. FILE *fd;
  108. char *str = NULL;
  109. size_t sz;
  110. irq_t *irq;
  111. cpumask_t local_cpus;
  112. irq = irq_list_search(irqs, num);
  113. if (!irq)
  114. return -1;
  115. sprintf(path, "%s/%s/local_cpus", SYSFS_PCI_PATH, sysfs_path);
  116. if (!(fd = fopen(path, "r")))
  117. return -1;
  118. if (getline(&str, &sz, fd) < 0) {
  119. fclose(fd);
  120. return -1;
  121. }
  122. fclose(fd);
  123. cpumask_parse_user(str, strlen(str), local_cpus);
  124. cpus_and(irq->local_cpus, irq->local_cpus, local_cpus);
  125. free(str);
  126. return 0;
  127. }
  128. static int parse_sysfs(lub_list_t *irqs)
  129. {
  130. DIR *dir;
  131. DIR *msi;
  132. struct dirent *dent;
  133. struct dirent *ment;
  134. FILE *fd;
  135. char path[PATH_MAX];
  136. int num;
  137. /* Now we can parse PCI devices only */
  138. /* Get info from /sys/bus/pci/devices */
  139. dir = opendir(SYSFS_PCI_PATH);
  140. if (!dir)
  141. return -1;
  142. while((dent = readdir(dir))) {
  143. if (!strcmp(dent->d_name, ".") ||
  144. !strcmp(dent->d_name, ".."))
  145. continue;
  146. /* Search for MSI IRQs. Since linux-3.2 */
  147. sprintf(path, "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
  148. if ((msi = opendir(path))) {
  149. while((ment = readdir(msi))) {
  150. if (!strcmp(ment->d_name, ".") ||
  151. !strcmp(ment->d_name, ".."))
  152. continue;
  153. num = strtol(ment->d_name, NULL, 10);
  154. if (!num)
  155. continue;
  156. parse_local_cpus(irqs, dent->d_name, num);
  157. }
  158. closedir(msi);
  159. continue;
  160. }
  161. /* Try to get IRQ number from irq file */
  162. sprintf(path, "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
  163. if (!(fd = fopen(path, "r")))
  164. continue;
  165. if (fscanf(fd, "%d", &num) < 0) {
  166. fclose(fd);
  167. continue;
  168. }
  169. fclose(fd);
  170. if (!num)
  171. continue;
  172. parse_local_cpus(irqs, dent->d_name, num);
  173. }
  174. closedir(dir);
  175. return 0;
  176. }
  177. int irq_get_affinity(irq_t *irq)
  178. {
  179. char path[PATH_MAX];
  180. FILE *fd;
  181. char *str = NULL;
  182. size_t sz;
  183. if (!irq)
  184. return -1;
  185. sprintf(path, "%s/%u/smp_affinity", PROC_IRQ, irq->irq);
  186. if (!(fd = fopen(path, "r")))
  187. return -1;
  188. if (getline(&str, &sz, fd) < 0) {
  189. fclose(fd);
  190. return -1;
  191. }
  192. fclose(fd);
  193. cpumask_parse_user(str, strlen(str), irq->affinity);
  194. free(str);
  195. return 0;
  196. }
  197. /* Parse /proc/interrupts to get actual IRQ list */
  198. int scan_irqs(lub_list_t *irqs, lub_list_t *balance_irqs)
  199. {
  200. FILE *fd;
  201. unsigned int num;
  202. char *str = NULL;
  203. size_t sz;
  204. irq_t *irq;
  205. lub_list_node_t *iter;
  206. if (!(fd = fopen(PROC_INTERRUPTS, "r")))
  207. return -1;
  208. while(getline(&str, &sz, fd) >= 0) {
  209. char *endptr, *tok;
  210. int new = 0;
  211. num = strtoul(str, &endptr, 10);
  212. if (endptr == str)
  213. continue;
  214. if (!(irq = irq_list_search(irqs, num))) {
  215. new = 1;
  216. irq = irq_list_add(irqs, num);
  217. }
  218. /* Set refresh flag because IRQ was found */
  219. irq->refresh = 1;
  220. /* Doesn't refresh info for blacklisted IRQs */
  221. if (irq->blacklisted)
  222. continue;
  223. /* Find IRQ type - first non-digital and non-space */
  224. while (*endptr && !isalpha(*endptr))
  225. endptr++;
  226. tok = endptr; /* It will be IRQ type */
  227. while (*endptr && !isblank(*endptr))
  228. endptr++;
  229. free(irq->type);
  230. irq->type = strndup(tok, endptr - tok);
  231. /* Find IRQ devices list */
  232. while (*endptr && !isalpha(*endptr))
  233. endptr++;
  234. tok = endptr; /* It will be device list */
  235. while (*endptr && !iscntrl(*endptr))
  236. endptr++;
  237. free(irq->desc);
  238. irq->desc = strndup(tok, endptr - tok);
  239. if (new) {
  240. /* By default all CPUs are local for IRQ. Real local
  241. CPUs will be find while sysfs scan. */
  242. cpus_setall(irq->local_cpus);
  243. irq_get_affinity(irq);
  244. lub_list_add(balance_irqs, irq);
  245. printf("Add IRQ %3d %s\n", irq->irq, STR(irq->desc));
  246. }
  247. }
  248. free(str);
  249. fclose(fd);
  250. /* Remove disapeared IRQs */
  251. iter = lub_list_iterator_init(irqs);
  252. while(iter) {
  253. irq_t *irq;
  254. lub_list_node_t *old_iter;
  255. irq = (irq_t *)lub_list_node__get_data(iter);
  256. old_iter = iter;
  257. iter = lub_list_iterator_next(iter);
  258. if (!irq->refresh) {
  259. lub_list_del(irqs, old_iter);
  260. irq_free(irq);
  261. printf("Remove IRQ %3d %s\n", irq->irq, STR(irq->desc));
  262. } else {
  263. /* Drop refresh flag for next iteration */
  264. irq->refresh = 0;
  265. }
  266. }
  267. /* No new IRQs were found. It doesn't need to scan sysfs. */
  268. if (lub_list_len(balance_irqs) == 0)
  269. return 0;
  270. /* Add IRQ info from sysfs */
  271. parse_sysfs(irqs);
  272. return 0;
  273. }