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)
100 printf(PACKAGE " " VERSION "\n");
101 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT);
104 static void handle_options(int *argc, const char ***argv)
106 int ret, x, new_argc = 0;
111 for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) {
112 const char *param = (*argv)[x];
113 if (!strcmp(param, "-h") || !strcmp(param, "--help")) {
116 } else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")) {
121 if (!strcmp((*argv)[x+1], "all"))
122 bitmask_setall(cpus_chosen);
124 ret = bitmask_parselist(
125 (*argv)[x+1], cpus_chosen);
127 fprintf(stderr, _("Error parsing cpu "
133 /* Cut out param: cpupower -c 1 info -> cpupower info */
136 } else if (!strcmp(param, "-v") ||
137 !strcmp(param, "--version")) {
141 } else if (!strcmp(param, "-d") || !strcmp(param, "--debug")) {
147 fprintf(stderr, "Unknown option: %s\n", param);
156 int main(int argc, const char *argv[])
161 cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
166 handle_options(&argc, &argv);
175 setlocale(LC_ALL, "");
178 /* Turn "perf cmd --help" into "perf help cmd" */
179 if (argc > 1 && !strcmp(argv[1], "--help")) {
181 argv[0] = cmd = "help";
184 get_cpu_info(0, &cpupower_cpu_info);
185 run_as_root = !getuid();
187 for (i = 0; i < ARRAY_SIZE(commands); i++) {
188 struct cmd_struct *p = commands + i;
189 if (strcmp(p->cmd, cmd))
191 if (!run_as_root && p->needs_root) {
192 fprintf(stderr, _("Subcommand %s needs root "
193 "privileges\n"), cmd);
196 ret = p->main(argc, argv);
198 bitmask_free(cpus_chosen);