PCI: Disable MPS configuration by default
[linux-btrfs-devel.git] / tools / power / cpupower / utils / cpupower.c
blob52bee591c1c565f3bf9760505c7a824122d2537c
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>
16 #include "builtin.h"
17 #include "helpers/helpers.h"
18 #include "helpers/bitmask.h"
20 struct cmd_struct {
21 const char *cmd;
22 int (*main)(int, const char **);
23 int needs_root;
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;
36 int run_as_root;
37 /* Affected cpus chosen by -c/--cpu param */
38 struct bitmask *cpus_chosen;
40 #ifdef DEBUG
41 int be_verbose;
42 #endif
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)
59 unsigned int i;
61 #ifdef DEBUG
62 printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
63 #else
64 printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
65 #endif
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)
75 int len;
76 char *page;
78 len = 10; /* enough for "cpupower-" */
79 if (subpage != NULL)
80 len += strlen(subpage);
82 page = malloc(len);
83 if (!page)
84 return -ENOMEM;
86 sprintf(page, "cpupower");
87 if ((subpage != NULL) && strcmp(subpage, "help")) {
88 strcat(page, "-");
89 strcat(page, subpage);
92 execlp("man", "man", page, NULL);
94 /* should not be reached */
95 return -EINVAL;
98 static int cmd_help(int argc, const char **argv)
100 if (argc > 1) {
101 print_man_page(argv[1]); /* exits within execlp() */
102 return EXIT_FAILURE;
105 print_help();
106 return EXIT_SUCCESS;
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;
119 if (*argc < 1)
120 return;
122 for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) {
123 const char *param = (*argv)[x];
124 if (!strcmp(param, "-h") || !strcmp(param, "--help")) {
125 print_help();
126 exit(EXIT_SUCCESS);
127 } else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")) {
128 if (*argc < 2) {
129 print_help();
130 exit(EXIT_FAILURE);
132 if (!strcmp((*argv)[x+1], "all"))
133 bitmask_setall(cpus_chosen);
134 else {
135 ret = bitmask_parselist(
136 (*argv)[x+1], cpus_chosen);
137 if (ret < 0) {
138 fprintf(stderr, _("Error parsing cpu "
139 "list\n"));
140 exit(EXIT_FAILURE);
143 x += 1;
144 /* Cut out param: cpupower -c 1 info -> cpupower info */
145 new_argc += 2;
146 continue;
147 } else if (!strcmp(param, "-v") ||
148 !strcmp(param, "--version")) {
149 print_version();
150 exit(EXIT_SUCCESS);
151 #ifdef DEBUG
152 } else if (!strcmp(param, "-d") || !strcmp(param, "--debug")) {
153 be_verbose = 1;
154 new_argc++;
155 continue;
156 #endif
157 } else {
158 fprintf(stderr, "Unknown option: %s\n", param);
159 print_help();
160 exit(EXIT_FAILURE);
163 *argc -= new_argc;
164 *argv += new_argc;
167 int main(int argc, const char *argv[])
169 const char *cmd;
170 unsigned int i, ret;
172 cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
174 argc--;
175 argv += 1;
177 handle_options(&argc, &argv);
179 cmd = argv[0];
181 if (argc < 1) {
182 print_help();
183 return EXIT_FAILURE;
186 setlocale(LC_ALL, "");
187 textdomain(PACKAGE);
189 /* Turn "perf cmd --help" into "perf help cmd" */
190 if (argc > 1 && !strcmp(argv[1], "--help")) {
191 argv[1] = argv[0];
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))
201 continue;
202 if (!run_as_root && p->needs_root) {
203 fprintf(stderr, _("Subcommand %s needs root "
204 "privileges\n"), cmd);
205 return EXIT_FAILURE;
207 ret = p->main(argc, argv);
208 if (cpus_chosen)
209 bitmask_free(cpus_chosen);
210 return ret;
212 print_help();
213 return EXIT_FAILURE;