Path: blob/master/tools/power/cpupower/utils/cpupower-set.c
26292 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* (C) 2011 Thomas Renninger <[email protected]>, Novell Inc.3*/456#include <unistd.h>7#include <stdio.h>8#include <stdlib.h>9#include <errno.h>10#include <string.h>11#include <getopt.h>12#include <sys/utsname.h>1314#include "helpers/helpers.h"15#include "helpers/sysfs.h"16#include "helpers/bitmask.h"1718static struct option set_opts[] = {19{"perf-bias", required_argument, NULL, 'b'},20{"epp", required_argument, NULL, 'e'},21{"amd-pstate-mode", required_argument, NULL, 'm'},22{"turbo-boost", required_argument, NULL, 't'},23{"boost", required_argument, NULL, 't'},24{ },25};2627static void print_wrong_arg_exit(void)28{29printf(_("invalid or unknown argument\n"));30exit(EXIT_FAILURE);31}3233int cmd_set(int argc, char **argv)34{35extern char *optarg;36extern int optind, opterr, optopt;37unsigned int cpu;38struct utsname uts;3940union {41struct {42int perf_bias:1;43int epp:1;44int mode:1;45int turbo_boost:1;46};47int params;48} params;49int perf_bias = 0, turbo_boost = 1;50int ret = 0;51char epp[30], mode[20];5253ret = uname(&uts);54if (!ret && (!strcmp(uts.machine, "ppc64le") ||55!strcmp(uts.machine, "ppc64"))) {56fprintf(stderr, _("Subcommand not supported on POWER.\n"));57return ret;58}5960setlocale(LC_ALL, "");61textdomain(PACKAGE);6263params.params = 0;64/* parameter parsing */65while ((ret = getopt_long(argc, argv, "b:e:m:t:",66set_opts, NULL)) != -1) {67switch (ret) {68case 'b':69if (params.perf_bias)70print_wrong_arg_exit();71perf_bias = atoi(optarg);72if (perf_bias < 0 || perf_bias > 15) {73printf(_("--perf-bias param out "74"of range [0-%d]\n"), 15);75print_wrong_arg_exit();76}77params.perf_bias = 1;78break;79case 'e':80if (params.epp)81print_wrong_arg_exit();82if (sscanf(optarg, "%29s", epp) != 1) {83print_wrong_arg_exit();84return -EINVAL;85}86params.epp = 1;87break;88case 'm':89if (cpupower_cpu_info.vendor != X86_VENDOR_AMD)90print_wrong_arg_exit();91if (params.mode)92print_wrong_arg_exit();93if (sscanf(optarg, "%19s", mode) != 1) {94print_wrong_arg_exit();95return -EINVAL;96}97params.mode = 1;98break;99case 't':100if (params.turbo_boost)101print_wrong_arg_exit();102turbo_boost = atoi(optarg);103if (turbo_boost < 0 || turbo_boost > 1) {104printf("--turbo-boost param out of range [0-1]\n");105print_wrong_arg_exit();106}107params.turbo_boost = 1;108break;109110111default:112print_wrong_arg_exit();113}114}115116if (!params.params)117print_wrong_arg_exit();118119if (params.mode) {120ret = cpupower_set_amd_pstate_mode(mode);121if (ret)122fprintf(stderr, "Error setting mode\n");123}124125if (params.turbo_boost) {126ret = cpupower_set_turbo_boost(turbo_boost);127if (ret)128fprintf(stderr, "Error setting turbo-boost\n");129}130131/* Default is: set all CPUs */132if (bitmask_isallclear(cpus_chosen))133bitmask_setall(cpus_chosen);134135/* loop over CPUs */136for (cpu = bitmask_first(cpus_chosen);137cpu <= bitmask_last(cpus_chosen); cpu++) {138139if (!bitmask_isbitset(cpus_chosen, cpu))140continue;141142if (sysfs_is_cpu_online(cpu) != 1){143fprintf(stderr, _("Cannot set values on CPU %d:"), cpu);144fprintf(stderr, _(" *is offline\n"));145continue;146}147148if (params.perf_bias) {149ret = cpupower_intel_set_perf_bias(cpu, perf_bias);150if (ret) {151fprintf(stderr, _("Error setting perf-bias "152"value on CPU %d\n"), cpu);153break;154}155}156157if (params.epp) {158ret = cpupower_set_epp(cpu, epp);159if (ret) {160fprintf(stderr,161"Error setting epp value on CPU %d\n", cpu);162break;163}164}165166}167return ret;168}169170171