numa.c 2.7 KB

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