cpu_parse.c 4.7 KB

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