cpuidle-info.c 4.6 KB

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