blob: 871b9475c7b226bd071f7019a99a88de3f645783 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
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 Arorae9e16b02010-08-03 10:15:20 +053017#include <getopt.h>
Amit Arorae9e16b02010-08-03 10:15:20 +053018#include "powerdebug.h"
19
Amit Arora47fd9182010-08-24 13:26:06 +053020int dump;
Amit Arora728e0c92010-09-14 12:06:09 +053021int highlighted_row;
Amit Arorac93e0712010-10-07 13:51:53 +053022int selectedwindow = -1;
Amit Arora728e0c92010-09-14 12:06:09 +053023double ticktime = 10.0; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053024
Amit Arorac93e0712010-10-07 13:51:53 +053025char *win_names[TOTAL_FEATURE_WINS] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053026 "Clocks",
Amit Kucheriaee851a12011-01-12 04:57:48 +053027 "Regulators",
28 "Sensors"
29};
Amit Arorac93e0712010-10-07 13:51:53 +053030
Amit Arora422c52f2010-12-02 16:22:29 +053031void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053032{
Amit Arora422c52f2010-12-02 16:22:29 +053033 printf("Usage: powerdebug [OPTIONS]\n");
34 printf("\n");
35 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
36 "[ -v ]\n");
37 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053038 printf(" -r, --regulator Show regulator information\n");
39 printf(" -s, --sensor Show sensor information\n");
40 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053041 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060042 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053043 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
44 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053045 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060046 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053047 printf(" -V, --version Show Version\n");
48 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053049
Amit Arora17552782010-12-02 12:23:14 +053050 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +053051}
52
Amit Arora17552782010-12-02 12:23:14 +053053void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053054{
Amit Arora17552782010-12-02 12:23:14 +053055 printf("powerdebug version %s\n", VERSION);
56 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +053057}
58
Amit Arorae9e16b02010-08-03 10:15:20 +053059int main(int argc, char **argv)
60{
Amit Arorac93e0712010-10-07 13:51:53 +053061 int c, i;
62 int firsttime[TOTAL_FEATURE_WINS];
Amit Aroraa06a7302010-12-02 15:59:37 +053063 int enter_hit = 0, verbose = 0, findparent_ncurses = 0, refreshwin = 0;
Amit Aroraf4fb8102010-11-30 13:55:50 +053064 int regulators = 0, sensors = 0, clocks = 0, findparent = 0;
Amit Arora3bd79162010-12-01 13:51:42 +053065 char clkarg[64], clkname_str[64];
Amit Arorae9e16b02010-08-03 10:15:20 +053066
Amit Arora6e774cd2010-10-28 11:31:24 +053067 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
68 firsttime[i] = 1;
Amit Arorac93e0712010-10-07 13:51:53 +053069
Amit Arorafefe8bf2010-08-05 13:31:20 +053070 /*
71 * Options:
Amit Arora6e774cd2010-10-28 11:31:24 +053072 * -r, --regulator : regulator
Amit Aroraa06a7302010-12-02 15:59:37 +053073 * -s, --sensor : sensors
74 * -c, --clock : clocks
Amit Aroraf4fb8102010-11-30 13:55:50 +053075 * -p, --findparents : clockname whose parents have to be found
Amit Arora728e0c92010-09-14 12:06:09 +053076 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +053077 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +053078 * -v, --verbose : verbose
79 * -V, --version : version
80 * -h, --help : help
81 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +053082 */
83
Amit Arorafefe8bf2010-08-05 13:31:20 +053084 while (1) {
85 int optindex = 0;
86 static struct option long_options[] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053087 {"regulator", 0, 0, 'r'},
88 {"sensor", 0, 0, 's'},
89 {"clock", 0, 0, 'c'},
Amit Arora203f4d42010-12-01 13:52:01 +053090 {"findparents", 1, 0, 'p'},
91 {"time", 1, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +053092 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +053093 {"verbose", 0, 0, 'v'},
94 {"version", 0, 0, 'V'},
95 {"help", 0, 0, 'h'},
96 {0, 0, 0, 0}
97 };
98
Amit Aroraf4fb8102010-11-30 13:55:50 +053099 c = getopt_long(argc, argv, "rscp:t:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530100 if (c == -1)
101 break;
102
103 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530104 case 'r':
105 regulators = 1;
106 selectedwindow = REGULATOR;
107 break;
108 case 's':
109 sensors = 1;
110 selectedwindow = SENSOR;
111 break;
112 case 'c':
113 clocks = 1;
114 selectedwindow = CLOCK;
115 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530116 case 'p':
117 findparent = 1;
118 strcpy(clkarg, optarg);
119 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530120 case 't':
121 ticktime = strtod(optarg, NULL);
122 break;
123 case 'd':
124 dump = 1;
125 break;
126 case 'v':
127 verbose = 1;
128 break;
129 case 'V':
130 version();
131 break;
132 case 'h':
Amit Arora422c52f2010-12-02 16:22:29 +0530133 usage();
Amit Arora6e774cd2010-10-28 11:31:24 +0530134 break;
135 case '?':
136 fprintf (stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600137 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530138 exit(1);
139 default:
Amit Arora422c52f2010-12-02 16:22:29 +0530140 usage();
Amit Arora6e774cd2010-10-28 11:31:24 +0530141 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530142 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530143 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530144
145 if (dump && !(regulators || clocks || sensors)) {
Amit Arora422c52f2010-12-02 16:22:29 +0530146 //fprintf(stderr, "Dump mode (-d) supported only with -c, -r "
147 // "or -s ..\n");
148 //usage();
149 // By Default lets show everything we have!
150 regulators = clocks = sensors = 1;
Amit Aroraa06a7302010-12-02 15:59:37 +0530151 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530152
Amit Aroraf4fb8102010-11-30 13:55:50 +0530153 if (findparent && (!clocks || !dump)) {
154 fprintf(stderr, "-p option passed without -c and -d."
155 " Exiting...\n");
Amit Arora422c52f2010-12-02 16:22:29 +0530156 usage();
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
162 init_regulator_ds();
163
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600164 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530165 int key = 0;
166 struct timeval tval;
167 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530168
Amit Arora47fd9182010-08-24 13:26:06 +0530169 if (!dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600170 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530171 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530172 create_windows();
173 show_header();
174 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530175
Amit Arora422c52f2010-12-02 16:22:29 +0530176 if (regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530177 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530178 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530179 create_selectedwindow();
Amit Arora47fd9182010-08-24 13:26:06 +0530180 show_regulator_info(verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530181 }
Amit Arora47fd9182010-08-24 13:26:06 +0530182 else
183 print_regulator_info(verbose);
184 }
185
Amit Arora422c52f2010-12-02 16:22:29 +0530186 if (clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530187 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530188 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530189 ret = init_clock_details();
190 if (!ret)
191 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530192 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530193 }
Amit Arora04f97742010-11-16 11:28:57 +0530194 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530195 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530196
Amit Arora6e774cd2010-10-28 11:31:24 +0530197 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530198 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530199 int command = 0;
200
201 if (enter_hit)
202 command = CLOCK_SELECTED;
203 if (refreshwin)
204 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530205 hrow = read_and_print_clock_info(
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600206 verbose,
207 highlighted_row,
208 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530209 highlighted_row = hrow;
210 enter_hit = 0;
211 } else
212 find_parents_for_clock(clkname_str,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600213 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530214 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530215 if (!ret && dump) {
216 if (findparent)
217 read_and_dump_clock_info_one(clkarg);
218 else
219 read_and_dump_clock_info(verbose);
220 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530221 }
Amit Arora47fd9182010-08-24 13:26:06 +0530222
Amit Arora422c52f2010-12-02 16:22:29 +0530223 if (sensors || selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530224 if (!dump) {
225 create_selectedwindow();
226 print_sensor_header();
227 } else
228 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530229 }
230
231 if (dump)
232 break;
233
234 FD_ZERO(&readfds);
235 FD_SET(0, &readfds);
236 tval.tv_sec = ticktime;
237 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
238
239 key = select(1, &readfds, NULL, NULL, &tval);
240
241 if (key) {
242 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530243 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530244 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530245
Amit Arora47fd9182010-08-24 13:26:06 +0530246 if (keystroke == EOF)
247 exit(0);
248
Amit Arora6e774cd2010-10-28 11:31:24 +0530249 if (keystroke == KEY_RIGHT || keystroke == 9)
250 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530251
Amit Arora6e774cd2010-10-28 11:31:24 +0530252 if (keystroke == KEY_LEFT || keystroke == 353)
253 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530254
Amit Arora6e774cd2010-10-28 11:31:24 +0530255 if (selectedwindow >= TOTAL_FEATURE_WINS)
256 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530257
Amit Arora6e774cd2010-10-28 11:31:24 +0530258 if (selectedwindow < 0)
259 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530260
Amit Arora6e774cd2010-10-28 11:31:24 +0530261 if (selectedwindow == CLOCK) {
262 if (keystroke == KEY_DOWN)
263 highlighted_row++;
264 if (keystroke == KEY_UP && highlighted_row > 0)
265 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530266 if (keystroke == 47)
267 findparent_ncurses = 1;
268
269 if ((keystroke == 27 || oldselectedwin !=
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600270 selectedwindow) && findparent_ncurses) {
Amit Arora3bd79162010-12-01 13:51:42 +0530271 findparent_ncurses = 0;
272 clkname_str[0] = '\0';
273 }
274
275 if (findparent_ncurses && keystroke != 13) {
276 int len = strlen(clkname_str);
277 char str[2];
278
279 if (keystroke == 263) {
280 if (len > 0)
281 len--;
282
283 clkname_str[len] = '\0';
284 } else {
285 if (strlen(clkname_str) ||
286 keystroke != '/') {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600287 str[0] = keystroke;
288 str[1] = '\0';
289 if (len < 63)
290 strcat(clkname_str,
291 str);
Amit Arora3bd79162010-12-01 13:51:42 +0530292 }
293 }
294 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530295 }
Amit Arora728e0c92010-09-14 12:06:09 +0530296
Amit Arora47fd9182010-08-24 13:26:06 +0530297 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530298//#define DEBUG
299#ifdef DEBUG
300 killall_windows(1); fini_curses();
301 printf("key entered %d:%c\n", keystroke, keychar);
302 exit(1);
303#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530304
Amit Arora6e774cd2010-10-28 11:31:24 +0530305 if (keystroke == 13)
306 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530307
Amit Arora3bd79162010-12-01 13:51:42 +0530308 if (keychar == 'Q' && !findparent_ncurses)
Amit Arora47fd9182010-08-24 13:26:06 +0530309 exit(0);
Amit Aroraa06a7302010-12-02 15:59:37 +0530310 if (keychar == 'R') {
311 refreshwin = 1;
Amit Arora47fd9182010-08-24 13:26:06 +0530312 ticktime = 3;
Amit Aroraa06a7302010-12-02 15:59:37 +0530313 } else
314 refreshwin = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530315 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530316 }
Amit Arora85fd4952010-08-05 14:04:50 +0530317 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530318}