cpupower-set.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. */
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <getopt.h>
  12. #include <cpufreq.h>
  13. #include "helpers/helpers.h"
  14. #include "helpers/sysfs.h"
  15. #include "helpers/bitmask.h"
  16. void set_help(void)
  17. {
  18. printf(_("Usage: cpupower set [ -b val ] [ -m val ] [ -s val ]\n"));
  19. printf(_("Options:\n"));
  20. printf(_(" -b, --perf-bias [VAL] Sets CPU's power vs performance policy on some\n"
  21. " Intel models [0-15], see manpage for details\n"));
  22. printf(_(" -m, --sched-mc [VAL] Sets the kernel's multi core scheduler policy.\n"));
  23. printf(_(" -s, --sched-smt [VAL] Sets the kernel's thread sibling scheduler policy.\n"));
  24. printf(_(" -h, --help Prints out this screen\n"));
  25. printf("\n");
  26. }
  27. static struct option set_opts[] = {
  28. { .name="perf-bias", .has_arg=optional_argument, .flag=NULL, .val='b'},
  29. { .name="sched-mc", .has_arg=optional_argument, .flag=NULL, .val='m'},
  30. { .name="sched-smt", .has_arg=optional_argument, .flag=NULL, .val='s'},
  31. { .name="help", .has_arg=no_argument, .flag=NULL, .val='h'},
  32. { },
  33. };
  34. static void print_wrong_arg_exit(void)
  35. {
  36. printf(_("invalid or unknown argument\n"));
  37. set_help();
  38. exit(EXIT_FAILURE);
  39. }
  40. int cmd_set(int argc, char **argv)
  41. {
  42. extern char *optarg;
  43. extern int optind, opterr, optopt;
  44. unsigned int cpu;
  45. union {
  46. struct {
  47. int sched_mc:1;
  48. int sched_smt:1;
  49. int perf_bias:1;
  50. };
  51. int params;
  52. } params;
  53. int sched_mc = 0, sched_smt = 0, perf_bias = 0;
  54. int ret = 0;
  55. setlocale(LC_ALL, "");
  56. textdomain (PACKAGE);
  57. params.params = 0;
  58. /* parameter parsing */
  59. while ((ret = getopt_long(argc, argv, "m:s:b:h", set_opts, NULL)) != -1) {
  60. switch (ret) {
  61. case 'h':
  62. set_help();
  63. return 0;
  64. case 'b':
  65. if (params.perf_bias)
  66. print_wrong_arg_exit();
  67. perf_bias = atoi(optarg);
  68. if (perf_bias < 0 || perf_bias > 15) {
  69. printf(_("--perf-bias param out "
  70. "of range [0-%d]\n"), 15);
  71. print_wrong_arg_exit();
  72. }
  73. params.perf_bias = 1;
  74. break;
  75. case 'm':
  76. if (params.sched_mc)
  77. print_wrong_arg_exit();
  78. sched_mc = atoi(optarg);
  79. if (sched_mc < 0 || sched_mc > 2) {
  80. printf(_("--sched-mc param out "
  81. "of range [0-%d]\n"), 2);
  82. print_wrong_arg_exit();
  83. }
  84. params.sched_mc = 1;
  85. break;
  86. case 's':
  87. if (params.sched_smt)
  88. print_wrong_arg_exit();
  89. sched_smt = atoi(optarg);
  90. if (sched_smt < 0 || sched_smt > 2) {
  91. printf(_("--sched-smt param out "
  92. "of range [0-%d]\n"), 2);
  93. print_wrong_arg_exit();
  94. }
  95. params.sched_smt = 1;
  96. break;
  97. default:
  98. print_wrong_arg_exit();
  99. }
  100. };
  101. if (!params.params) {
  102. set_help();
  103. return -EINVAL;
  104. }
  105. if (params.sched_mc) {
  106. ret = sysfs_set_sched("mc", sched_mc);
  107. if (ret)
  108. fprintf(stderr, _("Error setting sched-mc %s\n"),
  109. (ret == -ENODEV) ? "not supported" : "");
  110. }
  111. if (params.sched_smt) {
  112. ret = sysfs_set_sched("smt", sched_smt);
  113. if (ret)
  114. fprintf(stderr, _("Error setting sched-smt %s\n"),
  115. (ret == -ENODEV) ? "not supported" : "");
  116. }
  117. /* Default is: set all CPUs */
  118. if (bitmask_isallclear(cpus_chosen))
  119. bitmask_setall(cpus_chosen);
  120. /* loop over CPUs */
  121. for (cpu = bitmask_first(cpus_chosen);
  122. cpu <= bitmask_last(cpus_chosen); cpu++) {
  123. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  124. cpufreq_cpu_exists(cpu))
  125. continue;
  126. if (params.perf_bias) {
  127. ret = msr_intel_set_perf_bias(cpu, perf_bias);
  128. if (ret) {
  129. fprintf(stderr, _("Error setting perf-bias "
  130. "value on CPU %d\n"), cpu);
  131. break;
  132. }
  133. }
  134. }
  135. return ret;
  136. }