From patchwork Thu Jun 16 20:29:46 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 2003 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 1317F23E54 for ; Thu, 16 Jun 2011 20:31:53 +0000 (UTC) Received: from mail-vx0-f180.google.com (mail-vx0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id D62AAA18621 for ; Thu, 16 Jun 2011 20:31:52 +0000 (UTC) Received: by mail-vx0-f180.google.com with SMTP id 12so2092663vxk.11 for ; Thu, 16 Jun 2011 13:31:52 -0700 (PDT) Received: by 10.52.98.97 with SMTP id eh1mr1965166vdb.7.1308256312653; Thu, 16 Jun 2011 13:31:52 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs211219vdc; Thu, 16 Jun 2011 13:31:52 -0700 (PDT) Received: by 10.216.239.67 with SMTP id b45mr1421377wer.44.1308256307267; Thu, 16 Jun 2011 13:31:47 -0700 (PDT) Received: from smtp.smtpout.orange.fr (smtp08.smtpout.orange.fr [80.12.242.130]) by mx.google.com with ESMTP id m30si5070541weq.66.2011.06.16.13.31.46; Thu, 16 Jun 2011 13:31:47 -0700 (PDT) Received-SPF: neutral (google.com: 80.12.242.130 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=80.12.242.130; Authentication-Results: mx.google.com; spf=neutral (google.com: 80.12.242.130 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) smtp.mail=daniel.lezcano@linaro.org Received: from monster.dhcp.lxc ([92.134.76.78]) by mwinf5d16 with ME id wkXf1g0031hMfSL03kXmJz; Thu, 16 Jun 2011 22:31:46 +0200 From: Daniel Lezcano To: patches@linaro.org Subject: [PATCH 17/28] use 'find' tree function Date: Thu, 16 Jun 2011 22:29:46 +0200 Message-Id: <1308256197-29155-17-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1308256197-29155-1-git-send-email-daniel.lezcano@linaro.org> References: <1308256197-29155-1-git-send-email-daniel.lezcano@linaro.org> Signed-off-by: Daniel Lezcano --- clocks.c | 192 +++++++++++++++++++------------------------------------------- tree.c | 11 ++++ tree.h | 2 + 3 files changed, 71 insertions(+), 134 deletions(-) diff --git a/clocks.c b/clocks.c index 5f494cd..d70d480 100644 --- a/clocks.c +++ b/clocks.c @@ -149,93 +149,81 @@ static inline int file_read_hex(const char *file, int *value) return file_read_from_format(file, value, "%x"); } -static void dump_parent(struct clock_info *clk, int line, bool dump) +static inline const char *clock_rate(int *rate) { - char *unit = "Hz"; - double drate; - static char spaces[64]; - char str[256]; - static int maxline; + int r; - if (maxline < line) - maxline = line; + /* GHZ */ + r = *rate >> 30; + if (r) { + *rate = r; + return "GHZ"; + } - if (clk && clk->parent) - dump_parent(clk->parent, ++line, dump); + /* MHZ */ + r = *rate >> 20; + if (r) { + *rate = r; + return "MHZ"; + } - drate = (double)clk->rate; - if (drate > 1000 && drate < 1000000) { - unit = "KHz"; - drate /= 1000; - } - if (drate > 1000000) { - unit = "MHz"; - drate /= 1000000; - } - if (clk == clocks_info) { - line++; - strcpy(spaces, ""); - sprintf(str, "%s%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n", - spaces, clk->name, clk->flags, clk->usecount, drate, - unit); - } else { - if (!(clk->parent == clocks_info)) - strcat(spaces, " "); - sprintf(str, "%s`- %s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n", - spaces, clk->name, clk->flags, clk->usecount, drate, - unit); - } - if (dump) - //printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str); - printf("%s", str); - else - print_one_clock(maxline - line + 2, str, 1, 0); + /* KHZ */ + r = *rate >> 10; + if (r) { + *rate = r; + return "KHZ"; + } + + return ""; } -static struct clock_info *find_clock(struct clock_info *clk, char *clkarg) +static int dump_clock_cb(struct tree *t, void *data) { - int i; - struct clock_info *ret = clk; - - if (!strcmp(clk->name, clkarg)) - return ret; + struct clock_info *clk = t->private; + struct clock_info *pclk; + const char *unit; + int ret = 0; + int rate = clk->rate; - if (clk->children) { - for (i = 0; i < clk->num_children; i++) { - if (!strcmp(clk->children[i]->name, clkarg)) - return clk->children[i]; - } - for (i = 0; i < clk->num_children; i++) { - ret = find_clock(clk->children[i], clkarg); - if (ret) - return ret; - } + if (!t->parent) { + printf("/\n"); + clk->prefix = ""; + return 0; } - return NULL; + pclk = t->parent->private; + + if (!clk->prefix) + ret = asprintf(&clk->prefix, "%s%s%s", pclk->prefix, + t->depth > 1 ? " ": "", t->next ? "|" : " "); + if (ret < 0) + return -1; + + unit = clock_rate(&rate); + + printf("%s%s-- %s (flags:0x%x, usecount:%d, rate: %d %s)\n", + clk->prefix, !t->next ? "`" : "", t->name, clk->flags, + clk->usecount, rate, unit); + + return 0; } -static void dump_all_parents(char *clkarg, bool dump) +int dump_clock_info(void) { - struct clock_info *clk; - char spaces[1024]; - - strcpy(spaces, ""); + return tree_for_each(clock_tree, dump_clock_cb, NULL); +} - clk = find_clock(clocks_info, clkarg); +static int dump_all_parents(char *clkarg, bool dump) +{ + struct tree *tree; - if (!clk) + tree = tree_find(clock_tree, clkarg); + if (!tree) { printf("Clock NOT found!\n"); - else { - /* while(clk && clk != clocks_info) { */ - /* printf("%s\n", clk->name); */ - /* strcat(spaces, " "); */ - /* clk = clk->parent; */ - /* printf("%s <-- ", spaces); */ - /* } */ - /* printf(" /\n"); */ - dump_parent(clk, 1, dump); + return -1; } + + return tree_for_each_parent(tree, dump_clock_cb, NULL); } void find_parents_for_clock(char *clkname, int complete) @@ -587,70 +575,6 @@ void read_and_dump_clock_info_one(char *clk, bool dump) printf("\n\n"); } -static inline const char *clock_rate(int *rate) -{ - int r; - - /* GHZ */ - r = *rate >> 30; - if (r) { - *rate = r; - return "GHZ"; - } - - /* MHZ */ - r = *rate >> 20; - if (r) { - *rate = r; - return "MHZ"; - } - - /* KHZ */ - r = *rate >> 10; - if (r) { - *rate = r; - return "KHZ"; - } - - return ""; -} - -static int dump_clock_cb(struct tree *t, void *data) -{ - struct clock_info *clk = t->private; - struct clock_info *pclk; - const char *unit; - int ret = 0; - int rate = clk->rate; - - if (!t->parent) { - printf("/\n"); - clk->prefix = ""; - return 0; - } - - pclk = t->parent->private; - - if (!clk->prefix) - ret = asprintf(&clk->prefix, "%s%s%s", pclk->prefix, - t->depth > 1 ? " ": "", t->next ? "|" : " "); - if (ret < 0) - return -1; - - unit = clock_rate(&rate); - - printf("%s%s-- %s (flags:0x%x, usecount:%d, rate: %d %s)\n", - clk->prefix, !t->next ? "`" : "", t->name, clk->flags, - clk->usecount, rate, unit); - - return 0; -} - -int dump_clock_info(void) -{ - return tree_for_each(clock_tree, dump_clock_cb, NULL); -} - void read_and_dump_clock_info(int verbose) { (void)verbose; diff --git a/tree.c b/tree.c index 7466e31..0a9c119 100644 --- a/tree.c +++ b/tree.c @@ -224,6 +224,17 @@ int tree_for_each(struct tree *tree, tree_cb_t cb, void *data) return tree_for_each(tree->next, cb, data); } +int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data) +{ + if (!tree) + return 0; + + if (tree_for_each_parent(tree->parent, cb, data)) + return -1; + + return cb(tree, data); +} + struct tree *tree_find(struct tree *tree, const char *name) { struct tree *t; diff --git a/tree.h b/tree.h index 7be3fc4..176dd23 100644 --- a/tree.h +++ b/tree.h @@ -45,3 +45,5 @@ extern struct tree *tree_load(const char *path, tree_filter_t filter); extern struct tree *tree_find(struct tree *tree, const char *name); extern int tree_for_each(struct tree *tree, tree_cb_t cb, void *data); + +extern int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data);