blob: e12d917c949d537bcf89525e7635b7037b94e87c [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"
42 " 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"
46 " -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",
137 argv[0], optopt);
138 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 Arora47fd9182010-08-24 13:26:06 +0530164 while(1) {
165 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 Arorac93e0712010-10-07 13:51:53 +0530170 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 Arora47fd9182010-08-24 13:26:06 +0530176
Amit Arora422c52f2010-12-02 16:22:29 +0530177 if (regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530178 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530179 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530180 create_selectedwindow();
Amit Arora47fd9182010-08-24 13:26:06 +0530181 show_regulator_info(verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530182 }
Amit Arora47fd9182010-08-24 13:26:06 +0530183 else
184 print_regulator_info(verbose);
185 }
186
Amit Arora422c52f2010-12-02 16:22:29 +0530187 if (clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530188 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530189 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530190 ret = init_clock_details();
191 if (!ret)
192 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530193 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530194 }
Amit Arora04f97742010-11-16 11:28:57 +0530195 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530196 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530197
Amit Arora6e774cd2010-10-28 11:31:24 +0530198 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530199 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530200 int command = 0;
201
202 if (enter_hit)
203 command = CLOCK_SELECTED;
204 if (refreshwin)
205 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530206 hrow = read_and_print_clock_info(
207 verbose,
Amit Arora6e774cd2010-10-28 11:31:24 +0530208 highlighted_row,
Amit Aroraa06a7302010-12-02 15:59:37 +0530209 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530210 highlighted_row = hrow;
211 enter_hit = 0;
212 } else
213 find_parents_for_clock(clkname_str,
214 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530215 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530216 if (!ret && dump) {
217 if (findparent)
218 read_and_dump_clock_info_one(clkarg);
219 else
220 read_and_dump_clock_info(verbose);
221 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530222 }
Amit Arora47fd9182010-08-24 13:26:06 +0530223
Amit Arora422c52f2010-12-02 16:22:29 +0530224 if (sensors || selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530225 if (!dump) {
226 create_selectedwindow();
227 print_sensor_header();
228 } else
229 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530230 }
231
232 if (dump)
233 break;
234
235 FD_ZERO(&readfds);
236 FD_SET(0, &readfds);
237 tval.tv_sec = ticktime;
238 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
239
240 key = select(1, &readfds, NULL, NULL, &tval);
241
242 if (key) {
243 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530244 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530245 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530246
Amit Arora47fd9182010-08-24 13:26:06 +0530247 if (keystroke == EOF)
248 exit(0);
249
Amit Arora6e774cd2010-10-28 11:31:24 +0530250 if (keystroke == KEY_RIGHT || keystroke == 9)
251 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530252
Amit Arora6e774cd2010-10-28 11:31:24 +0530253 if (keystroke == KEY_LEFT || keystroke == 353)
254 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530255
Amit Arora6e774cd2010-10-28 11:31:24 +0530256 if (selectedwindow >= TOTAL_FEATURE_WINS)
257 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530258
Amit Arora6e774cd2010-10-28 11:31:24 +0530259 if (selectedwindow < 0)
260 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530261
Amit Arora6e774cd2010-10-28 11:31:24 +0530262 if (selectedwindow == CLOCK) {
263 if (keystroke == KEY_DOWN)
264 highlighted_row++;
265 if (keystroke == KEY_UP && highlighted_row > 0)
266 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530267 if (keystroke == 47)
268 findparent_ncurses = 1;
269
270 if ((keystroke == 27 || oldselectedwin !=
271 selectedwindow) && findparent_ncurses) {
272 findparent_ncurses = 0;
273 clkname_str[0] = '\0';
274 }
275
276 if (findparent_ncurses && keystroke != 13) {
277 int len = strlen(clkname_str);
278 char str[2];
279
280 if (keystroke == 263) {
281 if (len > 0)
282 len--;
283
284 clkname_str[len] = '\0';
285 } else {
286 if (strlen(clkname_str) ||
287 keystroke != '/') {
288 str[0] = keystroke;
289 str[1] = '\0';
290 if (len < 63)
291 strcat(clkname_str,
292 str);
293 }
294 }
295 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530296 }
Amit Arora728e0c92010-09-14 12:06:09 +0530297
Amit Arora47fd9182010-08-24 13:26:06 +0530298 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530299//#define DEBUG
300#ifdef DEBUG
301 killall_windows(1); fini_curses();
302 printf("key entered %d:%c\n", keystroke, keychar);
303 exit(1);
304#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530305
Amit Arora6e774cd2010-10-28 11:31:24 +0530306 if (keystroke == 13)
307 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530308
Amit Arora3bd79162010-12-01 13:51:42 +0530309 if (keychar == 'Q' && !findparent_ncurses)
Amit Arora47fd9182010-08-24 13:26:06 +0530310 exit(0);
Amit Aroraa06a7302010-12-02 15:59:37 +0530311 if (keychar == 'R') {
312 refreshwin = 1;
Amit Arora47fd9182010-08-24 13:26:06 +0530313 ticktime = 3;
Amit Aroraa06a7302010-12-02 15:59:37 +0530314 } else
315 refreshwin = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530316 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530317 }
Amit Arora85fd4952010-08-05 14:04:50 +0530318 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530319}
Amit Arora17552782010-12-02 12:23:14 +0530320