blob: 26f77671cd23fb9fcfd7a5915f54326c7eaf91e6 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora39f29542010-09-14 12:03:22 +05303 *
4 * This file is part of PowerDebug.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors:
12 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
13 * - initial API and implementation
14 *******************************************************************************/
15
Amit Arorae9e16b02010-08-03 10:15:20 +053016#include <getopt.h>
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010017#include <stdbool.h>
Amit Arorae9e16b02010-08-03 10:15:20 +053018#include "powerdebug.h"
19
Amit Arora728e0c92010-09-14 12:06:09 +053020int highlighted_row;
Amit Arorac93e0712010-10-07 13:51:53 +053021int selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053022
Amit Arora422c52f2010-12-02 16:22:29 +053023void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053024{
Amit Arora422c52f2010-12-02 16:22:29 +053025 printf("Usage: powerdebug [OPTIONS]\n");
26 printf("\n");
27 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
28 "[ -v ]\n");
29 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053030 printf(" -r, --regulator Show regulator information\n");
31 printf(" -s, --sensor Show sensor information\n");
32 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053033 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060034 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053035 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
36 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053037 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060038 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053039 printf(" -V, --version Show Version\n");
40 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053041}
42
Amit Arora17552782010-12-02 12:23:14 +053043void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053044{
Amit Arora17552782010-12-02 12:23:14 +053045 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053046}
47
Daniel Lezcano316bcae2011-03-23 14:37:30 +010048/*
49 * Options:
50 * -r, --regulator : regulator
51 * -s, --sensor : sensors
52 * -c, --clock : clocks
53 * -p, --findparents : clockname whose parents have to be found
54 * -t, --time : ticktime
55 * -d, --dump : dump
56 * -v, --verbose : verbose
57 * -V, --version : version
58 * -h, --help : help
59 * no option / default : show usage!
60 */
61
62static struct option long_options[] = {
63 { "regulator", 0, 0, 'r' },
64 { "sensor", 0, 0, 's' },
65 { "clock", 0, 0, 'c' },
66 { "findparents", 1, 0, 'p' },
67 { "time", 1, 0, 't' },
68 { "dump", 0, 0, 'd' },
69 { "verbose", 0, 0, 'v' },
70 { "version", 0, 0, 'V' },
71 { "help", 0, 0, 'h' },
72 { 0, 0, 0, 0 }
73};
74
75struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010076 bool verbose;
77 bool findparent;
78 bool regulators;
79 bool sensors;
80 bool clocks;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010081 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010082 unsigned int ticktime;
Daniel Lezcano9420fde2011-03-23 14:37:31 +010083 char *clkarg;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010084};
85
86int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053087{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010088 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053089
Daniel Lezcano316bcae2011-03-23 14:37:30 +010090 memset(options, 0, sizeof(*options));
91 options->ticktime = 10;
Amit Arorae9e16b02010-08-03 10:15:20 +053092
Amit Arorafefe8bf2010-08-05 13:31:20 +053093 while (1) {
94 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053095
Daniel Lezcano316bcae2011-03-23 14:37:30 +010096 c = getopt_long(argc, argv, "rscp:t:dvVh",
97 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +053098 if (c == -1)
99 break;
100
101 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530102 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100103 options->regulators = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530104 selectedwindow = REGULATOR;
105 break;
106 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100107 options->sensors = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530108 selectedwindow = SENSOR;
109 break;
110 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100111 options->clocks = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530112 selectedwindow = CLOCK;
113 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530114 case 'p':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100115 options->findparent = true;
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100116 options->clkarg = strdup(optarg);
117 if (!options->clkarg) {
118 fprintf(stderr, "failed to allocate memory");
119 return -1;
120 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530121 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530122 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100123 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530124 break;
125 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100126 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530127 break;
128 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100129 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 break;
131 case 'V':
132 version();
133 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530134 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100135 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600136 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530137 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100138 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530139 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530140 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530141
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100142 if (options->dump && !(options->regulators ||
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100143 options->clocks || options->sensors)) {
144 /* By Default lets show everything we have */
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100145 options->regulators = options->clocks = options->sensors = true;
Amit Aroraa06a7302010-12-02 15:59:37 +0530146 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530147
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100148 if (options->findparent && (!options->clocks || !options->dump)) {
Amit Aroraf4fb8102010-11-30 13:55:50 +0530149 fprintf(stderr, "-p option passed without -c and -d."
150 " Exiting...\n");
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100151 return -1;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530152 }
153
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100154 if (!options->dump && selectedwindow == -1)
Amit Kucheriaee851a12011-01-12 04:57:48 +0530155 selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530156
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100157 return 0;
158}
159
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100160int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
161 char *clkname_str, bool *refreshwin,
Daniel Lezcano60a41022011-03-23 14:37:35 +0100162 struct powerdebug_options *options)
163{
164 char keychar;
165 int keystroke = getch();
166 int oldselectedwin = selectedwindow;
167
168 if (keystroke == EOF)
169 exit(0);
170
171 if (keystroke == KEY_RIGHT || keystroke == 9)
172 selectedwindow++;
173
174 if (keystroke == KEY_LEFT || keystroke == 353)
175 selectedwindow--;
176
177 if (selectedwindow >= TOTAL_FEATURE_WINS)
178 selectedwindow = 0;
179
180 if (selectedwindow < 0)
181 selectedwindow = TOTAL_FEATURE_WINS - 1;
182
183 if (selectedwindow == CLOCK) {
184 if (keystroke == KEY_DOWN)
185 highlighted_row++;
186 if (keystroke == KEY_UP && highlighted_row > 0)
187 highlighted_row--;
188 if (keystroke == 47)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100189 *findparent_ncurses = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100190
191 if ((keystroke == 27 || oldselectedwin !=
192 selectedwindow) && *findparent_ncurses) {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100193 *findparent_ncurses = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100194 clkname_str[0] = '\0';
195 }
196
197 if (*findparent_ncurses && keystroke != 13) {
198 int len = strlen(clkname_str);
199 char str[2];
200
201 if (keystroke == 263) {
202 if (len > 0)
203 len--;
204
205 clkname_str[len] = '\0';
206 } else {
207 if (strlen(clkname_str) ||
208 keystroke != '/') {
209 str[0] = keystroke;
210 str[1] = '\0';
211 if (len < 63)
212 strcat(clkname_str,
213 str);
214 }
215 }
216 }
217 }
218
219 keychar = toupper(keystroke);
220//#define DEBUG
221#ifdef DEBUG
222 killall_windows(1); fini_curses();
223 printf("key entered %d:%c\n", keystroke, keychar);
224 exit(1);
225#endif
226
227 if (keystroke == 13)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100228 *enter_hit = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100229
230 if (keychar == 'Q' && !*findparent_ncurses)
231 return 1;
232 if (keychar == 'R') {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100233 *refreshwin = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100234 options->ticktime = 3;
235 } else
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100236 *refreshwin = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100237
238 return 0;
239}
240
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100241int mainloop(struct powerdebug_options *options)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100242{
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100243 bool findparent_ncurses = false;
244 bool refreshwin = false;
245 bool enter_hit = false;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100246 int firsttime[TOTAL_FEATURE_WINS];
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100247 int i;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100248 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100249
250 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
251 firsttime[i] = 1;
252
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600253 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530254 int key = 0;
255 struct timeval tval;
256 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530257
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100258 if (!options->dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600259 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530260 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530261 create_windows();
262 show_header();
263 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530264
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100265 if (options->regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530266 read_regulator_info();
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100267 if (!options->dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530268 create_selectedwindow();
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100269 show_regulator_info(options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530270 }
Amit Arora47fd9182010-08-24 13:26:06 +0530271 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100272 print_regulator_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530273 }
274
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100275 if (options->clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530276 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530277 if (firsttime[CLOCK]) {
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100278 ret = init_clock_details(options->dump);
Amit Arora04f97742010-11-16 11:28:57 +0530279 if (!ret)
280 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530281 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530282 }
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100283 if (!ret && !options->dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530284 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530285
Amit Arora6e774cd2010-10-28 11:31:24 +0530286 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530287 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530288 int command = 0;
289
290 if (enter_hit)
291 command = CLOCK_SELECTED;
292 if (refreshwin)
293 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530294 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100295 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600296 highlighted_row,
297 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530298 highlighted_row = hrow;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100299 enter_hit = false;
Amit Arora3bd79162010-12-01 13:51:42 +0530300 } else
301 find_parents_for_clock(clkname_str,
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100302 enter_hit,
303 options->dump);
Amit Arora04f97742010-11-16 11:28:57 +0530304 }
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100305 if (!ret && options->dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100306 if (options->findparent)
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100307 read_and_dump_clock_info_one(options->clkarg, options->dump);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530308 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100309 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530310 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530311 }
Amit Arora47fd9182010-08-24 13:26:06 +0530312
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100313 if (options->sensors || selectedwindow == SENSOR) {
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100314 if (!options->dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530315 create_selectedwindow();
316 print_sensor_header();
317 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100318 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530319 }
320
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100321 if (options->dump)
Amit Arora47fd9182010-08-24 13:26:06 +0530322 break;
323
324 FD_ZERO(&readfds);
325 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100326 tval.tv_sec = options->ticktime;
327 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530328
329 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100330 if (!key)
331 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530332
Daniel Lezcano60a41022011-03-23 14:37:35 +0100333 if (keystroke_callback(&enter_hit, &findparent_ncurses,
334 clkname_str, &refreshwin, options))
335 break;
Amit Arora97006e52010-10-28 11:56:08 +0530336
Amit Arorae9e16b02010-08-03 10:15:20 +0530337 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100338
339 return 0;
340}
341
342int main(int argc, char **argv)
343{
344 struct powerdebug_options *options;
345
346 options = malloc(sizeof(*options));
347 if (!options) {
348 fprintf(stderr, "failed to allocated memory\n");
349 return -1;
350 }
351
352 if (getoptions(argc, argv, options)) {
353 usage();
354 return 1;
355 }
356
357 if (init_regulator_ds())
358 return 1;
359
360 if (mainloop(options))
361 return 1;
362
363 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530364}