^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "helpers/helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "helpers/sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static struct option set_opts[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {"perf-bias", optional_argument, NULL, 'b'},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void print_wrong_arg_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) printf(_("invalid or unknown argument\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int cmd_info(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) extern char *optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) extern int optind, opterr, optopt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct utsname uts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int perf_bias:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } params = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ret = uname(&uts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!ret && (!strcmp(uts.machine, "ppc64le") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) !strcmp(uts.machine, "ppc64"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) fprintf(stderr, _("Subcommand not supported on POWER.\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) setlocale(LC_ALL, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) textdomain(PACKAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* parameter parsing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) while ((ret = getopt_long(argc, argv, "b", set_opts, NULL)) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) case 'b':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (params.perf_bias)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) print_wrong_arg_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) params.perf_bias = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) print_wrong_arg_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!params.params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) params.params = 0x7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Default is: show output of CPU 0 only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (bitmask_isallclear(cpus_chosen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) bitmask_setbit(cpus_chosen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* Add more per cpu options here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!params.perf_bias)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (params.perf_bias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!run_as_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) params.perf_bias = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) printf(_("Intel's performance bias setting needs root privileges\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) } else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) printf(_("System does not support Intel's performance"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) " bias setting\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) params.perf_bias = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* loop over CPUs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) for (cpu = bitmask_first(cpus_chosen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) cpu <= bitmask_last(cpus_chosen); cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!bitmask_isbitset(cpus_chosen, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) printf(_("analyzing CPU %d:\n"), cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (sysfs_is_cpu_online(cpu) != 1){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) printf(_(" *is offline\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (params.perf_bias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = msr_intel_get_perf_bias(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) _("Could not read perf-bias value[%d]\n"), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) printf(_("perf-bias: %d\n"), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }