xref: /linux/tools/power/cpupower/utils/powercap-info.c (revision a5778046a02570fd23bdf0b48dce330237d1996c)
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 Watts\n"),
42 		       pr_prefix);
43 
44 	if (zone->has_energy_uj)
45 		printf(_("%sPower can be monitored in micro Jules\n"),
46 		       pr_prefix);
47 
48 	printf("\n");
49 
50 	return ret;
51 }
52 
53 static int powercap_show(void)
54 {
55 	struct powercap_zone *root_zone;
56 	char line[MAX_LINE_LEN] = "";
57 	int ret, val;
58 
59 	ret = powercap_get_driver(line, MAX_LINE_LEN);
60 	if (ret < 0) {
61 		printf(_("No powercapping driver loaded\n"));
62 		return ret;
63 	}
64 
65 	printf("Driver: %s\n", line);
66 	ret = powercap_get_enabled(&val);
67 	if (ret < 0)
68 		return ret;
69 	if (!val) {
70 		printf(_("Powercapping is disabled\n"));
71 		return -1;
72 	}
73 
74 	printf(_("Powercap domain hierarchy:\n\n"));
75 	root_zone = powercap_init_zones();
76 
77 	if (root_zone == NULL) {
78 		printf(_("No powercap info found\n"));
79 		return 1;
80 	}
81 
82 	powercap_walk_zones(root_zone, powercap_print_one_zone);
83 
84 	return 0;
85 }
86 
87 int cmd_cap_set(int argc, char **argv)
88 {
89 	return 0;
90 };
91 int cmd_cap_info(int argc, char **argv)
92 {
93 	int ret = 0, cont = 1;
94 
95 	do {
96 		ret = getopt_long(argc, argv, "a", info_opts, NULL);
97 		switch (ret) {
98 		case '?':
99 			cont = 0;
100 			break;
101 		case -1:
102 			cont = 0;
103 			break;
104 		case 'a':
105 			powercap_show_all = 1;
106 			break;
107 		default:
108 			fprintf(stderr, _("invalid or unknown argument\n"));
109 			return EXIT_FAILURE;
110 		}
111 	} while (cont);
112 
113 	powercap_show();
114 	return 0;
115 }
116