cpu.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* cpu.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. #include "irq.h"
  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. int cpu_list_compare_len(const void *first, const void *second)
  23. {
  24. const cpu_t *f = (const cpu_t *)first;
  25. const cpu_t *s = (const cpu_t *)second;
  26. return (lub_list_len(f->irqs) - lub_list_len(s->irqs));
  27. }
  28. static cpu_t * cpu_new(unsigned int id)
  29. {
  30. cpu_t *new;
  31. if (!(new = malloc(sizeof(*new))))
  32. return NULL;
  33. new->id = id;
  34. new->old_load_all = 0;
  35. new->old_load_irq = 0;
  36. new->old_load = 0;
  37. new->load = 0;
  38. new->irqs = lub_list_new(irq_list_compare);
  39. cpus_init(new->cpumask);
  40. cpus_clear(new->cpumask);
  41. cpu_set(new->id, new->cpumask);
  42. new->use_cnt = 0;
  43. return new;
  44. }
  45. static void cpu_free(cpu_t *cpu)
  46. {
  47. lub_list_node_t *node;
  48. while ((node = lub_list__get_tail(cpu->irqs))) {
  49. lub_list_del(cpu->irqs, node);
  50. lub_list_node_free(node);
  51. }
  52. lub_list_free(cpu->irqs);
  53. cpus_free(cpu->cpumask);
  54. free(cpu);
  55. }
  56. /* Search for CPU with specified package and core IDs.
  57. The second CPU with the same IDs is a thread of Hyper Threading.
  58. We don't want to use HT for IRQ balancing. */
  59. static cpu_t * cpu_list_search_ht(lub_list_t *cpus,
  60. unsigned int package_id, unsigned int core_id,
  61. cpumask_t *thread_siblings)
  62. {
  63. lub_list_node_t *iter;
  64. /* Check if current CPU has thread siblings */
  65. /* The CPUs without thread siblings has no hyper
  66. threading. For example some AMD processors has
  67. two CPUs with the same package and core ids but
  68. has no thread siblings. Don't consider such CPUs as
  69. a hyper threading. */
  70. if (cpus_weight(*thread_siblings) < 2)
  71. return NULL;
  72. for (iter = lub_list_iterator_init(cpus); iter;
  73. iter = lub_list_iterator_next(iter)) {
  74. cpu_t *cpu;
  75. cpu = (cpu_t *)lub_list_node__get_data(iter);
  76. if (cpu->package_id != package_id)
  77. continue;
  78. if (cpu->core_id != core_id)
  79. continue;
  80. return cpu;
  81. }
  82. return NULL;
  83. }
  84. cpu_t * cpu_list_search(lub_list_t *cpus, unsigned int id)
  85. {
  86. lub_list_node_t *node;
  87. cpu_t search;
  88. search.id = id;
  89. node = lub_list_search(cpus, &search);
  90. if (!node)
  91. return NULL;
  92. return (cpu_t *)lub_list_node__get_data(node);
  93. }
  94. static cpu_t * cpu_list_add(lub_list_t *cpus, cpu_t *cpu)
  95. {
  96. cpu_t *old = cpu_list_search(cpus, cpu->id);
  97. if (old) /* CPU already exists. May be renew some fields later */
  98. return old;
  99. lub_list_add(cpus, cpu);
  100. return cpu;
  101. }
  102. int cpu_list_free(lub_list_t *cpus)
  103. {
  104. lub_list_node_t *iter;
  105. while ((iter = lub_list__get_head(cpus))) {
  106. cpu_t *cpu;
  107. cpu = (cpu_t *)lub_list_node__get_data(iter);
  108. cpu_free(cpu);
  109. lub_list_del(cpus, iter);
  110. lub_list_node_free(iter);
  111. }
  112. lub_list_free(cpus);
  113. return 0;
  114. }
  115. /* Show CPU information */
  116. static void show_cpu_info(cpu_t *cpu)
  117. {
  118. char buf[NR_CPUS + 1];
  119. cpumask_scnprintf(buf, sizeof(buf), cpu->cpumask);
  120. buf[sizeof(buf) - 1] = '\0';
  121. printf("CPU %d package %d core %d mask %s\n", cpu->id, cpu->package_id, cpu->core_id, buf);
  122. }
  123. /* Show CPU list */
  124. int show_cpus(lub_list_t *cpus)
  125. {
  126. lub_list_node_t *iter;
  127. for (iter = lub_list_iterator_init(cpus); iter;
  128. iter = lub_list_iterator_next(iter)) {
  129. cpu_t *cpu;
  130. cpu = (cpu_t *)lub_list_node__get_data(iter);
  131. show_cpu_info(cpu);
  132. }
  133. return 0;
  134. }
  135. /* Search for CPUs */
  136. int scan_cpus(lub_list_t *cpus, int ht)
  137. {
  138. FILE *fd;
  139. char path[PATH_MAX];
  140. unsigned int id;
  141. unsigned int package_id;
  142. unsigned int core_id;
  143. cpu_t *new;
  144. char *str = NULL;
  145. size_t sz;
  146. cpumask_t thread_siblings;
  147. cpus_init(thread_siblings);
  148. for (id = 0; id < NR_CPUS; id++) {
  149. snprintf(path, sizeof(path), "%s/cpu%d", SYSFS_CPU_PATH, id);
  150. path[sizeof(path) - 1] = '\0';
  151. if (access(path, F_OK))
  152. break;
  153. /* Try to get package_id */
  154. snprintf(path, sizeof(path),
  155. "%s/cpu%d/topology/physical_package_id",
  156. SYSFS_CPU_PATH, id);
  157. path[sizeof(path) - 1] = '\0';
  158. if (!(fd = fopen(path, "r")))
  159. continue;
  160. if (fscanf(fd, "%u", &package_id) < 0) {
  161. fclose(fd);
  162. continue;
  163. }
  164. fclose(fd);
  165. /* Try to get core_id */
  166. snprintf(path, sizeof(path), "%s/cpu%d/topology/core_id",
  167. SYSFS_CPU_PATH, id);
  168. path[sizeof(path) - 1] = '\0';
  169. if (!(fd = fopen(path, "r")))
  170. continue;
  171. if (fscanf(fd, "%u", &core_id) < 0) {
  172. fclose(fd);
  173. continue;
  174. }
  175. fclose(fd);
  176. /* Get thread siblings */
  177. cpus_clear(thread_siblings);
  178. cpu_set(id, thread_siblings);
  179. snprintf(path, sizeof(path), "%s/cpu%d/topology/thread_siblings",
  180. SYSFS_CPU_PATH, id);
  181. path[sizeof(path) - 1] = '\0';
  182. if ((fd = fopen(path, "r"))) {
  183. if (getline(&str, &sz, fd) >= 0)
  184. cpumask_parse_user(str, strlen(str), thread_siblings);
  185. fclose(fd);
  186. }
  187. /* Don't use second thread of Hyper Threading */
  188. if (!ht && cpu_list_search_ht(cpus, package_id, core_id,
  189. &thread_siblings))
  190. continue;
  191. new = cpu_new(id);
  192. new->package_id = package_id;
  193. new->core_id = core_id;
  194. cpu_list_add(cpus, new);
  195. }
  196. cpus_free(thread_siblings);
  197. free(str);
  198. return 0;
  199. }