topology.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. int sysfs_topology_read_file(unsigned int cpu, const char *fname)
  20. {
  21. unsigned long value;
  22. char linebuf[MAX_LINE_LEN];
  23. char *endp;
  24. char path[SYSFS_PATH_MAX];
  25. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s",
  26. cpu, fname);
  27. if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0)
  28. return -1;
  29. value = strtoul(linebuf, &endp, 0);
  30. if (endp == linebuf || errno == ERANGE)
  31. return -1;
  32. return value;
  33. }
  34. struct cpuid_core_info {
  35. unsigned int pkg;
  36. unsigned int thread;
  37. unsigned int cpu;
  38. };
  39. static int __compare(const void *t1, const void *t2)
  40. {
  41. struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1;
  42. struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2;
  43. if (top1->pkg < top2->pkg)
  44. return -1;
  45. else if (top1->pkg > top2->pkg)
  46. return 1;
  47. else if (top1->thread < top2->thread)
  48. return -1;
  49. else if (top1->thread > top2->thread)
  50. return 1;
  51. else if (top1->cpu < top2->cpu)
  52. return -1;
  53. else if (top1->cpu > top2->cpu)
  54. return 1;
  55. else
  56. return 0;
  57. }
  58. /*
  59. * Returns amount of cpus, negative on error, cpu_top must be
  60. * passed to cpu_topology_release to free resources
  61. *
  62. * Array is sorted after ->pkg, ->core, then ->cpu
  63. */
  64. int get_cpu_topology(struct cpupower_topology *cpu_top)
  65. {
  66. int cpu, cpus = sysconf(_SC_NPROCESSORS_CONF);
  67. cpu_top->core_info = malloc(sizeof(struct cpupower_topology) * cpus);
  68. if (cpu_top->core_info == NULL)
  69. return -ENOMEM;
  70. cpu_top->pkgs = cpu_top->cores = 0;
  71. for (cpu = 0; cpu < cpus; cpu++) {
  72. cpu_top->core_info[cpu].pkg =
  73. sysfs_topology_read_file(cpu, "physical_package_id");
  74. if ((int)cpu_top->core_info[cpu].pkg != -1 &&
  75. cpu_top->core_info[cpu].pkg > cpu_top->pkgs)
  76. cpu_top->pkgs = cpu_top->core_info[cpu].pkg;
  77. cpu_top->core_info[cpu].core =
  78. sysfs_topology_read_file(cpu, "core_id");
  79. cpu_top->core_info[cpu].cpu = cpu;
  80. }
  81. cpu_top->pkgs++;
  82. qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
  83. __compare);
  84. /* Intel's cores count is not consecutively numbered, there may
  85. * be a core_id of 3, but none of 2. Assume there always is 0
  86. * Get amount of cores by counting duplicates in a package
  87. for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
  88. if (cpu_top->core_info[cpu].core == 0)
  89. cpu_top->cores++;
  90. */
  91. return cpus;
  92. }
  93. void cpu_topology_release(struct cpupower_topology cpu_top)
  94. {
  95. free(cpu_top.core_info);
  96. }