Path: blob/master/tools/power/cpupower/utils/cpupower.c
26292 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* (C) 2010,2011 Thomas Renninger <[email protected]>, Novell Inc.3*4* Ideas taken over from the perf userspace tool (included in the Linus5* kernel git repo): subcommand builtins and param parsing.6*/78#include <stdio.h>9#include <stdlib.h>10#include <string.h>11#include <unistd.h>12#include <errno.h>13#include <sched.h>14#include <sys/types.h>15#include <sys/stat.h>16#include <sys/utsname.h>1718#include "builtin.h"19#include "helpers/helpers.h"20#include "helpers/bitmask.h"2122#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))2324static int cmd_help(int argc, const char **argv);2526/* Global cpu_info object available for all binaries27* Info only retrieved from CPU 028*29* Values will be zero/unknown on non X86 archs30*/31struct cpupower_cpu_info cpupower_cpu_info;32int run_as_root;33int base_cpu;34/* Affected cpus chosen by -c/--cpu param */35struct bitmask *cpus_chosen;36struct bitmask *online_cpus;37struct bitmask *offline_cpus;3839#ifdef DEBUG40int be_verbose;41#endif4243static void print_help(void);4445struct cmd_struct {46const char *cmd;47int (*main)(int, const char **);48int needs_root;49};5051static struct cmd_struct commands[] = {52{ "frequency-info", cmd_freq_info, 0 },53{ "frequency-set", cmd_freq_set, 1 },54{ "idle-info", cmd_idle_info, 0 },55{ "idle-set", cmd_idle_set, 1 },56{ "powercap-info", cmd_cap_info, 0 },57{ "set", cmd_set, 1 },58{ "info", cmd_info, 0 },59{ "monitor", cmd_monitor, 0 },60{ "help", cmd_help, 0 },61/* { "bench", cmd_bench, 1 }, */62};6364static void print_help(void)65{66unsigned int i;6768#ifdef DEBUG69printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));70#else71printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));72#endif73printf(_("Supported commands are:\n"));74for (i = 0; i < ARRAY_SIZE(commands); i++)75printf("\t%s\n", commands[i].cmd);76printf(_("\nNot all commands can make use of the -c cpulist option.\n"));77printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));78}7980static int print_man_page(const char *subpage)81{82int len;83char *page;8485len = 10; /* enough for "cpupower-" */86if (subpage != NULL)87len += strlen(subpage);8889page = malloc(len);90if (!page)91return -ENOMEM;9293sprintf(page, "cpupower");94if ((subpage != NULL) && strcmp(subpage, "help")) {95strcat(page, "-");96strcat(page, subpage);97}9899execlp("man", "man", page, NULL);100101/* should not be reached */102return -EINVAL;103}104105static int cmd_help(int argc, const char **argv)106{107if (argc > 1) {108print_man_page(argv[1]); /* exits within execlp() */109return EXIT_FAILURE;110}111112print_help();113return EXIT_SUCCESS;114}115116static void print_version(void)117{118printf(PACKAGE " " VERSION "\n");119printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT);120}121122static void handle_options(int *argc, const char ***argv)123{124int ret, x, new_argc = 0;125126if (*argc < 1)127return;128129for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) {130const char *param = (*argv)[x];131if (!strcmp(param, "-h") || !strcmp(param, "--help")) {132print_help();133exit(EXIT_SUCCESS);134} else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")) {135if (*argc < 2) {136print_help();137exit(EXIT_FAILURE);138}139if (!strcmp((*argv)[x+1], "all"))140bitmask_setall(cpus_chosen);141else {142ret = bitmask_parselist(143(*argv)[x+1], cpus_chosen);144if (ret < 0) {145fprintf(stderr, _("Error parsing cpu "146"list\n"));147exit(EXIT_FAILURE);148}149}150x += 1;151/* Cut out param: cpupower -c 1 info -> cpupower info */152new_argc += 2;153continue;154} else if (!strcmp(param, "-v") ||155!strcmp(param, "--version")) {156print_version();157exit(EXIT_SUCCESS);158#ifdef DEBUG159} else if (!strcmp(param, "-d") || !strcmp(param, "--debug")) {160be_verbose = 1;161new_argc++;162continue;163#endif164} else {165fprintf(stderr, "Unknown option: %s\n", param);166print_help();167exit(EXIT_FAILURE);168}169}170*argc -= new_argc;171*argv += new_argc;172}173174int main(int argc, const char *argv[])175{176const char *cmd;177unsigned int i, ret;178struct stat statbuf;179struct utsname uts;180char pathname[32];181182cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));183online_cpus = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));184offline_cpus = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));185186argc--;187argv += 1;188189handle_options(&argc, &argv);190191cmd = argv[0];192193if (argc < 1) {194print_help();195return EXIT_FAILURE;196}197198setlocale(LC_ALL, "");199textdomain(PACKAGE);200201/* Turn "perf cmd --help" into "perf help cmd" */202if (argc > 1 && !strcmp(argv[1], "--help")) {203argv[1] = argv[0];204argv[0] = cmd = "help";205}206207base_cpu = sched_getcpu();208if (base_cpu < 0) {209fprintf(stderr, _("No valid cpus found.\n"));210return EXIT_FAILURE;211}212213get_cpu_info(&cpupower_cpu_info);214run_as_root = !geteuid();215if (run_as_root) {216ret = uname(&uts);217sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);218if (!ret && !strcmp(uts.machine, "x86_64") &&219stat(pathname, &statbuf) != 0) {220if (system("modprobe msr") == -1)221fprintf(stderr, _("MSR access not available.\n"));222}223}224225for (i = 0; i < ARRAY_SIZE(commands); i++) {226struct cmd_struct *p = commands + i;227if (strcmp(p->cmd, cmd))228continue;229if (!run_as_root && p->needs_root) {230fprintf(stderr, _("Subcommand %s needs root "231"privileges\n"), cmd);232return EXIT_FAILURE;233}234ret = p->main(argc, argv);235if (cpus_chosen)236bitmask_free(cpus_chosen);237if (online_cpus)238bitmask_free(online_cpus);239if (offline_cpus)240bitmask_free(offline_cpus);241return ret;242}243print_help();244return EXIT_FAILURE;245}246247248