blob: 3cd95c6cb9747eb86123eb90aaa3948281d9f021 [file] [log] [blame]
Thomas Gleixner4f190482019-05-27 08:55:14 +02001// SPDX-License-Identifier: GPL-2.0-only
Dominik Brodowski7fe2f632011-03-30 16:30:11 +02002/*
3 * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
Dominik Brodowski7fe2f632011-03-30 16:30:11 +02004 */
5
6
7#include <unistd.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <errno.h>
11#include <string.h>
12#include <getopt.h>
13
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020014#include "helpers/helpers.h"
15#include "helpers/sysfs.h"
16#include "helpers/bitmask.h"
17
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020018static struct option set_opts[] = {
Sriram Raghunathan57ab3b082015-10-23 09:52:45 +020019 {"perf-bias", required_argument, NULL, 'b'},
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020020 { },
21};
22
23static void print_wrong_arg_exit(void)
24{
25 printf(_("invalid or unknown argument\n"));
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020026 exit(EXIT_FAILURE);
27}
28
29int cmd_set(int argc, char **argv)
30{
31 extern char *optarg;
32 extern int optind, opterr, optopt;
33 unsigned int cpu;
34
35 union {
36 struct {
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020037 int perf_bias:1;
38 };
39 int params;
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020040 } params;
Thomas Renninger7ea1bdb2014-05-13 12:41:45 +020041 int perf_bias = 0;
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020042 int ret = 0;
43
44 setlocale(LC_ALL, "");
Dominik Brodowskia1ce5ba2011-04-19 20:33:50 +020045 textdomain(PACKAGE);
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020046
47 params.params = 0;
48 /* parameter parsing */
Thomas Renninger7ea1bdb2014-05-13 12:41:45 +020049 while ((ret = getopt_long(argc, argv, "b:",
Dominik Brodowskia1ce5ba2011-04-19 20:33:50 +020050 set_opts, NULL)) != -1) {
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020051 switch (ret) {
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020052 case 'b':
53 if (params.perf_bias)
54 print_wrong_arg_exit();
55 perf_bias = atoi(optarg);
56 if (perf_bias < 0 || perf_bias > 15) {
57 printf(_("--perf-bias param out "
58 "of range [0-%d]\n"), 15);
59 print_wrong_arg_exit();
60 }
61 params.perf_bias = 1;
62 break;
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020063 default:
64 print_wrong_arg_exit();
65 }
66 };
67
Dominik Brodowski498ca792011-08-06 18:11:43 +020068 if (!params.params)
69 print_wrong_arg_exit();
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020070
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020071 /* Default is: set all CPUs */
72 if (bitmask_isallclear(cpus_chosen))
73 bitmask_setall(cpus_chosen);
74
75 /* loop over CPUs */
76 for (cpu = bitmask_first(cpus_chosen);
77 cpu <= bitmask_last(cpus_chosen); cpu++) {
Dominik Brodowskia1ce5ba2011-04-19 20:33:50 +020078
Thomas Renningerce512b82015-12-01 17:14:15 +010079 if (!bitmask_isbitset(cpus_chosen, cpu))
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020080 continue;
81
Thomas Renningerce512b82015-12-01 17:14:15 +010082 if (sysfs_is_cpu_online(cpu) != 1){
83 fprintf(stderr, _("Cannot set values on CPU %d:"), cpu);
84 fprintf(stderr, _(" *is offline\n"));
85 continue;
86 }
87
Dominik Brodowski7fe2f632011-03-30 16:30:11 +020088 if (params.perf_bias) {
89 ret = msr_intel_set_perf_bias(cpu, perf_bias);
90 if (ret) {
91 fprintf(stderr, _("Error setting perf-bias "
92 "value on CPU %d\n"), cpu);
93 break;
94 }
95 }
96 }
97 return ret;
98}