cpu.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. printf("CPU %d package %d core %d mask %s\n", cpu->id, cpu->package_id, cpu->core_id, buf);
  118. }
  119. /* Show CPU list */
  120. int show_cpus(lub_list_t *cpus)
  121. {
  122. lub_list_node_t *iter;
  123. for (iter = lub_list_iterator_init(cpus); iter;
  124. iter = lub_list_iterator_next(iter)) {
  125. cpu_t *cpu;
  126. cpu = (cpu_t *)lub_list_node__get_data(iter);
  127. show_cpu_info(cpu);
  128. }
  129. return 0;
  130. }
  131. /* Search for CPUs */
  132. int scan_cpus(lub_list_t *cpus, int ht)
  133. {
  134. FILE *fd;
  135. char path[PATH_MAX];
  136. unsigned int id;
  137. unsigned int package_id;
  138. unsigned int core_id;
  139. cpu_t *new;
  140. char *str = NULL;
  141. size_t sz;
  142. cpumask_t thread_siblings;
  143. for (id = 0; id < NR_CPUS; id++) {
  144. sprintf(path, "%s/cpu%d", SYSFS_CPU_PATH, id);
  145. if (access(path, F_OK))
  146. break;
  147. /* Try to get package_id */
  148. sprintf(path, "%s/cpu%d/topology/physical_package_id",
  149. SYSFS_CPU_PATH, id);
  150. if (!(fd = fopen(path, "r")))
  151. continue;
  152. if (fscanf(fd, "%u", &package_id) < 0) {
  153. fclose(fd);
  154. continue;
  155. }
  156. fclose(fd);
  157. /* Try to get core_id */
  158. sprintf(path, "%s/cpu%d/topology/core_id",
  159. SYSFS_CPU_PATH, id);
  160. if (!(fd = fopen(path, "r")))
  161. continue;
  162. if (fscanf(fd, "%u", &core_id) < 0) {
  163. fclose(fd);
  164. continue;
  165. }
  166. fclose(fd);
  167. /* Get thread siblings */
  168. cpus_clear(thread_siblings);
  169. cpu_set(id, thread_siblings);
  170. sprintf(path, "%s/cpu%d/topology/thread_siblings",
  171. SYSFS_CPU_PATH, id);
  172. if ((fd = fopen(path, "r"))) {
  173. if (getline(&str, &sz, fd) >= 0)
  174. cpumask_parse_user(str, strlen(str), thread_siblings);
  175. fclose(fd);
  176. }
  177. /* Don't use second thread of Hyper Threading */
  178. if (!ht && cpu_list_search_ht(cpus, package_id, core_id, thread_siblings))
  179. continue;
  180. new = cpu_new(id);
  181. new->package_id = package_id;
  182. new->core_id = core_id;
  183. cpu_list_add(cpus, new);
  184. }
  185. free(str);
  186. return 0;
  187. }