cpupower-set.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <getopt.h>
  11. #include "helpers/helpers.h"
  12. #include "helpers/sysfs.h"
  13. #include "helpers/bitmask.h"
  14. static struct option set_opts[] = {
  15. {"perf-bias", required_argument, NULL, 'b'},
  16. { },
  17. };
  18. static void print_wrong_arg_exit(void)
  19. {
  20. printf(_("invalid or unknown argument\n"));
  21. exit(EXIT_FAILURE);
  22. }
  23. int cmd_set(int argc, char **argv)
  24. {
  25. extern char *optarg;
  26. extern int optind, opterr, optopt;
  27. unsigned int cpu;
  28. union {
  29. struct {
  30. int perf_bias:1;
  31. };
  32. int params;
  33. } params;
  34. int perf_bias = 0;
  35. int ret = 0;
  36. setlocale(LC_ALL, "");
  37. textdomain(PACKAGE);
  38. params.params = 0;
  39. /* parameter parsing */
  40. while ((ret = getopt_long(argc, argv, "b:",
  41. set_opts, NULL)) != -1) {
  42. switch (ret) {
  43. case 'b':
  44. if (params.perf_bias)
  45. print_wrong_arg_exit();
  46. perf_bias = atoi(optarg);
  47. if (perf_bias < 0 || perf_bias > 15) {
  48. printf(_("--perf-bias param out "
  49. "of range [0-%d]\n"), 15);
  50. print_wrong_arg_exit();
  51. }
  52. params.perf_bias = 1;
  53. break;
  54. default:
  55. print_wrong_arg_exit();
  56. }
  57. };
  58. if (!params.params)
  59. print_wrong_arg_exit();
  60. /* Default is: set all CPUs */
  61. if (bitmask_isallclear(cpus_chosen))
  62. bitmask_setall(cpus_chosen);
  63. /* loop over CPUs */
  64. for (cpu = bitmask_first(cpus_chosen);
  65. cpu <= bitmask_last(cpus_chosen); cpu++) {
  66. if (!bitmask_isbitset(cpus_chosen, cpu))
  67. continue;
  68. if (sysfs_is_cpu_online(cpu) != 1){
  69. fprintf(stderr, _("Cannot set values on CPU %d:"), cpu);
  70. fprintf(stderr, _(" *is offline\n"));
  71. continue;
  72. }
  73. if (params.perf_bias) {
  74. ret = msr_intel_set_perf_bias(cpu, perf_bias);
  75. if (ret) {
  76. fprintf(stderr, _("Error setting perf-bias "
  77. "value on CPU %d\n"), cpu);
  78. break;
  79. }
  80. }
  81. }
  82. return ret;
  83. }