cpu_parse.c 3.8 KB

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