irq.c 7.3 KB

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