cpuidle-set.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <limits.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <getopt.h>
  9. #include "cpufreq.h"
  10. #include "helpers/helpers.h"
  11. #include "helpers/sysfs.h"
  12. static struct option info_opts[] = {
  13. {"disable", required_argument, NULL, 'd'},
  14. {"enable", required_argument, NULL, 'e'},
  15. {"disable-by-latency", required_argument, NULL, 'D'},
  16. {"enable-all", no_argument, NULL, 'E'},
  17. { },
  18. };
  19. int cmd_idle_set(int argc, char **argv)
  20. {
  21. extern char *optarg;
  22. extern int optind, opterr, optopt;
  23. int ret = 0, cont = 1, param = 0, disabled;
  24. unsigned long long latency = 0, state_latency;
  25. unsigned int cpu = 0, idlestate = 0, idlestates = 0;
  26. char *endptr;
  27. do {
  28. ret = getopt_long(argc, argv, "d:e:ED:", info_opts, NULL);
  29. if (ret == -1)
  30. break;
  31. switch (ret) {
  32. case '?':
  33. param = '?';
  34. cont = 0;
  35. break;
  36. case 'd':
  37. if (param) {
  38. param = -1;
  39. cont = 0;
  40. break;
  41. }
  42. param = ret;
  43. idlestate = atoi(optarg);
  44. break;
  45. case 'e':
  46. if (param) {
  47. param = -1;
  48. cont = 0;
  49. break;
  50. }
  51. param = ret;
  52. idlestate = atoi(optarg);
  53. break;
  54. case 'D':
  55. if (param) {
  56. param = -1;
  57. cont = 0;
  58. break;
  59. }
  60. param = ret;
  61. latency = strtoull(optarg, &endptr, 10);
  62. if (*endptr != '\0') {
  63. printf(_("Bad latency value: %s\n"), optarg);
  64. exit(EXIT_FAILURE);
  65. }
  66. break;
  67. case 'E':
  68. if (param) {
  69. param = -1;
  70. cont = 0;
  71. break;
  72. }
  73. param = ret;
  74. break;
  75. case -1:
  76. cont = 0;
  77. break;
  78. }
  79. } while (cont);
  80. switch (param) {
  81. case -1:
  82. printf(_("You can't specify more than one "
  83. "output-specific argument\n"));
  84. exit(EXIT_FAILURE);
  85. case '?':
  86. printf(_("invalid or unknown argument\n"));
  87. exit(EXIT_FAILURE);
  88. }
  89. /* Default is: set all CPUs */
  90. if (bitmask_isallclear(cpus_chosen))
  91. bitmask_setall(cpus_chosen);
  92. for (cpu = bitmask_first(cpus_chosen);
  93. cpu <= bitmask_last(cpus_chosen); cpu++) {
  94. if (!bitmask_isbitset(cpus_chosen, cpu))
  95. continue;
  96. if (sysfs_is_cpu_online(cpu) != 1)
  97. continue;
  98. idlestates = sysfs_get_idlestate_count(cpu);
  99. if (idlestates <= 0)
  100. continue;
  101. switch (param) {
  102. case 'd':
  103. ret = sysfs_idlestate_disable(cpu, idlestate, 1);
  104. if (ret == 0)
  105. printf(_("Idlestate %u disabled on CPU %u\n"), idlestate, cpu);
  106. else if (ret == -1)
  107. printf(_("Idlestate %u not available on CPU %u\n"),
  108. idlestate, cpu);
  109. else if (ret == -2)
  110. printf(_("Idlestate disabling not supported by kernel\n"));
  111. else
  112. printf(_("Idlestate %u not disabled on CPU %u\n"),
  113. idlestate, cpu);
  114. break;
  115. case 'e':
  116. ret = sysfs_idlestate_disable(cpu, idlestate, 0);
  117. if (ret == 0)
  118. printf(_("Idlestate %u enabled on CPU %u\n"), idlestate, cpu);
  119. else if (ret == -1)
  120. printf(_("Idlestate %u not available on CPU %u\n"),
  121. idlestate, cpu);
  122. else if (ret == -2)
  123. printf(_("Idlestate enabling not supported by kernel\n"));
  124. else
  125. printf(_("Idlestate %u not enabled on CPU %u\n"),
  126. idlestate, cpu);
  127. break;
  128. case 'D':
  129. for (idlestate = 0; idlestate < idlestates; idlestate++) {
  130. disabled = sysfs_is_idlestate_disabled
  131. (cpu, idlestate);
  132. state_latency = sysfs_get_idlestate_latency
  133. (cpu, idlestate);
  134. if (disabled == 1) {
  135. if (latency > state_latency){
  136. ret = sysfs_idlestate_disable
  137. (cpu, idlestate, 0);
  138. if (ret == 0)
  139. printf(_("Idlestate %u enabled on CPU %u\n"), idlestate, cpu);
  140. }
  141. continue;
  142. }
  143. if (latency <= state_latency){
  144. ret = sysfs_idlestate_disable
  145. (cpu, idlestate, 1);
  146. if (ret == 0)
  147. printf(_("Idlestate %u disabled on CPU %u\n"), idlestate, cpu);
  148. }
  149. }
  150. break;
  151. case 'E':
  152. for (idlestate = 0; idlestate < idlestates; idlestate++) {
  153. disabled = sysfs_is_idlestate_disabled
  154. (cpu, idlestate);
  155. if (disabled == 1) {
  156. ret = sysfs_idlestate_disable
  157. (cpu, idlestate, 0);
  158. if (ret == 0)
  159. printf(_("Idlestate %u enabled on CPU %u\n"), idlestate, cpu);
  160. }
  161. }
  162. break;
  163. default:
  164. /* Not reachable with proper args checking */
  165. printf(_("Invalid or unknown argument\n"));
  166. exit(EXIT_FAILURE);
  167. break;
  168. }
  169. }
  170. return EXIT_SUCCESS;
  171. }