cpupower-set.c 1.9 KB

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