cpupower.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. */
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include "cpupower.h"
  14. #include "cpupower_intern.h"
  15. unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
  16. {
  17. int fd;
  18. ssize_t numread;
  19. fd = open(path, O_RDONLY);
  20. if (fd == -1)
  21. return 0;
  22. numread = read(fd, buf, buflen - 1);
  23. if (numread < 1) {
  24. close(fd);
  25. return 0;
  26. }
  27. buf[numread] = '\0';
  28. close(fd);
  29. return (unsigned int) numread;
  30. }
  31. /*
  32. * Detect whether a CPU is online
  33. *
  34. * Returns:
  35. * 1 -> if CPU is online
  36. * 0 -> if CPU is offline
  37. * negative errno values in error case
  38. */
  39. int cpupower_is_cpu_online(unsigned int cpu)
  40. {
  41. char path[SYSFS_PATH_MAX];
  42. int fd;
  43. ssize_t numread;
  44. unsigned long long value;
  45. char linebuf[MAX_LINE_LEN];
  46. char *endp;
  47. struct stat statbuf;
  48. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u", cpu);
  49. if (stat(path, &statbuf) != 0)
  50. return 0;
  51. /*
  52. * kernel without CONFIG_HOTPLUG_CPU
  53. * -> cpuX directory exists, but not cpuX/online file
  54. */
  55. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/online", cpu);
  56. if (stat(path, &statbuf) != 0)
  57. return 1;
  58. fd = open(path, O_RDONLY);
  59. if (fd == -1)
  60. return -errno;
  61. numread = read(fd, linebuf, MAX_LINE_LEN - 1);
  62. if (numread < 1) {
  63. close(fd);
  64. return -EIO;
  65. }
  66. linebuf[numread] = '\0';
  67. close(fd);
  68. value = strtoull(linebuf, &endp, 0);
  69. if (value > 1)
  70. return -EINVAL;
  71. return value;
  72. }
  73. /* returns -1 on failure, 0 on success */
  74. static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result)
  75. {
  76. char linebuf[MAX_LINE_LEN];
  77. char *endp;
  78. char path[SYSFS_PATH_MAX];
  79. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s",
  80. cpu, fname);
  81. if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0)
  82. return -1;
  83. *result = strtol(linebuf, &endp, 0);
  84. if (endp == linebuf || errno == ERANGE)
  85. return -1;
  86. return 0;
  87. }
  88. static int __compare(const void *t1, const void *t2)
  89. {
  90. struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1;
  91. struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2;
  92. if (top1->pkg < top2->pkg)
  93. return -1;
  94. else if (top1->pkg > top2->pkg)
  95. return 1;
  96. else if (top1->core < top2->core)
  97. return -1;
  98. else if (top1->core > top2->core)
  99. return 1;
  100. else if (top1->cpu < top2->cpu)
  101. return -1;
  102. else if (top1->cpu > top2->cpu)
  103. return 1;
  104. else
  105. return 0;
  106. }
  107. /*
  108. * Returns amount of cpus, negative on error, cpu_top must be
  109. * passed to cpu_topology_release to free resources
  110. *
  111. * Array is sorted after ->pkg, ->core, then ->cpu
  112. */
  113. int get_cpu_topology(struct cpupower_topology *cpu_top)
  114. {
  115. int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF);
  116. cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
  117. if (cpu_top->core_info == NULL)
  118. return -ENOMEM;
  119. cpu_top->pkgs = cpu_top->cores = 0;
  120. for (cpu = 0; cpu < cpus; cpu++) {
  121. cpu_top->core_info[cpu].cpu = cpu;
  122. cpu_top->core_info[cpu].is_online = cpupower_is_cpu_online(cpu);
  123. if(sysfs_topology_read_file(
  124. cpu,
  125. "physical_package_id",
  126. &(cpu_top->core_info[cpu].pkg)) < 0) {
  127. cpu_top->core_info[cpu].pkg = -1;
  128. cpu_top->core_info[cpu].core = -1;
  129. continue;
  130. }
  131. if(sysfs_topology_read_file(
  132. cpu,
  133. "core_id",
  134. &(cpu_top->core_info[cpu].core)) < 0) {
  135. cpu_top->core_info[cpu].pkg = -1;
  136. cpu_top->core_info[cpu].core = -1;
  137. continue;
  138. }
  139. }
  140. qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
  141. __compare);
  142. /* Count the number of distinct pkgs values. This works
  143. because the primary sort of the core_info struct was just
  144. done by pkg value. */
  145. last_pkg = cpu_top->core_info[0].pkg;
  146. for(cpu = 1; cpu < cpus; cpu++) {
  147. if (cpu_top->core_info[cpu].pkg != last_pkg &&
  148. cpu_top->core_info[cpu].pkg != -1) {
  149. last_pkg = cpu_top->core_info[cpu].pkg;
  150. cpu_top->pkgs++;
  151. }
  152. }
  153. if (!(cpu_top->core_info[0].pkg == -1))
  154. cpu_top->pkgs++;
  155. /* Intel's cores count is not consecutively numbered, there may
  156. * be a core_id of 3, but none of 2. Assume there always is 0
  157. * Get amount of cores by counting duplicates in a package
  158. for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
  159. if (cpu_top->core_info[cpu].core == 0)
  160. cpu_top->cores++;
  161. */
  162. return cpus;
  163. }
  164. void cpu_topology_release(struct cpupower_topology cpu_top)
  165. {
  166. free(cpu_top.core_info);
  167. }