cpuidle-info.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
  3. * (C) 2010 Thomas Renninger <trenn@suse.de>
  4. *
  5. * Licensed under the terms of the GNU GPL License version 2.
  6. */
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <getopt.h>
  13. #include <cpufreq.h>
  14. #include "helpers/helpers.h"
  15. #include "helpers/sysfs.h"
  16. #include "helpers/bitmask.h"
  17. #define LINE_LEN 10
  18. static void cpuidle_cpu_output(unsigned int cpu, int verbose)
  19. {
  20. int idlestates, idlestate;
  21. char *tmp;
  22. printf(_ ("Analyzing CPU %d:\n"), cpu);
  23. idlestates = sysfs_get_idlestate_count(cpu);
  24. if (idlestates == 0) {
  25. printf(_("CPU %u: No idle states\n"), cpu);
  26. return;
  27. } else if (idlestates <= 0) {
  28. printf(_("CPU %u: Can't read idle state info\n"), cpu);
  29. return;
  30. }
  31. tmp = sysfs_get_idlestate_name(cpu, idlestates - 1);
  32. if (!tmp) {
  33. printf(_("Could not determine max idle state %u\n"),
  34. idlestates - 1);
  35. return;
  36. }
  37. printf(_("Number of idle states: %d\n"), idlestates);
  38. printf(_("Available idle states:"));
  39. for (idlestate = 1; idlestate < idlestates; idlestate++) {
  40. tmp = sysfs_get_idlestate_name(cpu, idlestate);
  41. if (!tmp)
  42. continue;
  43. printf(" %s", tmp);
  44. free(tmp);
  45. }
  46. printf("\n");
  47. if (!verbose)
  48. return;
  49. for (idlestate = 1; idlestate < idlestates; idlestate++) {
  50. tmp = sysfs_get_idlestate_name(cpu, idlestate);
  51. if (!tmp)
  52. continue;
  53. printf("%s:\n", tmp);
  54. free(tmp);
  55. tmp = sysfs_get_idlestate_desc(cpu, idlestate);
  56. if (!tmp)
  57. continue;
  58. printf(_("Flags/Description: %s\n"), tmp);
  59. free(tmp);
  60. printf(_("Latency: %lu\n"),
  61. sysfs_get_idlestate_latency(cpu, idlestate));
  62. printf(_("Usage: %lu\n"),
  63. sysfs_get_idlestate_usage(cpu, idlestate));
  64. printf(_("Duration: %llu\n"),
  65. sysfs_get_idlestate_time(cpu, idlestate));
  66. }
  67. printf("\n");
  68. }
  69. static void cpuidle_general_output(void)
  70. {
  71. char *tmp;
  72. tmp = sysfs_get_cpuidle_driver();
  73. if (!tmp) {
  74. printf(_("Could not determine cpuidle driver\n"));
  75. return;
  76. }
  77. printf(_("CPUidle driver: %s\n"), tmp);
  78. free(tmp);
  79. tmp = sysfs_get_cpuidle_governor();
  80. if (!tmp) {
  81. printf(_("Could not determine cpuidle governor\n"));
  82. return;
  83. }
  84. printf(_("CPUidle governor: %s\n"), tmp);
  85. free(tmp);
  86. }
  87. static void proc_cpuidle_cpu_output(unsigned int cpu)
  88. {
  89. long max_allowed_cstate = 2000000000;
  90. int cstates, cstate;
  91. cstates = sysfs_get_idlestate_count(cpu);
  92. if (cstates == 0) {
  93. /*
  94. * Go on and print same useless info as you'd see with
  95. * cat /proc/acpi/processor/../power
  96. * printf(_("CPU %u: No C-states available\n"), cpu);
  97. * return;
  98. */
  99. } else if (cstates <= 0) {
  100. printf(_("CPU %u: Can't read C-state info\n"), cpu);
  101. return;
  102. }
  103. /* printf("Cstates: %d\n", cstates); */
  104. printf(_("active state: C0\n"));
  105. printf(_("max_cstate: C%u\n"), cstates-1);
  106. printf(_("maximum allowed latency: %lu usec\n"), max_allowed_cstate);
  107. printf(_("states:\t\n"));
  108. for (cstate = 1; cstate < cstates; cstate++) {
  109. printf(_(" C%d: "
  110. "type[C%d] "), cstate, cstate);
  111. printf(_("promotion[--] demotion[--] "));
  112. printf(_("latency[%03lu] "),
  113. sysfs_get_idlestate_latency(cpu, cstate));
  114. printf(_("usage[%08lu] "),
  115. sysfs_get_idlestate_usage(cpu, cstate));
  116. printf(_("duration[%020Lu] \n"),
  117. sysfs_get_idlestate_time(cpu, cstate));
  118. }
  119. }
  120. /* --freq / -f */
  121. void idle_info_help(void)
  122. {
  123. printf(_ ("Usage: cpupower idleinfo [options]\n"));
  124. printf(_ ("Options:\n"));
  125. printf(_ (" -s, --silent Only show general C-state information\n"));
  126. printf(_ (" -o, --proc Prints out information like provided by the /proc/acpi/processor/*/power\n"
  127. " interface in older kernels\n"));
  128. printf(_ (" -h, --help Prints out this screen\n"));
  129. printf("\n");
  130. }
  131. static struct option info_opts[] = {
  132. { .name = "silent", .has_arg = no_argument, .flag = NULL, .val = 's'},
  133. { .name = "proc", .has_arg = no_argument, .flag = NULL, .val = 'o'},
  134. { .name = "help", .has_arg = no_argument, .flag = NULL, .val = 'h'},
  135. { },
  136. };
  137. static inline void cpuidle_exit(int fail)
  138. {
  139. idle_info_help();
  140. exit(EXIT_FAILURE);
  141. }
  142. int cmd_idle_info(int argc, char **argv)
  143. {
  144. extern char *optarg;
  145. extern int optind, opterr, optopt;
  146. int ret = 0, cont = 1, output_param = 0, verbose = 1;
  147. unsigned int cpu = 0;
  148. do {
  149. ret = getopt_long(argc, argv, "hos", info_opts, NULL);
  150. if (ret == -1)
  151. break;
  152. switch (ret) {
  153. case '?':
  154. output_param = '?';
  155. cont = 0;
  156. break;
  157. case 'h':
  158. output_param = 'h';
  159. cont = 0;
  160. break;
  161. case 's':
  162. verbose = 0;
  163. break;
  164. case -1:
  165. cont = 0;
  166. break;
  167. case 'o':
  168. if (output_param) {
  169. output_param = -1;
  170. cont = 0;
  171. break;
  172. }
  173. output_param = ret;
  174. break;
  175. }
  176. } while (cont);
  177. switch (output_param) {
  178. case -1:
  179. printf(_("You can't specify more than one "
  180. "output-specific argument\n"));
  181. cpuidle_exit(EXIT_FAILURE);
  182. case '?':
  183. printf(_("invalid or unknown argument\n"));
  184. cpuidle_exit(EXIT_FAILURE);
  185. case 'h':
  186. cpuidle_exit(EXIT_SUCCESS);
  187. }
  188. /* Default is: show output of CPU 0 only */
  189. if (bitmask_isallclear(cpus_chosen))
  190. bitmask_setbit(cpus_chosen, 0);
  191. if (output_param == 0)
  192. cpuidle_general_output();
  193. for (cpu = bitmask_first(cpus_chosen);
  194. cpu <= bitmask_last(cpus_chosen); cpu++) {
  195. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  196. cpufreq_cpu_exists(cpu))
  197. continue;
  198. switch (output_param) {
  199. case 'o':
  200. proc_cpuidle_cpu_output(cpu);
  201. break;
  202. case 0:
  203. printf("\n");
  204. cpuidle_cpu_output(cpu, verbose);
  205. break;
  206. }
  207. }
  208. return EXIT_SUCCESS;
  209. }