xref: /linux/tools/power/cpupower/utils/powercap-info.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  (C) 2016 SUSE Software Solutions GmbH
4  *           Thomas Renninger <trenn@suse.de>
5  */
6 
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <string.h>
13 
14 #include <getopt.h>
15 
16 #include "powercap.h"
17 #include "helpers/helpers.h"
18 
19 int powercap_show_all;
20 
21 static struct option info_opts[] = {
22 	{ "all",		no_argument,		 NULL,	 'a'},
23 	{ },
24 };
25 
26 static int powercap_print_one_zone(struct powercap_zone *zone)
27 {
28 	int mode, i, ret = 0;
29 	char pr_prefix[1024] = "";
30 
31 	for (i = 0; i < zone->tree_depth && i < POWERCAP_MAX_TREE_DEPTH; i++)
32 		strcat(pr_prefix, "\t");
33 
34 	printf("%sZone: %s", pr_prefix, zone->name);
35 	ret = powercap_zone_get_enabled(zone, &mode);
36 	if (ret < 0)
37 		return ret;
38 	printf(" (%s)\n", mode ? "enabled" : "disabled");
39 
40 	if (zone->has_power_uw)
41 		printf(_("%sPower can be monitored in micro Jules\n"),
42 		       pr_prefix);
43 
44 	if (zone->has_energy_uj)
45 		printf(_("%sPower can be monitored in micro Watts\n"),
46 		       pr_prefix);
47 
48 	printf("\n");
49 
50 	if (ret != 0)
51 		return ret;
52 	return ret;
53 }
54 
55 static int powercap_show(void)
56 {
57 	struct powercap_zone *root_zone;
58 	char line[MAX_LINE_LEN] = "";
59 	int ret, val;
60 
61 	ret = powercap_get_driver(line, MAX_LINE_LEN);
62 	if (ret < 0) {
63 		printf(_("No powercapping driver loaded\n"));
64 		return ret;
65 	}
66 
67 	printf("Driver: %s\n", line);
68 	ret = powercap_get_enabled(&val);
69 	if (ret < 0)
70 		return ret;
71 	if (!val) {
72 		printf(_("Powercapping is disabled\n"));
73 		return -1;
74 	}
75 
76 	printf(_("Powercap domain hierarchy:\n\n"));
77 	root_zone = powercap_init_zones();
78 
79 	if (root_zone == NULL) {
80 		printf(_("No powercap info found\n"));
81 		return 1;
82 	}
83 
84 	powercap_walk_zones(root_zone, powercap_print_one_zone);
85 
86 	return 0;
87 }
88 
89 int cmd_cap_set(int argc, char **argv)
90 {
91 	return 0;
92 };
93 int cmd_cap_info(int argc, char **argv)
94 {
95 	int ret = 0, cont = 1;
96 
97 	do {
98 		ret = getopt_long(argc, argv, "a", info_opts, NULL);
99 		switch (ret) {
100 		case '?':
101 			cont = 0;
102 			break;
103 		case -1:
104 			cont = 0;
105 			break;
106 		case 'a':
107 			powercap_show_all = 1;
108 			break;
109 		default:
110 			fprintf(stderr, _("invalid or unknown argument\n"));
111 			return EXIT_FAILURE;
112 		}
113 	} while (cont);
114 
115 	powercap_show();
116 	return 0;
117 }
118