From: Yong Shen <yong.s...@linaro.org>

push 'S' key to switch to 'sorting' mode, all the clocks will be sorted
by their name.

Signed-off-by: Yong Shen <yong.s...@linaro.org>
---
 clocks.c     |  140 +++++++++++++++++++++++++++++++++++++++++-----------------
 display.c    |   16 ++++---
 powerdebug.c |   25 +++++-----
 powerdebug.h |   17 ++++++-
 4 files changed, 136 insertions(+), 62 deletions(-)

diff --git a/clocks.c b/clocks.c
index 25588f7..ac8f411 100644
--- a/clocks.c
+++ b/clocks.c
@@ -31,8 +31,11 @@ static struct pollfd fds;
 static char clk_dir_path[PATH_MAX];
 static int  bold[MAX_LINES];
 static char clock_lines[MAX_LINES][128];
+static char clock_lines_ordered[MAX_LINES][128];
 static int clock_line_no;
 static int old_clock_line_no;
+static struct clock_info *clk_head;
+static unsigned int max_node_num, max_clk_num, alloc_index;
 
 static int locate_debugfs(char *clk_path)
 {
@@ -245,30 +248,6 @@ void find_parents_for_clock(char *clkname, int complete)
        dump_all_parents(clkname, false);
 }
 
-int read_and_print_clock_info(int verbose, int hrow, int selected)
-{
-       print_one_clock(0, "Reading Clock Tree ...", 1, 1);
-
-       if (!old_clock_line_no || selected == REFRESH_WINDOW) {
-               read_clock_info(clk_dir_path);
-       }
-
-       if (!clocks_info->num_children) {
-               fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
-               exit(1);
-       }
-
-       if (selected == CLOCK_SELECTED)
-               selected = 1;
-       else
-               selected = 0;
-
-       print_clock_info(verbose, hrow, selected);
-       hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
-
-       return hrow;
-}
-
 static int calc_delta_screen_size(int hrow)
 {
        if (hrow >= (maxy - 3))
@@ -277,15 +256,18 @@ static int calc_delta_screen_size(int hrow)
        return 0;
 }
 
-static void prepare_name_str(char *namestr, struct clock_info *clock)
+static void prepare_name_str(char *namestr, struct clock_info *clock, struct 
powerdebug_options *options)
 {
        int i;
 
-       strcpy(namestr, "");
-       if (clock->level > 1)
-               for (i = 0; i < (clock->level - 1); i++)
-                       strcat(namestr, "  ");
-       strcat(namestr, clock->name);
+       if (options->sorting == CLOCK_NO_SORT) {
+               strcpy(namestr, "");
+               if (clock->level > 1)
+                       for (i = 0; i < (clock->level - 1); i++)
+                               strcat(namestr, "  ");
+               strcat(namestr, clock->name);
+       } else
+               strcpy(namestr, clock->name);
 }
 
 static void collapse_all_subclocks(struct clock_info *clock)
@@ -300,7 +282,7 @@ static void collapse_all_subclocks(struct clock_info *clock)
 }
 
 static void add_clock_details_recur(struct clock_info *clock,
-                                   int hrow, int selected)
+                                   int hrow, int selected, struct 
powerdebug_options *options)
 {
        int i;
        char *unit = " Hz";
@@ -323,7 +305,8 @@ static void add_clock_details_recur(struct clock_info 
*clock,
                bold[clock_line_no] = 0;
 
        sprintf(rate_str, "%.2f %s", drate, unit);
-       prepare_name_str(name_str, clock);
+       prepare_name_str(name_str, clock, options);
+
        sprintf(clock_lines[clock_line_no++], "%-55s %-4d  %-12s %-12d %-12d",
                name_str, clock->flags, rate_str, clock->usecount,
                clock->num_children);
@@ -336,24 +319,102 @@ static void add_clock_details_recur(struct clock_info 
*clock,
                selected = 0;
        }
 
-       if (clock->expanded && clock->num_children)
+       if ((options->sorting == CLOCK_SORT) || (clock->expanded && 
clock->num_children))
                for (tmp = clock->children, i = 0; i < clock->num_children; 
i++, tmp = tmp->buddy)
-                       add_clock_details_recur(tmp, hrow, selected);
+                       add_clock_details_recur(tmp, hrow, selected, options);
 
        strcpy(clock_lines[clock_line_no], "");
 }
 
-void print_clock_info(int verbose, int hrow, int selected)
+static void clock_sort_out(void)
+{
+       int i, j, tmp;
+
+       for (j = 0; j < max_clk_num; j++) {
+               for (i = 0; i < max_clk_num; i++)
+                       if (clock_lines[i][0] != '\0') {
+                               tmp = i;
+                               break;
+                       }
+
+               for (i = tmp + 1; i < max_clk_num; i++) {
+                       if (clock_lines[i][0] == '\0')
+                               continue;
+                       if (strcmp(clock_lines[tmp], clock_lines[i]) > 0)
+                               tmp = i;
+               }
+               strcpy(clock_lines_ordered[j], clock_lines[tmp]);
+               clock_lines[tmp][0] = '\0';
+       }
+}
+
+static void print_clock_info_sorting(struct powerdebug_options *options, int 
hrow, int selected)
 {
        int i, count = 0, delta;
        struct clock_info *tmp;
 
-       (void)verbose;
+       print_clock_header();
+
+       for (tmp = clocks_info->children, i = 0; i < clocks_info->num_children; 
i++, tmp = tmp->buddy)
+               add_clock_details_recur(tmp, hrow, selected, options);
+
+       clock_sort_out();
+
+       delta = calc_delta_screen_size(hrow);
+
+       while (clock_lines_ordered[count + delta] &&
+               strcmp(clock_lines_ordered[count + delta], "")) {
+               if (count < delta) {
+                       count++;
+                       continue;
+               }
+               print_one_clock(count - delta, clock_lines_ordered[count + 
delta],
+                               bold[count + delta], (hrow == (count + delta)));
+               count++;
+       }
+
+       old_clock_line_no = clock_line_no;
+       clock_line_no = 0;
+}
+
+int read_and_print_clock_info(struct powerdebug_options *options, int hrow, 
int selected)
+{
+       print_one_clock(0, "Reading Clock Tree ...", 1, 1);
+
+       if (!old_clock_line_no || selected == REFRESH_WINDOW) {
+               read_clock_info(clk_dir_path);
+       }
+
+       if (!clocks_info->num_children) {
+               fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
+               exit(1);
+       }
+
+       if (selected == CLOCK_SELECTED)
+               selected = 1;
+       else
+               selected = 0;
+
+       if (options->sorting == CLOCK_NO_SORT) {
+               print_clock_info(options, hrow, selected);
+       } else if (options->sorting == CLOCK_SORT) {
+               print_clock_info_sorting(options, hrow, selected);
+       }
+
+       hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
+
+       return hrow;
+}
+
+void print_clock_info(struct powerdebug_options *options, int hrow, int 
selected)
+{
+       int i, count = 0, delta;
+       struct clock_info *tmp;
 
        print_clock_header();
 
        for (tmp = clocks_info->children, i = 0; i < clocks_info->num_children; 
i++, tmp = tmp->buddy)
-               add_clock_details_recur(tmp, hrow, selected);
+               add_clock_details_recur(tmp, hrow, selected, options);
 
        delta = calc_delta_screen_size(hrow);
 
@@ -390,8 +451,6 @@ void read_and_dump_clock_info(int verbose)
        printf("\n\n");
 }
 
-static struct clock_info *clk_head;
-static unsigned int max_node_num, max_clk_num, alloc_index;
 static int clk_number_recursive(char *clk_path)
 {
        DIR *dir;
@@ -500,9 +559,8 @@ void read_clock_info(char *clkpath)
 
        init_clk_info_memory_allocator(clkpath);
        clocks_info = alloc_clk_info();
-       memset(clocks_info, 0, sizeof(clocks_info));
+       memset(clocks_info, 0, sizeof(struct clock_info));
        strcpy(clocks_info->name, "/");
-       clocks_info->level = 0;
 
        while ((item = readdir(dir))) {
                /* skip hidden dirs except ".." */
diff --git a/display.c b/display.c
index 2d63908..2f7d655 100644
--- a/display.c
+++ b/display.c
@@ -18,7 +18,7 @@
 #include "display.h"
 
 #define print(w, x, y, fmt, args...) do { mvwprintw(w, y, x, fmt, ##args); } 
while (0)
-#define NUM_FOOTER_ITEMS 5
+#define NUM_FOOTER_ITEMS 6
 
 enum { PT_COLOR_DEFAULT = 1,
        PT_COLOR_HEADER_BAR,
@@ -112,16 +112,18 @@ void create_windows(int selectedwindow)
        header_win = subwin(stdscr, 1, maxx, 0, 0);
        footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
 
-       strcpy(footer_items[0], " Q (Quit) ");
-       strcpy(footer_items[1], " R (Refresh) ");
+       strcpy(footer_items[0], "Q(Quit)");
+       strcpy(footer_items[1], "R(Refresh)");
+       strcpy(footer_items[2], "S(Sorting)");
+       strcpy(footer_items[3], "T(Tree)");
 
        if (selectedwindow == CLOCK)
-               strcpy(footer_items[2], " Other Keys: 'Left', 'Right', 'Up', 
'Down', 'enter', "
-                       " '/', 'Esc' ");
+               strcpy(footer_items[4], " Other Keys: 'Left', 'Right', 'Up', 
'Down', 'enter', "
+                       " '/', 'Esc', ");
        else
-               strcpy(footer_items[2], " Other Keys: 'Left', 'Right' ");
+               strcpy(footer_items[4], " Other Keys: 'Left', 'Right' ");
 
-       strcpy(footer_items[3], "");
+       strcpy(footer_items[5], "");
 
        werase(stdscr);
        refresh();
diff --git a/powerdebug.c b/powerdebug.c
index 8afe588..b01a9d4 100644
--- a/powerdebug.c
+++ b/powerdebug.c
@@ -74,17 +74,6 @@ static struct option long_options[] = {
        { 0, 0, 0, 0 }
 };
 
-struct powerdebug_options {
-       bool verbose;
-       bool regulators;
-       bool sensors;
-       bool clocks;
-       bool dump;
-       unsigned int ticktime;
-       int selectedwindow;
-       char *clkname;
-};
-
 int getoptions(int argc, char *argv[], struct powerdebug_options *options)
 {
        int c;
@@ -92,6 +81,7 @@ int getoptions(int argc, char *argv[], struct 
powerdebug_options *options)
        memset(options, 0, sizeof(*options));
        options->ticktime = 10;
        options->selectedwindow = -1;
+       options->sorting = CLOCK_NO_SORT;
 
        while (1) {
                int optindex = 0;
@@ -231,6 +221,16 @@ int keystroke_callback(bool *enter_hit, bool 
*findparent_ncurses,
        } else
                *refreshwin = false;
 
+       if (keychar == 'S') {
+               *refreshwin = true;
+               options->sorting = CLOCK_SORT;
+       }
+
+       if (keychar == 'T') {
+               *refreshwin = true;
+               options->sorting = CLOCK_NO_SORT;
+       }
+
        return 0;
 }
 
@@ -272,8 +272,9 @@ int mainloop(struct powerdebug_options *options,
                                                command = CLOCK_SELECTED;
                                        if (refreshwin)
                                                command = REFRESH_WINDOW;
+
                                        hrow = read_and_print_clock_info(
-                                               options->verbose,
+                                               options,
                                                highlighted_row,
                                                command);
                                        highlighted_row = hrow;
diff --git a/powerdebug.h b/powerdebug.h
index 72c2578..a1c90eb 100644
--- a/powerdebug.h
+++ b/powerdebug.h
@@ -27,6 +27,19 @@
 #define TOTAL_FEATURE_WINS 3  /* Regulator, Clock and Sensor (for now) */
 enum {CLOCK, REGULATOR, SENSOR};
 enum {CLOCK_SELECTED = 1, REFRESH_WINDOW};
+enum {CLOCK_NO_SORT = 1, CLOCK_SORT};
+
+struct powerdebug_options {
+       bool verbose;
+       bool regulators;
+       bool sensors;
+       bool clocks;
+       bool dump;
+       unsigned int ticktime;
+       int selectedwindow;
+       int sorting;
+       char *clkname;
+};
 
 extern void read_and_dump_clock_info(int verbose);
 extern void read_and_dump_clock_info_one(char *clk, bool dump);
@@ -36,8 +49,8 @@ extern struct clock_info *read_clock_info_recur(char 
*clkpath, int level,
 extern void dump_clock_info(struct clock_info *clk, int level, int bmp);
 extern inline void insert_children(struct clock_info *parent, struct 
clock_info *clk);
 extern void find_parents_for_clock(char *clkname, int complete);
-extern int  read_and_print_clock_info(int verbose, int hrow, int selected);
-extern void print_clock_info(int verbose, int hrow, int selected);
+extern int  read_and_print_clock_info(struct powerdebug_options *options, int 
hrow, int selected);
+extern void print_clock_info(struct powerdebug_options *options, int hrow, int 
selected);
 extern void print_string_val(char *name, char *val);
 extern void print_clock_header(void);
 extern void print_one_clock(int line, char *str, int bold, int highlight);
-- 
1.7.0.4


_______________________________________________
linaro-dev mailing list
linaro-dev@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/linaro-dev

Reply via email to