Linux-libre 4.14.44-gnu
[librecmc/linux-libre.git] / tools / power / cpupower / utils / cpupower-info.c
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 "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         extern char *optarg;
32         extern int optind, opterr, optopt;
33         unsigned int cpu;
34
35         union {
36                 struct {
37                         int perf_bias:1;
38                 };
39                 int params;
40         } params = {};
41         int ret = 0;
42
43         setlocale(LC_ALL, "");
44         textdomain(PACKAGE);
45
46         /* parameter parsing */
47         while ((ret = getopt_long(argc, argv, "b", set_opts, NULL)) != -1) {
48                 switch (ret) {
49                 case 'b':
50                         if (params.perf_bias)
51                                 print_wrong_arg_exit();
52                         params.perf_bias = 1;
53                         break;
54                 default:
55                         print_wrong_arg_exit();
56                 }
57         };
58
59         if (!params.params)
60                 params.params = 0x7;
61
62         /* Default is: show output of CPU 0 only */
63         if (bitmask_isallclear(cpus_chosen))
64                 bitmask_setbit(cpus_chosen, 0);
65
66         /* Add more per cpu options here */
67         if (!params.perf_bias)
68                 return ret;
69
70         if (params.perf_bias) {
71                 if (!run_as_root) {
72                         params.perf_bias = 0;
73                         printf(_("Intel's performance bias setting needs root privileges\n"));
74                 } else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
75                         printf(_("System does not support Intel's performance"
76                                  " bias setting\n"));
77                         params.perf_bias = 0;
78                 }
79         }
80
81         /* loop over CPUs */
82         for (cpu = bitmask_first(cpus_chosen);
83              cpu <= bitmask_last(cpus_chosen); cpu++) {
84
85                 if (!bitmask_isbitset(cpus_chosen, cpu))
86                         continue;
87
88                 printf(_("analyzing CPU %d:\n"), cpu);
89
90                 if (sysfs_is_cpu_online(cpu) != 1){
91                         printf(_(" *is offline\n"));
92                         continue;
93                 }
94
95                 if (params.perf_bias) {
96                         ret = msr_intel_get_perf_bias(cpu);
97                         if (ret < 0) {
98                                 fprintf(stderr,
99                         _("Could not read perf-bias value[%d]\n"), ret);
100                                 exit(EXIT_FAILURE);
101                         } else
102                                 printf(_("perf-bias: %d\n"), ret);
103                 }
104         }
105         return 0;
106 }