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.
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
;
34 /* Affected cpus chosen by -c/--cpu param */
35 struct bitmask
*cpus_chosen
;
41 static void print_help(void);
45 int (*main
)(int, const char **);
49 static struct cmd_struct commands
[] = {
50 { "frequency-info", cmd_freq_info
, 0 },
51 { "frequency-set", cmd_freq_set
, 1 },
52 { "idle-info", cmd_idle_info
, 0 },
53 { "idle-set", cmd_idle_set
, 1 },
54 { "set", cmd_set
, 1 },
55 { "info", cmd_info
, 0 },
56 { "monitor", cmd_monitor
, 0 },
57 { "help", cmd_help
, 0 },
58 /* { "bench", cmd_bench, 1 }, */
61 static void print_help(void)
66 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
68 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
70 printf(_("Supported commands are:\n"));
71 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
72 printf("\t%s\n", commands
[i
].cmd
);
73 printf(_("\nNot all commands can make use of the -c cpulist option.\n"));
74 printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));
77 static int print_man_page(const char *subpage
)
82 len
= 10; /* enough for "cpupower-" */
84 len
+= strlen(subpage
);
90 sprintf(page
, "cpupower");
91 if ((subpage
!= NULL
) && strcmp(subpage
, "help")) {
93 strcat(page
, subpage
);
96 execlp("man", "man", page
, NULL
);
98 /* should not be reached */
102 static int cmd_help(int argc
, const char **argv
)
105 print_man_page(argv
[1]); /* exits within execlp() */
113 static void print_version(void)
115 printf(PACKAGE
" " VERSION
"\n");
116 printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT
);
119 static void handle_options(int *argc
, const char ***argv
)
121 int ret
, x
, new_argc
= 0;
126 for (x
= 0; x
< *argc
&& ((*argv
)[x
])[0] == '-'; x
++) {
127 const char *param
= (*argv
)[x
];
128 if (!strcmp(param
, "-h") || !strcmp(param
, "--help")) {
131 } else if (!strcmp(param
, "-c") || !strcmp(param
, "--cpu")) {
136 if (!strcmp((*argv
)[x
+1], "all"))
137 bitmask_setall(cpus_chosen
);
139 ret
= bitmask_parselist(
140 (*argv
)[x
+1], cpus_chosen
);
142 fprintf(stderr
, _("Error parsing cpu "
148 /* Cut out param: cpupower -c 1 info -> cpupower info */
151 } else if (!strcmp(param
, "-v") ||
152 !strcmp(param
, "--version")) {
156 } else if (!strcmp(param
, "-d") || !strcmp(param
, "--debug")) {
162 fprintf(stderr
, "Unknown option: %s\n", param
);
171 int main(int argc
, const char *argv
[])
178 cpus_chosen
= bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF
));
183 handle_options(&argc
, &argv
);
192 setlocale(LC_ALL
, "");
195 /* Turn "perf cmd --help" into "perf help cmd" */
196 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
198 argv
[0] = cmd
= "help";
201 get_cpu_info(0, &cpupower_cpu_info
);
202 run_as_root
= !geteuid();
205 if (!ret
&& !strcmp(uts
.machine
, "x86_64") &&
206 stat("/dev/cpu/0/msr", &statbuf
) != 0) {
207 if (system("modprobe msr") == -1)
208 fprintf(stderr
, _("MSR access not available.\n"));
213 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
214 struct cmd_struct
*p
= commands
+ i
;
215 if (strcmp(p
->cmd
, cmd
))
217 if (!run_as_root
&& p
->needs_root
) {
218 fprintf(stderr
, _("Subcommand %s needs root "
219 "privileges\n"), cmd
);
222 ret
= p
->main(argc
, argv
);
224 bitmask_free(cpus_chosen
);