blob: cf1ead0f8e01757b3b15c69273d1d6a4fcb33081 [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>
19
20static int clk_tree_level = 1;
21static char clk_dir_path[PATH_MAX];
22static char highlighted_path[PATH_MAX];
23static char clk_name[NAME_MAX];
24static int gadder = 0;
25
26
27void init_clock_details(void)
28{
29 strcpy(clk_dir_path, "/debug/clock");
30 strcpy(clk_name, "");
31 strcpy(highlighted_path, "");
32}
Amit Arora728e0c92010-09-14 12:06:09 +053033
34int get_int_from(char *file)
35{
36 FILE *filep;
37 char result[NAME_MAX];
38 int ret;
39
40 filep = fopen(file, "r");
41
42 if (!filep)
43 return -1; //TBD : What should we return on failure, here ?
44
45 ret = fscanf(filep, "%s", result);
46 fclose(filep);
47
48 return atoi(result);
49}
50
Amit Arora24ed7d12010-09-14 12:12:58 +053051int read_and_print_clock_info(int verbose, int hrow, int selected)
52{
53 if (selected) {
54 // if (!(strcmp(clk_dir_path, "/debug/clock") &&
55 // strcmp(highlighted_path, ".."))) {
56 strcpy(clk_dir_path, highlighted_path);
57 hrow = 0;
58 clk_tree_level += gadder;
59 if (clk_tree_level <=0)
60 clk_tree_level = 1;
61 }
62
63 hrow = read_and_print_clock_one_level(verbose, hrow, selected);
64
65 return hrow;
66}
67
68void set_hl_dir_path_to_parent(void)
69{
70 char *ptr;
71
72 strcpy(highlighted_path, clk_dir_path);
73 if (strcmp(clk_dir_path, "/clock/debug")) {
74 ptr = strrchr(highlighted_path, '/');
75 if (ptr)
76 ptr[0]='\0';
77 }
78}
79
80int read_and_print_clock_one_level(int verbose, int hrow, int selected)
Amit Arora728e0c92010-09-14 12:06:09 +053081{
82 int line = 0, usecount = 0, flags = 0, rate = 0;
Amit Arora24ed7d12010-09-14 12:12:58 +053083// int parent_dir_row = 1000;
Amit Arora728e0c92010-09-14 12:06:09 +053084 DIR *dir, *subdir;
85 char filename[PATH_MAX], devpath[PATH_MAX], clockname[NAME_MAX];
86 struct dirent *item, *subitem;
87
Amit Arora29cb7572010-10-05 17:40:29 +053088 (void)verbose;
89
Amit Arora24ed7d12010-09-14 12:12:58 +053090 print_clock_header(clk_tree_level);
Amit Arora728e0c92010-09-14 12:06:09 +053091
Amit Arora24ed7d12010-09-14 12:12:58 +053092 sprintf(filename, "%s", clk_dir_path);
Amit Arora728e0c92010-09-14 12:06:09 +053093
94 dir = opendir(filename);
95 if (!dir)
96 return 0;
97
98 while ((item = readdir(dir))) {
Amit Arora24ed7d12010-09-14 12:12:58 +053099 /* skip hidden dirs except ".." */
100 if (item->d_name[0] == '.' && strcmp(item->d_name, ".."))
Amit Arora728e0c92010-09-14 12:06:09 +0530101 continue;
102
Amit Arora24ed7d12010-09-14 12:12:58 +0530103 if (selected && hrow == line && !strcmp(item->d_name, "..")) {
104 sprintf(devpath, "%s", clk_dir_path);
105 strcpy(clockname, "..");
106 } else {
107 sprintf(devpath, "%s/%s", clk_dir_path, item->d_name);
108 strcpy(clockname, item->d_name);
109 }
Amit Arora728e0c92010-09-14 12:06:09 +0530110
111 subdir = opendir(devpath);
112
113 if (!subdir)
114 continue;
115
116 while ((subitem = readdir(subdir))) {
117 if (subitem->d_name[0] == '.') /* skip hidden files */
118 continue;
119
120 sprintf(filename, "%s/%s", devpath, subitem->d_name);
121
122 if (!strcmp(subitem->d_name, "flags"))
123 flags = get_int_from(filename);
124
125 if (!strcmp(subitem->d_name, "rate"))
126 rate = get_int_from(filename);
127
128 if (!strcmp(subitem->d_name, "usecount"))
129 usecount = get_int_from(filename);
130 }
131
Amit Arora24ed7d12010-09-14 12:12:58 +0530132 if (hrow == line) {
133 if (!strcmp(clockname, "..")) {
134 if (clk_tree_level != 1) {
135 set_hl_dir_path_to_parent();
136 gadder = -1;
137 }
138 } else {
139 strcpy(highlighted_path, devpath);
140 gadder = 1;
141 }
142 }
143
144// sprintf(clockname, "%s:dp-%s:n-%s:hp-%s",
145// clockname, clk_dir_path, clk_name, highlighted_path);
146
147/* if (strcmp(clockname, "..")) {
148 int row = line + 1;
149
150 if (line > parent_dir_row)
151 row--;
152 print_clock_info_line(row, clockname, flags, rate, usecount,
153 (hrow == line) ? 1 : 0);
154 } else {
155 print_clock_info_line(0, clockname, flags, rate, usecount,
156 (hrow == line) ? 1 : 0);
157 parent_dir_row = line;
158 }
159*/
160
Amit Arora29cb7572010-10-05 17:40:29 +0530161 print_clock_info_line(line, clockname, flags, rate, usecount,
162 (hrow == line) ? 1 : 0);
163 line++;
164
Amit Arora728e0c92010-09-14 12:06:09 +0530165 closedir(subdir);
166 }
167
168 closedir(dir);
169
170 if (hrow >= (line - 1))
171 hrow = -1;
172 return hrow;
173}
Amit Arora0e512722010-10-01 12:24:16 +0530174
175void dump_clock_info(int verbose)
176{
177 printf("Clock Tree :\n");
178 printf("**********\n");
179 printf("/\n");
180 dump_clock_info_recur(verbose, clk_dir_path);
181}
182
183void dump_clock_info_recur(int verbose, char *clkdirpath)
184{
185 int usecount = 0, flags = 0, rate = 0;
186 DIR *dir, *subdir;
187 char filename[PATH_MAX], devpath[PATH_MAX];
188 struct dirent *item, *subitem;
189 char *clock, *clockp;
190
191 sprintf(filename, "%s", clkdirpath);
192
193 dir = opendir(filename);
194 if (!dir)
195 return;
196
197 while ((item = readdir(dir))) {
198 int cnt = 0;
199
200 /* skip hidden dirs except ".." */
201 if (item->d_name[0] == '.' )
202 continue;
203
204 sprintf(devpath, "%s/%s", clkdirpath, item->d_name);
205
206 subdir = opendir(devpath);
207
208 if (!subdir)
209 continue;
210
211 while ((subitem = readdir(subdir))) {
212 if (subitem->d_name[0] == '.') /* skip hidden
213files */
214 continue;
215
216 sprintf(filename, "%s/%s", devpath, subitem->d_name);
217
218 if (!strcmp(subitem->d_name, "flags"))
219 flags = get_int_from(filename);
220
221 if (!strcmp(subitem->d_name, "rate"))
222 rate = get_int_from(filename);
223
224 if (!strcmp(subitem->d_name, "usecount"))
225 usecount = get_int_from(filename);
226 }
227
228 if (!usecount && !verbose)
229 continue;
230
231
232 clockp = strrchr(devpath, '/');
233 if (clockp)
234 clockp++;
235 else
236 continue;
237
238 clock = strchr(devpath, '/');
239 if (clock) {
240 clock++;
241 clock = strchr(clock, '/');
242 if (clock)
243 clock++;
244 }
245
246 while (clock) {
247 clock = strchr(clock, '/');
248 if (clock)
249 clock++;
250 else
251 break;
252 cnt ++;
253 }
254
255 printf("|");
256 if (cnt == 1) {
257 cnt --;
258 printf("-- ");
259 } else
260 printf(" ");
261
262
263 while (cnt) {
264 if (cnt == 2)
265 printf("|-");
266 else if (cnt == 1)
267 printf("- ");
268 else
269 printf("| ");
270 cnt --;
271 }
272
273 printf("%s <flags=0x%x:rate=%d:usecount=%d>\n",
274 clockp, flags, rate, usecount);
275 dump_clock_info_recur(verbose, devpath);
276 }
277}