cpu_parse.c 3.4 KB

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