topology.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /* flags */
  39. unsigned int is_online:1;
  40. };
  41. static int __compare(const void *t1, const void *t2)
  42. {
  43. struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1;
  44. struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2;
  45. if (top1->pkg < top2->pkg)
  46. return -1;
  47. else if (top1->pkg > top2->pkg)
  48. return 1;
  49. else if (top1->thread < top2->thread)
  50. return -1;
  51. else if (top1->thread > top2->thread)
  52. return 1;
  53. else if (top1->cpu < top2->cpu)
  54. return -1;
  55. else if (top1->cpu > top2->cpu)
  56. return 1;
  57. else
  58. return 0;
  59. }
  60. /*
  61. * Returns amount of cpus, negative on error, cpu_top must be
  62. * passed to cpu_topology_release to free resources
  63. *
  64. * Array is sorted after ->pkg, ->core, then ->cpu
  65. */
  66. int get_cpu_topology(struct cpupower_topology *cpu_top)
  67. {
  68. int cpu, cpus = sysconf(_SC_NPROCESSORS_CONF);
  69. cpu_top->core_info = malloc(sizeof(struct cpupower_topology) * cpus);
  70. if (cpu_top->core_info == NULL)
  71. return -ENOMEM;
  72. cpu_top->pkgs = cpu_top->cores = 0;
  73. for (cpu = 0; cpu < cpus; cpu++) {
  74. cpu_top->core_info[cpu].cpu = cpu;
  75. cpu_top->core_info[cpu].is_online = sysfs_is_cpu_online(cpu);
  76. cpu_top->core_info[cpu].pkg =
  77. sysfs_topology_read_file(cpu, "physical_package_id");
  78. if ((int)cpu_top->core_info[cpu].pkg != -1 &&
  79. cpu_top->core_info[cpu].pkg > cpu_top->pkgs)
  80. cpu_top->pkgs = cpu_top->core_info[cpu].pkg;
  81. cpu_top->core_info[cpu].core =
  82. sysfs_topology_read_file(cpu, "core_id");
  83. }
  84. cpu_top->pkgs++;
  85. qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
  86. __compare);
  87. /* Intel's cores count is not consecutively numbered, there may
  88. * be a core_id of 3, but none of 2. Assume there always is 0
  89. * Get amount of cores by counting duplicates in a package
  90. for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
  91. if (cpu_top->core_info[cpu].core == 0)
  92. cpu_top->cores++;
  93. */
  94. return cpus;
  95. }
  96. void cpu_topology_release(struct cpupower_topology cpu_top)
  97. {
  98. free(cpu_top.core_info);
  99. }