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",
  60. set_opts, NULL)) != -1) {
  61. switch (ret) {
  62. case 'h':
  63. set_help();
  64. return 0;
  65. case 'b':
  66. if (params.perf_bias)
  67. print_wrong_arg_exit();
  68. perf_bias = atoi(optarg);
  69. if (perf_bias < 0 || perf_bias > 15) {
  70. printf(_("--perf-bias param out "
  71. "of range [0-%d]\n"), 15);
  72. print_wrong_arg_exit();
  73. }
  74. params.perf_bias = 1;
  75. break;
  76. case 'm':
  77. if (params.sched_mc)
  78. print_wrong_arg_exit();
  79. sched_mc = atoi(optarg);
  80. if (sched_mc < 0 || sched_mc > 2) {
  81. printf(_("--sched-mc param out "
  82. "of range [0-%d]\n"), 2);
  83. print_wrong_arg_exit();
  84. }
  85. params.sched_mc = 1;
  86. break;
  87. case 's':
  88. if (params.sched_smt)
  89. print_wrong_arg_exit();
  90. sched_smt = atoi(optarg);
  91. if (sched_smt < 0 || sched_smt > 2) {
  92. printf(_("--sched-smt param out "
  93. "of range [0-%d]\n"), 2);
  94. print_wrong_arg_exit();
  95. }
  96. params.sched_smt = 1;
  97. break;
  98. default:
  99. print_wrong_arg_exit();
  100. }
  101. };
  102. if (!params.params) {
  103. set_help();
  104. return -EINVAL;
  105. }
  106. if (params.sched_mc) {
  107. ret = sysfs_set_sched("mc", sched_mc);
  108. if (ret)
  109. fprintf(stderr, _("Error setting sched-mc %s\n"),
  110. (ret == -ENODEV) ? "not supported" : "");
  111. }
  112. if (params.sched_smt) {
  113. ret = sysfs_set_sched("smt", sched_smt);
  114. if (ret)
  115. fprintf(stderr, _("Error setting sched-smt %s\n"),
  116. (ret == -ENODEV) ? "not supported" : "");
  117. }
  118. /* Default is: set all CPUs */
  119. if (bitmask_isallclear(cpus_chosen))
  120. bitmask_setall(cpus_chosen);
  121. /* loop over CPUs */
  122. for (cpu = bitmask_first(cpus_chosen);
  123. cpu <= bitmask_last(cpus_chosen); cpu++) {
  124. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  125. cpufreq_cpu_exists(cpu))
  126. continue;
  127. if (params.perf_bias) {
  128. ret = msr_intel_set_perf_bias(cpu, perf_bias);
  129. if (ret) {
  130. fprintf(stderr, _("Error setting perf-bias "
  131. "value on CPU %d\n"), cpu);
  132. break;
  133. }
  134. }
  135. }
  136. return ret;
  137. }