perf tools: Consolidate management of default sort orders
authorNamhyung Kim <namhyung@kernel.org>
Tue, 18 Mar 2014 02:31:39 +0000 (11:31 +0900)
committerJiri Olsa <jolsa@kernel.org>
Wed, 21 May 2014 09:45:35 +0000 (11:45 +0200)
The perf uses different default sort orders for different use-cases,
and this was scattered throughout the code.  Add get_default_sort_
order() function to handle this and change initial value of sort_order
to NULL to distinguish it from user-given one.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1400480762-22852-10-git-send-email-namhyung@kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
tools/perf/builtin-diff.c
tools/perf/builtin-report.c
tools/perf/builtin-top.c
tools/perf/util/sort.c
tools/perf/util/sort.h

index b60c711d4e724c3c42fa164b43343fa0c0c29dc6..8bff543acaab7a093ed3579b42b24bc15e8d14ab 100644 (file)
@@ -60,7 +60,6 @@ static int data__files_cnt;
 #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
 #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
 
-static char diff__default_sort_order[] = "dso,symbol";
 static bool force;
 static bool show_period;
 static bool show_formula;
@@ -1142,7 +1141,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
 {
        perf_config(perf_default_config, NULL);
 
-       sort_order = diff__default_sort_order;
        argc = parse_options(argc, argv, options, diff_usage, 0);
 
        if (symbol__init() < 0)
@@ -1153,6 +1151,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
 
        ui_init();
 
+       sort__mode = SORT_MODE__DIFF;
+
        if (setup_sorting() < 0)
                usage_with_options(diff_usage, options);
 
index d0180d5de781162b7068e447df1578666701b0b2..f4d640cfdf1667b8ab34d4a20b90bfd8ff131d82 100644 (file)
@@ -805,30 +805,12 @@ repeat:
        if (branch_mode == -1 && has_br_stack)
                sort__mode = SORT_MODE__BRANCH;
 
-       /* sort__mode could be NORMAL if --no-branch-stack */
-       if (sort__mode == SORT_MODE__BRANCH) {
-               /*
-                * if no sort_order is provided, then specify
-                * branch-mode specific order
-                */
-               if (sort_order == default_sort_order)
-                       sort_order = "comm,dso_from,symbol_from,"
-                                    "dso_to,symbol_to";
-
-       }
        if (report.mem_mode) {
                if (sort__mode == SORT_MODE__BRANCH) {
                        pr_err("branch and mem mode incompatible\n");
                        goto error;
                }
                sort__mode = SORT_MODE__MEMORY;
-
-               /*
-                * if no sort_order is provided, then specify
-                * branch-mode specific order
-                */
-               if (sort_order == default_sort_order)
-                       sort_order = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
        }
 
        if (setup_sorting() < 0) {
index 4fef1e415129262ecf0969b3a479f7db114ed48b..34764b6eabf95948305a095af619cecb9f3551fe 100644 (file)
@@ -1137,8 +1137,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
        if (argc)
                usage_with_options(top_usage, options);
 
-       if (sort_order == default_sort_order)
-               sort_order = "dso,symbol";
+       sort__mode = SORT_MODE__TOP;
 
        if (setup_sorting() < 0) {
                parse_options_usage(top_usage, options, "s", 1);
index 916652af8304d8a42d1b947f46c713663dbf9026..d64c1e58f1b1a54705ef28270dd3b5ff5b0c2936 100644 (file)
@@ -8,7 +8,11 @@ regex_t                parent_regex;
 const char     default_parent_pattern[] = "^sys_|^do_page_fault";
 const char     *parent_pattern = default_parent_pattern;
 const char     default_sort_order[] = "comm,dso,symbol";
-const char     *sort_order = default_sort_order;
+const char     default_branch_sort_order[] = "comm,dso_from,symbol_from,dso_to,symbol_to";
+const char     default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
+const char     default_top_sort_order[] = "dso,symbol";
+const char     default_diff_sort_order[] = "dso,symbol";
+const char     *sort_order;
 regex_t                ignore_callees_regex;
 int            have_ignore_callees = 0;
 int            sort__need_collapse = 0;
@@ -1218,11 +1222,31 @@ int sort_dimension__add(const char *tok)
        return -ESRCH;
 }
 
+static const char *get_default_sort_order(void)
+{
+       const char *default_sort_orders[] = {
+               default_sort_order,
+               default_branch_sort_order,
+               default_mem_sort_order,
+               default_top_sort_order,
+               default_diff_sort_order,
+       };
+
+       BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
+
+       return default_sort_orders[sort__mode];
+}
+
 int setup_sorting(void)
 {
-       char *tmp, *tok, *str = strdup(sort_order);
+       char *tmp, *tok, *str;
+       const char *sort_keys = sort_order;
        int ret = 0;
 
+       if (sort_keys == NULL)
+               sort_keys = get_default_sort_order();
+
+       str = strdup(sort_keys);
        if (str == NULL) {
                error("Not enough memory to setup sort keys");
                return -ENOMEM;
index 43e5ff42a609a6cb61675bd3cd3bacbd3d9b1c29..1a7295255dc96286e334f81bf2ec9a7ebded3d72 100644 (file)
@@ -133,6 +133,8 @@ enum sort_mode {
        SORT_MODE__NORMAL,
        SORT_MODE__BRANCH,
        SORT_MODE__MEMORY,
+       SORT_MODE__TOP,
+       SORT_MODE__DIFF,
 };
 
 enum sort_type {