cpu_parse.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* cpu_parse.c
  2. * Parse CPU-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 "cpu.h"
  15. #define STR(str) ( str ? str : "" )
  16. int cpu_list_compare(const void *first, const void *second)
  17. {
  18. const cpu_t *f = (const cpu_t *)first;
  19. const cpu_t *s = (const cpu_t *)second;
  20. return (f->id - s->id);
  21. }
  22. static cpu_t * cpu_new(unsigned int id,
  23. unsigned int package_id,
  24. unsigned int core_id)
  25. {
  26. cpu_t *new;
  27. if (!(new = malloc(sizeof(*new))))
  28. return NULL;
  29. new->id = id;
  30. new->package_id = package_id;
  31. new->core_id = core_id;
  32. return new;
  33. }
  34. static void cpu_free(cpu_t *cpu)
  35. {
  36. free(cpu);
  37. }
  38. static cpu_t * cpu_list_search_ht(lub_list_t *cpus,
  39. unsigned int package_id,
  40. unsigned int core_id)
  41. {
  42. lub_list_node_t *iter;
  43. for (iter = lub_list_iterator_init(cpus); iter;
  44. iter = lub_list_iterator_next(iter)) {
  45. cpu_t *cpu;
  46. cpu = (cpu_t *)lub_list_node__get_data(iter);
  47. if (cpu->package_id != package_id)
  48. continue;
  49. if (cpu->core_id != core_id)
  50. continue;
  51. return cpu;
  52. }
  53. return NULL;
  54. }
  55. #if 0
  56. static cpu_t * cpu_list_search(lub_list_t *cpus, unsigned int id)
  57. {
  58. lub_list_node_t *node;
  59. cpu_t search;
  60. search.id = id;
  61. node = lub_list_search(cpus, &search);
  62. if (!node)
  63. return NULL;
  64. return (cpu_t *)lub_list_node__get_data(node);
  65. }
  66. #endif
  67. static cpu_t * cpu_list_add(lub_list_t *cpus,
  68. unsigned int id,
  69. unsigned int package_id,
  70. unsigned int core_id)
  71. {
  72. lub_list_node_t *node;
  73. cpu_t *new;
  74. cpu_t search;
  75. search.id = id;
  76. node = lub_list_search(cpus, &search);
  77. if (node) /* CPU already exists. May be renew some fields later */
  78. return (cpu_t *)lub_list_node__get_data(node);
  79. if (!(new = cpu_new(id, package_id, core_id)))
  80. return NULL;
  81. lub_list_add(cpus, new);
  82. return new;
  83. }
  84. int cpu_list_free(lub_list_t *cpus)
  85. {
  86. lub_list_node_t *iter;
  87. while ((iter = lub_list__get_head(cpus))) {
  88. cpu_t *cpu;
  89. cpu = (cpu_t *)lub_list_node__get_data(iter);
  90. cpu_free(cpu);
  91. lub_list_del(cpus, iter);
  92. lub_list_node_free(iter);
  93. }
  94. lub_list_free(cpus);
  95. return 0;
  96. }
  97. /* Show CPU information */
  98. static void show_cpu_info(cpu_t *cpu)
  99. {
  100. printf("CPU %d package %d core %d\n", cpu->id, cpu->package_id, cpu->core_id);
  101. }
  102. /* Show CPU list */
  103. int show_cpus(lub_list_t *cpus)
  104. {
  105. lub_list_node_t *iter;
  106. for (iter = lub_list_iterator_init(cpus); iter;
  107. iter = lub_list_iterator_next(iter)) {
  108. cpu_t *cpu;
  109. cpu = (cpu_t *)lub_list_node__get_data(iter);
  110. show_cpu_info(cpu);
  111. }
  112. return 0;
  113. }
  114. int scan_cpus(lub_list_t *cpus)
  115. {
  116. FILE *fd;
  117. char path[PATH_MAX];
  118. unsigned int id;
  119. unsigned int package_id;
  120. unsigned int core_id;
  121. for (id = 0; id < NR_CPUS; id++) {
  122. sprintf(path, "%s/cpu%d", SYSFS_CPU_PATH, id);
  123. if (access(path, F_OK))
  124. break;
  125. /* Try to get package_id */
  126. sprintf(path, "%s/cpu%d/topology/physical_package_id",
  127. SYSFS_CPU_PATH, id);
  128. if (!(fd = fopen(path, "r")))
  129. continue;
  130. if (fscanf(fd, "%u", &package_id) < 0) {
  131. fclose(fd);
  132. continue;
  133. }
  134. fclose(fd);
  135. /* Try to get core_id */
  136. sprintf(path, "%s/cpu%d/topology/core_id",
  137. SYSFS_CPU_PATH, id);
  138. if (!(fd = fopen(path, "r")))
  139. continue;
  140. if (fscanf(fd, "%u", &core_id) < 0) {
  141. fclose(fd);
  142. continue;
  143. }
  144. fclose(fd);
  145. /* Don't use second thread of Hyper Threading */
  146. if (cpu_list_search_ht(cpus, package_id, core_id))
  147. continue;
  148. cpu_list_add(cpus, id, package_id, core_id);
  149. }
  150. return 0;
  151. }