Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 17 | #include <getopt.h> |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 18 | #include "powerdebug.h" |
| 19 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 20 | int numregulators; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 21 | int dump; |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 22 | int highlighted_row; |
| 23 | double ticktime = 10.0; /* in seconds */ |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 24 | |
| 25 | int 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 Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 44 | fprintf(stderr, "init_regulator_ds: Not enough memory to "); |
| 45 | fprintf(stderr, "read information for %d regulators!\n", |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 46 | numregulators); |
| 47 | return(1); |
| 48 | } |
| 49 | |
| 50 | return(0); |
| 51 | } |
| 52 | |
| 53 | int read_and_print_sensor_info(int verbose) |
| 54 | { |
| 55 | DIR *dir, *subdir; |
Amit Arora | 1c03789 | 2010-08-05 13:46:10 +0530 | [diff] [blame] | 56 | int len, found = 0; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 57 | char filename[PATH_MAX], devpath[PATH_MAX]; |
| 58 | char device[PATH_MAX]; |
| 59 | struct dirent *item, *subitem; |
| 60 | |
| 61 | /* |
| 62 | 1. readdir /sys/class/hwmon |
| 63 | 2. foreach subdir in above, do following |
| 64 | a) check if its virtual or physical (check for device) |
| 65 | b) for each *type* (in, temp, pwm, fan, curr, power, energy) |
| 66 | i> search for "_" (strrchr... check for last) |
| 67 | ii> check the *item* depending on type |
| 68 | i.e for "in" type items=min, max, input, label |
| 69 | for "fan" type items=min,max,input,div,target,label |
| 70 | for "pwm" type items=enable, mode, freq |
| 71 | for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest, |
| 72 | for "curr" type items=max,min,input |
| 73 | for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max |
| 74 | for "energy" type items=input |
| 75 | for "intrusion" type items=alarm,beep |
| 76 | */ |
| 77 | |
| 78 | sprintf(filename, "%s", "/sys/class/hwmon"); |
| 79 | |
| 80 | dir = opendir(filename); |
| 81 | if (!dir) |
| 82 | return errno; |
| 83 | |
| 84 | while ((item = readdir(dir))) { |
| 85 | if (item->d_name[0] == '.') /* skip the hidden files */ |
| 86 | continue; |
| 87 | |
Amit Arora | 1c03789 | 2010-08-05 13:46:10 +0530 | [diff] [blame] | 88 | found = 1; |
| 89 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 90 | sprintf(filename, "/sys/class/hwmon/%s", item->d_name); |
| 91 | sprintf(devpath, "%s/device", filename); |
| 92 | |
| 93 | len = readlink(devpath, device, PATH_MAX - 1); |
| 94 | |
| 95 | if (len < 0) |
| 96 | strcpy(devpath, filename); |
| 97 | else |
| 98 | device[len] = '\0'; |
| 99 | |
| 100 | subdir = opendir(devpath); |
| 101 | |
| 102 | printf("\nSensor Information for %s :\n", item->d_name); |
| 103 | fflush(stdin); |
| 104 | while ((subitem = readdir(subdir))) { |
| 105 | if (subitem->d_name[0] == '.') /* skip hidden files */ |
| 106 | continue; |
| 107 | |
| 108 | if(!strncmp(subitem->d_name, "in", 2)) |
| 109 | get_sensor_info(devpath, subitem->d_name, "in", |
| 110 | verbose); |
| 111 | else if (!strncmp(subitem->d_name, "temp", 4)) |
| 112 | get_sensor_info(devpath, subitem->d_name, |
| 113 | "temp", verbose); |
| 114 | else if (!strncmp(subitem->d_name, "fan", 4)) |
| 115 | get_sensor_info(devpath, subitem->d_name, |
| 116 | "fan", verbose); |
| 117 | else if (!strncmp(subitem->d_name, "pwm", 4)) |
| 118 | get_sensor_info(devpath, subitem->d_name, |
| 119 | "pwm", verbose); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | closedir(subdir); |
| 124 | } |
| 125 | closedir(dir); |
| 126 | |
Amit Arora | 1c03789 | 2010-08-05 13:46:10 +0530 | [diff] [blame] | 127 | if(!found && verbose) { |
| 128 | printf("Could not find sensor information!"); |
| 129 | printf(" Looks like /sys/class/hwmon is empty.\n"); |
| 130 | } |
| 131 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 135 | void read_info_from_dirent(struct dirent *ritem, char *str, int idx) |
| 136 | { |
| 137 | if (!strcmp(ritem->d_name, "name")) |
| 138 | strcpy(regulators_info[idx].name, str); |
| 139 | if (!strcmp(ritem->d_name, "state")) |
| 140 | strcpy(regulators_info[idx].state, str); |
| 141 | if (!strcmp(ritem->d_name, "status")) |
| 142 | strcpy(regulators_info[idx].status, str); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 143 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 144 | if (!strcmp(ritem->d_name, "type")) |
| 145 | strcpy(regulators_info[idx].type, str); |
| 146 | if (!strcmp(ritem->d_name, "opmode")) |
| 147 | strcpy(regulators_info[idx].opmode, str); |
| 148 | |
| 149 | if (!strcmp(ritem->d_name, "microvolts")) |
| 150 | regulators_info[idx].microvolts = atoi(str); |
| 151 | if (!strcmp(ritem->d_name, "min_microvolts")) |
| 152 | regulators_info[idx].min_microvolts = atoi(str); |
| 153 | if (!strcmp(ritem->d_name, "max_microvolts")) |
| 154 | regulators_info[idx].max_microvolts = atoi(str); |
| 155 | |
| 156 | if (!strcmp(ritem->d_name, "microamps")) |
| 157 | regulators_info[idx].microamps = atoi(str); |
| 158 | if (!strcmp(ritem->d_name, "min_microamps")) |
| 159 | regulators_info[idx].min_microamps = atoi(str); |
| 160 | if (!strcmp(ritem->d_name, "max_microamps")) |
| 161 | regulators_info[idx].max_microamps = atoi(str); |
| 162 | if (!strcmp(ritem->d_name, "requested_microamps")) |
| 163 | regulators_info[idx].requested_microamps = atoi(str); |
| 164 | |
| 165 | if (!strcmp(ritem->d_name, "num_users")) |
| 166 | regulators_info[idx].num_users = atoi(str); |
| 167 | } |
| 168 | |
| 169 | int read_regulator_info(void) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 170 | { |
| 171 | FILE *file = NULL; |
| 172 | DIR *regdir, *dir; |
| 173 | int len, count = 0, ret = 0; |
| 174 | char line[1024], filename[1024], *fptr; |
| 175 | struct dirent *item, *ritem; |
| 176 | |
| 177 | regdir = opendir("/sys/class/regulator"); |
| 178 | if (!regdir) |
| 179 | return(1); |
| 180 | while((item = readdir(regdir))) { |
| 181 | if (strlen(item->d_name) < 3) |
| 182 | continue; |
| 183 | |
| 184 | if (strncmp(item->d_name, "regulator", 9)) |
| 185 | continue; |
| 186 | |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 187 | len = sprintf(filename, "/sys/class/regulator/%s", |
| 188 | item->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 189 | |
| 190 | dir = opendir(filename); |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 191 | if (!dir) |
Amit Arora | 7ef178c | 2010-08-03 15:57:21 +0530 | [diff] [blame] | 192 | continue; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 193 | |
| 194 | count++; |
| 195 | if (count > numregulators) { |
| 196 | ret = 1; |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 197 | goto exit; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 198 | } |
| 199 | |
Amit Arora | fccac91 | 2010-08-03 16:15:09 +0530 | [diff] [blame] | 200 | strcpy(regulators_info[count-1].name, item->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 201 | while((ritem = readdir(dir))) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 202 | if (strlen(ritem->d_name) < 3) |
| 203 | continue; |
| 204 | |
| 205 | sprintf(filename + len, "/%s", ritem->d_name); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 206 | |
| 207 | file = fopen(filename, "r"); |
| 208 | if (!file) |
| 209 | continue; |
| 210 | |
| 211 | memset(line, 0, 1024); |
| 212 | fptr = fgets(line, 1024, file); |
| 213 | fclose(file); |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 214 | if (!fptr) |
Amit Arora | 7ef178c | 2010-08-03 15:57:21 +0530 | [diff] [blame] | 215 | continue; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 216 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 217 | read_info_from_dirent(ritem, fptr, count - 1); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 218 | } |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 219 | exit: |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 220 | closedir(dir); |
| 221 | if (ret) |
| 222 | break; |
| 223 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 224 | closedir(regdir); |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | int main(int argc, char **argv) |
| 231 | { |
| 232 | int c; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 233 | int firsttime = 1; |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 234 | int regulators = 0, sensors = 0, clocks = 0, verbose = 0; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 235 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 236 | /* |
| 237 | * Options: |
| 238 | * -r, --regulator : regulator |
| 239 | * -s, --sensor : sensors |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 240 | * -c, --clock : clocks |
| 241 | * -t, --time : ticktime |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 242 | * -d, --dump : dump |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 243 | * -v, --verbose : verbose |
| 244 | * -V, --version : version |
| 245 | * -h, --help : help |
| 246 | * no option / default : show usage! |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 247 | */ |
| 248 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 249 | while (1) { |
| 250 | int optindex = 0; |
| 251 | static struct option long_options[] = { |
| 252 | {"regulator", 0, 0, 'r'}, |
| 253 | {"sensor", 0, 0, 's'}, |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 254 | {"clock", 0, 0, 'c'}, |
| 255 | {"time", 0, 0, 't'}, |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 256 | {"dump", 0, 0, 'd'}, |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 257 | {"verbose", 0, 0, 'v'}, |
| 258 | {"version", 0, 0, 'V'}, |
| 259 | {"help", 0, 0, 'h'}, |
| 260 | {0, 0, 0, 0} |
| 261 | }; |
| 262 | |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 263 | c = getopt_long(argc, argv, "rsct:dvVh", long_options, &optindex); |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 264 | if (c == -1) |
| 265 | break; |
| 266 | |
| 267 | switch (c) { |
| 268 | case 'r': |
| 269 | regulators = 1; |
| 270 | break; |
| 271 | case 's': |
| 272 | sensors = 1; |
| 273 | break; |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 274 | case 'c': |
| 275 | clocks = 1; |
| 276 | break; |
| 277 | case 't': |
| 278 | ticktime = strtod(optarg, NULL); |
| 279 | break; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 280 | case 'd': |
| 281 | dump = 1; |
| 282 | break; |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 283 | case 'v': |
| 284 | verbose = 1; |
| 285 | break; |
| 286 | case 'V': |
| 287 | version(); |
| 288 | break; |
| 289 | case 'h': |
| 290 | usage(argv); |
| 291 | break; |
| 292 | case '?': |
| 293 | fprintf (stderr, "%s: Unknown option %c'.\n", |
| 294 | argv[0], optopt); |
| 295 | exit(1); |
| 296 | default: |
| 297 | usage(argv); |
| 298 | break; |
| 299 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 300 | } |
| 301 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 302 | |
| 303 | /* Need atleast one option specified */ |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 304 | if (!regulators && !sensors && !clocks) { |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 305 | usage(argv); |
| 306 | } |
| 307 | |
| 308 | init_regulator_ds(); |
| 309 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 310 | while(1) { |
| 311 | int key = 0; |
| 312 | struct timeval tval; |
| 313 | fd_set readfds; |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 314 | int row = 1; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 315 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 316 | if (!dump) { |
| 317 | if(firsttime) { |
| 318 | init_curses(); |
| 319 | firsttime = 0; |
| 320 | } |
| 321 | create_windows(); |
| 322 | show_header(); |
| 323 | } |
| 324 | |
| 325 | if (regulators) { |
| 326 | read_regulator_info(); |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 327 | if (!dump) { |
| 328 | row = create_regulator_win(row, |
| 329 | numregulators+2); |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 330 | show_regulator_info(verbose); |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 331 | } |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 332 | else |
| 333 | print_regulator_info(verbose); |
| 334 | } |
| 335 | |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 336 | if (clocks && !dump) { |
| 337 | int hrow; |
| 338 | row = create_clock_win(row, 100);//giv big no.as of now |
| 339 | hrow = read_and_print_clock_info(verbose, |
| 340 | highlighted_row); |
| 341 | highlighted_row = hrow; |
| 342 | } |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 343 | |
| 344 | if (sensors) { |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 345 | if (!dump) { |
| 346 | row = create_sensor_win(row, 100);//big no. as of now |
| 347 | print_sensor_header(); |
| 348 | } |
| 349 | else |
| 350 | read_and_print_sensor_info(verbose); |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if (dump) |
| 354 | break; |
| 355 | |
| 356 | FD_ZERO(&readfds); |
| 357 | FD_SET(0, &readfds); |
| 358 | tval.tv_sec = ticktime; |
| 359 | tval.tv_usec = (ticktime - tval.tv_sec) * 1000000; |
| 360 | |
| 361 | key = select(1, &readfds, NULL, NULL, &tval); |
| 362 | |
| 363 | if (key) { |
| 364 | char keychar; |
| 365 | |
| 366 | int keystroke = fgetc(stdin); |
| 367 | if (keystroke == EOF) |
| 368 | exit(0); |
| 369 | |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 370 | if (keystroke == 9) |
| 371 | highlighted_row++; |
| 372 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 373 | keychar = toupper(keystroke); |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame^] | 374 | #ifdef DEBUG_KEY |
| 375 | if (keystroke == 13) { |
| 376 | killall_windows(); |
| 377 | fini_curses(); |
| 378 | printf("powerdebug: key=%d : char=%c\n", keystroke, keychar); |
| 379 | printf("highlighted_row = %d\n", highlighted_row); |
| 380 | exit(0); |
| 381 | } |
| 382 | #endif |
| 383 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 384 | if (keychar == 'Q') |
| 385 | exit(0); |
| 386 | if (keychar == 'R') |
| 387 | ticktime = 3; |
| 388 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 389 | } |
| 390 | |
Amit Arora | 85fd495 | 2010-08-05 14:04:50 +0530 | [diff] [blame] | 391 | exit(0); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 392 | } |