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