blob: 6375900314bb11b6c69ea23889977061271e7d0c [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
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010020bool dump = false;
Amit Arora728e0c92010-09-14 12:06:09 +053021int highlighted_row;
Amit Arorac93e0712010-10-07 13:51:53 +053022int selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053023
Amit Arorac93e0712010-10-07 13:51:53 +053024char *win_names[TOTAL_FEATURE_WINS] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053025 "Clocks",
Amit Kucheriaee851a12011-01-12 04:57:48 +053026 "Regulators",
27 "Sensors"
28};
Amit Arorac93e0712010-10-07 13:51:53 +053029
Amit Arora422c52f2010-12-02 16:22:29 +053030void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053031{
Amit Arora422c52f2010-12-02 16:22:29 +053032 printf("Usage: powerdebug [OPTIONS]\n");
33 printf("\n");
34 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
35 "[ -v ]\n");
36 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053037 printf(" -r, --regulator Show regulator information\n");
38 printf(" -s, --sensor Show sensor information\n");
39 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053040 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060041 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053042 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
43 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053044 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060045 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053046 printf(" -V, --version Show Version\n");
47 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053048}
49
Amit Arora17552782010-12-02 12:23:14 +053050void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053051{
Amit Arora17552782010-12-02 12:23:14 +053052 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053053}
54
Daniel Lezcano316bcae2011-03-23 14:37:30 +010055/*
56 * Options:
57 * -r, --regulator : regulator
58 * -s, --sensor : sensors
59 * -c, --clock : clocks
60 * -p, --findparents : clockname whose parents have to be found
61 * -t, --time : ticktime
62 * -d, --dump : dump
63 * -v, --verbose : verbose
64 * -V, --version : version
65 * -h, --help : help
66 * no option / default : show usage!
67 */
68
69static struct option long_options[] = {
70 { "regulator", 0, 0, 'r' },
71 { "sensor", 0, 0, 's' },
72 { "clock", 0, 0, 'c' },
73 { "findparents", 1, 0, 'p' },
74 { "time", 1, 0, 't' },
75 { "dump", 0, 0, 'd' },
76 { "verbose", 0, 0, 'v' },
77 { "version", 0, 0, 'V' },
78 { "help", 0, 0, 'h' },
79 { 0, 0, 0, 0 }
80};
81
82struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010083 bool verbose;
84 bool findparent;
85 bool regulators;
86 bool sensors;
87 bool clocks;
88 unsigned int ticktime;
Daniel Lezcano9420fde2011-03-23 14:37:31 +010089 char *clkarg;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010090};
91
92int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053093{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010094 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053095
Daniel Lezcano316bcae2011-03-23 14:37:30 +010096 memset(options, 0, sizeof(*options));
97 options->ticktime = 10;
Amit Arorae9e16b02010-08-03 10:15:20 +053098
Amit Arorafefe8bf2010-08-05 13:31:20 +053099 while (1) {
100 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530101
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100102 c = getopt_long(argc, argv, "rscp:t:dvVh",
103 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530104 if (c == -1)
105 break;
106
107 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530108 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100109 options->regulators = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530110 selectedwindow = REGULATOR;
111 break;
112 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100113 options->sensors = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530114 selectedwindow = SENSOR;
115 break;
116 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100117 options->clocks = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530118 selectedwindow = CLOCK;
119 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530120 case 'p':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100121 options->findparent = true;
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100122 options->clkarg = strdup(optarg);
123 if (!options->clkarg) {
124 fprintf(stderr, "failed to allocate memory");
125 return -1;
126 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530127 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530128 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100129 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 break;
131 case 'd':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100132 dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530133 break;
134 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100135 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530136 break;
137 case 'V':
138 version();
139 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530140 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100141 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600142 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530143 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100144 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530145 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530146 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530147
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100148 if (dump && !(options->regulators ||
149 options->clocks || options->sensors)) {
150 /* By Default lets show everything we have */
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100151 options->regulators = options->clocks = options->sensors = true;
Amit Aroraa06a7302010-12-02 15:59:37 +0530152 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530153
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100154 if (options->findparent && (!options->clocks || !dump)) {
Amit Aroraf4fb8102010-11-30 13:55:50 +0530155 fprintf(stderr, "-p option passed without -c and -d."
156 " Exiting...\n");
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100157 return -1;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530158 }
159
Amit Aroraa06a7302010-12-02 15:59:37 +0530160 if (!dump && selectedwindow == -1)
Amit Kucheriaee851a12011-01-12 04:57:48 +0530161 selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530162
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100163 return 0;
164}
165
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100166int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
167 char *clkname_str, bool *refreshwin,
Daniel Lezcano60a41022011-03-23 14:37:35 +0100168 struct powerdebug_options *options)
169{
170 char keychar;
171 int keystroke = getch();
172 int oldselectedwin = selectedwindow;
173
174 if (keystroke == EOF)
175 exit(0);
176
177 if (keystroke == KEY_RIGHT || keystroke == 9)
178 selectedwindow++;
179
180 if (keystroke == KEY_LEFT || keystroke == 353)
181 selectedwindow--;
182
183 if (selectedwindow >= TOTAL_FEATURE_WINS)
184 selectedwindow = 0;
185
186 if (selectedwindow < 0)
187 selectedwindow = TOTAL_FEATURE_WINS - 1;
188
189 if (selectedwindow == CLOCK) {
190 if (keystroke == KEY_DOWN)
191 highlighted_row++;
192 if (keystroke == KEY_UP && highlighted_row > 0)
193 highlighted_row--;
194 if (keystroke == 47)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100195 *findparent_ncurses = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100196
197 if ((keystroke == 27 || oldselectedwin !=
198 selectedwindow) && *findparent_ncurses) {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100199 *findparent_ncurses = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100200 clkname_str[0] = '\0';
201 }
202
203 if (*findparent_ncurses && keystroke != 13) {
204 int len = strlen(clkname_str);
205 char str[2];
206
207 if (keystroke == 263) {
208 if (len > 0)
209 len--;
210
211 clkname_str[len] = '\0';
212 } else {
213 if (strlen(clkname_str) ||
214 keystroke != '/') {
215 str[0] = keystroke;
216 str[1] = '\0';
217 if (len < 63)
218 strcat(clkname_str,
219 str);
220 }
221 }
222 }
223 }
224
225 keychar = toupper(keystroke);
226//#define DEBUG
227#ifdef DEBUG
228 killall_windows(1); fini_curses();
229 printf("key entered %d:%c\n", keystroke, keychar);
230 exit(1);
231#endif
232
233 if (keystroke == 13)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100234 *enter_hit = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100235
236 if (keychar == 'Q' && !*findparent_ncurses)
237 return 1;
238 if (keychar == 'R') {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100239 *refreshwin = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100240 options->ticktime = 3;
241 } else
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100242 *refreshwin = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100243
244 return 0;
245}
246
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100247int mainloop(struct powerdebug_options *options)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100248{
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100249 bool findparent_ncurses = false;
250 bool refreshwin = false;
251 bool enter_hit = false;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100252 int firsttime[TOTAL_FEATURE_WINS];
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100253 int i;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100254 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100255
256 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
257 firsttime[i] = 1;
258
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600259 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530260 int key = 0;
261 struct timeval tval;
262 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530263
Amit Arora47fd9182010-08-24 13:26:06 +0530264 if (!dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600265 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530266 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530267 create_windows();
268 show_header();
269 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530270
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100271 if (options->regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530272 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530273 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530274 create_selectedwindow();
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100275 show_regulator_info(options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530276 }
Amit Arora47fd9182010-08-24 13:26:06 +0530277 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100278 print_regulator_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530279 }
280
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100281 if (options->clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530282 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530283 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530284 ret = init_clock_details();
285 if (!ret)
286 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530287 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530288 }
Amit Arora04f97742010-11-16 11:28:57 +0530289 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530290 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530291
Amit Arora6e774cd2010-10-28 11:31:24 +0530292 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530293 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530294 int command = 0;
295
296 if (enter_hit)
297 command = CLOCK_SELECTED;
298 if (refreshwin)
299 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530300 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100301 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600302 highlighted_row,
303 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530304 highlighted_row = hrow;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100305 enter_hit = false;
Amit Arora3bd79162010-12-01 13:51:42 +0530306 } else
307 find_parents_for_clock(clkname_str,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600308 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530309 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530310 if (!ret && dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100311 if (options->findparent)
312 read_and_dump_clock_info_one(options->clkarg);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530313 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100314 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530315 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530316 }
Amit Arora47fd9182010-08-24 13:26:06 +0530317
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100318 if (options->sensors || selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530319 if (!dump) {
320 create_selectedwindow();
321 print_sensor_header();
322 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100323 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530324 }
325
326 if (dump)
327 break;
328
329 FD_ZERO(&readfds);
330 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100331 tval.tv_sec = options->ticktime;
332 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530333
334 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100335 if (!key)
336 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530337
Daniel Lezcano60a41022011-03-23 14:37:35 +0100338 if (keystroke_callback(&enter_hit, &findparent_ncurses,
339 clkname_str, &refreshwin, options))
340 break;
Amit Arora97006e52010-10-28 11:56:08 +0530341
Amit Arorae9e16b02010-08-03 10:15:20 +0530342 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100343
344 return 0;
345}
346
347int main(int argc, char **argv)
348{
349 struct powerdebug_options *options;
350
351 options = malloc(sizeof(*options));
352 if (!options) {
353 fprintf(stderr, "failed to allocated memory\n");
354 return -1;
355 }
356
357 if (getoptions(argc, argv, options)) {
358 usage();
359 return 1;
360 }
361
362 if (init_regulator_ds())
363 return 1;
364
365 if (mainloop(options))
366 return 1;
367
368 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530369}