cpufreq-set.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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, new_pol->governor);
  176. default:
  177. /* slow path */
  178. return do_new_policy(cpu, new_pol);
  179. }
  180. }
  181. int cmd_freq_set(int argc, char **argv)
  182. {
  183. extern char *optarg;
  184. extern int optind, opterr, optopt;
  185. int ret = 0, cont = 1;
  186. int double_parm = 0, related = 0, policychange = 0;
  187. unsigned long freq = 0;
  188. char gov[20];
  189. unsigned int cpu;
  190. struct cpufreq_policy new_pol = {
  191. .min = 0,
  192. .max = 0,
  193. .governor = NULL,
  194. };
  195. /* parameter parsing */
  196. do {
  197. ret = getopt_long(argc, argv, "d:u:g:f:hr", set_opts, NULL);
  198. switch (ret) {
  199. case '?':
  200. print_unknown_arg();
  201. return -EINVAL;
  202. case 'h':
  203. freq_set_help();
  204. return 0;
  205. case -1:
  206. cont = 0;
  207. break;
  208. case 'r':
  209. if (related)
  210. double_parm++;
  211. related++;
  212. break;
  213. case 'd':
  214. if (new_pol.min)
  215. double_parm++;
  216. policychange++;
  217. new_pol.min = string_to_frequency(optarg);
  218. if (new_pol.min == 0) {
  219. print_unknown_arg();
  220. return -EINVAL;
  221. }
  222. break;
  223. case 'u':
  224. if (new_pol.max)
  225. double_parm++;
  226. policychange++;
  227. new_pol.max = string_to_frequency(optarg);
  228. if (new_pol.max == 0) {
  229. print_unknown_arg();
  230. return -EINVAL;
  231. }
  232. break;
  233. case 'f':
  234. if (freq)
  235. double_parm++;
  236. freq = string_to_frequency(optarg);
  237. if (freq == 0) {
  238. print_unknown_arg();
  239. return -EINVAL;
  240. }
  241. break;
  242. case 'g':
  243. if (new_pol.governor)
  244. double_parm++;
  245. policychange++;
  246. if ((strlen(optarg) < 3) || (strlen(optarg) > 18)) {
  247. print_unknown_arg();
  248. return -EINVAL;
  249. }
  250. if ((sscanf(optarg, "%s", gov)) != 1) {
  251. print_unknown_arg();
  252. return -EINVAL;
  253. }
  254. new_pol.governor = gov;
  255. break;
  256. }
  257. } while(cont);
  258. /* parameter checking */
  259. if (double_parm) {
  260. printf("the same parameter was passed more than once\n");
  261. return -EINVAL;
  262. }
  263. if (freq && policychange) {
  264. printf(_("the -f/--freq parameter cannot be combined with -d/--min, -u/--max or\n"
  265. "-g/--governor parameters\n"));
  266. return -EINVAL;
  267. }
  268. if (!freq && !policychange) {
  269. printf(_("At least one parameter out of -f/--freq, -d/--min, -u/--max, and\n"
  270. "-g/--governor must be passed\n"));
  271. return -EINVAL;
  272. }
  273. /* Default is: set all CPUs */
  274. if (bitmask_isallclear(cpus_chosen))
  275. bitmask_setall(cpus_chosen);
  276. /* Also set frequency settings for related CPUs if -r is passed */
  277. if (related) {
  278. for (cpu = bitmask_first(cpus_chosen);
  279. cpu <= bitmask_last(cpus_chosen); cpu++) {
  280. struct cpufreq_affected_cpus *cpus;
  281. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  282. cpufreq_cpu_exists(cpu))
  283. continue;
  284. cpus = cpufreq_get_related_cpus(cpu);
  285. if (!cpus)
  286. break;
  287. while (cpus->next) {
  288. bitmask_setbit(cpus_chosen, cpus->cpu);
  289. cpus = cpus->next;
  290. }
  291. cpufreq_put_related_cpus(cpus);
  292. }
  293. }
  294. /* loop over CPUs */
  295. for (cpu = bitmask_first(cpus_chosen);
  296. cpu <= bitmask_last(cpus_chosen); cpu++) {
  297. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  298. cpufreq_cpu_exists(cpu))
  299. continue;
  300. printf(_("Setting cpu: %d\n"), cpu);
  301. ret = do_one_cpu(cpu, &new_pol, freq, policychange);
  302. if (ret)
  303. break;
  304. }
  305. if (ret)
  306. print_error();
  307. return ret;
  308. }