cpu_parse.c 3.3 KB

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