irq_parse.c 6.0 KB

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