blob: e9b954afcf78fc7c58a5d858749335292fea5d15 [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
88 (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
161 print_clock_info_line(line, clockname, flags, rate, usecount,
162 (hrow == line) ? 1 : 0);
163
Amit Arora728e0c92010-09-14 12:06:09 +0530164 line++;
165 closedir(subdir);
166 }
167
168 closedir(dir);
169
170 if (hrow >= (line - 1))
171 hrow = -1;
172 return hrow;
173}