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 { "set", cmd_set
, 1 },
58 { "info", cmd_info
, 0 },
59 { "monitor", cmd_monitor
, 0 },
60 { "help", cmd_help
, 0 },
61 /* { "bench", cmd_bench, 1 }, */
64 static void print_help(void)
69 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
71 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
73 printf(_("Supported commands are:\n"));
74 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
75 printf("\t%s\n", commands
[i
].cmd
);
76 printf(_("\nNot all commands can make use of the -c cpulist option.\n"));
77 printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));
80 static int print_man_page(const char *subpage
)
85 len
= 10; /* enough for "cpupower-" */
87 len
+= strlen(subpage
);
93 sprintf(page
, "cpupower");
94 if ((subpage
!= NULL
) && strcmp(subpage
, "help")) {
96 strcat(page
, subpage
);
99 execlp("man", "man", page
, NULL
);
101 /* should not be reached */
105 static int cmd_help(int argc
, const char **argv
)
108 print_man_page(argv
[1]); /* exits within execlp() */
116 static void print_version(void)
118 printf(PACKAGE
" " VERSION
"\n");
119 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT
);
122 static void handle_options(int *argc
, const char ***argv
)
124 int ret
, x
, new_argc
= 0;
129 for (x
= 0; x
< *argc
&& ((*argv
)[x
])[0] == '-'; x
++) {
130 const char *param
= (*argv
)[x
];
131 if (!strcmp(param
, "-h") || !strcmp(param
, "--help")) {
134 } else if (!strcmp(param
, "-c") || !strcmp(param
, "--cpu")) {
139 if (!strcmp((*argv
)[x
+1], "all"))
140 bitmask_setall(cpus_chosen
);
142 ret
= bitmask_parselist(
143 (*argv
)[x
+1], cpus_chosen
);
145 fprintf(stderr
, _("Error parsing cpu "
151 /* Cut out param: cpupower -c 1 info -> cpupower info */
154 } else if (!strcmp(param
, "-v") ||
155 !strcmp(param
, "--version")) {
159 } else if (!strcmp(param
, "-d") || !strcmp(param
, "--debug")) {
165 fprintf(stderr
, "Unknown option: %s\n", param
);
174 int main(int argc
, const char *argv
[])
182 cpus_chosen
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
183 online_cpus
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
184 offline_cpus
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
189 handle_options(&argc
, &argv
);
198 setlocale(LC_ALL
, "");
201 /* Turn "perf cmd --help" into "perf help cmd" */
202 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
204 argv
[0] = cmd
= "help";
207 base_cpu
= sched_getcpu();
209 fprintf(stderr
, _("No valid cpus found.\n"));
213 get_cpu_info(&cpupower_cpu_info
);
214 run_as_root
= !geteuid();
217 sprintf(pathname
, "/dev/cpu/%d/msr", base_cpu
);
218 if (!ret
&& !strcmp(uts
.machine
, "x86_64") &&
219 stat(pathname
, &statbuf
) != 0) {
220 if (system("modprobe msr") == -1)
221 fprintf(stderr
, _("MSR access not available.\n"));
225 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
226 struct cmd_struct
*p
= commands
+ i
;
227 if (strcmp(p
->cmd
, cmd
))
229 if (!run_as_root
&& p
->needs_root
) {
230 fprintf(stderr
, _("Subcommand %s needs root "
231 "privileges\n"), cmd
);
234 ret
= p
->main(argc
, argv
);
236 bitmask_free(cpus_chosen
);
238 bitmask_free(online_cpus
);
240 bitmask_free(offline_cpus
);