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
;
37 struct bitmask
*online_cpus
;
38 struct bitmask
*offline_cpus
;
44 static void print_help(void);
48 int (*main
)(int, const char **);
52 static struct cmd_struct commands
[] = {
53 { "frequency-info", cmd_freq_info
, 0 },
54 { "frequency-set", cmd_freq_set
, 1 },
55 { "idle-info", cmd_idle_info
, 0 },
56 { "idle-set", cmd_idle_set
, 1 },
57 { "powercap-info", cmd_cap_info
, 0 },
58 { "set", cmd_set
, 1 },
59 { "info", cmd_info
, 0 },
60 { "monitor", cmd_monitor
, 0 },
61 { "help", cmd_help
, 0 },
62 /* { "bench", cmd_bench, 1 }, */
65 static void print_help(void)
70 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
72 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
74 printf(_("Supported commands are:\n"));
75 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
76 printf("\t%s\n", commands
[i
].cmd
);
77 printf(_("\nNot all commands can make use of the -c cpulist option.\n"));
78 printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));
81 static int print_man_page(const char *subpage
)
86 len
= 10; /* enough for "cpupower-" */
88 len
+= strlen(subpage
);
94 sprintf(page
, "cpupower");
95 if ((subpage
!= NULL
) && strcmp(subpage
, "help")) {
97 strcat(page
, subpage
);
100 execlp("man", "man", page
, NULL
);
102 /* should not be reached */
106 static int cmd_help(int argc
, const char **argv
)
109 print_man_page(argv
[1]); /* exits within execlp() */
117 static void print_version(void)
119 printf(PACKAGE
" " VERSION
"\n");
120 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT
);
123 static void handle_options(int *argc
, const char ***argv
)
125 int ret
, x
, new_argc
= 0;
130 for (x
= 0; x
< *argc
&& ((*argv
)[x
])[0] == '-'; x
++) {
131 const char *param
= (*argv
)[x
];
132 if (!strcmp(param
, "-h") || !strcmp(param
, "--help")) {
135 } else if (!strcmp(param
, "-c") || !strcmp(param
, "--cpu")) {
140 if (!strcmp((*argv
)[x
+1], "all"))
141 bitmask_setall(cpus_chosen
);
143 ret
= bitmask_parselist(
144 (*argv
)[x
+1], cpus_chosen
);
146 fprintf(stderr
, _("Error parsing cpu "
152 /* Cut out param: cpupower -c 1 info -> cpupower info */
155 } else if (!strcmp(param
, "-v") ||
156 !strcmp(param
, "--version")) {
160 } else if (!strcmp(param
, "-d") || !strcmp(param
, "--debug")) {
166 fprintf(stderr
, "Unknown option: %s\n", param
);
175 int main(int argc
, const char *argv
[])
183 cpus_chosen
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
184 online_cpus
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
185 offline_cpus
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
190 handle_options(&argc
, &argv
);
199 setlocale(LC_ALL
, "");
202 /* Turn "perf cmd --help" into "perf help cmd" */
203 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
205 argv
[0] = cmd
= "help";
208 base_cpu
= sched_getcpu();
210 fprintf(stderr
, _("No valid cpus found.\n"));
214 get_cpu_info(&cpupower_cpu_info
);
215 run_as_root
= !geteuid();
218 sprintf(pathname
, "/dev/cpu/%d/msr", base_cpu
);
219 if (!ret
&& !strcmp(uts
.machine
, "x86_64") &&
220 stat(pathname
, &statbuf
) != 0) {
221 if (system("modprobe msr") == -1)
222 fprintf(stderr
, _("MSR access not available.\n"));
226 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
227 struct cmd_struct
*p
= commands
+ i
;
228 if (strcmp(p
->cmd
, cmd
))
230 if (!run_as_root
&& p
->needs_root
) {
231 fprintf(stderr
, _("Subcommand %s needs root "
232 "privileges\n"), cmd
);
235 ret
= p
->main(argc
, argv
);
237 bitmask_free(cpus_chosen
);
239 bitmask_free(online_cpus
);
241 bitmask_free(offline_cpus
);