blob: 5088ed5c16ceb35be0289d85288849e067d435af [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>
Amit Arorae9e16b02010-08-03 10:15:20 +053017#include "powerdebug.h"
18
Amit Arora47fd9182010-08-24 13:26:06 +053019int dump;
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 Arorac93e0712010-10-07 13:51:53 +053023char *win_names[TOTAL_FEATURE_WINS] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053024 "Clocks",
Amit Kucheriaee851a12011-01-12 04:57:48 +053025 "Regulators",
26 "Sensors"
27};
Amit Arorac93e0712010-10-07 13:51:53 +053028
Amit Arora422c52f2010-12-02 16:22:29 +053029void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053030{
Amit Arora422c52f2010-12-02 16:22:29 +053031 printf("Usage: powerdebug [OPTIONS]\n");
32 printf("\n");
33 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
34 "[ -v ]\n");
35 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053036 printf(" -r, --regulator Show regulator information\n");
37 printf(" -s, --sensor Show sensor information\n");
38 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053039 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060040 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053041 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
42 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053043 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060044 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053045 printf(" -V, --version Show Version\n");
46 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053047}
48
Amit Arora17552782010-12-02 12:23:14 +053049void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053050{
Amit Arora17552782010-12-02 12:23:14 +053051 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053052}
53
Daniel Lezcano316bcae2011-03-23 14:37:30 +010054/*
55 * Options:
56 * -r, --regulator : regulator
57 * -s, --sensor : sensors
58 * -c, --clock : clocks
59 * -p, --findparents : clockname whose parents have to be found
60 * -t, --time : ticktime
61 * -d, --dump : dump
62 * -v, --verbose : verbose
63 * -V, --version : version
64 * -h, --help : help
65 * no option / default : show usage!
66 */
67
68static struct option long_options[] = {
69 { "regulator", 0, 0, 'r' },
70 { "sensor", 0, 0, 's' },
71 { "clock", 0, 0, 'c' },
72 { "findparents", 1, 0, 'p' },
73 { "time", 1, 0, 't' },
74 { "dump", 0, 0, 'd' },
75 { "verbose", 0, 0, 'v' },
76 { "version", 0, 0, 'V' },
77 { "help", 0, 0, 'h' },
78 { 0, 0, 0, 0 }
79};
80
81struct powerdebug_options {
82 int findparent;
83 int verbose;
84 int regulators;
85 int sensors;
86 int clocks;
87 int ticktime;
Daniel Lezcano9420fde2011-03-23 14:37:31 +010088 char *clkarg;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010089};
90
91int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053092{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010093 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053094
Daniel Lezcano316bcae2011-03-23 14:37:30 +010095 memset(options, 0, sizeof(*options));
96 options->ticktime = 10;
Amit Arorae9e16b02010-08-03 10:15:20 +053097
Amit Arorafefe8bf2010-08-05 13:31:20 +053098 while (1) {
99 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530100
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100101 c = getopt_long(argc, argv, "rscp:t:dvVh",
102 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530103 if (c == -1)
104 break;
105
106 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530107 case 'r':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100108 options->regulators = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530109 selectedwindow = REGULATOR;
110 break;
111 case 's':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100112 options->sensors = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530113 selectedwindow = SENSOR;
114 break;
115 case 'c':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100116 options->clocks = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530117 selectedwindow = CLOCK;
118 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530119 case 'p':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100120 options->findparent = 1;
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100121 options->clkarg = strdup(optarg);
122 if (!options->clkarg) {
123 fprintf(stderr, "failed to allocate memory");
124 return -1;
125 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530126 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530127 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100128 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530129 break;
130 case 'd':
131 dump = 1;
132 break;
133 case 'v':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100134 options->verbose = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530135 break;
136 case 'V':
137 version();
138 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100140 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600141 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530142 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100143 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530144 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530145 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530146
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100147 if (dump && !(options->regulators ||
148 options->clocks || options->sensors)) {
149 /* By Default lets show everything we have */
150 options->regulators = options->clocks = options->sensors = 1;
Amit Aroraa06a7302010-12-02 15:59:37 +0530151 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530152
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100153 if (options->findparent && (!options->clocks || !dump)) {
Amit Aroraf4fb8102010-11-30 13:55:50 +0530154 fprintf(stderr, "-p option passed without -c and -d."
155 " Exiting...\n");
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100156 return -1;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530157 }
158
Amit Aroraa06a7302010-12-02 15:59:37 +0530159 if (!dump && selectedwindow == -1)
Amit Kucheriaee851a12011-01-12 04:57:48 +0530160 selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530161
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100162 return 0;
163}
164
Daniel Lezcano60a41022011-03-23 14:37:35 +0100165int keystroke_callback(int *enter_hit, int *findparent_ncurses,
166 char *clkname_str, int *refreshwin,
167 struct powerdebug_options *options)
168{
169 char keychar;
170 int keystroke = getch();
171 int oldselectedwin = selectedwindow;
172
173 if (keystroke == EOF)
174 exit(0);
175
176 if (keystroke == KEY_RIGHT || keystroke == 9)
177 selectedwindow++;
178
179 if (keystroke == KEY_LEFT || keystroke == 353)
180 selectedwindow--;
181
182 if (selectedwindow >= TOTAL_FEATURE_WINS)
183 selectedwindow = 0;
184
185 if (selectedwindow < 0)
186 selectedwindow = TOTAL_FEATURE_WINS - 1;
187
188 if (selectedwindow == CLOCK) {
189 if (keystroke == KEY_DOWN)
190 highlighted_row++;
191 if (keystroke == KEY_UP && highlighted_row > 0)
192 highlighted_row--;
193 if (keystroke == 47)
194 *findparent_ncurses = 1;
195
196 if ((keystroke == 27 || oldselectedwin !=
197 selectedwindow) && *findparent_ncurses) {
198 *findparent_ncurses = 0;
199 clkname_str[0] = '\0';
200 }
201
202 if (*findparent_ncurses && keystroke != 13) {
203 int len = strlen(clkname_str);
204 char str[2];
205
206 if (keystroke == 263) {
207 if (len > 0)
208 len--;
209
210 clkname_str[len] = '\0';
211 } else {
212 if (strlen(clkname_str) ||
213 keystroke != '/') {
214 str[0] = keystroke;
215 str[1] = '\0';
216 if (len < 63)
217 strcat(clkname_str,
218 str);
219 }
220 }
221 }
222 }
223
224 keychar = toupper(keystroke);
225//#define DEBUG
226#ifdef DEBUG
227 killall_windows(1); fini_curses();
228 printf("key entered %d:%c\n", keystroke, keychar);
229 exit(1);
230#endif
231
232 if (keystroke == 13)
233 *enter_hit = 1;
234
235 if (keychar == 'Q' && !*findparent_ncurses)
236 return 1;
237 if (keychar == 'R') {
238 *refreshwin = 1;
239 options->ticktime = 3;
240 } else
241 *refreshwin = 0;
242
243 return 0;
244}
245
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100246int mainloop(struct powerdebug_options *options)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100247{
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100248 int findparent_ncurses = 0, refreshwin = 0;
249 int enter_hit = 0;
250 int firsttime[TOTAL_FEATURE_WINS];
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100251 int i;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100252 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100253
254 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
255 firsttime[i] = 1;
256
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600257 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530258 int key = 0;
259 struct timeval tval;
260 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530261
Amit Arora47fd9182010-08-24 13:26:06 +0530262 if (!dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600263 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530264 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530265 create_windows();
266 show_header();
267 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530268
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100269 if (options->regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530270 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530271 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530272 create_selectedwindow();
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100273 show_regulator_info(options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530274 }
Amit Arora47fd9182010-08-24 13:26:06 +0530275 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100276 print_regulator_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530277 }
278
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100279 if (options->clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530280 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530281 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530282 ret = init_clock_details();
283 if (!ret)
284 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530285 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530286 }
Amit Arora04f97742010-11-16 11:28:57 +0530287 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530288 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530289
Amit Arora6e774cd2010-10-28 11:31:24 +0530290 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530291 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530292 int command = 0;
293
294 if (enter_hit)
295 command = CLOCK_SELECTED;
296 if (refreshwin)
297 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530298 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100299 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600300 highlighted_row,
301 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530302 highlighted_row = hrow;
303 enter_hit = 0;
304 } else
305 find_parents_for_clock(clkname_str,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600306 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530307 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530308 if (!ret && dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100309 if (options->findparent)
310 read_and_dump_clock_info_one(options->clkarg);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530311 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100312 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530313 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530314 }
Amit Arora47fd9182010-08-24 13:26:06 +0530315
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100316 if (options->sensors || selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530317 if (!dump) {
318 create_selectedwindow();
319 print_sensor_header();
320 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100321 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530322 }
323
324 if (dump)
325 break;
326
327 FD_ZERO(&readfds);
328 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100329 tval.tv_sec = options->ticktime;
330 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530331
332 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100333 if (!key)
334 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530335
Daniel Lezcano60a41022011-03-23 14:37:35 +0100336 if (keystroke_callback(&enter_hit, &findparent_ncurses,
337 clkname_str, &refreshwin, options))
338 break;
Amit Arora97006e52010-10-28 11:56:08 +0530339
Amit Arorae9e16b02010-08-03 10:15:20 +0530340 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100341
342 return 0;
343}
344
345int main(int argc, char **argv)
346{
347 struct powerdebug_options *options;
348
349 options = malloc(sizeof(*options));
350 if (!options) {
351 fprintf(stderr, "failed to allocated memory\n");
352 return -1;
353 }
354
355 if (getoptions(argc, argv, options)) {
356 usage();
357 return 1;
358 }
359
360 if (init_regulator_ds())
361 return 1;
362
363 if (mainloop(options))
364 return 1;
365
366 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530367}