perf tools: Assorted cleanup for TUI (v2)
Namhyung Kim
maintainercover perf tools: Assorted cleanup for TUI (v2)
Hello,
This patches are to cleanup TUI hists browser code for later work. I moved hist_entry_diff and hist_entry_tui under an union in order to reduce memory footprint of hist entry. Also split out hist browser functions to make it easier to read.
* changes in v2)
- add comment on the new union fields (Jiri)
- add missing sym->namelen check (Jiri)
- fix transient bug in zoom out on LEFT key press (Jiri)
It's available on 'perf/tui-cleanup-v2' branch in my tree:
git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git
Thanks,
Namhyung
Namhyung Kim (7):
perf tools: Get rid of position field from struct hist_entry perf diff: Make hist_entry_diff fields union
perf tools: Move TUI-specific fields to struct hist_entry_tui perf tools: Move init_have_children field to struct hist_entry_tui perf hists browser: Fix possible memory leak
perf hists browser: Split popup menu actions
perf hists browser: Simplify zooming code a bit
tools/perf/ui/browsers/hists.c | 612 ++++++++++++++++++++++++++--------------- tools/perf/util/hist.c | 4 +- tools/perf/util/pstack.c | 7 + tools/perf/util/pstack.h | 1 + tools/perf/util/sort.h | 36 ++- 5 files changed, 416 insertions(+), 244 deletions(-)
Signature · 1 line
2.3.5
Namhyung Kim
maintainerPATCH 1/7 perf tools: Get rid of position field from struct hist_entry
It's not used anywhere, let's get rid of it.
--- tools/perf/util/sort.h | 1 - 1 file changed, 1 deletion(-)
tools/perf/util/sort.h +0 -1
@@ -106,7 +106,6 @@ struct hist_entry {u8 filtered;char *srcline;struct symbol *parent; -unsigned long position;struct rb_root sorted_chain;struct branch_info *branch_info;struct hists *hists;
Signature · 1 line
2.3.5
Namhyung Kim
maintainerPATCH 2/7 perf diff: Make hist_entry_diff fields union
In reply to Namhyung KimThe period_ratio_delta, period_ratio and wdiff are never by used at the same time. Instead, Just one of them is accessed according to a comparison method. So make it union to reduce memory footprint.
--- tools/perf/util/sort.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
tools/perf/util/sort.h +8 -7
@@ -58,15 +58,16 @@ struct he_stat {struct hist_entry_diff {bool computed; +union { +/* PERF_HPP__DELTA */ +double period_ratio_delta; -/* PERF_HPP__DELTA */ -double period_ratio_delta; - -/* PERF_HPP__RATIO */ -double period_ratio; +/* PERF_HPP__RATIO */ +double period_ratio; -/* HISTC_WEIGHTED_DIFF */ -s64 wdiff; +/* HISTC_WEIGHTED_DIFF */ +s64 wdiff; + }; };/**
Signature · 1 line
2.3.5
Namhyung Kim
maintainerPATCH 3/7 perf tools: Move TUI-specific fields to struct hist_entry_tui
In reply to Namhyung KimSince perf diff only supports stdio output, TUI fields are only accessed from perf report (or perf top). So add new struct hist_entry_tui and move those fields into them. And include it as an union member.
--- tools/perf/ui/browsers/hists.c | 52 +++++++++++++++++++++--------------------- tools/perf/util/hist.c | 4 ++-- tools/perf/util/sort.h | 20 +++++++++++----- 3 files changed, 42 insertions(+), 34 deletions(-)
tools/perf/ui/browsers/hists.c +26 -26
@@ -61,7 +61,7 @@ static int hist_browser__get_folding(struct hist_browser *browser) rb_entry(nd, struct hist_entry, rb_node);if (he->ms.unfolded) - unfolded_rows += he->nr_rows; + unfolded_rows += he->tui.nr_rows; }return unfolded_rows; }
@@ -288,16 +288,16 @@ static bool hist_browser__toggle_fold(struct hist_browser *browser)struct hist_entry *he = browser->he_selection;hist_entry__init_have_children (he); - browser->b.nr_entries -= he->nr_rows; - browser->nr_callchain_rows -= he->nr_rows; + browser->b.nr_entries -= he->tui.nr_rows; + browser->nr_callchain_rows -= he->tui.nr_rows;if (he->ms.unfolded) - he->nr_rows =callchain__count_rows (&he->sorted_chain); + he->tui.nr_rows =callchain__count_rows (&he->sorted_chain);else - he->nr_rows =0 ; + he->tui.nr_rows =0 ; - browser->b.nr_entries += he->nr_rows; - browser->nr_callchain_rows += he->nr_rows; + browser->b.nr_entries += he->tui.nr_rows; + browser->nr_callchain_rows += he->tui.nr_rows;return true; }
@@ -367,9 +367,9 @@ static void hist_entry__set_folding(struct hist_entry *he, bool unfold)if (he->ms.has_children) {int n =callchain__set_folding (&he->sorted_chain, unfold); - he->nr_rows = unfold ? n :0 ; + he->tui.nr_rows = unfold ? n :0 ; }else - he->nr_rows =0 ; + he->tui.nr_rows =0 ; } static void
@@ -383,7 +383,7 @@ __hist_browser__set_folding(struct hist_browser *browser, bool unfold) nd = rb_next(nd)) { struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);hist_entry__set_folding (he, unfold); - browser->nr_callchain_rows += he->nr_rows; + browser->nr_callchain_rows += he->tui.nr_rows; } }
@@ -459,7 +459,7 @@ static int hist_browser__run(struct hist_browser *browser, browser->b.rows, browser->b.index, browser->b.top_idx, - h->row_offset, h->nr_rows); + h->tui.row_offset, h->tui.nr_rows); }break ;case 'C' :
@@ -742,7 +742,7 @@ static int hist_browser__show_entry(struct hist_browser *browser,int width = browser->b.width;char folded_sign =' ' ;bool current_entry =ui_browser__is_current_entry (&browser->b, row); -off_t row_offset = entry->row_offset; +off_t row_offset = entry->tui.row_offset;bool first = true;struct perf_hpp_fmt *fmt;
@@ -997,7 +997,7 @@ static void ui_browser__hists_seek(struct ui_browser *browser,* row_offset: */ h = rb_entry(browser->top, struct hist_entry, rb_node); - h->row_offset =0 ; + h->tui.row_offset =0 ;/* * Here we have to check if nd is expanded (+), if it is we can't go
@@ -1017,12 +1017,12 @@ do_offset: do { h = rb_entry(nd, struct hist_entry, rb_node); if (h->ms.unfolded) { - u16 remaining = h->nr_rows - h->row_offset; + u16 remaining = h->tui.nr_rows - h->tui.row_offset; if (offset > remaining) { offset -= remaining; - h->row_offset = 0; + h->tui.row_offset = 0; } else { - h->row_offset += offset; + h->tui.row_offset += offset; offset = 0; browser->top = nd; break;
@@ -1039,21 +1039,21 @@ do_offset: h = rb_entry(nd, struct hist_entry, rb_node);if (h->ms.unfolded) {if (first) { -if (-offset > h->row_offset) { - offset += h->row_offset; - h->row_offset =0 ; +if (-offset > h->tui.row_offset) { + offset += h->tui.row_offset; + h->tui.row_offset =0 ; }else { - h->row_offset += offset; + h->tui.row_offset += offset; offset =0 ; browser->top = nd;break ; } }else { -if (-offset > h->nr_rows) { - offset += h->nr_rows; - h->row_offset =0 ; +if (-offset > h->tui.nr_rows) { + offset += h->tui.nr_rows; + h->tui.row_offset =0 ; }else { - h->row_offset = h->nr_rows + offset; + h->tui.row_offset = h->tui.nr_rows + offset; offset =0 ; browser->top = nd;break ;
@@ -1075,7 +1075,7 @@ do_offset:*/ h = rb_entry(nd, struct hist_entry, rb_node);if (h->ms.unfolded) - h->row_offset = h->nr_rows; + h->tui.row_offset = h->tui.nr_rows;break ; } first = false;
@@ -1083,7 +1083,7 @@ do_offset: }else { browser->top = nd; h = rb_entry(nd, struct hist_entry, rb_node); - h->row_offset =0 ; + h->tui.row_offset =0 ; } }
tools/perf/util/hist.c +2 -2
@@ -1164,8 +1164,8 @@ static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h/* force fold unfiltered entry for simplicity */ h->ms.unfolded = false; - h->row_offset =0 ; - h->nr_rows =0 ; + h->tui.row_offset =0 ; + h->tui.nr_rows =0 ; hists->stats.nr_non_filtered_samples += h->stat.nr_events;
tools/perf/util/sort.h +14 -6
@@ -70,6 +70,11 @@ struct hist_entry_diff { }; }; +struct hist_entry_tui { +u16 row_offset; +u16 nr_rows; +}; +/** * struct hist_entry - histogram entry *
@@ -93,18 +98,21 @@ struct hist_entry {s32 cpu;u8 cpumode; -struct hist_entry_diff diff; -/* We are added by hists__add_dummy_entry. */ bool dummy; -/* XXX These two should move to some tree widget lib */ -u16 row_offset; -u16 nr_rows; -bool init_have_children;char level;u8 filtered; +union { +/* +* Since perf diff only supports the stdio output, TUI +* fields are only accessed from perf report (or perf +* top). So make it an union to reduce memory usage. +*/ +struct hist_entry_diff diff; +struct hist_entry_tui tui; + };char *srcline;struct symbol *parent;struct rb_root sorted_chain;
Signature · 1 line
2.3.5
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 3/7] perf tools: Move TUI-specific fields to struct hist_entry_tui
Em Mon, Apr 20, 2015 at 10:00:46PM +0900, Namhyung Kim escreveu:
tools/perf/ui/browsers/hists.c static int hist_browser__get_folding(struct hist_browser *browser) · line 64
Quoted 7 lines
+++
b/tools/perf/ui/browsers/hists.ctools/perf/ui/browsers/hists.c +1
-1@@ -61,7 +61,7 @@ static int hist_browser__get_folding(struct hist_browser *browser) rb_entry(nd, struct hist_entry, rb_node);if (he->ms.unfolded) - unfolded_rows += he->nr_rows; + unfolded_rows += he->tui.nr_rows;
To avoid all these changes, I wonder if we can't use unamed structs in addition to the unnamed union?
Like this what is done in include/net/sock.h, struct sock_common, and here:
[root@zoo ~]# cat unnamed_struct_union.c
#include <stdbool.h>
#include <stdio.h>
struct hist_entry {
union {
struct /* tui */ {
int nr_rows;
int row_offset;
/* other fields */
};
struct /* diff */ {
bool computed;
/* other fields */
};
};
};int main(int argc, char *argv[])
{
struct hist_entry he = { .nr_rows = 11, .row_offset = 19, };
printf("he.nr_rows=%d, he.row_offset=%d\n", he.nr_rows,he.row_offset);
}
[root@zoo ~]# make unnamed_struct_union
cc unnamed_struct_union.c -o unnamed_struct_union
[root@zoo ~]# ./unnamed_struct_union
he.nr_rows=11, he.row_offset=19
[root@zoo ~]#
- Arnaldo
Namhyung Kim
maintainerRe: [PATCH 3/7] perf tools: Move TUI-specific fields to struct hist_entry_tui
Quoted 11 lines · Arnaldo Carvalho de Melo
Em Mon, Apr 20, 2015 at 10:00:46PM +0900, Namhyung Kim escreveu:
tools/perf/ui/browsers/hists.c static int hist_browser__get_folding(struct hist_browser *browser) · line 64
Quoted 7 lines
+++
b/tools/perf/ui/browsers/hists.ctools/perf/ui/browsers/hists.c +1
-1@@ -61,7 +61,7 @@ static int hist_browser__get_folding(struct hist_browser *browser) rb_entry(nd, struct hist_entry, rb_node);if (he->ms.unfolded) - unfolded_rows += he->nr_rows; + unfolded_rows += he->tui.nr_rows;To avoid all these changes, I wonder if we can't use unamed structs in addition to the unnamed union?
But diff code uses named struct. Does it support a mixed union including named and unnamed members?
Hmm.. looks like it is.
#include <stdbool.h>
#include <stdio.h>
struct hist_entry {
union {
struct /* tui */ {
int nr_rows;
int row_offset;
/* other fields */
};
struct /* diff */ {
bool computed;
/* other fields */
} diff;
};
};int main(int argc, char *argv[])
{
struct hist_entry he = { .nr_rows = 11, .row_offset = 19, };
printf("he.nr_rows=%d, he.row_offset=%d, he.diff.computed=%d\n",
he.nr_rows, he.row_offset, he.diff.computed);
}namhyung@sejong:tmp$ make mixed cc mixed.c -o mixed namhyung@sejong:tmp$ ./mixed he.nr_rows=11, he.row_offset=19, he.diff.computed=11
OK, I'll change it to unnamed struct.
Thanks,
Namhyung
Quoted 41 lines
Like this what is done in
include/net/sock.h, structsock_common, and here:[root@zoo ~]# cat
unnamed_struct_union.c
#include <stdbool.h>
#include <stdio.h>struct hist_entry { union { struct /* tui */ { int nr_rows; int row_offset; /* other fields */ }; struct /* diff */ { bool computed; /* other fields */ }; }; };int
main(int argc, char *argv[]){ struct hist_entry he = { .nr_rows = 11, .row_offset = 19, }; printf("he.nr_rows=%d, he.row_offset=%d\n", he.nr_rows,he.row_offset);
}
[root@zoo ~]# makeunnamed_struct_union
ccunnamed_struct_union.c-ounnamed_struct_union
[root@zoo ~]# ./unnamed_struct_union
he.nr_rows=11, he.row_offset=19
[root@zoo ~]#- Arnaldo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Namhyung Kim
maintainerPATCH 4/7 perf tools: Move init_have_children field to struct hist_entry_tui
In reply to Namhyung KimThe init_have_children is used to init callchain info only for TUI. So it'd be better to move it to the hist_entry_tui struct.
--- tools/perf/ui/browsers/hists.c | 4 ++-- tools/perf/util/sort.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
tools/perf/ui/browsers/hists.c +2 -2
@@ -275,10 +275,10 @@ static void callchain__init_have_children(struct rb_root *root)static void hist_entry__init_have_children (struct hist_entry *he) { -if (!he->init_have_children) { +if (!he->tui.init_have_children) { he->ms.has_children = !RB_EMPTY_ROOT (&he->sorted_chain);callchain__init_have_children (&he->sorted_chain); - he->init_have_children = true; + he->tui.init_have_children = true; } }
tools/perf/util/sort.h +1 -1
@@ -73,6 +73,7 @@ struct hist_entry_diff {struct hist_entry_tui {u16 row_offset;u16 nr_rows; +bool init_have_children; };/**
@@ -101,7 +102,6 @@ struct hist_entry {/* We are added by hists__add_dummy_entry. */ bool dummy; -bool init_have_children;char level;u8 filtered;union {
Signature · 1 line
2.3.5
Namhyung Kim
maintainerPATCH 5/7 perf hists browser: Fix possible memory leak
In reply to Namhyung KimThe options array saves strings for each popup menu item. The number of items can be vary according to the currently selected item. So it can leak some memory if it's exited from a small item. Fix it by freeing all items when loop terminates.
--- tools/perf/ui/browsers/hists.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/ui/browsers/hists.c +3 -2
@@ -1691,7 +1691,8 @@ skip_annotation: "Switch to another data file in PWD") > 0) switch_data = nr_options++; add_exit_option: - options[nr_options++] = (char *)"Exit" ; +if (asprintf (&options[nr_options],"Exit" ) >0 ) + nr_options++; retry_popup_menu: choice =ui__popup_menu (nr_options, options);
@@ -1812,7 +1813,7 @@ out_free_stack:pstack__delete (fstack); out:hist_browser__delete (browser); -free_popup_options (options, nr_options -1 ); +free_popup_options (options,ARRAY_SIZE (options));return key; }
Signature · 1 line
2.3.5
Namhyung Kim
maintainerPATCH 6/7 perf hists browser: Split popup menu actions
In reply to Namhyung KimCurrently perf_evsel__hists_browse() function spins on a huge loop and handles many key actions. Since it's hard to read and modify, let's split it out into small helper functions.
The add_XXX_opt() functions are to register popup menu item on the selected entry. When it adds an item, it also saves related data into struct popup_option and returns 1 so that it can increase the number of items (nr_opts). A callback function named do_XXX is called with saved data when the item is selected by user.
No functional change intended.
--- tools/perf/ui/browsers/hists.c | 567 ++++++++++++++++++++++++++--------------- 1 file changed, 365 insertions(+), 202 deletions(-)
tools/perf/ui/browsers/hists.c +365 -202
@@ -1216,11 +1216,6 @@ static void hist_browser__delete(struct hist_browser *browser)free (browser); } -static struct hist_entry *hist_browser__selected_entry (struct hist_browser *browser) -{ -return browser->he_selection; -} -static struct thread *hist_browser__selected_thread (struct hist_browser *browser) {return browser->he_selection->thread;
@@ -1395,6 +1390,281 @@ close_file_and_continue:return ret; } +struct popup_option { +struct thread *thread; +struct map *map; +struct dso *dso; +struct symbol *sym; + +int (*fn)(struct popup_option *opt,struct hist_browser *browser, +struct hist_browser_timer *hbt,struct pstack *pstack, +struct perf_session_env *env); +}; + +static int +do_annotate (struct popup_option *opt,struct hist_browser *browser, +struct hist_browser_timer *hbt,struct pstack *pstack__maybe_unused , +struct perf_session_env *env) +{ +struct perf_evsel *evsel; +struct annotation *notes; +struct map_symbol ms; +int err; + +if (!objdump_path &&perf_session_env__lookup_objdump (env)) +return 0 ; + + ms.map = opt->map; + ms.sym = opt->sym; + + notes =symbol__annotation (ms.sym); +if (!notes->src) +return 0 ; + + evsel =hists_to_evsel (browser->hists); + err =map_symbol__tui_annotate (&ms, evsel, hbt); +/* +* offer option to annotate the other branch source or target +* (if they exists) when returning from annotate +*/ +if ((err =='q' || err ==CTRL ('c' )) + && browser->he_selection->branch_info) +return 1 ; + +ui_browser__update_nr_entries (&browser->b, browser->hists->nr_entries); +if (err) +ui_browser__handle_resize (&browser->b); +return 0 ; +} + +static int +add_annotate_opt (struct popup_option *opt,char **optstr, +struct hist_browser *browser__maybe_unused , +struct map *map,struct symbol *sym) +{ +if (sym ==NULL || map->dso->annotate_warned) +return 0 ; + +if (asprintf (optstr,"Annotate %s" , sym->name) <0 ) +return 0 ; + + opt->map = map; + opt->sym = sym; + opt->fn = do_annotate; +return 1 ; +} + +static int do_zoom_thread (struct popup_option *opt, +struct hist_browser *browser, +struct hist_browser_timer *hbt__maybe_unused , +struct pstack *pstack, +struct perf_session_env *env__maybe_unused ) +{ +struct thread *thread = opt->thread; + +if (browser->hists->thread_filter) { +pstack__remove (pstack, &browser->hists->thread_filter); +perf_hpp__set_elide (HISTC_THREAD , false); +thread__zput (browser->hists->thread_filter); +ui_helpline__pop (); + }else { +ui_helpline__fpush ("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"" , + thread->comm_set ?thread__comm_str (thread) :"" , + thread->tid); + browser->hists->thread_filter =thread__get (thread); +perf_hpp__set_elide (HISTC_THREAD , false); +pstack__push (pstack, &browser->hists->thread_filter); + } + +hists__filter_by_thread (browser->hists); +hist_browser__reset (browser); +return 0 ; +} + +static int +add_thread_opt (struct popup_option *opt,char **optstr, +struct hist_browser *browser,struct thread *thread) +{ +if (thread ==NULL ) +return 0 ; + +if (asprintf (optstr,"Zoom %s %s(%d) thread" , + browser->hists->thread_filter ?"out of" :"into" , + thread->comm_set ?thread__comm_str (thread) :"" , + thread->tid) <0 ) +return 0 ; + + opt->thread = thread; + opt->fn = do_zoom_thread; +return 1 ; +} + +static int do_zoom_dso (struct popup_option *opt, +struct hist_browser *browser, +struct hist_browser_timer *hbt__maybe_unused , +struct pstack *pstack, +struct perf_session_env *env__maybe_unused ) +{ +struct dso *dso = opt->dso; + +if (browser->hists->dso_filter) { +pstack__remove (pstack, &browser->hists->dso_filter); +perf_hpp__set_elide (HISTC_DSO , false); + browser->hists->dso_filter =NULL ; +ui_helpline__pop (); + }else { +if (dso ==NULL ) +return 0 ; +ui_helpline__fpush ("To zoom out press <- or -> + \"Zoom out of %s DSO\"" , + dso->kernel ?"the Kernel" : dso->short_name); + browser->hists->dso_filter = dso; +perf_hpp__set_elide (HISTC_DSO , true); +pstack__push (pstack, &browser->hists->dso_filter); + } + +hists__filter_by_dso (browser->hists); +hist_browser__reset (browser); +return 0 ; +} + +static int +add_dso_opt (struct popup_option *opt,char **optstr, +struct hist_browser *browser,struct dso *dso) +{ +if (dso ==NULL ) +return 0 ; + +if (asprintf (optstr,"Zoom %s %s DSO" , + browser->hists->dso_filter ?"out of" :"into" , + dso->kernel ?"the Kernel" : dso->short_name) <0 ) +return 0 ; + + opt->dso = dso; + opt->fn = do_zoom_dso; +return 1 ; +} + +static int do_browse_map (struct popup_option *opt, +struct hist_browser *browser__maybe_unused , +struct hist_browser_timer *hbt__maybe_unused , +struct pstack *pstack__maybe_unused , +struct perf_session_env *env__maybe_unused ) +{ +map__browse (opt->map); +return 0 ; +} + +static int +add_map_opt (struct popup_option *opt,char **optstr, +struct hist_browser *browser__maybe_unused ,struct map *map) +{ +if (map ==NULL ) +return 0 ; + +if (asprintf (optstr,"Browse map details" ) <0 ) +return 0 ; + + opt->map = map; + opt->fn = do_browse_map; +return 1 ; +} + +static int do_run_script (struct popup_option *opt, +struct hist_browser *browser__maybe_unused , +struct hist_browser_timer *hbt__maybe_unused , +struct pstack *pstack__maybe_unused , +struct perf_session_env *env__maybe_unused ) +{ +char script_opt[64 ]; +memset (script_opt,0 ,sizeof (script_opt)); + +if (opt->thread) { +scnprintf (script_opt,sizeof (script_opt)," -c %s " , +thread__comm_str (opt->thread)); + }else if (opt->sym) { +scnprintf (script_opt,sizeof (script_opt)," -S %s " , + opt->sym->name); + } + +script_browse (script_opt); +return 0 ; +} + +static int +add_script_opt (struct popup_option *opt,char **optstr, +struct hist_browser *browser__maybe_unused , +struct thread *thread,struct symbol *sym) +{ +if (thread) { +if (asprintf (optstr,"Run scripts for samples of thread [%s]" , +thread__comm_str (thread)) <0 ) +return 0 ; + }else if (sym) { +if (asprintf (optstr,"Run scripts for samples of symbol [%s]" , + sym->name) <0 ) +return 0 ; + }else { +if (asprintf (optstr,"Run scripts for all samples" ) <0 ) +return 0 ; + } + + opt->thread = thread; + opt->sym = sym; + opt->fn = do_run_script; + +return 1 ; +} + +static int do_switch_data (struct popup_option *opt__maybe_unused , +struct hist_browser *browser__maybe_unused , +struct hist_browser_timer *hbt__maybe_unused , +struct pstack *pstack__maybe_unused , +struct perf_session_env *env__maybe_unused ) +{ +if (switch_data_file ()) { +ui__warning ("Won't switch the data files due to\n" +"no valid data file get selected!\n" ); +return 0 ; + } + +return K_SWITCH_INPUT_DATA ; +} + +static int +add_switch_opt (struct popup_option *opt,char **optstr, +struct hist_browser *browser__maybe_unused , +struct hist_browser_timer *hbt) +{ +if (!is_report_browser (hbt)) +return 0 ; + +if (asprintf (optstr,"Switch to another data file in PWD" ) <0 ) +return 0 ; + + opt->fn = do_switch_data; +return 1 ; +} + +static int do_exit_browser (struct popup_option *opt__maybe_unused , +struct hist_browser *browser__maybe_unused , +struct hist_browser_timer *hbt__maybe_unused , +struct pstack *pstack__maybe_unused , +struct perf_session_env *env__maybe_unused ) +{ +return 0 ; +} + +static int +add_exit_opt (struct popup_option *opt,char **optstr, +struct hist_browser *browser__maybe_unused ) +{ +if (asprintf (optstr,"Exit" ) <0 ) +return 0 ; + + opt->fn = do_exit_browser; +return 1 ; +} +static void hist_browser__update_nr_entries (struct hist_browser *hb) {u64 nr_entries =0 ;
@@ -1424,11 +1694,11 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,struct hist_browser *browser =hist_browser__new (hists);struct branch_info *bi;struct pstack *fstack; -char *options[16 ]; -int nr_options =0 ; +char *optstr[16 ]; +struct popup_option opts[16 ]; +int nr_opts =0 ;int key =-1 ;char buf[64 ]; -char script_opt[64 ];int delay_secs = hbt ? hbt->refresh :0 ;struct perf_hpp_fmt *fmt;
@@ -1479,7 +1749,8 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,ui_helpline__push (helpline); -memset (options,0 ,sizeof (options)); +memset (optstr,0 ,sizeof (optstr)); +memset (opts,0 ,sizeof (opts));perf_hpp__for_each_format (fmt)perf_hpp__reset_width (fmt, hists);
@@ -1489,14 +1760,10 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,while (1 ) {struct thread *thread =NULL ; -const struct dso *dso =NULL ; -int choice =0 , - annotate =-2 , zoom_dso =-2 , zoom_thread =-2 , - annotate_f =-2 , annotate_t =-2 , browse_map =-2 ; -int scripts_comm =-2 , scripts_symbol =-2 , - scripts_all =-2 , switch_data =-2 ; +struct dso *dso =NULL ; +int choice =0 ; - nr_options =0 ; + nr_opts =0 ; key =hist_browser__run (browser, hbt);
@@ -1526,17 +1793,28 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events, browser->selection->sym ==NULL || browser->selection->map->dso->annotate_warned) continue; - goto do_annotate; + + opts->map = browser->selection->map; + opts->sym = browser->selection->sym; + +do_annotate (opts, browser, hbt, fstack, env); +continue ;case 'P' :hist_browser__dump (browser);continue ;case 'd' : - goto zoom_dso; + opts->dso = dso; + +do_zoom_dso (opts, browser, hbt, fstack, env); +continue ;case 'V' : browser->show_dso = !browser->show_dso;continue ;case 't' : - goto zoom_thread; + opts->thread = thread; + +do_zoom_thread (opts, browser, hbt, fstack, env); +continue ;case '/' : if (ui_browser__input_window("Symbol to show", "Please enter the name of symbol you want to see",
@@ -1548,12 +1826,20 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events, }continue ;case 'r' : -if (is_report_browser (hbt)) - goto do_scripts; +if (is_report_browser (hbt)) { + opts->thread =NULL ; + opts->sym =NULL ; + +do_run_script (opts, browser, hbt, fstack, env); + }continue ;case 's' : -if (is_report_browser (hbt)) - goto do_data_switch; +if (is_report_browser (hbt)) { + key =do_switch_data (opts, browser, hbt, + fstack, env); +if (key ==K_SWITCH_INPUT_DATA ) + goto out_free_stack; + }continue ;case 'i' :/* env->arch is NULL for live-mode (i.e. perf top) */
@@ -1592,10 +1878,18 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,continue ; } top =pstack__pop (fstack); -if (top == &browser->hists->dso_filter) - goto zoom_out_dso; -if (top == &browser->hists->thread_filter) - goto zoom_out_thread; +if (top == &browser->hists->dso_filter) { +perf_hpp__set_elide (HISTC_DSO , false); + browser->hists->dso_filter =NULL ; +hists__filter_by_dso (browser->hists); + } +if (top == &browser->hists->thread_filter) { +perf_hpp__set_elide (HISTC_THREAD , false); +thread__zput (browser->hists->thread_filter); +hists__filter_by_thread (browser->hists); + } +ui_helpline__pop (); +hist_browser__reset (browser);continue ; }case K_ESC :
@@ -1620,200 +1914,69 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,if (sort__mode ==SORT_MODE__BRANCH ) { bi = browser->he_selection->branch_info; -if (bi ==NULL ) - goto skip_annotation; - -if (bi->from.sym !=NULL && - !bi->from.map->dso->annotate_warned && -asprintf (&options[nr_options],"Annotate %s" , bi->from.sym->name) >0 ) { - annotate_f = nr_options++; - } - -if (bi->to.sym !=NULL && - !bi->to.map->dso->annotate_warned && - (bi->to.sym != bi->from.sym || - bi->to.map->dso != bi->from.map->dso) && -asprintf (&options[nr_options],"Annotate %s" , bi->to.sym->name) >0 ) { - annotate_t = nr_options++; +if (bi !=NULL ) { + nr_opts +=add_annotate_opt (&opts[nr_opts], + &optstr[nr_opts], + browser, + bi->from.map, + bi->from.sym); +if (bi->to.sym != bi->from.sym) + nr_opts +=add_annotate_opt (&opts[nr_opts], + &optstr[nr_opts], + browser, + bi->to.map, + bi->to.sym); } }else { -if (browser->selection->sym !=NULL && - !browser->selection->map->dso->annotate_warned) { -struct annotation *notes; - - notes =symbol__annotation (browser->selection->sym); - -if (notes->src && -asprintf (&options[nr_options],"Annotate %s" , - browser->selection->sym->name) >0 ) { - annotate = nr_options++; - } - } + nr_opts +=add_annotate_opt (&opts[nr_opts], + &optstr[nr_opts], browser, + browser->selection->map, + browser->selection->sym); } skip_annotation: -if (thread !=NULL && -asprintf (&options[nr_options],"Zoom %s %s(%d) thread" , - (browser->hists->thread_filter ?"out of" :"into" ), - (thread->comm_set ?thread__comm_str (thread) :"" ), - thread->tid) >0 ) - zoom_thread = nr_options++; - -if (dso !=NULL && -asprintf (&options[nr_options],"Zoom %s %s DSO" , - (browser->hists->dso_filter ?"out of" :"into" ), - (dso->kernel ?"the Kernel" : dso->short_name)) >0 ) - zoom_dso = nr_options++; - -if (browser->selection !=NULL && - browser->selection->map !=NULL && -asprintf (&options[nr_options],"Browse map details" ) >0 ) - browse_map = nr_options++; + nr_opts +=add_thread_opt (&opts[nr_opts], &optstr[nr_opts], + browser, thread); + nr_opts +=add_dso_opt (&opts[nr_opts], &optstr[nr_opts], + browser, dso); + nr_opts +=add_map_opt (&opts[nr_opts], &optstr[nr_opts], + browser, browser->selection->map);/* perf script support */ if (browser->he_selection) { -struct symbol *sym; - -if (asprintf (&options[nr_options],"Run scripts for samples of thread [%s]" , -thread__comm_str (browser->he_selection->thread)) >0 ) - scripts_comm = nr_options++; - - sym = browser->he_selection->ms.sym; -if (sym && sym->namelen && -asprintf (&options[nr_options],"Run scripts for samples of symbol [%s]" , - sym->name) >0 ) - scripts_symbol = nr_options++; + nr_opts +=add_script_opt (&opts[nr_opts], &optstr[nr_opts], + browser, thread,NULL ); + nr_opts +=add_script_opt (&opts[nr_opts], &optstr[nr_opts], + browser,NULL , + browser->selection->sym); } + nr_opts +=add_script_opt (&opts[nr_opts], &optstr[nr_opts], + browser,NULL ,NULL ); -if (asprintf (&options[nr_options],"Run scripts for all samples" ) >0 ) - scripts_all = nr_options++; - -if (is_report_browser (hbt) &&asprintf (&options[nr_options], -"Switch to another data file in PWD" ) >0 ) - switch_data = nr_options++; + nr_opts +=add_switch_opt (&opts[nr_opts], &optstr[nr_opts], + browser, hbt); add_exit_option: -if (asprintf (&options[nr_options],"Exit" ) >0 ) - nr_options++; -retry_popup_menu: - choice =ui__popup_menu (nr_options, options); - -if (choice == nr_options -1 ) -break ; - -if (choice ==-1 ) { -free_popup_options (options, nr_options -1 ); -continue ; - } - -if (choice == annotate || choice == annotate_t || choice == annotate_f) { -struct hist_entry *he; -struct annotation *notes; -struct map_symbol ms; -int err; -do_annotate: -if (!objdump_path &&perf_session_env__lookup_objdump (env)) -continue ; + nr_opts +=add_exit_opt (&opts[nr_opts], &optstr[nr_opts], + browser); - he =hist_browser__selected_entry (browser); -if (he ==NULL ) -continue ; - -if (choice == annotate_f) { - ms.map = he->branch_info->from.map; - ms.sym = he->branch_info->from.sym; - }else if (choice == annotate_t) { - ms.map = he->branch_info->to.map; - ms.sym = he->branch_info->to.sym; - }else { - ms = *browser->selection; - } - - notes =symbol__annotation (ms.sym); -if (!notes->src) -continue ; - - err =map_symbol__tui_annotate (&ms, evsel, hbt); -/* -* offer option to annotate the other branch source or target -* (if they exists) when returning from annotate -*/ -if ((err =='q' || err ==CTRL ('c' )) - && annotate_t !=-2 && annotate_f !=-2 ) - goto retry_popup_menu; - -ui_browser__update_nr_entries (&browser->b, browser->hists->nr_entries); -if (err) -ui_browser__handle_resize (&browser->b); - - }else if (choice == browse_map) -map__browse (browser->selection->map); -else if (choice == zoom_dso) { -zoom_dso: -if (browser->hists->dso_filter) { -pstack__remove (fstack, &browser->hists->dso_filter); -zoom_out_dso: -ui_helpline__pop (); - browser->hists->dso_filter =NULL ; -perf_hpp__set_elide (HISTC_DSO , false); - }else { -if (dso ==NULL ) -continue ; -ui_helpline__fpush ("To zoom out press <- or -> + \"Zoom out of %s DSO\"" , - dso->kernel ?"the Kernel" : dso->short_name); - browser->hists->dso_filter = dso; -perf_hpp__set_elide (HISTC_DSO , true); -pstack__push (fstack, &browser->hists->dso_filter); - } -hists__filter_by_dso (hists); -hist_browser__reset (browser); - }else if (choice == zoom_thread) { -zoom_thread: -if (browser->hists->thread_filter) { -pstack__remove (fstack, &browser->hists->thread_filter); -zoom_out_thread: -ui_helpline__pop (); -thread__zput (browser->hists->thread_filter); -perf_hpp__set_elide (HISTC_THREAD , false); - }else { -ui_helpline__fpush ("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"" , - thread->comm_set ?thread__comm_str (thread) :"" , - thread->tid); - browser->hists->thread_filter =thread__get (thread); -perf_hpp__set_elide (HISTC_THREAD , false); -pstack__push (fstack, &browser->hists->thread_filter); - } -hists__filter_by_thread (hists); -hist_browser__reset (browser); - } -/* perf scripts support */ -else if (choice == scripts_all || choice == scripts_comm || - choice == scripts_symbol) { -do_scripts: -memset (script_opt,0 ,64 ); +do { +struct popup_option *opt; -if (choice == scripts_comm) -sprintf (script_opt," -c %s " ,thread__comm_str (browser->he_selection->thread)); + choice =ui__popup_menu (nr_opts, optstr); +if (choice ==-1 || choice >= nr_opts) +break ; -if (choice == scripts_symbol) -sprintf (script_opt," -S %s " , browser->he_selection->ms.sym->name); + opt = &opts[choice]; + key = opt->fn (opt, browser, hbt, fstack, env); + }while (key ==1 ); -script_browse (script_opt); - } -/* Switch to another data file */ -else if (choice == switch_data) { -do_data_switch: -if (!switch_data_file ()) { - key =K_SWITCH_INPUT_DATA ; -break ; - }else -ui__warning ("Won't switch the data files due to\n" -"no valid data file get selected!\n" ); - } +if (key ==K_SWITCH_INPUT_DATA ) +break ; } out_free_stack:pstack__delete (fstack); out:hist_browser__delete (browser); -free_popup_options (options,ARRAY_SIZE (options)); +free_popup_options (optstr,ARRAY_SIZE (optstr));return key; }
Signature · 1 line
2.3.5
Namhyung Kim
maintainerPATCH 7/7 perf hists browser: Simplify zooming code a bit
In reply to Namhyung KimIntroduce pstack_peek() and reuse do_zoom_dso/thread() function.
--- tools/perf/ui/browsers/hists.c | 18 +++++------------- tools/perf/util/pstack.c | 7 +++++++ tools/perf/util/pstack.h | 1 + 3 files changed, 13 insertions(+), 13 deletions(-)
tools/perf/ui/browsers/hists.c +5 -13
@@ -1877,19 +1877,11 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events, goto out_free_stack;continue ; } - top =pstack__pop (fstack); -if (top == &browser->hists->dso_filter) { -perf_hpp__set_elide (HISTC_DSO , false); - browser->hists->dso_filter =NULL ; -hists__filter_by_dso (browser->hists); - } -if (top == &browser->hists->thread_filter) { -perf_hpp__set_elide (HISTC_THREAD , false); -thread__zput (browser->hists->thread_filter); -hists__filter_by_thread (browser->hists); - } -ui_helpline__pop (); -hist_browser__reset (browser); + top =pstack__peek (fstack); +if (top == &browser->hists->dso_filter) +do_zoom_dso (opts, browser, hbt, fstack, env); +if (top == &browser->hists->thread_filter) +do_zoom_thread (opts, browser, hbt, fstack, env);continue ; }case K_ESC :
tools/perf/util/pstack.c +7 -0
@@ -74,3 +74,10 @@ void *pstack__pop(struct pstack *pstack) pstack->entries[pstack->top] =NULL ;return ret; } + +void *pstack__peek (struct pstack *pstack) +{ +if (pstack->top ==0 ) +return NULL ; +return pstack->entries[pstack->top -1 ]; +}
tools/perf/util/pstack.h +1 -0
@@ -10,5 +10,6 @@ bool pstack__empty(const struct pstack *pstack);void pstack__remove (struct pstack *pstack,void *key);void pstack__push (struct pstack *pstack,void *key);void *pstack__pop (struct pstack *pstack); +void *pstack__peek (struct pstack *pstack);#endif /* _PERF_PSTACK_ */
Signature · 1 line
2.3.5