1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
5 * Ideas taken over from the perf userspace tool (included in the Linus
6 * kernel git repo): subcommand builtins and param parsing.
15 #include <sys/types.h>
17 #include <sys/utsname.h>
20 #include "helpers/helpers.h"
21 #include "helpers/bitmask.h"
23 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
25 static int cmd_help(int argc
, const char **argv
);
27 /* Global cpu_info object available for all binaries
28 * Info only retrieved from CPU 0
30 * Values will be zero/unknown on non X86 archs
32 struct cpupower_cpu_info cpupower_cpu_info
;
35 /* Affected cpus chosen by -c/--cpu param */
36 struct bitmask
*cpus_chosen
;
42 static void print_help(void);
46 int (*main
)(int, const char **);
50 static struct cmd_struct commands
[] = {
51 { "frequency-info", cmd_freq_info
, 0 },
52 { "frequency-set", cmd_freq_set
, 1 },
53 { "idle-info", cmd_idle_info
, 0 },
54 { "idle-set", cmd_idle_set
, 1 },
55 { "set", cmd_set
, 1 },
56 { "info", cmd_info
, 0 },
57 { "monitor", cmd_monitor
, 0 },
58 { "help", cmd_help
, 0 },
59 /* { "bench", cmd_bench, 1 }, */
62 static void print_help(void)
67 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
69 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
71 printf(_("Supported commands are:\n"));
72 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
73 printf("\t%s\n", commands
[i
].cmd
);
74 printf(_("\nNot all commands can make use of the -c cpulist option.\n"));
75 printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));
78 static int print_man_page(const char *subpage
)
83 len
= 10; /* enough for "cpupower-" */
85 len
+= strlen(subpage
);
91 sprintf(page
, "cpupower");
92 if ((subpage
!= NULL
) && strcmp(subpage
, "help")) {
94 strcat(page
, subpage
);
97 execlp("man", "man", page
, NULL
);
99 /* should not be reached */
103 static int cmd_help(int argc
, const char **argv
)
106 print_man_page(argv
[1]); /* exits within execlp() */
114 static void print_version(void)
116 printf(PACKAGE
" " VERSION
"\n");
117 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT
);
120 static void handle_options(int *argc
, const char ***argv
)
122 int ret
, x
, new_argc
= 0;
127 for (x
= 0; x
< *argc
&& ((*argv
)[x
])[0] == '-'; x
++) {
128 const char *param
= (*argv
)[x
];
129 if (!strcmp(param
, "-h") || !strcmp(param
, "--help")) {
132 } else if (!strcmp(param
, "-c") || !strcmp(param
, "--cpu")) {
137 if (!strcmp((*argv
)[x
+1], "all"))
138 bitmask_setall(cpus_chosen
);
140 ret
= bitmask_parselist(
141 (*argv
)[x
+1], cpus_chosen
);
143 fprintf(stderr
, _("Error parsing cpu "
149 /* Cut out param: cpupower -c 1 info -> cpupower info */
152 } else if (!strcmp(param
, "-v") ||
153 !strcmp(param
, "--version")) {
157 } else if (!strcmp(param
, "-d") || !strcmp(param
, "--debug")) {
163 fprintf(stderr
, "Unknown option: %s\n", param
);
172 int main(int argc
, const char *argv
[])
180 cpus_chosen
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
185 handle_options(&argc
, &argv
);
194 setlocale(LC_ALL
, "");
197 /* Turn "perf cmd --help" into "perf help cmd" */
198 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
200 argv
[0] = cmd
= "help";
203 base_cpu
= sched_getcpu();
205 fprintf(stderr
, _("No valid cpus found.\n"));
209 get_cpu_info(&cpupower_cpu_info
);
210 run_as_root
= !geteuid();
213 sprintf(pathname
, "/dev/cpu/%d/msr", base_cpu
);
214 if (!ret
&& !strcmp(uts
.machine
, "x86_64") &&
215 stat(pathname
, &statbuf
) != 0) {
216 if (system("modprobe msr") == -1)
217 fprintf(stderr
, _("MSR access not available.\n"));
221 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
222 struct cmd_struct
*p
= commands
+ i
;
223 if (strcmp(p
->cmd
, cmd
))
225 if (!run_as_root
&& p
->needs_root
) {
226 fprintf(stderr
, _("Subcommand %s needs root "
227 "privileges\n"), cmd
);
230 ret
= p
->main(argc
, argv
);
232 bitmask_free(cpus_chosen
);