numa.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. printf("NUMA node %d cpumap %s\n", numa->id, buf);
  71. }
  72. /* Show NUMA list */
  73. int show_numas(lub_list_t *numas)
  74. {
  75. lub_list_node_t *iter;
  76. for (iter = lub_list_iterator_init(numas); iter;
  77. iter = lub_list_iterator_next(iter)) {
  78. numa_t *numa;
  79. numa = (numa_t *)lub_list_node__get_data(iter);
  80. show_numa_info(numa);
  81. }
  82. return 0;
  83. }
  84. /* Search for NUMA nodes */
  85. int scan_numas(lub_list_t *numas)
  86. {
  87. FILE *fd;
  88. char path[PATH_MAX];
  89. unsigned int id;
  90. numa_t *numa;
  91. char *str = NULL;
  92. size_t sz;
  93. cpumask_t cpumap;
  94. for (id = 0; id < NR_NUMA_NODES; id++) {
  95. sprintf(path, "%s/node%d", SYSFS_NUMA_PATH, id);
  96. if (access(path, F_OK))
  97. break;
  98. if (!(numa = numa_list_search(numas, id))) {
  99. numa = numa_new(id);
  100. numa_list_add(numas, numa);
  101. }
  102. /* Get NUMA node cpumap */
  103. sprintf(path, "%s/node%d/cpumap",
  104. SYSFS_NUMA_PATH, id);
  105. if ((fd = fopen(path, "r"))) {
  106. if (getline(&str, &sz, fd) >= 0)
  107. cpumask_parse_user(str, strlen(str), cpumap);
  108. fclose(fd);
  109. cpus_and(numa->cpumap, numa->cpumap, cpumap);
  110. }
  111. }
  112. free(str);
  113. return 0;
  114. }