cpupower-set.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. static struct option set_opts[] = {
  17. { .name = "perf-bias", .has_arg = required_argument, .flag = NULL, .val = 'b'},
  18. { .name = "sched-mc", .has_arg = required_argument, .flag = NULL, .val = 'm'},
  19. { .name = "sched-smt", .has_arg = required_argument, .flag = NULL, .val = 's'},
  20. { },
  21. };
  22. static void print_wrong_arg_exit(void)
  23. {
  24. printf(_("invalid or unknown argument\n"));
  25. exit(EXIT_FAILURE);
  26. }
  27. int cmd_set(int argc, char **argv)
  28. {
  29. extern char *optarg;
  30. extern int optind, opterr, optopt;
  31. unsigned int cpu;
  32. union {
  33. struct {
  34. int sched_mc:1;
  35. int sched_smt:1;
  36. int perf_bias:1;
  37. };
  38. int params;
  39. } params;
  40. int sched_mc = 0, sched_smt = 0, perf_bias = 0;
  41. int ret = 0;
  42. setlocale(LC_ALL, "");
  43. textdomain(PACKAGE);
  44. params.params = 0;
  45. /* parameter parsing */
  46. while ((ret = getopt_long(argc, argv, "m:s:b:",
  47. set_opts, NULL)) != -1) {
  48. switch (ret) {
  49. case 'b':
  50. if (params.perf_bias)
  51. print_wrong_arg_exit();
  52. perf_bias = atoi(optarg);
  53. if (perf_bias < 0 || perf_bias > 15) {
  54. printf(_("--perf-bias param out "
  55. "of range [0-%d]\n"), 15);
  56. print_wrong_arg_exit();
  57. }
  58. params.perf_bias = 1;
  59. break;
  60. case 'm':
  61. if (params.sched_mc)
  62. print_wrong_arg_exit();
  63. sched_mc = atoi(optarg);
  64. if (sched_mc < 0 || sched_mc > 2) {
  65. printf(_("--sched-mc param out "
  66. "of range [0-%d]\n"), 2);
  67. print_wrong_arg_exit();
  68. }
  69. params.sched_mc = 1;
  70. break;
  71. case 's':
  72. if (params.sched_smt)
  73. print_wrong_arg_exit();
  74. sched_smt = atoi(optarg);
  75. if (sched_smt < 0 || sched_smt > 2) {
  76. printf(_("--sched-smt param out "
  77. "of range [0-%d]\n"), 2);
  78. print_wrong_arg_exit();
  79. }
  80. params.sched_smt = 1;
  81. break;
  82. default:
  83. print_wrong_arg_exit();
  84. }
  85. };
  86. if (!params.params)
  87. print_wrong_arg_exit();
  88. if (params.sched_mc) {
  89. ret = sysfs_set_sched("mc", sched_mc);
  90. if (ret)
  91. fprintf(stderr, _("Error setting sched-mc %s\n"),
  92. (ret == -ENODEV) ? "not supported" : "");
  93. }
  94. if (params.sched_smt) {
  95. ret = sysfs_set_sched("smt", sched_smt);
  96. if (ret)
  97. fprintf(stderr, _("Error setting sched-smt %s\n"),
  98. (ret == -ENODEV) ? "not supported" : "");
  99. }
  100. /* Default is: set all CPUs */
  101. if (bitmask_isallclear(cpus_chosen))
  102. bitmask_setall(cpus_chosen);
  103. /* loop over CPUs */
  104. for (cpu = bitmask_first(cpus_chosen);
  105. cpu <= bitmask_last(cpus_chosen); cpu++) {
  106. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  107. cpufreq_cpu_exists(cpu))
  108. continue;
  109. if (params.perf_bias) {
  110. ret = msr_intel_set_perf_bias(cpu, perf_bias);
  111. if (ret) {
  112. fprintf(stderr, _("Error setting perf-bias "
  113. "value on CPU %d\n"), cpu);
  114. break;
  115. }
  116. }
  117. }
  118. return ret;
  119. }