blob: c413694cd3db0c5bbec6cf81846010e5b8f0f665 [file] [log] [blame]
Amit Arora728e0c92010-09-14 12:06:09 +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
17#include "powerdebug.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053018#include "clocks.h"
Amit Arora24ed7d12010-09-14 12:12:58 +053019
Amit Arora24ed7d12010-09-14 12:12:58 +053020static char clk_dir_path[PATH_MAX];
Amit Arora24ed7d12010-09-14 12:12:58 +053021static char clk_name[NAME_MAX];
Amit Aroraac4e8652010-11-09 11:16:53 +053022static int bold[MAX_LINES];
Amit Arora24ed7d12010-09-14 12:12:58 +053023
Amit Arora04f97742010-11-16 11:28:57 +053024int init_clock_details(void)
Amit Arora24ed7d12010-09-14 12:12:58 +053025{
Amit Arora6e774cd2010-10-28 11:31:24 +053026 char *path = debugfs_locate_mpoint();
27 struct stat buf;
Amit Aroraed3e5652010-10-27 12:02:53 +053028
Amit Arora6e774cd2010-10-28 11:31:24 +053029 if (path)
30 strcpy(clk_dir_path, path);
31 else {
Amit Arora04f97742010-11-16 11:28:57 +053032 create_selectedwindow();
33 sprintf(clock_lines[0], "Unable to locate debugfs mount point."
34 " Mount debugfs and try again..\n");
35 print_one_clock(0, clock_lines[0], 1, 0);
36 old_clock_line_no = 1;
37 return(1);
Amit Arora6e774cd2010-10-28 11:31:24 +053038 }
39 sprintf(clk_dir_path, "%s/clock", clk_dir_path);
Amit Arora3bc8c922010-11-16 11:27:38 +053040 strcpy(clk_dir_path, "/debug/clock"); // Hardcoded for testing..
Amit Arora6e774cd2010-10-28 11:31:24 +053041 if (stat(clk_dir_path, &buf)) {
Amit Arora04f97742010-11-16 11:28:57 +053042 create_selectedwindow();
43 sprintf(clock_lines[0], "Unable to find clock tree"
44 " information at %s.\n", clk_dir_path);
45 print_one_clock(0, clock_lines[0], 1, 0);
46 old_clock_line_no = 1;
47 return(1);
Amit Arora6e774cd2010-10-28 11:31:24 +053048 }
49 strcpy(clk_name, "");
Amit Arora04f97742010-11-16 11:28:57 +053050 return(0);
Amit Arora24ed7d12010-09-14 12:12:58 +053051}
Amit Arora728e0c92010-09-14 12:06:09 +053052
53int get_int_from(char *file)
54{
Amit Arora6e774cd2010-10-28 11:31:24 +053055 FILE *filep;
56 char result[NAME_MAX];
57 int ret;
Amit Arora728e0c92010-09-14 12:06:09 +053058
Amit Arora6e774cd2010-10-28 11:31:24 +053059 filep = fopen(file, "r");
Amit Arora728e0c92010-09-14 12:06:09 +053060
Amit Arora6e774cd2010-10-28 11:31:24 +053061 if (!filep)
62 return -1; //TBD : What should we return on failure, here ?
Amit Arora728e0c92010-09-14 12:06:09 +053063
Amit Arora6e774cd2010-10-28 11:31:24 +053064 ret = fscanf(filep, "%s", result);
65 fclose(filep);
Amit Arora728e0c92010-09-14 12:06:09 +053066
Amit Arora6e774cd2010-10-28 11:31:24 +053067 return atoi(result);
Amit Arora728e0c92010-09-14 12:06:09 +053068}
69
Amit Arora24ed7d12010-09-14 12:12:58 +053070int read_and_print_clock_info(int verbose, int hrow, int selected)
71{
Amit Aroraac4e8652010-11-09 11:16:53 +053072 if (!old_clock_line_no)
73 read_clock_info(clk_dir_path);
Amit Arora29cb7572010-10-05 17:40:29 +053074
Amit Arora031263a2010-11-09 11:12:41 +053075 if (!clocks_info->num_children) {
76 fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
77 exit(1);
Amit Arora6e774cd2010-10-28 11:31:24 +053078 }
Amit Arora728e0c92010-09-14 12:06:09 +053079
Amit Aroraac4e8652010-11-09 11:16:53 +053080 print_clock_info(verbose, hrow, selected);
81 //destroy_clocks_info();
82 hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
83
84 return hrow;
85}
86
Amit Arora3bc8c922010-11-16 11:27:38 +053087int calc_delta_screen_size(int hrow)
88{
89 if (hrow > (maxy - 3))
90 return hrow - (maxy - 3);
91
92 return 0;
93}
94
Amit Aroraac4e8652010-11-09 11:16:53 +053095void print_clock_info(int verbose, int hrow, int selected)
96{
Amit Arora3bc8c922010-11-16 11:27:38 +053097 int i, count = 0, delta;
Amit Aroraac4e8652010-11-09 11:16:53 +053098
99 (void)verbose;
100
101 print_clock_header();
Amit Arora728e0c92010-09-14 12:06:09 +0530102
Amit Arora031263a2010-11-09 11:12:41 +0530103 for (i = 0; i < clocks_info->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530104 add_clock_details_recur(clocks_info->children[i],
105 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530106
Amit Arora3bc8c922010-11-16 11:27:38 +0530107 delta = calc_delta_screen_size(hrow);
108
109 while (clock_lines[count + delta] &&
110 strcmp(clock_lines[count + delta], "")) {
111 if (count < delta) {
112 count++;
113 continue;
114 }
115 print_one_clock(count - delta, clock_lines[count + delta],
116 bold[count + delta], (hrow == (count + delta)));
Amit Arora031263a2010-11-09 11:12:41 +0530117 count++;
118 }
119
Amit Aroraac4e8652010-11-09 11:16:53 +0530120 old_clock_line_no = clock_line_no;
Amit Arora031263a2010-11-09 11:12:41 +0530121 clock_line_no = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530122}
123
Amit Aroraac4e8652010-11-09 11:16:53 +0530124void prepare_name_str(char *namestr, struct clock_info *clock)
125{
126 int i;
127
128 strcpy(namestr, "");
129 if (clock->level > 1)
130 for (i = 0; i < (clock->level - 1); i++)
131 strcat(namestr, " ");
132 strcat(namestr, clock->name);
133}
134
135void add_clock_details_recur(struct clock_info *clock, int hrow, int selected)
Amit Arora031263a2010-11-09 11:12:41 +0530136{
137 int i;
138 char *unit = " Hz";
139 char rate_str[64];
Amit Aroraac4e8652010-11-09 11:16:53 +0530140 char name_str[256];
Amit Arora031263a2010-11-09 11:12:41 +0530141 double drate = (double)clock->rate;
142
143 if (drate > 1000 && drate < 1000000) {
144 unit = "KHz";
145 drate /= 1000;
146 }
147 if (drate > 1000000) {
148 unit = "MHz";
149 drate /= 1000000;
150 }
151 if (clock->usecount)
152 bold[clock_line_no] = 1;
153 else
154 bold[clock_line_no] = 0;
155
156 sprintf(rate_str, "%.2f %s", drate, unit);
Amit Aroraac4e8652010-11-09 11:16:53 +0530157 prepare_name_str(name_str, clock);
158 sprintf(clock_lines[clock_line_no++], "%-55s %-4d %-12s %-12d %-12d",
159 name_str, clock->flags, rate_str, clock->usecount,
160 clock->num_children);
161
162 if (selected && (hrow == (clock_line_no - 1))) {
163 if (clock->expanded)
164 collapse_all_subclocks(clock);
165 else
166 clock->expanded = 1;
167 selected = 0;
168 }
Amit Arora031263a2010-11-09 11:12:41 +0530169
170 if (clock->expanded && clock->num_children)
171 for (i = 0; i < clock->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530172 add_clock_details_recur(clock->children[i],
173 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530174 strcpy(clock_lines[clock_line_no], "");
175}
176
Amit Aroraac4e8652010-11-09 11:16:53 +0530177void collapse_all_subclocks(struct clock_info *clock)
178{
179 int i;
180
181 clock->expanded = 0;
182 if (clock->num_children)
183 for (i = 0; i < clock->num_children; i++)
184 collapse_all_subclocks(clock->children[i]);
185}
186
Amit Arora031263a2010-11-09 11:12:41 +0530187void destroy_clocks_info(void)
188{
189 int i;
190
191 if (clocks_info->num_children) {
192 for (i = (clocks_info->num_children - 1); i >= 0 ; i--) {
193 destroy_clocks_info_recur(clocks_info->children[i]);
194 if (!i) {
195 free(clocks_info->children);
196 clocks_info->children = NULL;
197 }
198 }
199 }
200 free(clocks_info);
201 clocks_info = NULL;
202}
203
204void destroy_clocks_info_recur(struct clock_info *clock)
205{
206 int i;
207
208 if (clock && clock->num_children) {
209 for (i = (clock->num_children - 1); i >= 0; i--) {
210 fflush(stdin);
211 destroy_clocks_info_recur(clock->children[i]);
212 if (!i) {
Amit Arora031263a2010-11-09 11:12:41 +0530213 free(clock->children);
214 clock->children = NULL;
215 clock->num_children = 0;
216 }
217 }
218 }
Amit Arora728e0c92010-09-14 12:06:09 +0530219}
Amit Arora0e512722010-10-01 12:24:16 +0530220
Amit Aroraac4e8652010-11-09 11:16:53 +0530221void read_and_dump_clock_info(int verbose)
Amit Arora0e512722010-10-01 12:24:16 +0530222{
Amit Arora6e774cd2010-10-28 11:31:24 +0530223 (void)verbose;
224 printf("Clock Tree :\n");
225 printf("**********\n");
226 read_clock_info(clk_dir_path);
Amit Aroraac4e8652010-11-09 11:16:53 +0530227 dump_clock_info(clocks_info, 1, 1);
Amit Arora0e512722010-10-01 12:24:16 +0530228}
229
Amit Aroraeb6cba92010-10-25 16:03:21 +0530230void read_clock_info(char *clkpath)
Amit Arora0e512722010-10-01 12:24:16 +0530231{
Amit Arora6e774cd2010-10-28 11:31:24 +0530232 DIR *dir;
233 struct dirent *item;
234 char filename[NAME_MAX], clockname[NAME_MAX];
235 struct clock_info *child;
236 struct clock_info *cur;
Amit Arora0e512722010-10-01 12:24:16 +0530237
Amit Arora6e774cd2010-10-28 11:31:24 +0530238 dir = opendir(clkpath);
239 if (!dir)
240 return;
Amit Arora0e512722010-10-01 12:24:16 +0530241
Amit Arora6e774cd2010-10-28 11:31:24 +0530242 clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
243 memset(clocks_info, 0, sizeof(clocks_info));
244 strcpy(clocks_info->name, "/");
Amit Aroraac4e8652010-11-09 11:16:53 +0530245 clocks_info->level = 0;
Amit Arora0e512722010-10-01 12:24:16 +0530246
Amit Arora6e774cd2010-10-28 11:31:24 +0530247 while ((item = readdir(dir))) {
248 /* skip hidden dirs except ".." */
249 if (item->d_name[0] == '.')
250 continue;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530251
Amit Arora6e774cd2010-10-28 11:31:24 +0530252 strcpy(clockname, item->d_name);
253 sprintf(filename, "%s/%s", clkpath, item->d_name);
254 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
Amit Aroraac4e8652010-11-09 11:16:53 +0530255 memset(cur, 0, sizeof(struct clock_info));
Amit Arora6e774cd2010-10-28 11:31:24 +0530256 strcpy(cur->name, clockname);
257 cur->parent = clocks_info;
Amit Arora031263a2010-11-09 11:12:41 +0530258 cur->num_children = 0;
259 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530260 cur->level = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530261 insert_children(&clocks_info, cur);
262 child = read_clock_info_recur(filename, 2, cur);
263 }
264 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530265}
266
267struct clock_info *read_clock_info_recur(char *clkpath, int level,
Amit Arora6e774cd2010-10-28 11:31:24 +0530268 struct clock_info *parent)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530269{
Amit Arora6e774cd2010-10-28 11:31:24 +0530270 int ret = 0;
271 DIR *dir;
272 char filename[PATH_MAX];
273 struct dirent *item;
274 struct clock_info *cur = NULL;
275 struct stat buf;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530276
Amit Arora6e774cd2010-10-28 11:31:24 +0530277 dir = opendir(clkpath);
278 if (!dir)
279 return NULL;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530280
Amit Arora6e774cd2010-10-28 11:31:24 +0530281 while ((item = readdir(dir))) {
282 struct clock_info *child;
283 /* skip hidden dirs except ".." */
284 if (item->d_name[0] == '.' )
285 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530286
Amit Arora6e774cd2010-10-28 11:31:24 +0530287 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530288
Amit Arora6e774cd2010-10-28 11:31:24 +0530289 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530290
Amit Arora6e774cd2010-10-28 11:31:24 +0530291 if (ret < 0) {
292 printf("Error doing a stat on %s\n", filename);
293 exit(1);
294 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530295
Amit Arora6e774cd2010-10-28 11:31:24 +0530296 if (S_ISREG(buf.st_mode)) {
297 if (!strcmp(item->d_name, "flags"))
298 parent->flags = get_int_from(filename);
299 if (!strcmp(item->d_name, "rate"))
300 parent->rate = get_int_from(filename);
301 if (!strcmp(item->d_name, "usecount"))
302 parent->usecount = get_int_from(filename);
303 continue;
304 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530305
Amit Arora6e774cd2010-10-28 11:31:24 +0530306 if (!S_ISDIR(buf.st_mode))
307 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530308
Amit Arora6e774cd2010-10-28 11:31:24 +0530309 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
310 memset(cur, 0, sizeof(cur));
311 strcpy(cur->name, item->d_name);
312 cur->children = NULL;
313 cur->parent = NULL;
314 cur->num_children = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530315 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530316 cur->level = level;
Amit Arora6e774cd2010-10-28 11:31:24 +0530317 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora6e774cd2010-10-28 11:31:24 +0530318 insert_children(&parent, cur);
319 cur->parent = parent;
320 }
321 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530322
Amit Arora6e774cd2010-10-28 11:31:24 +0530323 return cur;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530324}
325
326void insert_children(struct clock_info **parent, struct clock_info *clk)
327{
Amit Arora031263a2010-11-09 11:12:41 +0530328 if (!(*parent)->num_children || (*parent)->children == NULL) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530329 (*parent)->children = (struct clock_info **)
330 malloc(sizeof(struct clock_info *)*2);
331 (*parent)->num_children = 0;
332 } else
333 (*parent)->children = (struct clock_info **)
334 realloc((*parent)->children,
335 sizeof(struct clock_info *) *
336 ((*parent)->num_children + 2));
337 if ((*parent)->num_children > 0)
338 (*parent)->children[(*parent)->num_children - 1]->last_child
339 = 0;
340 clk->last_child = 1;
341 (*parent)->children[(*parent)->num_children] = clk;
342 (*parent)->children[(*parent)->num_children + 1] = NULL;
343 (*parent)->num_children++;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530344}
345
346
Amit Aroraac4e8652010-11-09 11:16:53 +0530347void dump_clock_info(struct clock_info *clk, int level, int bmp)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530348{
Amit Arora6e774cd2010-10-28 11:31:24 +0530349 int i, j;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530350
Amit Arora6e774cd2010-10-28 11:31:24 +0530351 if (!clk)
352 return;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530353
Amit Arora6e774cd2010-10-28 11:31:24 +0530354 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
355 if (i == (level - 1)) {
356 if (clk->last_child)
357 printf("`-- ");
358 else
359 printf("|-- ");
360 } else {
361 if ((1<<j) & bmp)
362 printf("| ");
363 else
364 printf(" ");
365 }
366 }
Amit Arora031263a2010-11-09 11:12:41 +0530367
Amit Arora6e774cd2010-10-28 11:31:24 +0530368 if (clk == clocks_info)
369 printf("%s\n", clk->name);
370 else {
371 char *unit = "Hz";
372 double drate = (double)clk->rate;
373
374 if (drate > 1000 && drate < 1000000) {
375 unit = "KHz";
376 drate /= 1000;
377 }
378 if (drate > 1000000) {
379 unit = "MHz";
380 drate /= 1000000;
381 }
382 printf("%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
383 clk->name, clk->flags, clk->usecount, drate, unit);
Amit Arora6e774cd2010-10-28 11:31:24 +0530384 }
385 if (clk->children) {
386 int tbmp = bmp;
387 int xbmp = -1;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530388
Amit Arora6e774cd2010-10-28 11:31:24 +0530389 if (clk->last_child) {
390 xbmp ^= 1 << (level - 2);
391
392 xbmp = tbmp & xbmp;
393 } else
394 xbmp = bmp;
395 for (i = 0; i < clk->num_children; i++) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530396 tbmp = xbmp | (1 << level);
Amit Aroraac4e8652010-11-09 11:16:53 +0530397 dump_clock_info(clk->children[i], level + 1, tbmp);
Amit Arora6e774cd2010-10-28 11:31:24 +0530398 }
399 }
Amit Arora0e512722010-10-01 12:24:16 +0530400}
Amit Aroraed3e5652010-10-27 12:02:53 +0530401
402char *debugfs_locate_mpoint(void)
403{
404 int ret;
405 FILE *filep;
406 char **path;
407 char fsname[64];
408 struct statfs sfs;
409
410 path = likely_mpoints;
411 while (*path) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530412 ret = statfs(*path, &sfs);
413 if (ret >= 0 && sfs.f_type == (long)DEBUGFS_MAGIC)
Amit Aroraed3e5652010-10-27 12:02:53 +0530414 return *path;
415 path++;
416 }
417
418 filep = fopen("/proc/mounts", "r");
419 if (filep == NULL) {
420 fprintf(stderr, "powerdebug: Error opening /proc/mounts.");
Amit Arora6e774cd2010-10-28 11:31:24 +0530421 exit(1);
422 }
Amit Aroraed3e5652010-10-27 12:02:53 +0530423
424 while (fscanf(filep, "%*s %s %s %*s %*d %*d\n",
425 debugfs_mntpoint, fsname) == 2)
426 if (!strcmp(fsname, "debugfs"))
427 break;
428 fclose(filep);
429
430 if (strcmp(fsname, "debugfs"))
431 return NULL;
432
433 return debugfs_mntpoint;
434}