cpu_parse.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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,
  57. unsigned int core_id)
  58. {
  59. lub_list_node_t *iter;
  60. for (iter = lub_list_iterator_init(cpus); iter;
  61. iter = lub_list_iterator_next(iter)) {
  62. cpu_t *cpu;
  63. cpu = (cpu_t *)lub_list_node__get_data(iter);
  64. if (cpu->package_id != package_id)
  65. continue;
  66. if (cpu->core_id != core_id)
  67. continue;
  68. return cpu;
  69. }
  70. return NULL;
  71. }
  72. cpu_t * cpu_list_search(lub_list_t *cpus, unsigned int id)
  73. {
  74. lub_list_node_t *node;
  75. cpu_t search;
  76. search.id = id;
  77. node = lub_list_search(cpus, &search);
  78. if (!node)
  79. return NULL;
  80. return (cpu_t *)lub_list_node__get_data(node);
  81. }
  82. static cpu_t * cpu_list_add(lub_list_t *cpus, cpu_t *cpu)
  83. {
  84. cpu_t *old = cpu_list_search(cpus, cpu->id);
  85. if (old) /* CPU already exists. May be renew some fields later */
  86. return old;
  87. lub_list_add(cpus, cpu);
  88. return cpu;
  89. }
  90. int cpu_list_free(lub_list_t *cpus)
  91. {
  92. lub_list_node_t *iter;
  93. while ((iter = lub_list__get_head(cpus))) {
  94. cpu_t *cpu;
  95. cpu = (cpu_t *)lub_list_node__get_data(iter);
  96. cpu_free(cpu);
  97. lub_list_del(cpus, iter);
  98. lub_list_node_free(iter);
  99. }
  100. lub_list_free(cpus);
  101. return 0;
  102. }
  103. /* Show CPU information */
  104. static void show_cpu_info(cpu_t *cpu)
  105. {
  106. char buf[NR_CPUS + 1];
  107. cpumask_scnprintf(buf, sizeof(buf), cpu->cpumask);
  108. printf("CPU %d package %d core %d mask %s\n", cpu->id, cpu->package_id, cpu->core_id, buf);
  109. }
  110. /* Show CPU list */
  111. int show_cpus(lub_list_t *cpus)
  112. {
  113. lub_list_node_t *iter;
  114. for (iter = lub_list_iterator_init(cpus); iter;
  115. iter = lub_list_iterator_next(iter)) {
  116. cpu_t *cpu;
  117. cpu = (cpu_t *)lub_list_node__get_data(iter);
  118. show_cpu_info(cpu);
  119. }
  120. return 0;
  121. }
  122. /* Search for CPUs */
  123. int scan_cpus(lub_list_t *cpus)
  124. {
  125. FILE *fd;
  126. char path[PATH_MAX];
  127. unsigned int id;
  128. unsigned int package_id;
  129. unsigned int core_id;
  130. cpu_t *new;
  131. for (id = 0; id < NR_CPUS; id++) {
  132. sprintf(path, "%s/cpu%d", SYSFS_CPU_PATH, id);
  133. if (access(path, F_OK))
  134. break;
  135. /* Try to get package_id */
  136. sprintf(path, "%s/cpu%d/topology/physical_package_id",
  137. SYSFS_CPU_PATH, id);
  138. if (!(fd = fopen(path, "r")))
  139. continue;
  140. if (fscanf(fd, "%u", &package_id) < 0) {
  141. fclose(fd);
  142. continue;
  143. }
  144. fclose(fd);
  145. /* Try to get core_id */
  146. sprintf(path, "%s/cpu%d/topology/core_id",
  147. SYSFS_CPU_PATH, id);
  148. if (!(fd = fopen(path, "r")))
  149. continue;
  150. if (fscanf(fd, "%u", &core_id) < 0) {
  151. fclose(fd);
  152. continue;
  153. }
  154. fclose(fd);
  155. /* Don't use second thread of Hyper Threading */
  156. if (cpu_list_search_ht(cpus, package_id, core_id))
  157. continue;
  158. new = cpu_new(id);
  159. new->package_id = package_id;
  160. new->core_id = core_id;
  161. cpu_list_add(cpus, new);
  162. }
  163. return 0;
  164. }