~ubuntu-branches/ubuntu/oneiric/powerdebug/oneiric

« back to all changes in this revision

Viewing changes to clocks.c

  • Committer: Bazaar Package Importer
  • Author(s): Amit Kucheria
  • Date: 2011-01-24 17:58:46 UTC
  • Revision ID: james.westby@ubuntu.com-20110124175846-bz13uqdo4h0wl3wo
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (C) 2010, Linaro Limited.
 
3
 *
 
4
 * This file is part of PowerDebug.
 
5
 *
 
6
 * All rights reserved. This program and the accompanying materials
 
7
 * are made available under the terms of the Eclipse Public License v1.0
 
8
 * which accompanies this distribution, and is available at
 
9
 * http://www.eclipse.org/legal/epl-v10.html
 
10
 *
 
11
 * Contributors:
 
12
 *     Amit Arora <amit.arora@linaro.org> (IBM Corporation)
 
13
 *       - initial API and implementation
 
14
 *******************************************************************************/
 
15
 
 
16
#include "powerdebug.h"
 
17
#include "clocks.h"
 
18
 
 
19
static char clk_dir_path[PATH_MAX];
 
20
static char clk_name[NAME_MAX];
 
21
static int  bold[MAX_LINES];
 
22
 
 
23
int init_clock_details(void)
 
24
{
 
25
        char *path = debugfs_locate_mpoint();
 
26
        struct stat buf;
 
27
 
 
28
        if (path)
 
29
                strcpy(clk_dir_path, path);
 
30
        else {
 
31
                if (!dump) {
 
32
                        create_selectedwindow();
 
33
                        sprintf(clock_lines[0], "Unable to locate debugfs "
 
34
                                "mount point. Mount debugfs "
 
35
                                "and try again..\n");
 
36
                        print_one_clock(0, clock_lines[0], 1, 0);
 
37
                        old_clock_line_no = 1;
 
38
                        return(1);
 
39
                } else {
 
40
                        fprintf(stderr, "powerdebug: Unable to locate debugfs "
 
41
                                "mount point. Mount debugfs and try "
 
42
                                "again..\n");
 
43
                        exit(1);
 
44
                }
 
45
        }
 
46
        sprintf(clk_dir_path, "%s/clock", clk_dir_path);
 
47
        //strcpy(clk_dir_path, "/debug/clock"); // Hardcoded for testing..
 
48
        if (stat(clk_dir_path, &buf)) {
 
49
                if (!dump) {
 
50
                        create_selectedwindow();
 
51
                        sprintf(clock_lines[0], "Unable to find clock tree"
 
52
                                " information at %s.\n", clk_dir_path);
 
53
                        print_one_clock(0, clock_lines[0], 1, 0);
 
54
                        old_clock_line_no = 1;
 
55
                        return(1);
 
56
                } else {
 
57
                        fprintf(stderr, "powerdebug: Unable to find clock tree"
 
58
                                " information at %s.\n", clk_dir_path);
 
59
                        exit(1);
 
60
                }
 
61
        }
 
62
        strcpy(clk_name, "");
 
63
        return(0);
 
64
}
 
65
 
 
66
int get_int_from(char *file)
 
67
{
 
68
        FILE *filep;
 
69
        char result[NAME_MAX];
 
70
        int ret;
 
71
 
 
72
        filep = fopen(file, "r");
 
73
 
 
74
        if (!filep)
 
75
                return -1;  //TBD : What should we return on failure, here ?
 
76
 
 
77
        ret = fscanf(filep, "%s", result);
 
78
        fclose(filep);
 
79
 
 
80
        return atoi(result);
 
81
}
 
82
 
 
83
void find_parents_for_clock(char *clkname, int complete)
 
84
{
 
85
        char name[256];
 
86
 
 
87
        name[0] = '\0';
 
88
        if (!complete) {
 
89
                char str[256];
 
90
 
 
91
                strcat(name, clkname);
 
92
                sprintf(str, "Enter Clock Name : %s\n", name);
 
93
                print_one_clock(2, str, 1, 0);
 
94
                return;
 
95
        }
 
96
        sprintf(name, "Parents for \"%s\" Clock : \n", clkname);
 
97
        print_one_clock(0, name, 1, 1);
 
98
        dump_all_parents(clkname);
 
99
}
 
100
 
 
101
int read_and_print_clock_info(int verbose, int hrow, int selected)
 
102
{
 
103
        print_one_clock(0, "Reading Clock Tree ...", 1, 1);
 
104
 
 
105
        if (!old_clock_line_no || selected == REFRESH_WINDOW) {
 
106
                destroy_clocks_info();
 
107
                read_clock_info(clk_dir_path);
 
108
        }
 
109
 
 
110
        if (!clocks_info->num_children) {
 
111
                fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
 
112
                exit(1);
 
113
        }
 
114
 
 
115
        if (selected == CLOCK_SELECTED)
 
116
                selected = 1;
 
117
        else
 
118
                selected = 0;
 
119
 
 
120
        print_clock_info(verbose, hrow, selected);
 
121
        hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
 
122
 
 
123
        return hrow;
 
124
}
 
125
 
 
126
int calc_delta_screen_size(int hrow)
 
127
{
 
128
        if (hrow >= (maxy - 3))
 
129
                return hrow - (maxy - 4);
 
130
 
 
131
        return 0;
 
132
}
 
133
 
 
134
void print_clock_info(int verbose, int hrow, int selected)
 
135
{
 
136
        int i, count = 0, delta;
 
137
 
 
138
        (void)verbose;
 
139
 
 
140
        print_clock_header();
 
141
 
 
142
        for (i = 0; i < clocks_info->num_children; i++)
 
143
                add_clock_details_recur(clocks_info->children[i],
 
144
                                        hrow, selected);
 
145
 
 
146
        delta = calc_delta_screen_size(hrow);
 
147
 
 
148
        while (clock_lines[count + delta] &&
 
149
                strcmp(clock_lines[count + delta], "")) {
 
150
                if (count < delta) {
 
151
                        count++;
 
152
                        continue;
 
153
                }
 
154
                print_one_clock(count - delta, clock_lines[count + delta],
 
155
                                bold[count + delta], (hrow == (count + delta)));
 
156
                count++;
 
157
        }
 
158
 
 
159
        old_clock_line_no = clock_line_no;
 
160
        clock_line_no = 0;
 
161
}
 
162
 
 
163
void prepare_name_str(char *namestr, struct clock_info *clock)
 
164
{
 
165
        int i;
 
166
 
 
167
        strcpy(namestr, "");
 
168
        if (clock->level > 1)
 
169
                for (i = 0; i < (clock->level - 1); i++)
 
170
                        strcat(namestr, "  ");
 
171
        strcat(namestr, clock->name);
 
172
}
 
173
 
 
174
void add_clock_details_recur(struct clock_info *clock, int hrow, int selected)
 
175
{
 
176
        int i;
 
177
        char *unit = " Hz";
 
178
        char rate_str[64];
 
179
        char name_str[256];
 
180
        double drate = (double)clock->rate;
 
181
 
 
182
        if (drate > 1000 && drate < 1000000) {
 
183
                unit = "KHz";
 
184
                drate /= 1000;
 
185
        }
 
186
        if (drate > 1000000) {
 
187
                unit = "MHz";
 
188
                drate /= 1000000;
 
189
        }
 
190
        if (clock->usecount)
 
191
                bold[clock_line_no] = 1;
 
192
        else
 
193
                bold[clock_line_no] = 0;
 
194
 
 
195
        sprintf(rate_str, "%.2f %s", drate, unit);
 
196
        prepare_name_str(name_str, clock);
 
197
        sprintf(clock_lines[clock_line_no++], "%-55s %-4d  %-12s %-12d %-12d",
 
198
                name_str, clock->flags, rate_str, clock->usecount,
 
199
                clock->num_children);
 
200
 
 
201
        if (selected && (hrow == (clock_line_no - 1))) {
 
202
                if (clock->expanded)
 
203
                        collapse_all_subclocks(clock);
 
204
                else
 
205
                        clock->expanded = 1;
 
206
                selected = 0;
 
207
        }
 
208
 
 
209
        if (clock->expanded && clock->num_children)
 
210
                for (i = 0; i < clock->num_children; i++)
 
211
                        add_clock_details_recur(clock->children[i],
 
212
                                                hrow, selected);
 
213
        strcpy(clock_lines[clock_line_no], "");
 
214
}
 
215
 
 
216
void collapse_all_subclocks(struct clock_info *clock)
 
217
{
 
218
        int i;
 
219
 
 
220
        clock->expanded = 0;
 
221
        if (clock->num_children)
 
222
                for (i = 0; i < clock->num_children; i++)
 
223
                        collapse_all_subclocks(clock->children[i]);
 
224
}
 
225
 
 
226
void destroy_clocks_info(void)
 
227
{
 
228
        int i;
 
229
 
 
230
        if (!clocks_info)
 
231
                return;
 
232
 
 
233
        if (clocks_info->num_children) {
 
234
                for (i = (clocks_info->num_children - 1); i >= 0 ; i--) {
 
235
                        destroy_clocks_info_recur(clocks_info->children[i]);
 
236
                        if (!i) {
 
237
                                free(clocks_info->children);
 
238
                                clocks_info->children = NULL;
 
239
                        }
 
240
                }
 
241
        }
 
242
        clocks_info->num_children = 0;
 
243
        free(clocks_info);
 
244
        clocks_info = NULL;
 
245
}
 
246
 
 
247
void destroy_clocks_info_recur(struct clock_info *clock)
 
248
{
 
249
        int i;
 
250
 
 
251
        if (clock && clock->num_children) {
 
252
                for (i = (clock->num_children - 1); i >= 0; i--) {
 
253
                        fflush(stdin);
 
254
                        destroy_clocks_info_recur(clock->children[i]);
 
255
                        if (!i) {
 
256
                                free(clock->children);
 
257
                                clock->children = NULL;
 
258
                                clock->num_children = 0;
 
259
                        }
 
260
                }
 
261
        }
 
262
}
 
263
 
 
264
void read_and_dump_clock_info_one(char *clk)
 
265
{
 
266
        printf("\nParents for \"%s\" Clock :\n\n", clk);
 
267
        read_clock_info(clk_dir_path);
 
268
        dump_all_parents(clk);
 
269
        printf("\n\n");
 
270
}
 
271
 
 
272
void read_and_dump_clock_info(int verbose)
 
273
{
 
274
        (void)verbose;
 
275
        printf("\nClock Tree :\n");
 
276
        printf("**********\n");
 
277
        read_clock_info(clk_dir_path);
 
278
        dump_clock_info(clocks_info, 1, 1);
 
279
        printf("\n\n");
 
280
}
 
281
 
 
282
void read_clock_info(char *clkpath)
 
283
{
 
284
        DIR *dir;
 
285
        struct dirent *item;
 
286
        char filename[NAME_MAX], clockname[NAME_MAX];
 
287
        struct clock_info *child;
 
288
        struct clock_info *cur;
 
289
 
 
290
        dir = opendir(clkpath);
 
291
        if (!dir)
 
292
                return;
 
293
 
 
294
        clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
 
295
        memset(clocks_info, 0, sizeof(clocks_info));
 
296
        strcpy(clocks_info->name, "/");
 
297
        clocks_info->level = 0;
 
298
 
 
299
        while ((item = readdir(dir))) {
 
300
                /* skip hidden dirs except ".." */
 
301
                if (item->d_name[0] == '.')
 
302
                        continue;
 
303
 
 
304
                strcpy(clockname, item->d_name);
 
305
                sprintf(filename, "%s/%s", clkpath, item->d_name);
 
306
                cur = (struct clock_info *)malloc(sizeof(struct clock_info));
 
307
                memset(cur, 0, sizeof(struct clock_info));
 
308
                strcpy(cur->name, clockname);
 
309
                cur->parent = clocks_info;
 
310
                cur->num_children = 0;
 
311
                cur->expanded = 0;
 
312
                cur->level = 1;
 
313
                insert_children(&clocks_info, cur);
 
314
                child = read_clock_info_recur(filename, 2, cur);
 
315
        }
 
316
        closedir(dir);
 
317
}
 
318
 
 
319
struct clock_info *read_clock_info_recur(char *clkpath, int level,
 
320
                                        struct clock_info *parent)
 
321
{
 
322
        int ret = 0;
 
323
        DIR *dir;
 
324
        char filename[PATH_MAX];
 
325
        struct dirent *item;
 
326
        struct clock_info *cur = NULL;
 
327
        struct stat buf;
 
328
 
 
329
        dir = opendir(clkpath);
 
330
        if (!dir)
 
331
                return NULL;
 
332
 
 
333
        while ((item = readdir(dir))) {
 
334
                struct clock_info *child;
 
335
                /* skip hidden dirs except ".." */
 
336
                if (item->d_name[0] == '.' )
 
337
                        continue;
 
338
 
 
339
                sprintf(filename, "%s/%s", clkpath, item->d_name);
 
340
 
 
341
                ret = stat(filename, &buf);
 
342
 
 
343
                if (ret < 0) {
 
344
                        printf("Error doing a stat on %s\n", filename);
 
345
                        exit(1);
 
346
                }
 
347
 
 
348
                if (S_ISREG(buf.st_mode)) {
 
349
                        if (!strcmp(item->d_name, "flags"))
 
350
                                parent->flags = get_int_from(filename);
 
351
                        if (!strcmp(item->d_name, "rate"))
 
352
                                parent->rate = get_int_from(filename);
 
353
                        if (!strcmp(item->d_name, "usecount"))
 
354
                                parent->usecount = get_int_from(filename);
 
355
                        continue;
 
356
                }
 
357
 
 
358
                if (!S_ISDIR(buf.st_mode))
 
359
                        continue;
 
360
 
 
361
                cur = (struct clock_info *)malloc(sizeof(struct clock_info));
 
362
                memset(cur, 0, sizeof(cur));
 
363
                strcpy(cur->name, item->d_name);
 
364
                cur->children = NULL;
 
365
                cur->parent = NULL;
 
366
                cur->num_children = 0;
 
367
                cur->expanded = 0;
 
368
                cur->level = level;
 
369
                child = read_clock_info_recur(filename, level + 1, cur);
 
370
                insert_children(&parent, cur);
 
371
                cur->parent = parent;
 
372
        }
 
373
        closedir(dir);
 
374
 
 
375
        return cur;
 
376
}
 
377
 
 
378
void insert_children(struct clock_info **parent, struct clock_info *clk)
 
379
{
 
380
        if (!(*parent)->num_children || (*parent)->children == NULL) {
 
381
                (*parent)->children = (struct clock_info **)
 
382
                        malloc(sizeof(struct clock_info *)*2);
 
383
                (*parent)->num_children = 0;
 
384
        } else
 
385
                (*parent)->children = (struct clock_info **)
 
386
                        realloc((*parent)->children,
 
387
                                sizeof(struct clock_info *) *
 
388
                                ((*parent)->num_children + 2));
 
389
        if ((*parent)->num_children > 0)
 
390
                (*parent)->children[(*parent)->num_children - 1]->last_child
 
391
                        = 0;
 
392
        clk->last_child = 1;
 
393
        (*parent)->children[(*parent)->num_children] = clk;
 
394
        (*parent)->children[(*parent)->num_children + 1] = NULL;
 
395
        (*parent)->num_children++;
 
396
}
 
397
 
 
398
void dump_parent(struct clock_info *clk, int line)
 
399
{
 
400
        char *unit = "Hz";
 
401
        double drate;
 
402
        static char spaces[64];
 
403
        char str[256];
 
404
        static int maxline;
 
405
 
 
406
        if (maxline < line)
 
407
                maxline = line;
 
408
 
 
409
        if (clk && clk->parent)
 
410
                dump_parent(clk->parent, ++line);
 
411
 
 
412
        drate = (double)clk->rate;
 
413
        if (drate > 1000 && drate < 1000000) {
 
414
                unit = "KHz";
 
415
                drate /= 1000;
 
416
        }
 
417
        if (drate > 1000000) {
 
418
                unit = "MHz";
 
419
                drate /= 1000000;
 
420
        }
 
421
        if (clk == clocks_info) {
 
422
                line++;
 
423
                strcpy(spaces, "");
 
424
                sprintf(str, "%s%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
 
425
                        spaces, clk->name, clk->flags, clk->usecount, drate,
 
426
                        unit);
 
427
        } else {
 
428
                if (!(clk->parent == clocks_info))
 
429
                        strcat(spaces, "  ");
 
430
                sprintf(str, "%s`- %s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
 
431
                        spaces, clk->name, clk->flags, clk->usecount, drate,
 
432
                        unit);
 
433
        }
 
434
        if (dump)
 
435
                //printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str);
 
436
                printf("%s", str);
 
437
        else
 
438
                print_one_clock(maxline - line + 2, str, 1, 0);
 
439
}
 
440
 
 
441
void dump_all_parents(char *clkarg)
 
442
{
 
443
        struct clock_info *clk;
 
444
        char spaces[1024];
 
445
 
 
446
        strcpy(spaces, "");
 
447
 
 
448
        clk = find_clock(clocks_info, clkarg);
 
449
 
 
450
        if (!clk)
 
451
                printf("Clock NOT found!\n");
 
452
        else {
 
453
//              while(clk && clk != clocks_info) {
 
454
//                      printf("%s\n", clk->name);
 
455
//                      strcat(spaces, "  ");
 
456
//                      clk = clk->parent;
 
457
//                      printf("%s <-- ", spaces);
 
458
//              }
 
459
//              printf("  /\n");
 
460
                dump_parent(clk, 1);
 
461
        }
 
462
}
 
463
 
 
464
struct clock_info *find_clock(struct clock_info *clk, char *clkarg)
 
465
{
 
466
        int i;
 
467
        struct clock_info *ret = clk;
 
468
 
 
469
        if (!strcmp(clk->name, clkarg))
 
470
                return ret;
 
471
 
 
472
        if (clk->children) {
 
473
                for (i = 0; i < clk->num_children; i++) {
 
474
                        if (!strcmp(clk->children[i]->name, clkarg))
 
475
                                return clk->children[i];
 
476
                }
 
477
                for (i = 0; i < clk->num_children; i++) {
 
478
                        ret = find_clock(clk->children[i], clkarg);
 
479
                        if (ret)
 
480
                                return ret;
 
481
                }
 
482
        }
 
483
 
 
484
        return NULL;
 
485
}
 
486
 
 
487
 
 
488
void dump_clock_info(struct clock_info *clk, int level, int bmp)
 
489
{
 
490
        int i, j;
 
491
 
 
492
        if (!clk)
 
493
                return;
 
494
 
 
495
        for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
 
496
                if (i == (level - 1)) {
 
497
                        if (clk->last_child)
 
498
                                printf("`-- ");
 
499
                        else
 
500
                                printf("|-- ");
 
501
                } else {
 
502
                        if ((1<<j) & bmp)
 
503
                                printf("|   ");
 
504
                        else
 
505
                                printf("    ");
 
506
                }
 
507
        }
 
508
 
 
509
        if (clk == clocks_info)
 
510
                printf("%s\n", clk->name);
 
511
        else {
 
512
                char *unit = "Hz";
 
513
                double drate = (double)clk->rate;
 
514
 
 
515
                if (drate > 1000 && drate < 1000000) {
 
516
                        unit = "KHz";
 
517
                        drate /= 1000;
 
518
                }
 
519
                if (drate > 1000000) {
 
520
                        unit = "MHz";
 
521
                        drate /= 1000000;
 
522
                }
 
523
                printf("%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
 
524
                        clk->name, clk->flags, clk->usecount, drate, unit);
 
525
        }
 
526
        if (clk->children) {
 
527
                int tbmp = bmp;
 
528
                int xbmp = -1;
 
529
 
 
530
                if (clk->last_child) {
 
531
                        xbmp ^= 1 << (level - 2);
 
532
 
 
533
                        xbmp = tbmp & xbmp;
 
534
                } else
 
535
                        xbmp = bmp;
 
536
                for (i = 0; i < clk->num_children; i++) {
 
537
                        tbmp = xbmp | (1 << level);
 
538
                        dump_clock_info(clk->children[i], level + 1, tbmp);
 
539
                }
 
540
        }
 
541
}
 
542
 
 
543
char *debugfs_locate_mpoint(void)
 
544
{
 
545
        int ret;
 
546
        FILE *filep;
 
547
        char **path;
 
548
        char fsname[64];
 
549
        struct statfs sfs;
 
550
 
 
551
        path = likely_mpoints;
 
552
        while (*path) {
 
553
                ret = statfs(*path, &sfs);
 
554
                if (ret >= 0 && sfs.f_type == (long)DEBUGFS_MAGIC)
 
555
                        return *path;
 
556
                path++;
 
557
        }
 
558
 
 
559
        filep = fopen("/proc/mounts", "r");
 
560
        if (filep == NULL) {
 
561
                fprintf(stderr, "powerdebug: Error opening /proc/mounts.");
 
562
                exit(1);
 
563
        }
 
564
 
 
565
        while (fscanf(filep, "%*s %s %s %*s %*d %*d\n",
 
566
                        debugfs_mntpoint, fsname) == 2)
 
567
                if (!strcmp(fsname, "debugfs"))
 
568
                        break;
 
569
        fclose(filep);
 
570
 
 
571
        if (strcmp(fsname, "debugfs"))
 
572
                return NULL;
 
573
 
 
574
        return debugfs_mntpoint;
 
575
}