cpufreq-set.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <unistd.h>
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <limits.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <getopt.h>
  14. #include "cpufreq.h"
  15. #include "helpers/helpers.h"
  16. #define NORM_FREQ_LEN 32
  17. void freq_set_help(void)
  18. {
  19. printf(_("Usage: cpupower frequency-set [options]\n"));
  20. printf(_("Options:\n"));
  21. printf(_(" -d FREQ, --min FREQ new minimum CPU frequency the governor may select\n"));
  22. printf(_(" -u FREQ, --max FREQ new maximum CPU frequency the governor may select\n"));
  23. printf(_(" -g GOV, --governor GOV new cpufreq governor\n"));
  24. printf(_(" -f FREQ, --freq FREQ specific frequency to be set. Requires userspace\n"
  25. " governor to be available and loaded\n"));
  26. printf(_(" -r, --related Switches all hardware-related CPUs\n"));
  27. printf(_(" -h, --help Prints out this screen\n"));
  28. printf("\n");
  29. printf(_("Notes:\n"
  30. "1. Omitting the -c or --cpu argument is equivalent to setting it to \"all\"\n"));
  31. printf(_("2. The -f FREQ, --freq FREQ parameter cannot be combined with any other parameter\n"
  32. " except the -c CPU, --cpu CPU parameter\n"
  33. "3. FREQuencies can be passed in Hz, kHz (default), MHz, GHz, or THz\n"
  34. " by postfixing the value with the wanted unit name, without any space\n"
  35. " (FREQuency in kHz =^ Hz * 0.001 =^ MHz * 1000 =^ GHz * 1000000).\n"));
  36. }
  37. static struct option set_opts[] = {
  38. { .name = "min", .has_arg = required_argument, .flag = NULL, .val = 'd'},
  39. { .name = "max", .has_arg = required_argument, .flag = NULL, .val = 'u'},
  40. { .name = "governor", .has_arg = required_argument, .flag = NULL, .val = 'g'},
  41. { .name = "freq", .has_arg = required_argument, .flag = NULL, .val = 'f'},
  42. { .name = "help", .has_arg = no_argument, .flag = NULL, .val = 'h'},
  43. { .name = "related", .has_arg = no_argument, .flag = NULL, .val='r'},
  44. { },
  45. };
  46. static void print_error(void)
  47. {
  48. printf(_("Error setting new values. Common errors:\n"
  49. "- Do you have proper administration rights? (super-user?)\n"
  50. "- Is the governor you requested available and modprobed?\n"
  51. "- Trying to set an invalid policy?\n"
  52. "- Trying to set a specific frequency, but userspace governor is not available,\n"
  53. " for example because of hardware which cannot be set to a specific frequency\n"
  54. " or because the userspace governor isn't loaded?\n"));
  55. };
  56. struct freq_units {
  57. char *str_unit;
  58. int power_of_ten;
  59. };
  60. const struct freq_units def_units[] = {
  61. {"hz", -3},
  62. {"khz", 0}, /* default */
  63. {"mhz", 3},
  64. {"ghz", 6},
  65. {"thz", 9},
  66. {NULL, 0}
  67. };
  68. static void print_unknown_arg(void)
  69. {
  70. printf(_("invalid or unknown argument\n"));
  71. freq_set_help();
  72. }
  73. static unsigned long string_to_frequency(const char *str)
  74. {
  75. char normalized[NORM_FREQ_LEN];
  76. const struct freq_units *unit;
  77. const char *scan;
  78. char *end;
  79. unsigned long freq;
  80. int power = 0, match_count = 0, i, cp, pad;
  81. while (*str == '0')
  82. str++;
  83. for (scan = str; isdigit(*scan) || *scan == '.'; scan++) {
  84. if (*scan == '.' && match_count == 0)
  85. match_count = 1;
  86. else if (*scan == '.' && match_count == 1)
  87. return 0;
  88. }
  89. if (*scan) {
  90. match_count = 0;
  91. for (unit = def_units; unit->str_unit; unit++) {
  92. for (i = 0;
  93. scan[i] && tolower(scan[i]) == unit->str_unit[i];
  94. ++i)
  95. continue;
  96. if (scan[i])
  97. continue;
  98. match_count++;
  99. power = unit->power_of_ten;
  100. }
  101. if (match_count != 1)
  102. return 0;
  103. }
  104. /* count the number of digits to be copied */
  105. for (cp = 0; isdigit(str[cp]); cp++)
  106. continue;
  107. if (str[cp] == '.') {
  108. while (power > -1 && isdigit(str[cp+1]))
  109. cp++, power--;
  110. }
  111. if (power >= -1) /* not enough => pad */
  112. pad = power + 1;
  113. else /* to much => strip */
  114. pad = 0, cp += power + 1;
  115. /* check bounds */
  116. if (cp <= 0 || cp + pad > NORM_FREQ_LEN - 1)
  117. return 0;
  118. /* copy digits */
  119. for (i = 0; i < cp; i++, str++) {
  120. if (*str == '.')
  121. str++;
  122. normalized[i] = *str;
  123. }
  124. /* and pad */
  125. for (; i < cp + pad; i++)
  126. normalized[i] = '0';
  127. /* round up, down ? */
  128. match_count = (normalized[i-1] >= '5');
  129. /* and drop the decimal part */
  130. normalized[i-1] = 0; /* cp > 0 && pad >= 0 ==> i > 0 */
  131. /* final conversion (and applying rounding) */
  132. errno = 0;
  133. freq = strtoul(normalized, &end, 10);
  134. if (errno)
  135. return 0;
  136. else {
  137. if (match_count && freq != ULONG_MAX)
  138. freq++;
  139. return freq;
  140. }
  141. }
  142. static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
  143. {
  144. struct cpufreq_policy *cur_pol = cpufreq_get_policy(cpu);
  145. int ret;
  146. if (!cur_pol) {
  147. printf(_("wrong, unknown or unhandled CPU?\n"));
  148. return -EINVAL;
  149. }
  150. if (!new_pol->min)
  151. new_pol->min = cur_pol->min;
  152. if (!new_pol->max)
  153. new_pol->max = cur_pol->max;
  154. if (!new_pol->governor)
  155. new_pol->governor = cur_pol->governor;
  156. ret = cpufreq_set_policy(cpu, new_pol);
  157. cpufreq_put_policy(cur_pol);
  158. return ret;
  159. }
  160. static int do_one_cpu(unsigned int cpu, struct cpufreq_policy *new_pol,
  161. unsigned long freq, unsigned int pc)
  162. {
  163. switch (pc) {
  164. case 0:
  165. return cpufreq_set_frequency(cpu, freq);
  166. case 1:
  167. /* if only one value of a policy is to be changed, we can
  168. * use a "fast path".
  169. */
  170. if (new_pol->min)
  171. return cpufreq_modify_policy_min(cpu, new_pol->min);
  172. else if (new_pol->max)
  173. return cpufreq_modify_policy_max(cpu, new_pol->max);
  174. else if (new_pol->governor)
  175. return cpufreq_modify_policy_governor(cpu,
  176. new_pol->governor);
  177. default:
  178. /* slow path */
  179. return do_new_policy(cpu, new_pol);
  180. }
  181. }
  182. int cmd_freq_set(int argc, char **argv)
  183. {
  184. extern char *optarg;
  185. extern int optind, opterr, optopt;
  186. int ret = 0, cont = 1;
  187. int double_parm = 0, related = 0, policychange = 0;
  188. unsigned long freq = 0;
  189. char gov[20];
  190. unsigned int cpu;
  191. struct cpufreq_policy new_pol = {
  192. .min = 0,
  193. .max = 0,
  194. .governor = NULL,
  195. };
  196. /* parameter parsing */
  197. do {
  198. ret = getopt_long(argc, argv, "d:u:g:f:hr", set_opts, NULL);
  199. switch (ret) {
  200. case '?':
  201. print_unknown_arg();
  202. return -EINVAL;
  203. case 'h':
  204. freq_set_help();
  205. return 0;
  206. case -1:
  207. cont = 0;
  208. break;
  209. case 'r':
  210. if (related)
  211. double_parm++;
  212. related++;
  213. break;
  214. case 'd':
  215. if (new_pol.min)
  216. double_parm++;
  217. policychange++;
  218. new_pol.min = string_to_frequency(optarg);
  219. if (new_pol.min == 0) {
  220. print_unknown_arg();
  221. return -EINVAL;
  222. }
  223. break;
  224. case 'u':
  225. if (new_pol.max)
  226. double_parm++;
  227. policychange++;
  228. new_pol.max = string_to_frequency(optarg);
  229. if (new_pol.max == 0) {
  230. print_unknown_arg();
  231. return -EINVAL;
  232. }
  233. break;
  234. case 'f':
  235. if (freq)
  236. double_parm++;
  237. freq = string_to_frequency(optarg);
  238. if (freq == 0) {
  239. print_unknown_arg();
  240. return -EINVAL;
  241. }
  242. break;
  243. case 'g':
  244. if (new_pol.governor)
  245. double_parm++;
  246. policychange++;
  247. if ((strlen(optarg) < 3) || (strlen(optarg) > 18)) {
  248. print_unknown_arg();
  249. return -EINVAL;
  250. }
  251. if ((sscanf(optarg, "%s", gov)) != 1) {
  252. print_unknown_arg();
  253. return -EINVAL;
  254. }
  255. new_pol.governor = gov;
  256. break;
  257. }
  258. } while (cont);
  259. /* parameter checking */
  260. if (double_parm) {
  261. printf("the same parameter was passed more than once\n");
  262. return -EINVAL;
  263. }
  264. if (freq && policychange) {
  265. printf(_("the -f/--freq parameter cannot be combined with -d/--min, -u/--max or\n"
  266. "-g/--governor parameters\n"));
  267. return -EINVAL;
  268. }
  269. if (!freq && !policychange) {
  270. printf(_("At least one parameter out of -f/--freq, -d/--min, -u/--max, and\n"
  271. "-g/--governor must be passed\n"));
  272. return -EINVAL;
  273. }
  274. /* Default is: set all CPUs */
  275. if (bitmask_isallclear(cpus_chosen))
  276. bitmask_setall(cpus_chosen);
  277. /* Also set frequency settings for related CPUs if -r is passed */
  278. if (related) {
  279. for (cpu = bitmask_first(cpus_chosen);
  280. cpu <= bitmask_last(cpus_chosen); cpu++) {
  281. struct cpufreq_affected_cpus *cpus;
  282. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  283. cpufreq_cpu_exists(cpu))
  284. continue;
  285. cpus = cpufreq_get_related_cpus(cpu);
  286. if (!cpus)
  287. break;
  288. while (cpus->next) {
  289. bitmask_setbit(cpus_chosen, cpus->cpu);
  290. cpus = cpus->next;
  291. }
  292. cpufreq_put_related_cpus(cpus);
  293. }
  294. }
  295. /* loop over CPUs */
  296. for (cpu = bitmask_first(cpus_chosen);
  297. cpu <= bitmask_last(cpus_chosen); cpu++) {
  298. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  299. cpufreq_cpu_exists(cpu))
  300. continue;
  301. printf(_("Setting cpu: %d\n"), cpu);
  302. ret = do_one_cpu(cpu, &new_pol, freq, policychange);
  303. if (ret)
  304. break;
  305. }
  306. if (ret)
  307. print_error();
  308. return ret;
  309. }