blob: 8c849325e8025b0b06c8c497e85d45320fed9e9b [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;
Amit Arorac93e0712010-10-07 13:51:53 +053023int selectedwindow = -1;
Amit Arora728e0c92010-09-14 12:06:09 +053024double ticktime = 10.0; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053025
Amit Arorac93e0712010-10-07 13:51:53 +053026char *win_names[TOTAL_FEATURE_WINS] = {
27 "Regulators",
28 "Clocks",
29 "Sensors" };
30
Amit Arorae9e16b02010-08-03 10:15:20 +053031int init_regulator_ds(void)
32{
33 DIR *regdir;
34 struct dirent *item;
35
36 regdir = opendir("/sys/class/regulator");
37 if (!regdir)
38 return(1);
39 while((item = readdir(regdir))) {
40 if (strncmp(item->d_name, "regulator", 9))
41 continue;
42
43 numregulators++;
44 }
45 closedir(regdir);
46
47 regulators_info = (struct regulator_info *)malloc(numregulators*
48 sizeof(struct regulator_info));
49 if (!regulators_info) {
Amit Arora24ed7d12010-09-14 12:12:58 +053050 fprintf(stderr, "init_regulator_ds: Not enough memory to "
51 "read information for %d regulators!\n", numregulators);
Amit Arorae9e16b02010-08-03 10:15:20 +053052 return(1);
53 }
54
55 return(0);
56}
57
58int read_and_print_sensor_info(int verbose)
59{
60 DIR *dir, *subdir;
Amit Arora1c037892010-08-05 13:46:10 +053061 int len, found = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +053062 char filename[PATH_MAX], devpath[PATH_MAX];
63 char device[PATH_MAX];
64 struct dirent *item, *subitem;
65
66/*
67 1. readdir /sys/class/hwmon
68 2. foreach subdir in above, do following
69 a) check if its virtual or physical (check for device)
70 b) for each *type* (in, temp, pwm, fan, curr, power, energy)
71 i> search for "_" (strrchr... check for last)
72 ii> check the *item* depending on type
73 i.e for "in" type items=min, max, input, label
74 for "fan" type items=min,max,input,div,target,label
75 for "pwm" type items=enable, mode, freq
76 for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
77 for "curr" type items=max,min,input
78 for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
79 for "energy" type items=input
80 for "intrusion" type items=alarm,beep
81*/
82
83 sprintf(filename, "%s", "/sys/class/hwmon");
84
85 dir = opendir(filename);
86 if (!dir)
87 return errno;
88
89 while ((item = readdir(dir))) {
90 if (item->d_name[0] == '.') /* skip the hidden files */
91 continue;
92
Amit Arora1c037892010-08-05 13:46:10 +053093 found = 1;
94
Amit Arorae9e16b02010-08-03 10:15:20 +053095 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
96 sprintf(devpath, "%s/device", filename);
97
98 len = readlink(devpath, device, PATH_MAX - 1);
99
100 if (len < 0)
101 strcpy(devpath, filename);
102 else
103 device[len] = '\0';
104
105 subdir = opendir(devpath);
106
107 printf("\nSensor Information for %s :\n", item->d_name);
108 fflush(stdin);
109 while ((subitem = readdir(subdir))) {
110 if (subitem->d_name[0] == '.') /* skip hidden files */
111 continue;
112
113 if(!strncmp(subitem->d_name, "in", 2))
114 get_sensor_info(devpath, subitem->d_name, "in",
115 verbose);
116 else if (!strncmp(subitem->d_name, "temp", 4))
117 get_sensor_info(devpath, subitem->d_name,
118 "temp", verbose);
119 else if (!strncmp(subitem->d_name, "fan", 4))
120 get_sensor_info(devpath, subitem->d_name,
121 "fan", verbose);
122 else if (!strncmp(subitem->d_name, "pwm", 4))
123 get_sensor_info(devpath, subitem->d_name,
124 "pwm", verbose);
125
126 }
127
128 closedir(subdir);
129 }
130 closedir(dir);
131
Amit Arora1c037892010-08-05 13:46:10 +0530132 if(!found && verbose) {
133 printf("Could not find sensor information!");
134 printf(" Looks like /sys/class/hwmon is empty.\n");
135 }
136
Amit Arorae9e16b02010-08-03 10:15:20 +0530137 return 0;
138}
139
Amit Arora47fd9182010-08-24 13:26:06 +0530140void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
141{
142 if (!strcmp(ritem->d_name, "name"))
143 strcpy(regulators_info[idx].name, str);
144 if (!strcmp(ritem->d_name, "state"))
145 strcpy(regulators_info[idx].state, str);
146 if (!strcmp(ritem->d_name, "status"))
147 strcpy(regulators_info[idx].status, str);
Amit Arorae9e16b02010-08-03 10:15:20 +0530148
Amit Arora47fd9182010-08-24 13:26:06 +0530149 if (!strcmp(ritem->d_name, "type"))
150 strcpy(regulators_info[idx].type, str);
151 if (!strcmp(ritem->d_name, "opmode"))
152 strcpy(regulators_info[idx].opmode, str);
153
154 if (!strcmp(ritem->d_name, "microvolts"))
155 regulators_info[idx].microvolts = atoi(str);
156 if (!strcmp(ritem->d_name, "min_microvolts"))
157 regulators_info[idx].min_microvolts = atoi(str);
158 if (!strcmp(ritem->d_name, "max_microvolts"))
159 regulators_info[idx].max_microvolts = atoi(str);
160
161 if (!strcmp(ritem->d_name, "microamps"))
162 regulators_info[idx].microamps = atoi(str);
163 if (!strcmp(ritem->d_name, "min_microamps"))
164 regulators_info[idx].min_microamps = atoi(str);
165 if (!strcmp(ritem->d_name, "max_microamps"))
166 regulators_info[idx].max_microamps = atoi(str);
167 if (!strcmp(ritem->d_name, "requested_microamps"))
168 regulators_info[idx].requested_microamps = atoi(str);
169
170 if (!strcmp(ritem->d_name, "num_users"))
171 regulators_info[idx].num_users = atoi(str);
172}
173
174int read_regulator_info(void)
Amit Arorae9e16b02010-08-03 10:15:20 +0530175{
176 FILE *file = NULL;
177 DIR *regdir, *dir;
178 int len, count = 0, ret = 0;
179 char line[1024], filename[1024], *fptr;
180 struct dirent *item, *ritem;
181
182 regdir = opendir("/sys/class/regulator");
183 if (!regdir)
184 return(1);
185 while((item = readdir(regdir))) {
186 if (strlen(item->d_name) < 3)
187 continue;
188
189 if (strncmp(item->d_name, "regulator", 9))
190 continue;
191
Amit Arora85fd4952010-08-05 14:04:50 +0530192 len = sprintf(filename, "/sys/class/regulator/%s",
193 item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530194
195 dir = opendir(filename);
Amit Arora85fd4952010-08-05 14:04:50 +0530196 if (!dir)
Amit Arora7ef178c2010-08-03 15:57:21 +0530197 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530198
199 count++;
200 if (count > numregulators) {
201 ret = 1;
Amit Arora85fd4952010-08-05 14:04:50 +0530202 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +0530203 }
204
Amit Arorafccac912010-08-03 16:15:09 +0530205 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530206 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530207 if (strlen(ritem->d_name) < 3)
208 continue;
209
210 sprintf(filename + len, "/%s", ritem->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530211
212 file = fopen(filename, "r");
213 if (!file)
214 continue;
215
216 memset(line, 0, 1024);
217 fptr = fgets(line, 1024, file);
218 fclose(file);
Amit Arora85fd4952010-08-05 14:04:50 +0530219 if (!fptr)
Amit Arora7ef178c2010-08-03 15:57:21 +0530220 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530221
Amit Arora47fd9182010-08-24 13:26:06 +0530222 read_info_from_dirent(ritem, fptr, count - 1);
Amit Arorae9e16b02010-08-03 10:15:20 +0530223 }
Amit Arora85fd4952010-08-05 14:04:50 +0530224exit:
Amit Arorae9e16b02010-08-03 10:15:20 +0530225 closedir(dir);
226 if (ret)
227 break;
228 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530229 closedir(regdir);
230
231 return ret;
232}
233
234
235int main(int argc, char **argv)
236{
Amit Arorac93e0712010-10-07 13:51:53 +0530237 int c, i;
238 int firsttime[TOTAL_FEATURE_WINS];
Amit Arora24ed7d12010-09-14 12:12:58 +0530239 int enter_hit = 0;
Amit Arora728e0c92010-09-14 12:06:09 +0530240 int regulators = 0, sensors = 0, clocks = 0, verbose = 0;
Amit Arora24ed7d12010-09-14 12:12:58 +0530241 int r_share = 0, s_share = 0, c_share = 0; //%age share of the win size
Amit Arorae9e16b02010-08-03 10:15:20 +0530242
Amit Arorac93e0712010-10-07 13:51:53 +0530243 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
244 firsttime[i] = 1;
245
Amit Arorafefe8bf2010-08-05 13:31:20 +0530246 /*
247 * Options:
Amit Arorac93e0712010-10-07 13:51:53 +0530248 * -r, --regulator : regulator
249 * -s, --sensor : sensors
250 * -c, --clock : clocks
Amit Arora728e0c92010-09-14 12:06:09 +0530251 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +0530252 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +0530253 * -v, --verbose : verbose
254 * -V, --version : version
255 * -h, --help : help
256 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530257 */
258
Amit Arorafefe8bf2010-08-05 13:31:20 +0530259 while (1) {
260 int optindex = 0;
261 static struct option long_options[] = {
Amit Arorac93e0712010-10-07 13:51:53 +0530262 {"regulator", 0, 0, 'r'},
263 {"sensor", 0, 0, 's'},
264 {"clock", 0, 0, 'c'},
Amit Arora728e0c92010-09-14 12:06:09 +0530265 {"time", 0, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +0530266 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +0530267 {"verbose", 0, 0, 'v'},
268 {"version", 0, 0, 'V'},
269 {"help", 0, 0, 'h'},
270 {0, 0, 0, 0}
271 };
272
Amit Arora728e0c92010-09-14 12:06:09 +0530273 c = getopt_long(argc, argv, "rsct:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530274 if (c == -1)
275 break;
276
277 switch (c) {
Amit Arorac93e0712010-10-07 13:51:53 +0530278 case 'r':
279 regulators = 1;
280 selectedwindow = REGULATOR;
281 break;
282 case 's':
283 sensors = 1;
284 selectedwindow = SENSOR;
285 break;
286 case 'c':
287 clocks = 1;
288 selectedwindow = CLOCK;
289 break;
Amit Arora728e0c92010-09-14 12:06:09 +0530290 case 't':
291 ticktime = strtod(optarg, NULL);
292 break;
Amit Arora47fd9182010-08-24 13:26:06 +0530293 case 'd':
294 dump = 1;
295 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530296 case 'v':
297 verbose = 1;
298 break;
299 case 'V':
300 version();
301 break;
302 case 'h':
303 usage(argv);
304 break;
305 case '?':
306 fprintf (stderr, "%s: Unknown option %c'.\n",
307 argv[0], optopt);
308 exit(1);
309 default:
310 usage(argv);
311 break;
312 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530313 }
314
Amit Arorac93e0712010-10-07 13:51:53 +0530315 if (!dump && (regulators || clocks || sensors)) {
316 fprintf(stderr, "Option supported only in dump mode (-d)\n");
317 usage(argv);
318 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530319
Amit Arorac93e0712010-10-07 13:51:53 +0530320 if (!dump)
321 selectedwindow = REGULATOR;
Amit Arorae9e16b02010-08-03 10:15:20 +0530322
323 init_regulator_ds();
324
Amit Arora47fd9182010-08-24 13:26:06 +0530325 while(1) {
326 int key = 0;
327 struct timeval tval;
328 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530329
Amit Arora47fd9182010-08-24 13:26:06 +0530330 if (!dump) {
Amit Arorac93e0712010-10-07 13:51:53 +0530331 if(firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530332 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530333 create_windows();
334 show_header();
Amit Arora24ed7d12010-09-14 12:12:58 +0530335 if (sensors)
336 s_share = 20;
337 if (regulators) {
338 if (!sensors && clocks)
339 r_share = 50;
340 else if (clocks)
341 r_share = 40;
342 else
343 r_share = 80;
344 }
345 c_share = 100 - (r_share + s_share);
Amit Arora47fd9182010-08-24 13:26:06 +0530346 }
347
Amit Arorac93e0712010-10-07 13:51:53 +0530348 if (selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530349 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530350 if (!dump) {
Amit Arorac93e0712010-10-07 13:51:53 +0530351 create_selectedwindow();
Amit Arora47fd9182010-08-24 13:26:06 +0530352 show_regulator_info(verbose);
Amit Arora728e0c92010-09-14 12:06:09 +0530353 }
Amit Arora47fd9182010-08-24 13:26:06 +0530354 else
355 print_regulator_info(verbose);
356 }
357
Amit Arorac93e0712010-10-07 13:51:53 +0530358 if (selectedwindow == CLOCK) {
359 if (firsttime[CLOCK]) {
Amit Arora24ed7d12010-09-14 12:12:58 +0530360 init_clock_details();
Amit Arorac93e0712010-10-07 13:51:53 +0530361 firsttime[CLOCK] = 0;
362 }
Amit Arora0e512722010-10-01 12:24:16 +0530363 if (!dump) {
364 int hrow;
365
Amit Arorac93e0712010-10-07 13:51:53 +0530366 create_selectedwindow();
Amit Arora0e512722010-10-01 12:24:16 +0530367 hrow = read_and_print_clock_info(verbose,
368 highlighted_row,
369 enter_hit);
370 highlighted_row = hrow;
371 enter_hit = 0;
372 } else
373 dump_clock_info(verbose);
Amit Arora728e0c92010-09-14 12:06:09 +0530374 }
Amit Arora47fd9182010-08-24 13:26:06 +0530375
Amit Arorac93e0712010-10-07 13:51:53 +0530376 if (selectedwindow == SENSOR) {
Amit Arora728e0c92010-09-14 12:06:09 +0530377 if (!dump) {
Amit Arorac93e0712010-10-07 13:51:53 +0530378 create_selectedwindow();
Amit Arora728e0c92010-09-14 12:06:09 +0530379 print_sensor_header();
380 }
381 else
382 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530383 }
384
385 if (dump)
386 break;
387
388 FD_ZERO(&readfds);
389 FD_SET(0, &readfds);
390 tval.tv_sec = ticktime;
391 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
392
393 key = select(1, &readfds, NULL, NULL, &tval);
394
395 if (key) {
396 char keychar;
397
Amit Arorac93e0712010-10-07 13:51:53 +0530398 //int keystroke = fgetc(stdin);
399 int keystroke = getch();
Amit Arora47fd9182010-08-24 13:26:06 +0530400 if (keystroke == EOF)
401 exit(0);
402
Amit Arorac93e0712010-10-07 13:51:53 +0530403 if (keystroke == KEY_RIGHT || keystroke == 9)
404 selectedwindow++;
405
406 if (keystroke == KEY_LEFT || keystroke == 353)
407 selectedwindow--;
408
409 if (selectedwindow >= TOTAL_FEATURE_WINS)
410 selectedwindow = 0;
411
412 if (selectedwindow < 0)
413 selectedwindow = TOTAL_FEATURE_WINS - 1;
414
415 if (selectedwindow == CLOCK) {
416 if (keystroke == KEY_DOWN)
417 highlighted_row++;
418 if (keystroke == KEY_UP && highlighted_row > 0)
419 highlighted_row--;
420 }
Amit Arora728e0c92010-09-14 12:06:09 +0530421
Amit Arora47fd9182010-08-24 13:26:06 +0530422 keychar = toupper(keystroke);
Amit Arorac93e0712010-10-07 13:51:53 +0530423
424 //killall_windows(1); fini_curses();
425 //printf("key entered %d:%c\n", keystroke, keychar);
426 //exit(1);
427
Amit Arora24ed7d12010-09-14 12:12:58 +0530428 if (keystroke == 13)
429 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530430
Amit Arora47fd9182010-08-24 13:26:06 +0530431 if (keychar == 'Q')
432 exit(0);
433 if (keychar == 'R')
434 ticktime = 3;
435 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530436 }
437
Amit Arora85fd4952010-08-05 14:04:50 +0530438 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530439}