cpupower-set.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. {"perf-bias", required_argument, NULL, 'b'},
  18. { },
  19. };
  20. static void print_wrong_arg_exit(void)
  21. {
  22. printf(_("invalid or unknown argument\n"));
  23. exit(EXIT_FAILURE);
  24. }
  25. int cmd_set(int argc, char **argv)
  26. {
  27. extern char *optarg;
  28. extern int optind, opterr, optopt;
  29. unsigned int cpu;
  30. union {
  31. struct {
  32. int perf_bias:1;
  33. };
  34. int params;
  35. } params;
  36. int perf_bias = 0;
  37. int ret = 0;
  38. setlocale(LC_ALL, "");
  39. textdomain(PACKAGE);
  40. params.params = 0;
  41. /* parameter parsing */
  42. while ((ret = getopt_long(argc, argv, "b:",
  43. set_opts, NULL)) != -1) {
  44. switch (ret) {
  45. case 'b':
  46. if (params.perf_bias)
  47. print_wrong_arg_exit();
  48. perf_bias = atoi(optarg);
  49. if (perf_bias < 0 || perf_bias > 15) {
  50. printf(_("--perf-bias param out "
  51. "of range [0-%d]\n"), 15);
  52. print_wrong_arg_exit();
  53. }
  54. params.perf_bias = 1;
  55. break;
  56. default:
  57. print_wrong_arg_exit();
  58. }
  59. };
  60. if (!params.params)
  61. print_wrong_arg_exit();
  62. /* Default is: set all CPUs */
  63. if (bitmask_isallclear(cpus_chosen))
  64. bitmask_setall(cpus_chosen);
  65. /* loop over CPUs */
  66. for (cpu = bitmask_first(cpus_chosen);
  67. cpu <= bitmask_last(cpus_chosen); cpu++) {
  68. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  69. cpufreq_cpu_exists(cpu))
  70. continue;
  71. if (params.perf_bias) {
  72. ret = msr_intel_set_perf_bias(cpu, perf_bias);
  73. if (ret) {
  74. fprintf(stderr, _("Error setting perf-bias "
  75. "value on CPU %d\n"), cpu);
  76. break;
  77. }
  78. }
  79. }
  80. return ret;
  81. }