xref: /linux/tools/power/cpupower/utils/cpupower-info.c (revision ceb1c532ba6220900e61ec7073a9234661efa450)
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 	{ .name = "sched-mc",	.has_arg = optional_argument,	.flag = NULL,	.val = 'm'},
22 	{ .name = "sched-smt",	.has_arg = optional_argument,	.flag = NULL,	.val = 's'},
23 	{ },
24 };
25 
26 static void print_wrong_arg_exit(void)
27 {
28 	printf(_("invalid or unknown argument\n"));
29 	exit(EXIT_FAILURE);
30 }
31 
32 int cmd_info(int argc, char **argv)
33 {
34 	extern char *optarg;
35 	extern int optind, opterr, optopt;
36 	unsigned int cpu;
37 
38 	union {
39 		struct {
40 			int sched_mc:1;
41 			int sched_smt:1;
42 			int perf_bias:1;
43 		};
44 		int params;
45 	} params = {};
46 	int ret = 0;
47 
48 	setlocale(LC_ALL, "");
49 	textdomain(PACKAGE);
50 
51 	/* parameter parsing */
52 	while ((ret = getopt_long(argc, argv, "msb", set_opts, NULL)) != -1) {
53 		switch (ret) {
54 		case 'b':
55 			if (params.perf_bias)
56 				print_wrong_arg_exit();
57 			params.perf_bias = 1;
58 			break;
59 		case 'm':
60 			if (params.sched_mc)
61 				print_wrong_arg_exit();
62 			params.sched_mc = 1;
63 			break;
64 		case 's':
65 			if (params.sched_smt)
66 				print_wrong_arg_exit();
67 			params.sched_smt = 1;
68 			break;
69 		default:
70 			print_wrong_arg_exit();
71 		}
72 	};
73 
74 	if (!params.params)
75 		params.params = 0x7;
76 
77 	/* Default is: show output of CPU 0 only */
78 	if (bitmask_isallclear(cpus_chosen))
79 		bitmask_setbit(cpus_chosen, 0);
80 
81 	if (params.sched_mc) {
82 		ret = sysfs_get_sched("mc");
83 		printf(_("System's multi core scheduler setting: "));
84 		if (ret < 0)
85 			/* if sysfs file is missing it's: errno == ENOENT */
86 			printf(_("not supported\n"));
87 		else
88 			printf("%d\n", ret);
89 	}
90 	if (params.sched_smt) {
91 		ret = sysfs_get_sched("smt");
92 		printf(_("System's thread sibling scheduler setting: "));
93 		if (ret < 0)
94 			/* if sysfs file is missing it's: errno == ENOENT */
95 			printf(_("not supported\n"));
96 		else
97 			printf("%d\n", ret);
98 	}
99 
100 	/* Add more per cpu options here */
101 	if (!params.perf_bias)
102 		return ret;
103 
104 	if (params.perf_bias) {
105 		if (!run_as_root) {
106 			params.perf_bias = 0;
107 			printf(_("Intel's performance bias setting needs root privileges\n"));
108 		} else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
109 			printf(_("System does not support Intel's performance"
110 				 " bias setting\n"));
111 			params.perf_bias = 0;
112 		}
113 	}
114 
115 	/* loop over CPUs */
116 	for (cpu = bitmask_first(cpus_chosen);
117 	     cpu <= bitmask_last(cpus_chosen); cpu++) {
118 
119 		if (!bitmask_isbitset(cpus_chosen, cpu) ||
120 		    cpufreq_cpu_exists(cpu))
121 			continue;
122 
123 		printf(_("analyzing CPU %d:\n"), cpu);
124 
125 		if (params.perf_bias) {
126 			ret = msr_intel_get_perf_bias(cpu);
127 			if (ret < 0) {
128 				printf(_("Could not read perf-bias value\n"));
129 				break;
130 			} else
131 				printf(_("perf-bias: %d\n"), ret);
132 		}
133 	}
134 	return ret;
135 }
136