topology.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. *
  6. * ToDo: Needs to be done more properly for AMD/Intel specifics
  7. */
  8. /* Helper struct for qsort, must be in sync with cpupower_topology.cpu_info */
  9. /* Be careful: Need to pass unsigned to the sort, so that offlined cores are
  10. in the end, but double check for -1 for offlined cpus at other places */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <helpers/helpers.h>
  17. #include <helpers/sysfs.h>
  18. /* returns -1 on failure, 0 on success */
  19. static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result)
  20. {
  21. char linebuf[MAX_LINE_LEN];
  22. char *endp;
  23. char path[SYSFS_PATH_MAX];
  24. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s",
  25. cpu, fname);
  26. if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0)
  27. return -1;
  28. *result = strtol(linebuf, &endp, 0);
  29. if (endp == linebuf || errno == ERANGE)
  30. return -1;
  31. return 0;
  32. }
  33. static int __compare(const void *t1, const void *t2)
  34. {
  35. struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1;
  36. struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2;
  37. if (top1->pkg < top2->pkg)
  38. return -1;
  39. else if (top1->pkg > top2->pkg)
  40. return 1;
  41. else if (top1->core < top2->core)
  42. return -1;
  43. else if (top1->core > top2->core)
  44. return 1;
  45. else if (top1->cpu < top2->cpu)
  46. return -1;
  47. else if (top1->cpu > top2->cpu)
  48. return 1;
  49. else
  50. return 0;
  51. }
  52. /*
  53. * Returns amount of cpus, negative on error, cpu_top must be
  54. * passed to cpu_topology_release to free resources
  55. *
  56. * Array is sorted after ->pkg, ->core, then ->cpu
  57. */
  58. int get_cpu_topology(struct cpupower_topology *cpu_top)
  59. {
  60. int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF);
  61. cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
  62. if (cpu_top->core_info == NULL)
  63. return -ENOMEM;
  64. cpu_top->pkgs = cpu_top->cores = 0;
  65. for (cpu = 0; cpu < cpus; cpu++) {
  66. cpu_top->core_info[cpu].cpu = cpu;
  67. cpu_top->core_info[cpu].is_online = sysfs_is_cpu_online(cpu);
  68. if(sysfs_topology_read_file(
  69. cpu,
  70. "physical_package_id",
  71. &(cpu_top->core_info[cpu].pkg)) < 0)
  72. return -1;
  73. if(sysfs_topology_read_file(
  74. cpu,
  75. "core_id",
  76. &(cpu_top->core_info[cpu].core)) < 0)
  77. return -1;
  78. }
  79. qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
  80. __compare);
  81. /* Count the number of distinct pkgs values. This works
  82. because the primary sort of the core_info struct was just
  83. done by pkg value. */
  84. last_pkg = cpu_top->core_info[0].pkg;
  85. for(cpu = 1; cpu < cpus; cpu++) {
  86. if(cpu_top->core_info[cpu].pkg != last_pkg) {
  87. last_pkg = cpu_top->core_info[cpu].pkg;
  88. cpu_top->pkgs++;
  89. }
  90. }
  91. cpu_top->pkgs++;
  92. /* Intel's cores count is not consecutively numbered, there may
  93. * be a core_id of 3, but none of 2. Assume there always is 0
  94. * Get amount of cores by counting duplicates in a package
  95. for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
  96. if (cpu_top->core_info[cpu].core == 0)
  97. cpu_top->cores++;
  98. */
  99. return cpus;
  100. }
  101. void cpu_topology_release(struct cpupower_topology cpu_top)
  102. {
  103. free(cpu_top.core_info);
  104. }