2 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
4 * Licensed under the terms of the GNU GPL License version 2.
6 * Ideas taken over from the perf userspace tool (included in the Linus
7 * kernel git repo): subcommand builtins and param parsing.
16 #include <sys/types.h>
18 #include <sys/utsname.h>
21 #include "helpers/helpers.h"
22 #include "helpers/bitmask.h"
24 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
26 static int cmd_help(int argc, const char **argv);
28 /* Global cpu_info object available for all binaries
29 * Info only retrieved from CPU 0
31 * Values will be zero/unknown on non X86 archs
33 struct cpupower_cpu_info cpupower_cpu_info;
36 /* Affected cpus chosen by -c/--cpu param */
37 struct bitmask *cpus_chosen;
43 static void print_help(void);
47 int (*main)(int, const char **);
51 static struct cmd_struct commands[] = {
52 { "frequency-info", cmd_freq_info, 0 },
53 { "frequency-set", cmd_freq_set, 1 },
54 { "idle-info", cmd_idle_info, 0 },
55 { "idle-set", cmd_idle_set, 1 },
56 { "set", cmd_set, 1 },
57 { "info", cmd_info, 0 },
58 { "monitor", cmd_monitor, 0 },
59 { "help", cmd_help, 0 },
60 /* { "bench", cmd_bench, 1 }, */
63 static void print_help(void)
68 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
70 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
72 printf(_("Supported commands are:\n"));
73 for (i = 0; i < ARRAY_SIZE(commands); i++)
74 printf("\t%s\n", commands[i].cmd);
75 printf(_("\nNot all commands can make use of the -c cpulist option.\n"));
76 printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));
79 static int print_man_page(const char *subpage)
84 len = 10; /* enough for "cpupower-" */
86 len += strlen(subpage);
92 sprintf(page, "cpupower");
93 if ((subpage != NULL) && strcmp(subpage, "help")) {
95 strcat(page, subpage);
98 execlp("man", "man", page, NULL);
100 /* should not be reached */
104 static int cmd_help(int argc, const char **argv)
107 print_man_page(argv[1]); /* exits within execlp() */
115 static void print_version(void)
117 printf(PACKAGE " " VERSION "\n");
118 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT);
121 static void handle_options(int *argc, const char ***argv)
123 int ret, x, new_argc = 0;
128 for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) {
129 const char *param = (*argv)[x];
130 if (!strcmp(param, "-h") || !strcmp(param, "--help")) {
133 } else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")) {
138 if (!strcmp((*argv)[x+1], "all"))
139 bitmask_setall(cpus_chosen);
141 ret = bitmask_parselist(
142 (*argv)[x+1], cpus_chosen);
144 fprintf(stderr, _("Error parsing cpu "
150 /* Cut out param: cpupower -c 1 info -> cpupower info */
153 } else if (!strcmp(param, "-v") ||
154 !strcmp(param, "--version")) {
158 } else if (!strcmp(param, "-d") || !strcmp(param, "--debug")) {
164 fprintf(stderr, "Unknown option: %s\n", param);
173 int main(int argc, const char *argv[])
181 cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
186 handle_options(&argc, &argv);
195 setlocale(LC_ALL, "");
198 /* Turn "perf cmd --help" into "perf help cmd" */
199 if (argc > 1 && !strcmp(argv[1], "--help")) {
201 argv[0] = cmd = "help";
204 base_cpu = sched_getcpu();
206 fprintf(stderr, _("No valid cpus found.\n"));
210 get_cpu_info(&cpupower_cpu_info);
211 run_as_root = !geteuid();
214 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
215 if (!ret && !strcmp(uts.machine, "x86_64") &&
216 stat(pathname, &statbuf) != 0) {
217 if (system("modprobe msr") == -1)
218 fprintf(stderr, _("MSR access not available.\n"));
222 for (i = 0; i < ARRAY_SIZE(commands); i++) {
223 struct cmd_struct *p = commands + i;
224 if (strcmp(p->cmd, cmd))
226 if (!run_as_root && p->needs_root) {
227 fprintf(stderr, _("Subcommand %s needs root "
228 "privileges\n"), cmd);
231 ret = p->main(argc, argv);
233 bitmask_free(cpus_chosen);