blob: 1635e9dbbad4c5376bb2f87066fd01dbe9f03d67 [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 Arora24ed7d12010-09-14 12:12:58 +053018#include <errno.h>
Amit Aroraeb6cba92010-10-25 16:03:21 +053019#include <sys/stat.h>
Amit Arora24ed7d12010-09-14 12:12:58 +053020
21static int clk_tree_level = 1;
22static char clk_dir_path[PATH_MAX];
23static char highlighted_path[PATH_MAX];
24static char clk_name[NAME_MAX];
25static int gadder = 0;
26
27
28void init_clock_details(void)
29{
30 strcpy(clk_dir_path, "/debug/clock");
31 strcpy(clk_name, "");
32 strcpy(highlighted_path, "");
33}
Amit Arora728e0c92010-09-14 12:06:09 +053034
35int get_int_from(char *file)
36{
37 FILE *filep;
38 char result[NAME_MAX];
39 int ret;
40
41 filep = fopen(file, "r");
42
43 if (!filep)
44 return -1; //TBD : What should we return on failure, here ?
45
46 ret = fscanf(filep, "%s", result);
47 fclose(filep);
48
49 return atoi(result);
50}
51
Amit Arora24ed7d12010-09-14 12:12:58 +053052int read_and_print_clock_info(int verbose, int hrow, int selected)
53{
54 if (selected) {
55 // if (!(strcmp(clk_dir_path, "/debug/clock") &&
56 // strcmp(highlighted_path, ".."))) {
57 strcpy(clk_dir_path, highlighted_path);
58 hrow = 0;
59 clk_tree_level += gadder;
60 if (clk_tree_level <=0)
61 clk_tree_level = 1;
62 }
63
64 hrow = read_and_print_clock_one_level(verbose, hrow, selected);
65
66 return hrow;
67}
68
69void set_hl_dir_path_to_parent(void)
70{
71 char *ptr;
72
73 strcpy(highlighted_path, clk_dir_path);
74 if (strcmp(clk_dir_path, "/clock/debug")) {
75 ptr = strrchr(highlighted_path, '/');
76 if (ptr)
77 ptr[0]='\0';
78 }
79}
80
81int read_and_print_clock_one_level(int verbose, int hrow, int selected)
Amit Arora728e0c92010-09-14 12:06:09 +053082{
83 int line = 0, usecount = 0, flags = 0, rate = 0;
Amit Arora24ed7d12010-09-14 12:12:58 +053084// int parent_dir_row = 1000;
Amit Arora728e0c92010-09-14 12:06:09 +053085 DIR *dir, *subdir;
86 char filename[PATH_MAX], devpath[PATH_MAX], clockname[NAME_MAX];
87 struct dirent *item, *subitem;
88
Amit Arora29cb7572010-10-05 17:40:29 +053089 (void)verbose;
90
Amit Arora24ed7d12010-09-14 12:12:58 +053091 print_clock_header(clk_tree_level);
Amit Arora728e0c92010-09-14 12:06:09 +053092
Amit Arora24ed7d12010-09-14 12:12:58 +053093 sprintf(filename, "%s", clk_dir_path);
Amit Arora728e0c92010-09-14 12:06:09 +053094
95 dir = opendir(filename);
96 if (!dir)
97 return 0;
98
99 while ((item = readdir(dir))) {
Amit Arora24ed7d12010-09-14 12:12:58 +0530100 /* skip hidden dirs except ".." */
101 if (item->d_name[0] == '.' && strcmp(item->d_name, ".."))
Amit Arora728e0c92010-09-14 12:06:09 +0530102 continue;
103
Amit Arora24ed7d12010-09-14 12:12:58 +0530104 if (selected && hrow == line && !strcmp(item->d_name, "..")) {
105 sprintf(devpath, "%s", clk_dir_path);
106 strcpy(clockname, "..");
107 } else {
108 sprintf(devpath, "%s/%s", clk_dir_path, item->d_name);
109 strcpy(clockname, item->d_name);
110 }
Amit Arora728e0c92010-09-14 12:06:09 +0530111
112 subdir = opendir(devpath);
113
114 if (!subdir)
115 continue;
116
117 while ((subitem = readdir(subdir))) {
118 if (subitem->d_name[0] == '.') /* skip hidden files */
119 continue;
120
121 sprintf(filename, "%s/%s", devpath, subitem->d_name);
122
123 if (!strcmp(subitem->d_name, "flags"))
124 flags = get_int_from(filename);
125
126 if (!strcmp(subitem->d_name, "rate"))
127 rate = get_int_from(filename);
128
129 if (!strcmp(subitem->d_name, "usecount"))
130 usecount = get_int_from(filename);
131 }
132
Amit Arora24ed7d12010-09-14 12:12:58 +0530133 if (hrow == line) {
134 if (!strcmp(clockname, "..")) {
135 if (clk_tree_level != 1) {
136 set_hl_dir_path_to_parent();
137 gadder = -1;
138 }
139 } else {
140 strcpy(highlighted_path, devpath);
141 gadder = 1;
142 }
143 }
144
145// sprintf(clockname, "%s:dp-%s:n-%s:hp-%s",
146// clockname, clk_dir_path, clk_name, highlighted_path);
147
148/* if (strcmp(clockname, "..")) {
149 int row = line + 1;
150
151 if (line > parent_dir_row)
152 row--;
153 print_clock_info_line(row, clockname, flags, rate, usecount,
154 (hrow == line) ? 1 : 0);
155 } else {
156 print_clock_info_line(0, clockname, flags, rate, usecount,
157 (hrow == line) ? 1 : 0);
158 parent_dir_row = line;
159 }
160*/
161
Amit Arora29cb7572010-10-05 17:40:29 +0530162 print_clock_info_line(line, clockname, flags, rate, usecount,
163 (hrow == line) ? 1 : 0);
164 line++;
165
Amit Arora728e0c92010-09-14 12:06:09 +0530166 closedir(subdir);
167 }
168
169 closedir(dir);
170
171 if (hrow >= (line - 1))
172 hrow = -1;
173 return hrow;
174}
Amit Arora0e512722010-10-01 12:24:16 +0530175
176void dump_clock_info(int verbose)
177{
Amit Aroraeb6cba92010-10-25 16:03:21 +0530178 (void)verbose;
Amit Arora0e512722010-10-01 12:24:16 +0530179 printf("Clock Tree :\n");
180 printf("**********\n");
Amit Aroraeb6cba92010-10-25 16:03:21 +0530181 read_clock_info(clk_dir_path);
182 print_clock_info(clocks_info, 1, 1);
Amit Arora0e512722010-10-01 12:24:16 +0530183}
184
Amit Aroraeb6cba92010-10-25 16:03:21 +0530185void read_clock_info(char *clkpath)
Amit Arora0e512722010-10-01 12:24:16 +0530186{
Amit Aroraeb6cba92010-10-25 16:03:21 +0530187 DIR *dir;
188 struct dirent *item;
189 char filename[NAME_MAX], clockname[NAME_MAX];
190 struct clock_info *child;
191 struct clock_info *cur;
Amit Arora0e512722010-10-01 12:24:16 +0530192
Amit Aroraeb6cba92010-10-25 16:03:21 +0530193 dir = opendir(clkpath);
Amit Arora0e512722010-10-01 12:24:16 +0530194 if (!dir)
195 return;
196
Amit Aroraeb6cba92010-10-25 16:03:21 +0530197 clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
198 memset(clocks_info, 0, sizeof(clocks_info));
199 strcpy(clocks_info->name, "/");
Amit Arora0e512722010-10-01 12:24:16 +0530200
Amit Aroraeb6cba92010-10-25 16:03:21 +0530201 while ((item = readdir(dir))) {
202 /* skip hidden dirs except ".." */
203 if (item->d_name[0] == '.')
204 continue;
205
206 strcpy(clockname, item->d_name);
207 sprintf(filename, "%s/%s", clkpath, item->d_name);
208 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
209 memset(cur, 0, sizeof(cur));
210 strcpy(cur->name, clockname);
211 cur->parent = clocks_info;
212 insert_children(&clocks_info, cur);
213 child = read_clock_info_recur(filename, 2, cur);
214 }
215 closedir(dir);
216}
217
218struct clock_info *read_clock_info_recur(char *clkpath, int level,
219 struct clock_info *parent)
220{
221 int ret = 0;
222 DIR *dir;
223 char filename[PATH_MAX];
224 struct dirent *item;
225 struct clock_info *cur = NULL;
226 struct stat buf;
227
228 dir = opendir(clkpath);
229 if (!dir)
230 return NULL;
231
232 while ((item = readdir(dir))) {
233 struct clock_info *child;
Amit Arora0e512722010-10-01 12:24:16 +0530234 /* skip hidden dirs except ".." */
235 if (item->d_name[0] == '.' )
236 continue;
237
Amit Aroraeb6cba92010-10-25 16:03:21 +0530238 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530239
Amit Aroraeb6cba92010-10-25 16:03:21 +0530240 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530241
Amit Aroraeb6cba92010-10-25 16:03:21 +0530242 if (ret < 0) {
243 printf("Error doing a stat on %s\n", filename);
244 exit(1);
245 }
246
247 if (S_ISREG(buf.st_mode))
248 {
249 if (!strcmp(item->d_name, "flags"))
250 parent->flags = get_int_from(filename);
251 if (!strcmp(item->d_name, "rate"))
252 parent->rate = get_int_from(filename);
253 if (!strcmp(item->d_name, "usecount"))
254 parent->usecount = get_int_from(filename);
255 continue;
256 }
257
258 if (!S_ISDIR(buf.st_mode))
Amit Arora0e512722010-10-01 12:24:16 +0530259 continue;
260
Amit Aroraeb6cba92010-10-25 16:03:21 +0530261 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
262 memset(cur, 0, sizeof(cur));
263 strcpy(cur->name, item->d_name);
264 cur->children = NULL;
265 cur->parent = NULL;
266 cur->num_children = 0;
267 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora0e512722010-10-01 12:24:16 +0530268
Amit Aroraeb6cba92010-10-25 16:03:21 +0530269 insert_children(&parent, cur);
270 cur->parent = parent;
Amit Arora0e512722010-10-01 12:24:16 +0530271 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530272 closedir(dir);
273
274 return cur;
275}
276
277void insert_children(struct clock_info **parent, struct clock_info *clk)
278{
279 if (!(*parent)->children) {
280 (*parent)->children = (struct clock_info **)
281 malloc(sizeof(struct clock_info *)*2);
282 (*parent)->num_children = 0;
283 } else
284 (*parent)->children = (struct clock_info **)
285 realloc((*parent)->children,
286 sizeof(struct clock_info *) *
287 ((*parent)->num_children + 2));
288 if ((*parent)->num_children > 0)
289 (*parent)->children[(*parent)->num_children - 1]->last_child = 0;
290 clk->last_child = 1;
291 (*parent)->children[(*parent)->num_children] = clk;
292 (*parent)->children[(*parent)->num_children + 1] = NULL;
293 (*parent)->num_children++;
294}
295
296
297void print_clock_info(struct clock_info *clk, int level, int bmp)
298{
299 int i, j;
300
301 if (!clk)
302 return;
303
304 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
305 if (i == (level - 1)) {
306 if (clk->last_child)
307 printf("`-- ");
308 else
309 printf("|-- ");
310 } else {
311 if ((1<<j) & bmp)
312 printf("| ");
313 else
314 printf(" ");
315 }
316 }
317 if (clk == clocks_info)
318 printf("%s\n", clk->name);
319 else {
320 char *unit = "Hz";
321 double drate = (double)clk->rate;
322
323 if (drate > 1000 && drate < 1000000) {
324 unit = "KHz";
325 drate /= 1000;
326 }
327 if (drate > 1000000) {
328 unit = "MHz";
329 drate /= 1000000;
330 }
331 printf("%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
332 clk->name, clk->flags, clk->usecount, drate, unit);
333 //printf("%s (flags:%d,usecount:%d,rate:%5.2f %s, bmp=0x%x)\n",
334 // clk->name, clk->flags, clk->usecount, drate, unit, bmp);
335 }
336 if (clk->children) {
337 int tbmp = bmp;
338 int xbmp = -1;
339
340 if (clk->last_child) {
341 xbmp ^= 1 << (level - 2 );
342
343 xbmp = tbmp & xbmp;
344 } else
345 xbmp = bmp;
346 for (i = 0; i<clk->num_children; i++) {
347 //if (clk->children[i]->last_child)
348 tbmp = xbmp | (1<<level);
349 print_clock_info(clk->children[i], level + 1, tbmp);
350 }
351 }
Amit Arora0e512722010-10-01 12:24:16 +0530352}