blob: 2f0992b53455dd96ef2a183e93801ee7699fbdf9 [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 Arorae9e16b02010-08-03 10:15:20 +053021
Amit Arora422c52f2010-12-02 16:22:29 +053022void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053023{
Amit Arora422c52f2010-12-02 16:22:29 +053024 printf("Usage: powerdebug [OPTIONS]\n");
25 printf("\n");
26 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
27 "[ -v ]\n");
28 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053029 printf(" -r, --regulator Show regulator information\n");
30 printf(" -s, --sensor Show sensor information\n");
31 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053032 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060033 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053034 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
35 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053036 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060037 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053038 printf(" -V, --version Show Version\n");
39 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053040}
41
Amit Arora17552782010-12-02 12:23:14 +053042void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053043{
Amit Arora17552782010-12-02 12:23:14 +053044 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053045}
46
Daniel Lezcano316bcae2011-03-23 14:37:30 +010047/*
48 * Options:
49 * -r, --regulator : regulator
50 * -s, --sensor : sensors
51 * -c, --clock : clocks
52 * -p, --findparents : clockname whose parents have to be found
53 * -t, --time : ticktime
54 * -d, --dump : dump
55 * -v, --verbose : verbose
56 * -V, --version : version
57 * -h, --help : help
58 * no option / default : show usage!
59 */
60
61static struct option long_options[] = {
62 { "regulator", 0, 0, 'r' },
63 { "sensor", 0, 0, 's' },
64 { "clock", 0, 0, 'c' },
65 { "findparents", 1, 0, 'p' },
66 { "time", 1, 0, 't' },
67 { "dump", 0, 0, 'd' },
68 { "verbose", 0, 0, 'v' },
69 { "version", 0, 0, 'V' },
70 { "help", 0, 0, 'h' },
71 { 0, 0, 0, 0 }
72};
73
74struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010075 bool verbose;
76 bool findparent;
77 bool regulators;
78 bool sensors;
79 bool clocks;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010080 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010081 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010082 int selectedwindow;
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;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010092 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053093
Amit Arorafefe8bf2010-08-05 13:31:20 +053094 while (1) {
95 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053096
Daniel Lezcano316bcae2011-03-23 14:37:30 +010097 c = getopt_long(argc, argv, "rscp:t:dvVh",
98 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +053099 if (c == -1)
100 break;
101
102 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530103 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100104 options->regulators = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100105 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530106 break;
107 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100108 options->sensors = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100109 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530110 break;
111 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100112 options->clocks = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100113 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530114 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530115 case 'p':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100116 options->findparent = true;
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100117 options->clkarg = strdup(optarg);
118 if (!options->clkarg) {
119 fprintf(stderr, "failed to allocate memory");
120 return -1;
121 }
Amit Kucheriaeab558a2011-03-25 09:51:41 +0200122 options->dump = true; /* Assume -dc in case of -p */
123 options->clocks = true;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530124 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530125 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100126 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530127 break;
128 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100129 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 break;
131 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100132 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530133 break;
134 case 'V':
135 version();
136 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530137 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100138 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600139 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530140 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100141 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530142 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530143 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530144
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100145 if (options->dump && !(options->regulators ||
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100146 options->clocks || options->sensors)) {
147 /* By Default lets show everything we have */
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100148 options->regulators = options->clocks = options->sensors = true;
Amit Aroraa06a7302010-12-02 15:59:37 +0530149 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530150
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100151 if (!options->dump && options->selectedwindow == -1)
152 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530153
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100154 return 0;
155}
156
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100157int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
158 char *clkname_str, bool *refreshwin,
Daniel Lezcano60a41022011-03-23 14:37:35 +0100159 struct powerdebug_options *options)
160{
161 char keychar;
162 int keystroke = getch();
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100163 int oldselectedwin = options->selectedwindow;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100164
165 if (keystroke == EOF)
166 exit(0);
167
168 if (keystroke == KEY_RIGHT || keystroke == 9)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100169 options->selectedwindow++;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100170
171 if (keystroke == KEY_LEFT || keystroke == 353)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100172 options->selectedwindow--;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100173
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100174 if (options->selectedwindow >= TOTAL_FEATURE_WINS)
175 options->selectedwindow = 0;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100176
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100177 if (options->selectedwindow < 0)
178 options->selectedwindow = TOTAL_FEATURE_WINS - 1;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100179
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100180 if (options->selectedwindow == CLOCK) {
Daniel Lezcano60a41022011-03-23 14:37:35 +0100181 if (keystroke == KEY_DOWN)
182 highlighted_row++;
183 if (keystroke == KEY_UP && highlighted_row > 0)
184 highlighted_row--;
185 if (keystroke == 47)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100186 *findparent_ncurses = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100187
188 if ((keystroke == 27 || oldselectedwin !=
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100189 options->selectedwindow) && *findparent_ncurses) {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100190 *findparent_ncurses = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100191 clkname_str[0] = '\0';
192 }
193
194 if (*findparent_ncurses && keystroke != 13) {
195 int len = strlen(clkname_str);
196 char str[2];
197
198 if (keystroke == 263) {
199 if (len > 0)
200 len--;
201
202 clkname_str[len] = '\0';
203 } else {
204 if (strlen(clkname_str) ||
205 keystroke != '/') {
206 str[0] = keystroke;
207 str[1] = '\0';
208 if (len < 63)
209 strcat(clkname_str,
210 str);
211 }
212 }
213 }
214 }
215
216 keychar = toupper(keystroke);
217//#define DEBUG
218#ifdef DEBUG
219 killall_windows(1); fini_curses();
220 printf("key entered %d:%c\n", keystroke, keychar);
221 exit(1);
222#endif
223
224 if (keystroke == 13)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100225 *enter_hit = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100226
227 if (keychar == 'Q' && !*findparent_ncurses)
228 return 1;
229 if (keychar == 'R') {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100230 *refreshwin = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100231 options->ticktime = 3;
232 } else
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100233 *refreshwin = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100234
235 return 0;
236}
237
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100238int mainloop(struct powerdebug_options *options)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100239{
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100240 bool findparent_ncurses = false;
241 bool refreshwin = false;
242 bool enter_hit = false;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100243 int firsttime[TOTAL_FEATURE_WINS];
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100244 int i;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100245 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100246
247 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
248 firsttime[i] = 1;
249
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600250 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530251 int key = 0;
252 struct timeval tval;
253 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530254
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100255 if (!options->dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600256 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530257 init_curses();
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100258 create_windows(options->selectedwindow);
259 show_header(options->selectedwindow);
Amit Arora47fd9182010-08-24 13:26:06 +0530260 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530261
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100262 if (options->regulators || options->selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530263 read_regulator_info();
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100264 if (!options->dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100265 create_selectedwindow(options->selectedwindow);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100266 show_regulator_info(options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530267 }
Amit Arora47fd9182010-08-24 13:26:06 +0530268 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100269 print_regulator_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530270 }
271
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100272 if (options->clocks || options->selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530273 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530274 if (firsttime[CLOCK]) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100275 ret = init_clock_details(options->dump,
276 options->selectedwindow);
Amit Arora04f97742010-11-16 11:28:57 +0530277 if (!ret)
278 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530279 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530280 }
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100281 if (!ret && !options->dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530282 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530283
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100284 create_selectedwindow(options->selectedwindow);
Amit Arora3bd79162010-12-01 13:51:42 +0530285 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530286 int command = 0;
287
288 if (enter_hit)
289 command = CLOCK_SELECTED;
290 if (refreshwin)
291 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530292 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100293 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600294 highlighted_row,
295 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530296 highlighted_row = hrow;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100297 enter_hit = false;
Amit Arora3bd79162010-12-01 13:51:42 +0530298 } else
299 find_parents_for_clock(clkname_str,
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100300 enter_hit,
301 options->dump);
Amit Arora04f97742010-11-16 11:28:57 +0530302 }
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100303 if (!ret && options->dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100304 if (options->findparent)
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100305 read_and_dump_clock_info_one(options->clkarg, options->dump);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530306 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100307 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530308 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530309 }
Amit Arora47fd9182010-08-24 13:26:06 +0530310
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100311 if (options->sensors || options->selectedwindow == SENSOR) {
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100312 if (!options->dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100313 create_selectedwindow(options->selectedwindow);
Amit Arora6e774cd2010-10-28 11:31:24 +0530314 print_sensor_header();
315 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100316 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530317 }
318
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100319 if (options->dump)
Amit Arora47fd9182010-08-24 13:26:06 +0530320 break;
321
322 FD_ZERO(&readfds);
323 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100324 tval.tv_sec = options->ticktime;
325 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530326
327 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100328 if (!key)
329 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530330
Daniel Lezcano60a41022011-03-23 14:37:35 +0100331 if (keystroke_callback(&enter_hit, &findparent_ncurses,
332 clkname_str, &refreshwin, options))
333 break;
Amit Arora97006e52010-10-28 11:56:08 +0530334
Amit Arorae9e16b02010-08-03 10:15:20 +0530335 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100336
337 return 0;
338}
339
340int main(int argc, char **argv)
341{
342 struct powerdebug_options *options;
343
344 options = malloc(sizeof(*options));
345 if (!options) {
346 fprintf(stderr, "failed to allocated memory\n");
347 return -1;
348 }
349
350 if (getoptions(argc, argv, options)) {
351 usage();
352 return 1;
353 }
354
355 if (init_regulator_ds())
356 return 1;
357
358 if (mainloop(options))
359 return 1;
360
361 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530362}