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.
17 #include "helpers/helpers.h"
18 #include "helpers/bitmask.h"
22 int (*main
)(int, const char **);
26 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
28 static int cmd_help(int argc
, const char **argv
);
30 /* Global cpu_info object available for all binaries
31 * Info only retrieved from CPU 0
33 * Values will be zero/unknown on non X86 archs
35 struct cpupower_cpu_info cpupower_cpu_info
;
37 /* Affected cpus chosen by -c/--cpu param */
38 struct bitmask
*cpus_chosen
;
44 static void print_help(void);
46 static struct cmd_struct commands
[] = {
47 { "frequency-info", cmd_freq_info
, 0 },
48 { "frequency-set", cmd_freq_set
, 1 },
49 { "idle-info", cmd_idle_info
, 0 },
50 { "set", cmd_set
, 1 },
51 { "info", cmd_info
, 0 },
52 { "monitor", cmd_monitor
, 0 },
53 { "help", cmd_help
, 0 },
54 /* { "bench", cmd_bench, 1 }, */
57 static void print_help(void)
62 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
64 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
66 printf(_("Supported commands are:\n"));
67 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
68 printf("\t%s\n", commands
[i
].cmd
);
69 printf(_("\nNot all commands can make use of the -c cpulist option.\n"));
70 printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));
73 static int print_man_page(const char *subpage
)
78 len
= 10; /* enough for "cpupower-" */
80 len
+= strlen(subpage
);
86 sprintf(page
, "cpupower");
87 if ((subpage
!= NULL
) && strcmp(subpage
, "help")) {
89 strcat(page
, subpage
);
92 execlp("man", "man", page
, NULL
);
94 /* should not be reached */
98 static int cmd_help(int argc
, const char **argv
)
101 print_man_page(argv
[1]); /* exits within execlp() */
109 static void print_version(void)
111 printf(PACKAGE
" " VERSION
"\n");
112 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT
);
115 static void handle_options(int *argc
, const char ***argv
)
117 int ret
, x
, new_argc
= 0;
122 for (x
= 0; x
< *argc
&& ((*argv
)[x
])[0] == '-'; x
++) {
123 const char *param
= (*argv
)[x
];
124 if (!strcmp(param
, "-h") || !strcmp(param
, "--help")) {
127 } else if (!strcmp(param
, "-c") || !strcmp(param
, "--cpu")) {
132 if (!strcmp((*argv
)[x
+1], "all"))
133 bitmask_setall(cpus_chosen
);
135 ret
= bitmask_parselist(
136 (*argv
)[x
+1], cpus_chosen
);
138 fprintf(stderr
, _("Error parsing cpu "
144 /* Cut out param: cpupower -c 1 info -> cpupower info */
147 } else if (!strcmp(param
, "-v") ||
148 !strcmp(param
, "--version")) {
152 } else if (!strcmp(param
, "-d") || !strcmp(param
, "--debug")) {
158 fprintf(stderr
, "Unknown option: %s\n", param
);
167 int main(int argc
, const char *argv
[])
172 cpus_chosen
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
177 handle_options(&argc
, &argv
);
186 setlocale(LC_ALL
, "");
189 /* Turn "perf cmd --help" into "perf help cmd" */
190 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
192 argv
[0] = cmd
= "help";
195 get_cpu_info(0, &cpupower_cpu_info
);
196 run_as_root
= !getuid();
198 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
199 struct cmd_struct
*p
= commands
+ i
;
200 if (strcmp(p
->cmd
, cmd
))
202 if (!run_as_root
&& p
->needs_root
) {
203 fprintf(stderr
, _("Subcommand %s needs root "
204 "privileges\n"), cmd
);
207 ret
= p
->main(argc
, argv
);
209 bitmask_free(cpus_chosen
);