irq_parse.c 6.2 KB

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