Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 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 | |
| 7 | |
| 8 | #include <unistd.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <errno.h> |
| 12 | #include <string.h> |
| 13 | #include <getopt.h> |
| 14 | |
| 15 | #include <cpufreq.h> |
| 16 | #include "helpers/helpers.h" |
| 17 | #include "helpers/sysfs.h" |
| 18 | #include "helpers/bitmask.h" |
| 19 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 20 | static struct option set_opts[] = { |
Josh Boyer | f447ef4 | 2013-10-11 08:45:51 -0400 | [diff] [blame] | 21 | { .name = "perf-bias", .has_arg = required_argument, .flag = NULL, .val = 'b'}, |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 22 | { }, |
| 23 | }; |
| 24 | |
| 25 | static void print_wrong_arg_exit(void) |
| 26 | { |
| 27 | printf(_("invalid or unknown argument\n")); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 28 | exit(EXIT_FAILURE); |
| 29 | } |
| 30 | |
| 31 | int cmd_set(int argc, char **argv) |
| 32 | { |
| 33 | extern char *optarg; |
| 34 | extern int optind, opterr, optopt; |
| 35 | unsigned int cpu; |
| 36 | |
| 37 | union { |
| 38 | struct { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 39 | int perf_bias:1; |
| 40 | }; |
| 41 | int params; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 42 | } params; |
Thomas Renninger | 7ea1bdb | 2014-05-13 12:41:45 +0200 | [diff] [blame] | 43 | int perf_bias = 0; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 44 | int ret = 0; |
| 45 | |
| 46 | setlocale(LC_ALL, ""); |
Dominik Brodowski | a1ce5ba | 2011-04-19 20:33:50 +0200 | [diff] [blame] | 47 | textdomain(PACKAGE); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 48 | |
| 49 | params.params = 0; |
| 50 | /* parameter parsing */ |
Thomas Renninger | 7ea1bdb | 2014-05-13 12:41:45 +0200 | [diff] [blame] | 51 | while ((ret = getopt_long(argc, argv, "b:", |
Dominik Brodowski | a1ce5ba | 2011-04-19 20:33:50 +0200 | [diff] [blame] | 52 | set_opts, NULL)) != -1) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 53 | switch (ret) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 54 | case 'b': |
| 55 | if (params.perf_bias) |
| 56 | print_wrong_arg_exit(); |
| 57 | perf_bias = atoi(optarg); |
| 58 | if (perf_bias < 0 || perf_bias > 15) { |
| 59 | printf(_("--perf-bias param out " |
| 60 | "of range [0-%d]\n"), 15); |
| 61 | print_wrong_arg_exit(); |
| 62 | } |
| 63 | params.perf_bias = 1; |
| 64 | break; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 65 | default: |
| 66 | print_wrong_arg_exit(); |
| 67 | } |
| 68 | }; |
| 69 | |
Dominik Brodowski | 498ca79 | 2011-08-06 18:11:43 +0200 | [diff] [blame] | 70 | if (!params.params) |
| 71 | print_wrong_arg_exit(); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 72 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 73 | /* Default is: set all CPUs */ |
| 74 | if (bitmask_isallclear(cpus_chosen)) |
| 75 | bitmask_setall(cpus_chosen); |
| 76 | |
| 77 | /* loop over CPUs */ |
| 78 | for (cpu = bitmask_first(cpus_chosen); |
| 79 | cpu <= bitmask_last(cpus_chosen); cpu++) { |
Dominik Brodowski | a1ce5ba | 2011-04-19 20:33:50 +0200 | [diff] [blame] | 80 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 81 | if (!bitmask_isbitset(cpus_chosen, cpu) || |
| 82 | cpufreq_cpu_exists(cpu)) |
| 83 | continue; |
| 84 | |
| 85 | if (params.perf_bias) { |
| 86 | ret = msr_intel_set_perf_bias(cpu, perf_bias); |
| 87 | if (ret) { |
| 88 | fprintf(stderr, _("Error setting perf-bias " |
| 89 | "value on CPU %d\n"), cpu); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return ret; |
| 95 | } |