blob: 87e60bc76ce91abf9b677a1fedd8be72f6a7f244 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
2 * Copyright (C) 2010, Linaro
3 * Copyright (C) 2010, IBM Corporation
4 *
5 * This file is part of PowerDebug.
6 *
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * http://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors:
13 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
14 * - initial API and implementation
15 *******************************************************************************/
16
Amit Arorae9e16b02010-08-03 10:15:20 +053017#include <getopt.h>
Amit Arorae9e16b02010-08-03 10:15:20 +053018#include "powerdebug.h"
19
Amit Arorae9e16b02010-08-03 10:15:20 +053020int numregulators;
Amit Arora47fd9182010-08-24 13:26:06 +053021int dump;
Amit Arora728e0c92010-09-14 12:06:09 +053022int highlighted_row;
23double ticktime = 10.0; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053024
25int init_regulator_ds(void)
26{
27 DIR *regdir;
28 struct dirent *item;
29
30 regdir = opendir("/sys/class/regulator");
31 if (!regdir)
32 return(1);
33 while((item = readdir(regdir))) {
34 if (strncmp(item->d_name, "regulator", 9))
35 continue;
36
37 numregulators++;
38 }
39 closedir(regdir);
40
41 regulators_info = (struct regulator_info *)malloc(numregulators*
42 sizeof(struct regulator_info));
43 if (!regulators_info) {
Amit Arora24ed7d12010-09-14 12:12:58 +053044 fprintf(stderr, "init_regulator_ds: Not enough memory to "
45 "read information for %d regulators!\n", numregulators);
Amit Arorae9e16b02010-08-03 10:15:20 +053046 return(1);
47 }
48
49 return(0);
50}
51
52int read_and_print_sensor_info(int verbose)
53{
54 DIR *dir, *subdir;
Amit Arora1c037892010-08-05 13:46:10 +053055 int len, found = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +053056 char filename[PATH_MAX], devpath[PATH_MAX];
57 char device[PATH_MAX];
58 struct dirent *item, *subitem;
59
60/*
61 1. readdir /sys/class/hwmon
62 2. foreach subdir in above, do following
63 a) check if its virtual or physical (check for device)
64 b) for each *type* (in, temp, pwm, fan, curr, power, energy)
65 i> search for "_" (strrchr... check for last)
66 ii> check the *item* depending on type
67 i.e for "in" type items=min, max, input, label
68 for "fan" type items=min,max,input,div,target,label
69 for "pwm" type items=enable, mode, freq
70 for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
71 for "curr" type items=max,min,input
72 for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
73 for "energy" type items=input
74 for "intrusion" type items=alarm,beep
75*/
76
77 sprintf(filename, "%s", "/sys/class/hwmon");
78
79 dir = opendir(filename);
80 if (!dir)
81 return errno;
82
83 while ((item = readdir(dir))) {
84 if (item->d_name[0] == '.') /* skip the hidden files */
85 continue;
86
Amit Arora1c037892010-08-05 13:46:10 +053087 found = 1;
88
Amit Arorae9e16b02010-08-03 10:15:20 +053089 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
90 sprintf(devpath, "%s/device", filename);
91
92 len = readlink(devpath, device, PATH_MAX - 1);
93
94 if (len < 0)
95 strcpy(devpath, filename);
96 else
97 device[len] = '\0';
98
99 subdir = opendir(devpath);
100
101 printf("\nSensor Information for %s :\n", item->d_name);
102 fflush(stdin);
103 while ((subitem = readdir(subdir))) {
104 if (subitem->d_name[0] == '.') /* skip hidden files */
105 continue;
106
107 if(!strncmp(subitem->d_name, "in", 2))
108 get_sensor_info(devpath, subitem->d_name, "in",
109 verbose);
110 else if (!strncmp(subitem->d_name, "temp", 4))
111 get_sensor_info(devpath, subitem->d_name,
112 "temp", verbose);
113 else if (!strncmp(subitem->d_name, "fan", 4))
114 get_sensor_info(devpath, subitem->d_name,
115 "fan", verbose);
116 else if (!strncmp(subitem->d_name, "pwm", 4))
117 get_sensor_info(devpath, subitem->d_name,
118 "pwm", verbose);
119
120 }
121
122 closedir(subdir);
123 }
124 closedir(dir);
125
Amit Arora1c037892010-08-05 13:46:10 +0530126 if(!found && verbose) {
127 printf("Could not find sensor information!");
128 printf(" Looks like /sys/class/hwmon is empty.\n");
129 }
130
Amit Arorae9e16b02010-08-03 10:15:20 +0530131 return 0;
132}
133
Amit Arora47fd9182010-08-24 13:26:06 +0530134void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
135{
136 if (!strcmp(ritem->d_name, "name"))
137 strcpy(regulators_info[idx].name, str);
138 if (!strcmp(ritem->d_name, "state"))
139 strcpy(regulators_info[idx].state, str);
140 if (!strcmp(ritem->d_name, "status"))
141 strcpy(regulators_info[idx].status, str);
Amit Arorae9e16b02010-08-03 10:15:20 +0530142
Amit Arora47fd9182010-08-24 13:26:06 +0530143 if (!strcmp(ritem->d_name, "type"))
144 strcpy(regulators_info[idx].type, str);
145 if (!strcmp(ritem->d_name, "opmode"))
146 strcpy(regulators_info[idx].opmode, str);
147
148 if (!strcmp(ritem->d_name, "microvolts"))
149 regulators_info[idx].microvolts = atoi(str);
150 if (!strcmp(ritem->d_name, "min_microvolts"))
151 regulators_info[idx].min_microvolts = atoi(str);
152 if (!strcmp(ritem->d_name, "max_microvolts"))
153 regulators_info[idx].max_microvolts = atoi(str);
154
155 if (!strcmp(ritem->d_name, "microamps"))
156 regulators_info[idx].microamps = atoi(str);
157 if (!strcmp(ritem->d_name, "min_microamps"))
158 regulators_info[idx].min_microamps = atoi(str);
159 if (!strcmp(ritem->d_name, "max_microamps"))
160 regulators_info[idx].max_microamps = atoi(str);
161 if (!strcmp(ritem->d_name, "requested_microamps"))
162 regulators_info[idx].requested_microamps = atoi(str);
163
164 if (!strcmp(ritem->d_name, "num_users"))
165 regulators_info[idx].num_users = atoi(str);
166}
167
168int read_regulator_info(void)
Amit Arorae9e16b02010-08-03 10:15:20 +0530169{
170 FILE *file = NULL;
171 DIR *regdir, *dir;
172 int len, count = 0, ret = 0;
173 char line[1024], filename[1024], *fptr;
174 struct dirent *item, *ritem;
175
176 regdir = opendir("/sys/class/regulator");
177 if (!regdir)
178 return(1);
179 while((item = readdir(regdir))) {
180 if (strlen(item->d_name) < 3)
181 continue;
182
183 if (strncmp(item->d_name, "regulator", 9))
184 continue;
185
Amit Arora85fd4952010-08-05 14:04:50 +0530186 len = sprintf(filename, "/sys/class/regulator/%s",
187 item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530188
189 dir = opendir(filename);
Amit Arora85fd4952010-08-05 14:04:50 +0530190 if (!dir)
Amit Arora7ef178c2010-08-03 15:57:21 +0530191 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530192
193 count++;
194 if (count > numregulators) {
195 ret = 1;
Amit Arora85fd4952010-08-05 14:04:50 +0530196 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +0530197 }
198
Amit Arorafccac912010-08-03 16:15:09 +0530199 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530200 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530201 if (strlen(ritem->d_name) < 3)
202 continue;
203
204 sprintf(filename + len, "/%s", ritem->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530205
206 file = fopen(filename, "r");
207 if (!file)
208 continue;
209
210 memset(line, 0, 1024);
211 fptr = fgets(line, 1024, file);
212 fclose(file);
Amit Arora85fd4952010-08-05 14:04:50 +0530213 if (!fptr)
Amit Arora7ef178c2010-08-03 15:57:21 +0530214 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530215
Amit Arora47fd9182010-08-24 13:26:06 +0530216 read_info_from_dirent(ritem, fptr, count - 1);
Amit Arorae9e16b02010-08-03 10:15:20 +0530217 }
Amit Arora85fd4952010-08-05 14:04:50 +0530218exit:
Amit Arorae9e16b02010-08-03 10:15:20 +0530219 closedir(dir);
220 if (ret)
221 break;
222 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530223 closedir(regdir);
224
225 return ret;
226}
227
228
229int main(int argc, char **argv)
230{
231 int c;
Amit Arora47fd9182010-08-24 13:26:06 +0530232 int firsttime = 1;
Amit Arora24ed7d12010-09-14 12:12:58 +0530233 int enter_hit = 0;
Amit Arora728e0c92010-09-14 12:06:09 +0530234 int regulators = 0, sensors = 0, clocks = 0, verbose = 0;
Amit Arora24ed7d12010-09-14 12:12:58 +0530235 int r_share = 0, s_share = 0, c_share = 0; //%age share of the win size
Amit Arorae9e16b02010-08-03 10:15:20 +0530236
Amit Arorafefe8bf2010-08-05 13:31:20 +0530237 /*
238 * Options:
239 * -r, --regulator : regulator
240 * -s, --sensor : sensors
Amit Arora728e0c92010-09-14 12:06:09 +0530241 * -c, --clock : clocks
242 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +0530243 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +0530244 * -v, --verbose : verbose
245 * -V, --version : version
246 * -h, --help : help
247 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530248 */
249
Amit Arorafefe8bf2010-08-05 13:31:20 +0530250 while (1) {
251 int optindex = 0;
252 static struct option long_options[] = {
253 {"regulator", 0, 0, 'r'},
254 {"sensor", 0, 0, 's'},
Amit Arora728e0c92010-09-14 12:06:09 +0530255 {"clock", 0, 0, 'c'},
256 {"time", 0, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +0530257 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +0530258 {"verbose", 0, 0, 'v'},
259 {"version", 0, 0, 'V'},
260 {"help", 0, 0, 'h'},
261 {0, 0, 0, 0}
262 };
263
Amit Arora728e0c92010-09-14 12:06:09 +0530264 c = getopt_long(argc, argv, "rsct:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530265 if (c == -1)
266 break;
267
268 switch (c) {
269 case 'r':
270 regulators = 1;
271 break;
272 case 's':
273 sensors = 1;
274 break;
Amit Arora728e0c92010-09-14 12:06:09 +0530275 case 'c':
276 clocks = 1;
277 break;
278 case 't':
279 ticktime = strtod(optarg, NULL);
280 break;
Amit Arora47fd9182010-08-24 13:26:06 +0530281 case 'd':
282 dump = 1;
283 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530284 case 'v':
285 verbose = 1;
286 break;
287 case 'V':
288 version();
289 break;
290 case 'h':
291 usage(argv);
292 break;
293 case '?':
294 fprintf (stderr, "%s: Unknown option %c'.\n",
295 argv[0], optopt);
296 exit(1);
297 default:
298 usage(argv);
299 break;
300 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530301 }
302
Amit Arorafefe8bf2010-08-05 13:31:20 +0530303
304 /* Need atleast one option specified */
Amit Arora728e0c92010-09-14 12:06:09 +0530305 if (!regulators && !sensors && !clocks) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530306 usage(argv);
307 }
308
309 init_regulator_ds();
310
Amit Arora47fd9182010-08-24 13:26:06 +0530311 while(1) {
312 int key = 0;
313 struct timeval tval;
314 fd_set readfds;
Amit Arora728e0c92010-09-14 12:06:09 +0530315 int row = 1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530316
Amit Arora47fd9182010-08-24 13:26:06 +0530317 if (!dump) {
Amit Arora24ed7d12010-09-14 12:12:58 +0530318 if(firsttime)
Amit Arora47fd9182010-08-24 13:26:06 +0530319 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530320 create_windows();
321 show_header();
Amit Arora24ed7d12010-09-14 12:12:58 +0530322 if (sensors)
323 s_share = 20;
324 if (regulators) {
325 if (!sensors && clocks)
326 r_share = 50;
327 else if (clocks)
328 r_share = 40;
329 else
330 r_share = 80;
331 }
332 c_share = 100 - (r_share + s_share);
Amit Arora47fd9182010-08-24 13:26:06 +0530333 }
334
335 if (regulators) {
336 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530337 if (!dump) {
Amit Arora24ed7d12010-09-14 12:12:58 +0530338 int orig_r_share = r_share;
339
Amit Arora728e0c92010-09-14 12:06:09 +0530340 row = create_regulator_win(row,
Amit Arora24ed7d12010-09-14 12:12:58 +0530341 numregulators + 2,
342 &r_share);
343 c_share += (orig_r_share - r_share);
Amit Arora47fd9182010-08-24 13:26:06 +0530344 show_regulator_info(verbose);
Amit Arora728e0c92010-09-14 12:06:09 +0530345 }
Amit Arora47fd9182010-08-24 13:26:06 +0530346 else
347 print_regulator_info(verbose);
348 }
349
Amit Arora0e512722010-10-01 12:24:16 +0530350 if (clocks) {
Amit Arora24ed7d12010-09-14 12:12:58 +0530351 if (firsttime)
352 init_clock_details();
Amit Arora0e512722010-10-01 12:24:16 +0530353 if (!dump) {
354 int hrow;
355
356 //giv big no. in second arg as of now
357 row = create_clock_win(row, 100, &c_share);
358 hrow = read_and_print_clock_info(verbose,
359 highlighted_row,
360 enter_hit);
361 highlighted_row = hrow;
362 enter_hit = 0;
363 } else
364 dump_clock_info(verbose);
Amit Arora728e0c92010-09-14 12:06:09 +0530365 }
Amit Arora47fd9182010-08-24 13:26:06 +0530366
367 if (sensors) {
Amit Arora728e0c92010-09-14 12:06:09 +0530368 if (!dump) {
Amit Arora24ed7d12010-09-14 12:12:58 +0530369 row = create_sensor_win(row, 100, &s_share);//big no. as of now
Amit Arora728e0c92010-09-14 12:06:09 +0530370 print_sensor_header();
371 }
372 else
373 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530374 }
375
376 if (dump)
377 break;
378
379 FD_ZERO(&readfds);
380 FD_SET(0, &readfds);
381 tval.tv_sec = ticktime;
382 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
383
384 key = select(1, &readfds, NULL, NULL, &tval);
385
386 if (key) {
387 char keychar;
388
389 int keystroke = fgetc(stdin);
390 if (keystroke == EOF)
391 exit(0);
392
Amit Arora728e0c92010-09-14 12:06:09 +0530393 if (keystroke == 9)
394 highlighted_row++;
395
Amit Arora47fd9182010-08-24 13:26:06 +0530396 keychar = toupper(keystroke);
Amit Arora24ed7d12010-09-14 12:12:58 +0530397 if (keystroke == 13)
398 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530399
Amit Arora47fd9182010-08-24 13:26:06 +0530400 if (keychar == 'Q')
401 exit(0);
402 if (keychar == 'R')
403 ticktime = 3;
404 }
Amit Arora24ed7d12010-09-14 12:12:58 +0530405 if (firsttime)
406 firsttime = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530407 }
408
Amit Arora85fd4952010-08-05 14:04:50 +0530409 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530410}