numa.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* numa.c
  2. * Parse NUMA-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 <unistd.h>
  12. #include "lub/list.h"
  13. #include "cpumask.h"
  14. #include "numa.h"
  15. int numa_list_compare(const void *first, const void *second)
  16. {
  17. const numa_t *f = (const numa_t *)first;
  18. const numa_t *s = (const numa_t *)second;
  19. return (f->id - s->id);
  20. }
  21. static numa_t * numa_new(unsigned int id)
  22. {
  23. numa_t *new;
  24. if (!(new = malloc(sizeof(*new))))
  25. return NULL;
  26. new->id = id;
  27. cpus_init(new->cpumap);
  28. cpus_setall(new->cpumap);
  29. return new;
  30. }
  31. static void numa_free(numa_t *numa)
  32. {
  33. cpus_free(numa->cpumap);
  34. free(numa);
  35. }
  36. numa_t * numa_list_search(lub_list_t *numas, unsigned int id)
  37. {
  38. lub_list_node_t *node;
  39. numa_t search;
  40. search.id = id;
  41. node = lub_list_search(numas, &search);
  42. if (!node)
  43. return NULL;
  44. return (numa_t *)lub_list_node__get_data(node);
  45. }
  46. static numa_t * numa_list_add(lub_list_t *numas, numa_t *numa)
  47. {
  48. numa_t *old = numa_list_search(numas, numa->id);
  49. if (old) /* NUMA already exists. May be renew some fields later */
  50. return old;
  51. lub_list_add(numas, numa);
  52. return numa;
  53. }
  54. int numa_list_free(lub_list_t *numas)
  55. {
  56. lub_list_node_t *iter;
  57. while ((iter = lub_list__get_head(numas))) {
  58. numa_t *numa;
  59. numa = (numa_t *)lub_list_node__get_data(iter);
  60. numa_free(numa);
  61. lub_list_del(numas, iter);
  62. lub_list_node_free(iter);
  63. }
  64. lub_list_free(numas);
  65. return 0;
  66. }
  67. /* Show NUMA information */
  68. static void show_numa_info(numa_t *numa)
  69. {
  70. char buf[NR_CPUS + 1];
  71. cpumask_scnprintf(buf, sizeof(buf), numa->cpumap);
  72. buf[sizeof(buf) - 1] = '\0';
  73. printf("NUMA node %d cpumap %s\n", numa->id, buf);
  74. }
  75. /* Show NUMA list */
  76. int show_numas(lub_list_t *numas)
  77. {
  78. lub_list_node_t *iter;
  79. for (iter = lub_list_iterator_init(numas); iter;
  80. iter = lub_list_iterator_next(iter)) {
  81. numa_t *numa;
  82. numa = (numa_t *)lub_list_node__get_data(iter);
  83. show_numa_info(numa);
  84. }
  85. return 0;
  86. }
  87. /* Search for NUMA nodes */
  88. int scan_numas(lub_list_t *numas)
  89. {
  90. FILE *fd;
  91. char path[PATH_MAX];
  92. unsigned int id;
  93. numa_t *numa;
  94. char *str = NULL;
  95. size_t sz;
  96. cpumask_t cpumap;
  97. cpus_init(cpumap);
  98. for (id = 0; id < NR_NUMA_NODES; id++) {
  99. snprintf(path, sizeof(path),
  100. "%s/node%d", SYSFS_NUMA_PATH, id);
  101. path[sizeof(path) - 1] = '\0';
  102. if (access(path, F_OK))
  103. break;
  104. if (!(numa = numa_list_search(numas, id))) {
  105. numa = numa_new(id);
  106. numa_list_add(numas, numa);
  107. }
  108. /* Get NUMA node cpumap */
  109. snprintf(path, sizeof(path),
  110. "%s/node%d/cpumap", SYSFS_NUMA_PATH, id);
  111. path[sizeof(path) - 1] = '\0';
  112. if ((fd = fopen(path, "r"))) {
  113. if (getline(&str, &sz, fd) >= 0)
  114. cpumask_parse_user(str, strlen(str), cpumap);
  115. fclose(fd);
  116. cpus_and(numa->cpumap, numa->cpumap, cpumap);
  117. }
  118. }
  119. free(str);
  120. cpus_free(cpumap);
  121. return 0;
  122. }