irq_parse.c 6.6 KB

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