cpuidle-info.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. printf(_("Number of idle states: %d\n"), idlestates);
  32. printf(_("Available idle states:"));
  33. for (idlestate = 0; idlestate < idlestates; idlestate++) {
  34. tmp = sysfs_get_idlestate_name(cpu, idlestate);
  35. if (!tmp)
  36. continue;
  37. printf(" %s", tmp);
  38. free(tmp);
  39. }
  40. printf("\n");
  41. if (!verbose)
  42. return;
  43. for (idlestate = 0; idlestate < idlestates; idlestate++) {
  44. tmp = sysfs_get_idlestate_name(cpu, idlestate);
  45. if (!tmp)
  46. continue;
  47. printf("%s:\n", tmp);
  48. free(tmp);
  49. tmp = sysfs_get_idlestate_desc(cpu, idlestate);
  50. if (!tmp)
  51. continue;
  52. printf(_("Flags/Description: %s\n"), tmp);
  53. free(tmp);
  54. printf(_("Latency: %lu\n"),
  55. sysfs_get_idlestate_latency(cpu, idlestate));
  56. printf(_("Usage: %lu\n"),
  57. sysfs_get_idlestate_usage(cpu, idlestate));
  58. printf(_("Duration: %llu\n"),
  59. sysfs_get_idlestate_time(cpu, idlestate));
  60. }
  61. printf("\n");
  62. }
  63. static void cpuidle_general_output(void)
  64. {
  65. char *tmp;
  66. tmp = sysfs_get_cpuidle_driver();
  67. if (!tmp) {
  68. printf(_("Could not determine cpuidle driver\n"));
  69. return;
  70. }
  71. printf(_("CPUidle driver: %s\n"), tmp);
  72. free(tmp);
  73. tmp = sysfs_get_cpuidle_governor();
  74. if (!tmp) {
  75. printf(_("Could not determine cpuidle governor\n"));
  76. return;
  77. }
  78. printf(_("CPUidle governor: %s\n"), tmp);
  79. free(tmp);
  80. }
  81. static void proc_cpuidle_cpu_output(unsigned int cpu)
  82. {
  83. long max_allowed_cstate = 2000000000;
  84. int cstates, cstate;
  85. cstates = sysfs_get_idlestate_count(cpu);
  86. if (cstates == 0) {
  87. /*
  88. * Go on and print same useless info as you'd see with
  89. * cat /proc/acpi/processor/../power
  90. * printf(_("CPU %u: No C-states available\n"), cpu);
  91. * return;
  92. */
  93. } else if (cstates <= 0) {
  94. printf(_("CPU %u: Can't read C-state info\n"), cpu);
  95. return;
  96. }
  97. /* printf("Cstates: %d\n", cstates); */
  98. printf(_("active state: C0\n"));
  99. printf(_("max_cstate: C%u\n"), cstates-1);
  100. printf(_("maximum allowed latency: %lu usec\n"), max_allowed_cstate);
  101. printf(_("states:\t\n"));
  102. for (cstate = 1; cstate < cstates; cstate++) {
  103. printf(_(" C%d: "
  104. "type[C%d] "), cstate, cstate);
  105. printf(_("promotion[--] demotion[--] "));
  106. printf(_("latency[%03lu] "),
  107. sysfs_get_idlestate_latency(cpu, cstate));
  108. printf(_("usage[%08lu] "),
  109. sysfs_get_idlestate_usage(cpu, cstate));
  110. printf(_("duration[%020Lu] \n"),
  111. sysfs_get_idlestate_time(cpu, cstate));
  112. }
  113. }
  114. static struct option info_opts[] = {
  115. { .name = "silent", .has_arg = no_argument, .flag = NULL, .val = 's'},
  116. { .name = "proc", .has_arg = no_argument, .flag = NULL, .val = 'o'},
  117. { },
  118. };
  119. static inline void cpuidle_exit(int fail)
  120. {
  121. exit(EXIT_FAILURE);
  122. }
  123. int cmd_idle_info(int argc, char **argv)
  124. {
  125. extern char *optarg;
  126. extern int optind, opterr, optopt;
  127. int ret = 0, cont = 1, output_param = 0, verbose = 1;
  128. unsigned int cpu = 0;
  129. do {
  130. ret = getopt_long(argc, argv, "os", info_opts, NULL);
  131. if (ret == -1)
  132. break;
  133. switch (ret) {
  134. case '?':
  135. output_param = '?';
  136. cont = 0;
  137. break;
  138. case 's':
  139. verbose = 0;
  140. break;
  141. case -1:
  142. cont = 0;
  143. break;
  144. case 'o':
  145. if (output_param) {
  146. output_param = -1;
  147. cont = 0;
  148. break;
  149. }
  150. output_param = ret;
  151. break;
  152. }
  153. } while (cont);
  154. switch (output_param) {
  155. case -1:
  156. printf(_("You can't specify more than one "
  157. "output-specific argument\n"));
  158. cpuidle_exit(EXIT_FAILURE);
  159. case '?':
  160. printf(_("invalid or unknown argument\n"));
  161. cpuidle_exit(EXIT_FAILURE);
  162. }
  163. /* Default is: show output of CPU 0 only */
  164. if (bitmask_isallclear(cpus_chosen))
  165. bitmask_setbit(cpus_chosen, 0);
  166. if (output_param == 0)
  167. cpuidle_general_output();
  168. for (cpu = bitmask_first(cpus_chosen);
  169. cpu <= bitmask_last(cpus_chosen); cpu++) {
  170. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  171. cpufreq_cpu_exists(cpu))
  172. continue;
  173. switch (output_param) {
  174. case 'o':
  175. proc_cpuidle_cpu_output(cpu);
  176. break;
  177. case 0:
  178. printf("\n");
  179. cpuidle_cpu_output(cpu, verbose);
  180. break;
  181. }
  182. }
  183. return EXIT_SUCCESS;
  184. }