Blame view
tools/power/cpupower/utils/cpupower.c
4.62 KB
7fe2f6399 cpupowerutils - c... |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc. * * Licensed under the terms of the GNU GPL License version 2. * * Ideas taken over from the perf userspace tool (included in the Linus * kernel git repo): subcommand builtins and param parsing. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> |
498ca793d cpupower: use man... |
14 |
#include <errno.h> |
7fe2f6399 cpupowerutils - c... |
15 16 17 18 19 20 21 22 |
#include "builtin.h" #include "helpers/helpers.h" #include "helpers/bitmask.h" struct cmd_struct { const char *cmd; int (*main)(int, const char **); |
7fe2f6399 cpupowerutils - c... |
23 24 25 26 |
int needs_root; }; #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
498ca793d cpupower: use man... |
27 |
static int cmd_help(int argc, const char **argv); |
7fe2f6399 cpupowerutils - c... |
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* Global cpu_info object available for all binaries * Info only retrieved from CPU 0 * * Values will be zero/unknown on non X86 archs */ struct cpupower_cpu_info cpupower_cpu_info; int run_as_root; /* Affected cpus chosen by -c/--cpu param */ struct bitmask *cpus_chosen; #ifdef DEBUG int be_verbose; #endif static void print_help(void); static struct cmd_struct commands[] = { |
498ca793d cpupower: use man... |
46 47 48 49 50 51 52 53 |
{ "frequency-info", cmd_freq_info, 0 }, { "frequency-set", cmd_freq_set, 1 }, { "idle-info", cmd_idle_info, 0 }, { "set", cmd_set, 1 }, { "info", cmd_info, 0 }, { "monitor", cmd_monitor, 0 }, { "help", cmd_help, 0 }, /* { "bench", cmd_bench, 1 }, */ |
7fe2f6399 cpupowerutils - c... |
54 |
}; |
7fe2f6399 cpupowerutils - c... |
55 56 57 58 59 |
static void print_help(void) { unsigned int i; #ifdef DEBUG |
498ca793d cpupower: use man... |
60 61 |
printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>] ")); |
7fe2f6399 cpupowerutils - c... |
62 |
#else |
498ca793d cpupower: use man... |
63 64 |
printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>] ")); |
7fe2f6399 cpupowerutils - c... |
65 |
#endif |
498ca793d cpupower: use man... |
66 67 |
printf(_("Supported commands are: ")); |
7fe2f6399 cpupowerutils - c... |
68 69 70 |
for (i = 0; i < ARRAY_SIZE(commands); i++) printf("\t%s ", commands[i].cmd); |
498ca793d cpupower: use man... |
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
printf(_(" Not all commands can make use of the -c cpulist option. ")); printf(_(" Use 'cpupower help <command>' for getting help for above commands. ")); } static int print_man_page(const char *subpage) { int len; char *page; len = 10; /* enough for "cpupower-" */ if (subpage != NULL) len += strlen(subpage); page = malloc(len); if (!page) return -ENOMEM; sprintf(page, "cpupower"); if ((subpage != NULL) && strcmp(subpage, "help")) { strcat(page, "-"); strcat(page, subpage); } execlp("man", "man", page, NULL); /* should not be reached */ return -EINVAL; } static int cmd_help(int argc, const char **argv) { if (argc > 1) { print_man_page(argv[1]); /* exits within execlp() */ return EXIT_FAILURE; } print_help(); return EXIT_SUCCESS; |
7fe2f6399 cpupowerutils - c... |
113 |
} |
a1ce5ba2b cpupowerutils: ut... |
114 115 116 117 |
static void print_version(void) { printf(PACKAGE " " VERSION " "); |
7fe2f6399 cpupowerutils - c... |
118 119 120 121 122 123 124 125 126 127 128 129 130 |
printf(_("Report errors and bugs to %s, please. "), PACKAGE_BUGREPORT); } static void handle_options(int *argc, const char ***argv) { int ret, x, new_argc = 0; if (*argc < 1) return; for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) { const char *param = (*argv)[x]; |
a1ce5ba2b cpupowerutils: ut... |
131 |
if (!strcmp(param, "-h") || !strcmp(param, "--help")) { |
7fe2f6399 cpupowerutils - c... |
132 133 |
print_help(); exit(EXIT_SUCCESS); |
a1ce5ba2b cpupowerutils: ut... |
134 |
} else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")) { |
7fe2f6399 cpupowerutils - c... |
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
if (*argc < 2) { print_help(); exit(EXIT_FAILURE); } if (!strcmp((*argv)[x+1], "all")) bitmask_setall(cpus_chosen); else { ret = bitmask_parselist( (*argv)[x+1], cpus_chosen); if (ret < 0) { fprintf(stderr, _("Error parsing cpu " "list ")); exit(EXIT_FAILURE); } } x += 1; /* Cut out param: cpupower -c 1 info -> cpupower info */ new_argc += 2; continue; |
a1ce5ba2b cpupowerutils: ut... |
155 156 |
} else if (!strcmp(param, "-v") || !strcmp(param, "--version")) { |
7fe2f6399 cpupowerutils - c... |
157 158 159 |
print_version(); exit(EXIT_SUCCESS); #ifdef DEBUG |
a1ce5ba2b cpupowerutils: ut... |
160 |
} else if (!strcmp(param, "-d") || !strcmp(param, "--debug")) { |
7fe2f6399 cpupowerutils - c... |
161 |
be_verbose = 1; |
a1ce5ba2b cpupowerutils: ut... |
162 |
new_argc++; |
7fe2f6399 cpupowerutils - c... |
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
continue; #endif } else { fprintf(stderr, "Unknown option: %s ", param); print_help(); exit(EXIT_FAILURE); } } *argc -= new_argc; *argv += new_argc; } int main(int argc, const char *argv[]) { const char *cmd; unsigned int i, ret; cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF)); argc--; argv += 1; handle_options(&argc, &argv); cmd = argv[0]; if (argc < 1) { print_help(); return EXIT_FAILURE; } setlocale(LC_ALL, ""); |
a1ce5ba2b cpupowerutils: ut... |
196 |
textdomain(PACKAGE); |
7fe2f6399 cpupowerutils - c... |
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
/* Turn "perf cmd --help" into "perf help cmd" */ if (argc > 1 && !strcmp(argv[1], "--help")) { argv[1] = argv[0]; argv[0] = cmd = "help"; } get_cpu_info(0, &cpupower_cpu_info); run_as_root = !getuid(); for (i = 0; i < ARRAY_SIZE(commands); i++) { struct cmd_struct *p = commands + i; if (strcmp(p->cmd, cmd)) continue; if (!run_as_root && p->needs_root) { fprintf(stderr, _("Subcommand %s needs root " "privileges "), cmd); return EXIT_FAILURE; } ret = p->main(argc, argv); if (cpus_chosen) bitmask_free(cpus_chosen); return ret; } print_help(); return EXIT_FAILURE; } |