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 "helpers/helpers.h"
17 #include "helpers/bitmask.h"
21 int (*main)(int, const char **);
26 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
28 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, freq_info_help, 0 },
48 { "frequency-set", cmd_freq_set, freq_set_help, 1 },
49 { "idle-info", cmd_idle_info, idle_info_help, 0 },
50 { "set", cmd_set, set_help, 1 },
51 { "info", cmd_info, info_help, 0 },
52 { "monitor", cmd_monitor, monitor_help, 0 },
53 { "help", cmd_help, print_help, 0 },
54 // { "bench", cmd_bench, NULL, 1 },
57 int cmd_help(int argc, const char **argv)
62 for (i = 0; i < ARRAY_SIZE(commands); i++) {
63 struct cmd_struct *p = commands + i;
64 if (strcmp(p->cmd, argv[1]))
74 return EXIT_SUCCESS; /* cpupower help */
78 static void print_help(void)
83 printf(_("cpupower [ -d ][ -c cpulist ] subcommand [ARGS]\n"));
84 printf(_(" -d, --debug May increase output (stderr) on some subcommands\n"));
86 printf(_("cpupower [ -c cpulist ] subcommand [ARGS]\n"));
88 printf(_("cpupower --version\n"));
89 printf(_("Supported subcommands are:\n"));
90 for (i = 0; i < ARRAY_SIZE(commands); i++)
91 printf("\t%s\n", commands[i].cmd);
92 printf(_("\nSome subcommands can make use of the -c cpulist option.\n"));
93 printf(_("Look at the general cpupower manpage how to use it\n"));
94 printf(_("and read up the subcommand's manpage whether it is supported.\n"));
95 printf(_("\nUse cpupower help subcommand for getting help for above subcommands.\n"));
98 static void print_version(void) {
99 printf(PACKAGE " " VERSION "\n");
100 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT);
103 static void handle_options(int *argc, const char ***argv)
105 int ret, x, new_argc = 0;
110 for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) {
111 const char *param = (*argv)[x];
112 if (!strcmp(param, "-h") || !strcmp(param, "--help")){
115 } else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")){
120 if (!strcmp((*argv)[x+1], "all"))
121 bitmask_setall(cpus_chosen);
123 ret = bitmask_parselist(
124 (*argv)[x+1], cpus_chosen);
126 fprintf(stderr, _("Error parsing cpu "
132 /* Cut out param: cpupower -c 1 info -> cpupower info */
135 } else if (!strcmp(param, "-v") || !strcmp(param, "--version")){
139 } else if (!strcmp(param, "-d") || !strcmp(param, "--debug")){
145 fprintf(stderr, "Unknown option: %s\n", param);
154 int main(int argc, const char *argv[])
159 cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
164 handle_options(&argc, &argv);
173 setlocale(LC_ALL, "");
174 textdomain (PACKAGE);
176 /* Turn "perf cmd --help" into "perf help cmd" */
177 if (argc > 1 && !strcmp(argv[1], "--help")) {
179 argv[0] = cmd = "help";
182 get_cpu_info(0, &cpupower_cpu_info);
183 run_as_root = !getuid();
185 for (i = 0; i < ARRAY_SIZE(commands); i++) {
186 struct cmd_struct *p = commands + i;
187 if (strcmp(p->cmd, cmd))
189 if (!run_as_root && p->needs_root) {
190 fprintf(stderr, _("Subcommand %s needs root "
191 "privileges\n"), cmd);
194 ret = p->main(argc, argv);
196 bitmask_free(cpus_chosen);