sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / tools / power / cpupower / utils / cpupower.c
blob9ea91437898598bf31982f6073ec483e3d51f687
1 /*
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.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/utsname.h>
19 #include "builtin.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;
33 int run_as_root;
34 /* Affected cpus chosen by -c/--cpu param */
35 struct bitmask *cpus_chosen;
37 #ifdef DEBUG
38 int be_verbose;
39 #endif
41 static void print_help(void);
43 struct cmd_struct {
44 const char *cmd;
45 int (*main)(int, const char **);
46 int needs_root;
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)
63 unsigned int i;
65 #ifdef DEBUG
66 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
67 #else
68 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
69 #endif
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)
79 int len;
80 char *page;
82 len = 10; /* enough for "cpupower-" */
83 if (subpage != NULL)
84 len += strlen(subpage);
86 page = malloc(len);
87 if (!page)
88 return -ENOMEM;
90 sprintf(page, "cpupower");
91 if ((subpage != NULL) && strcmp(subpage, "help")) {
92 strcat(page, "-");
93 strcat(page, subpage);
96 execlp("man", "man", page, NULL);
98 /* should not be reached */
99 return -EINVAL;
102 static int cmd_help(int argc, const char **argv)
104 if (argc > 1) {
105 print_man_page(argv[1]); /* exits within execlp() */
106 return EXIT_FAILURE;
109 print_help();
110 return EXIT_SUCCESS;
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;
123 if (*argc < 1)
124 return;
126 for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) {
127 const char *param = (*argv)[x];
128 if (!strcmp(param, "-h") || !strcmp(param, "--help")) {
129 print_help();
130 exit(EXIT_SUCCESS);
131 } else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")) {
132 if (*argc < 2) {
133 print_help();
134 exit(EXIT_FAILURE);
136 if (!strcmp((*argv)[x+1], "all"))
137 bitmask_setall(cpus_chosen);
138 else {
139 ret = bitmask_parselist(
140 (*argv)[x+1], cpus_chosen);
141 if (ret < 0) {
142 fprintf(stderr, _("Error parsing cpu "
143 "list\n"));
144 exit(EXIT_FAILURE);
147 x += 1;
148 /* Cut out param: cpupower -c 1 info -> cpupower info */
149 new_argc += 2;
150 continue;
151 } else if (!strcmp(param, "-v") ||
152 !strcmp(param, "--version")) {
153 print_version();
154 exit(EXIT_SUCCESS);
155 #ifdef DEBUG
156 } else if (!strcmp(param, "-d") || !strcmp(param, "--debug")) {
157 be_verbose = 1;
158 new_argc++;
159 continue;
160 #endif
161 } else {
162 fprintf(stderr, "Unknown option: %s\n", param);
163 print_help();
164 exit(EXIT_FAILURE);
167 *argc -= new_argc;
168 *argv += new_argc;
171 int main(int argc, const char *argv[])
173 const char *cmd;
174 unsigned int i, ret;
175 struct stat statbuf;
176 struct utsname uts;
178 cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
180 argc--;
181 argv += 1;
183 handle_options(&argc, &argv);
185 cmd = argv[0];
187 if (argc < 1) {
188 print_help();
189 return EXIT_FAILURE;
192 setlocale(LC_ALL, "");
193 textdomain(PACKAGE);
195 /* Turn "perf cmd --help" into "perf help cmd" */
196 if (argc > 1 && !strcmp(argv[1], "--help")) {
197 argv[1] = argv[0];
198 argv[0] = cmd = "help";
201 get_cpu_info(0, &cpupower_cpu_info);
202 run_as_root = !geteuid();
203 if (run_as_root) {
204 ret = uname(&uts);
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))
216 continue;
217 if (!run_as_root && p->needs_root) {
218 fprintf(stderr, _("Subcommand %s needs root "
219 "privileges\n"), cmd);
220 return EXIT_FAILURE;
222 ret = p->main(argc, argv);
223 if (cpus_chosen)
224 bitmask_free(cpus_chosen);
225 return ret;
227 print_help();
228 return EXIT_FAILURE;