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