cpu.c 4.9 KB

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