irq_parse.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. int irq_set_affinity(irq_t *irq, cpumask_t cpumask)
  175. {
  176. char path[PATH_MAX];
  177. char buf[NR_CPUS + 1];
  178. FILE *fd;
  179. if (!irq)
  180. return -1;
  181. sprintf(path, "%s/%u/smp_affinity", PROC_IRQ, irq->irq);
  182. if (!(fd = fopen(path, "w")))
  183. return -1;
  184. cpumask_scnprintf(buf, sizeof(buf), cpumask);
  185. fprintf(fd, "%s", buf);
  186. fclose(fd);
  187. return 0;
  188. }
  189. static int irq_get_affinity(irq_t *irq)
  190. {
  191. char path[PATH_MAX];
  192. FILE *fd;
  193. char *str = NULL;
  194. size_t sz;
  195. if (!irq)
  196. return -1;
  197. sprintf(path, "%s/%u/smp_affinity", PROC_IRQ, irq->irq);
  198. if (!(fd = fopen(path, "r")))
  199. return -1;
  200. if (getline(&str, &sz, fd) < 0) {
  201. fclose(fd);
  202. return -1;
  203. }
  204. fclose(fd);
  205. cpumask_parse_user(str, strlen(str), irq->affinity);
  206. free(str);
  207. return 0;
  208. }
  209. /* Parse /proc/interrupts to get actual IRQ list */
  210. int scan_irqs(lub_list_t *irqs, lub_list_t *balance_irqs)
  211. {
  212. FILE *fd;
  213. unsigned int num;
  214. char *str = NULL;
  215. size_t sz;
  216. irq_t *irq;
  217. lub_list_node_t *iter;
  218. if (!(fd = fopen(PROC_INTERRUPTS, "r")))
  219. return -1;
  220. while(getline(&str, &sz, fd) >= 0) {
  221. char *endptr, *tok;
  222. int new = 0;
  223. num = strtoul(str, &endptr, 10);
  224. if (endptr == str)
  225. continue;
  226. if (!(irq = irq_list_search(irqs, num))) {
  227. new = 1;
  228. irq = irq_list_add(irqs, num);
  229. }
  230. /* Set refresh flag because IRQ was found */
  231. irq->refresh = 1;
  232. /* Find IRQ type - first non-digital and non-space */
  233. while (*endptr && !isalpha(*endptr))
  234. endptr++;
  235. tok = endptr; /* It will be IRQ type */
  236. while (*endptr && !isblank(*endptr))
  237. endptr++;
  238. free(irq->type);
  239. irq->type = strndup(tok, endptr - tok);
  240. /* Find IRQ devices list */
  241. while (*endptr && !isalpha(*endptr))
  242. endptr++;
  243. tok = endptr; /* It will be device list */
  244. while (*endptr && !iscntrl(*endptr))
  245. endptr++;
  246. free(irq->desc);
  247. irq->desc = strndup(tok, endptr - tok);
  248. irq_get_affinity(irq);
  249. if (new) {
  250. /* By default all CPUs are local for IRQ. Real local
  251. CPUs will be find while sysfs scan. */
  252. cpus_setall(irq->local_cpus);
  253. lub_list_add(balance_irqs, irq);
  254. printf("Add IRQ %3d %s\n", irq->irq, STR(irq->desc));
  255. }
  256. }
  257. free(str);
  258. fclose(fd);
  259. /* Remove disapeared IRQs */
  260. iter = lub_list_iterator_init(irqs);
  261. while(iter) {
  262. irq_t *irq;
  263. lub_list_node_t *old_iter;
  264. irq = (irq_t *)lub_list_node__get_data(iter);
  265. old_iter = iter;
  266. iter = lub_list_iterator_next(iter);
  267. if (!irq->refresh) {
  268. lub_list_del(irqs, old_iter);
  269. irq_free(irq);
  270. printf("Remove IRQ %3d %s\n", irq->irq, STR(irq->desc));
  271. } else {
  272. /* Drop refresh flag for next iteration */
  273. irq->refresh = 0;
  274. }
  275. }
  276. /* No new IRQs were found. It doesn't need to scan sysfs. */
  277. if (lub_list_len(balance_irqs) == 0)
  278. return 0;
  279. /* Add IRQ info from sysfs */
  280. parse_sysfs(irqs);
  281. return 0;
  282. }