perf TUI: Move "Yes" button to before "No"
[safe/jmp/linux-2.6] / tools / perf / util / newt.c
index a2ba3ff..7a123a9 100644 (file)
@@ -94,7 +94,7 @@ static newtComponent newt_form__new(void)
        return self;
 }
 
-static int popup_menu(int argc, const char *argv[])
+static int popup_menu(int argc, char * const argv[])
 {
        struct newtExitStruct es;
        int i, rc = -1, max_len = 5;
@@ -132,7 +132,7 @@ static bool dialog_yesno(const char *msg)
 {
        /* newtWinChoice should really be accepting const char pointers... */
        char yes[] = "Yes", no[] = "No";
-       return newtWinChoice(NULL, no, yes, (char *)msg) == 2;
+       return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
 }
 
 /*
@@ -317,7 +317,8 @@ static size_t hist_entry__append_browser(struct hist_entry *self,
        return ret;
 }
 
-static void map_symbol__annotate_browser(const struct map_symbol *self)
+static void map_symbol__annotate_browser(const struct map_symbol *self,
+                                        const char *input_name)
 {
        FILE *fp;
        int cols, rows;
@@ -331,8 +332,8 @@ static void map_symbol__annotate_browser(const struct map_symbol *self)
        if (self->sym == NULL)
                return;
 
-       if (asprintf(&str, "perf annotate -d \"%s\" %s 2>&1 | expand",
-                    self->map->dso->name, self->sym->name) < 0)
+       if (asprintf(&str, "perf annotate -i \"%s\" -d \"%s\" %s 2>&1 | expand",
+                    input_name, self->map->dso->name, self->sym->name) < 0)
                return;
 
        fp = popen(str, "r");
@@ -381,53 +382,84 @@ static const void *newt__symbol_tree_get_current(newtComponent self)
        return newtListboxGetCurrent(self);
 }
 
-static void perf_session__selection(newtComponent self, void *data)
+static void hist_browser__selection(newtComponent self, void *data)
 {
        const struct map_symbol **symbol_ptr = data;
        *symbol_ptr = newt__symbol_tree_get_current(self);
 }
 
-int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists,
-                              u64 session_total, const char *helpline)
+struct hist_browser {
+       newtComponent           form, tree;
+       const struct map_symbol *selection;
+};
+
+static struct hist_browser *hist_browser__new(void)
+{
+       struct hist_browser *self = malloc(sizeof(*self));
+
+       if (self != NULL)
+               self->form = NULL;
+
+       return self;
+}
+
+static void hist_browser__delete(struct hist_browser *self)
+{
+       newtFormDestroy(self->form);
+       newtPopWindow();
+       free(self);
+}
+
+static int hist_browser__populate(struct hist_browser *self, struct rb_root *hists,
+                                 u64 nr_hists, u64 session_total, const char *title)
 {
+       int max_len = 0, idx, cols, rows;
+       struct ui_progress *progress;
        struct rb_node *nd;
-       char seq[] = ".";
-       int rows, cols, idx;
-       int max_len = 0;
-       char str[1024];
-       newtComponent form, tree;
-       struct newtExitStruct es;
-       const struct map_symbol *selection;
        u64 curr_hist = 0;
-       struct ui_progress *progress;
+       char seq[] = ".";
+       char str[256];
 
-       progress = ui_progress__new("Adding entries to the browser...", nr_hists);
-       if (progress == NULL)
-               return -1;
+       if (self->form) {
+               newtFormDestroy(self->form);
+               newtPopWindow();
+       }
 
-       snprintf(str, sizeof(str), "Samples: %Ld", session_total);
+       snprintf(str, sizeof(str), "Samples: %Ld                            ",
+                session_total);
        newtDrawRootText(0, 0, str);
-       newtPushHelpLine(helpline);
 
-       newtGetScreenSize(&cols, &rows);
+       newtGetScreenSize(NULL, &rows);
 
        if (symbol_conf.use_callchain)
-               tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
-                                               NEWT_FLAG_SCROLL);
+               self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
+                                                  NEWT_FLAG_SCROLL);
        else
-               tree = newtListbox(0, 0, rows - 5, (NEWT_FLAG_SCROLL |
-                                                      NEWT_FLAG_RETURNEXIT));
+               self->tree = newtListbox(0, 0, rows - 5,
+                                       (NEWT_FLAG_SCROLL |
+                                        NEWT_FLAG_RETURNEXIT));
 
-       newtComponentAddCallback(tree, perf_session__selection, &selection);
+       newtComponentAddCallback(self->tree, hist_browser__selection,
+                                &self->selection);
+
+       progress = ui_progress__new("Adding entries to the browser...", nr_hists);
+       if (progress == NULL)
+               return -1;
 
        idx = 0;
        for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
                struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
-               int len = hist_entry__append_browser(h, tree, session_total);
+               int len;
+
+               if (h->filtered)
+                       continue;
+
+               len = hist_entry__append_browser(h, self->tree, session_total);
                if (len > max_len)
                        max_len = len;
                if (symbol_conf.use_callchain)
-                       hist_entry__append_callchain_browser(h, tree, session_total, idx++);
+                       hist_entry__append_callchain_browser(h, self->tree,
+                                                            session_total, idx++);
                ++curr_hist;
                if (curr_hist % 5)
                        ui_progress__update(progress, curr_hist);
@@ -435,27 +467,145 @@ int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists,
 
        ui_progress__delete(progress);
 
+       newtGetScreenSize(&cols, &rows);
+
        if (max_len > cols)
                max_len = cols - 3;
 
        if (!symbol_conf.use_callchain)
-               newtListboxSetWidth(tree, max_len);
+               newtListboxSetWidth(self->tree, max_len);
 
        newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
-                          rows - 5, "Report");
-       form = newt_form__new();
-       newtFormAddHotKey(form, 'A');
-       newtFormAddHotKey(form, 'a');
-       newtFormAddHotKey(form, NEWT_KEY_RIGHT);
-       newtFormAddComponents(form, tree, NULL);
-       selection = newt__symbol_tree_get_current(tree);
+                          rows - 5, title);
+       self->form = newt_form__new();
+       if (self->form == NULL)
+               return -1;
+
+       newtFormAddHotKey(self->form, 'A');
+       newtFormAddHotKey(self->form, 'a');
+       newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
+       newtFormAddComponents(self->form, self->tree, NULL);
+       self->selection = newt__symbol_tree_get_current(self->tree);
+
+       return 0;
+}
+
+enum hist_filter {
+       HIST_FILTER__DSO,
+       HIST_FILTER__THREAD,
+};
+
+static u64 hists__filter_by_dso(struct rb_root *hists, const struct dso *dso,
+                               u64 *session_total)
+{
+       struct rb_node *nd;
+       u64 nr_hists = 0;
+
+       *session_total = 0;
+
+       for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
+               struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
+
+               if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
+                       h->filtered |= (1 << HIST_FILTER__DSO);
+                       continue;
+               }
+               h->filtered &= ~(1 << HIST_FILTER__DSO);
+               ++nr_hists;
+               *session_total += h->count;
+       }
+
+       return nr_hists;
+}
+
+static u64 hists__filter_by_thread(struct rb_root *hists, const struct thread *thread,
+                                  u64 *session_total)
+{
+       struct rb_node *nd;
+       u64 nr_hists = 0;
+
+       *session_total = 0;
+
+       for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
+               struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
+
+               if (thread != NULL && h->thread != thread) {
+                       h->filtered |= (1 << HIST_FILTER__THREAD);
+                       continue;
+               }
+               h->filtered &= ~(1 << HIST_FILTER__THREAD);
+               ++nr_hists;
+               *session_total += h->count;
+       }
+
+       return nr_hists;
+}
+
+static struct thread *hist_browser__selected_thread(struct hist_browser *self)
+{
+       int *indexes;
+
+       if (!symbol_conf.use_callchain)
+               goto out;
+
+       indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
+       if (indexes) {
+               bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
+               free(indexes);
+               if (is_hist_entry)
+                       goto out;
+       }
+       return NULL;
+out:
+       return *(struct thread **)(self->selection + 1);
+}
+
+static int hist_browser__title(char *bf, size_t size, const char *input_name,
+                              const struct dso *dso, const struct thread *thread)
+{
+       int printed = 0;
+
+       if (thread)
+               printed += snprintf(bf + printed, size - printed,
+                                   "Thread: %s(%d)",
+                                   (thread->comm_set ?  thread->comm : ""),
+                                   thread->pid);
+       if (dso)
+               printed += snprintf(bf + printed, size - printed,
+                                   "%sDSO: %s", thread ? " " : "",
+                                   dso->short_name);
+       return printed ?: snprintf(bf, size, "Report: %s", input_name);
+}
+
+int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists,
+                              u64 session_total, const char *helpline,
+                              const char *input_name)
+{
+       struct hist_browser *browser = hist_browser__new();
+       const struct thread *thread_filter = NULL;
+       const struct dso *dso_filter = NULL;
+       struct newtExitStruct es;
+       char msg[160];
+       int err = -1;
+
+       if (browser == NULL)
+               return -1;
+
+       newtPushHelpLine(helpline);
+
+       hist_browser__title(msg, sizeof(msg), input_name,
+                           dso_filter, thread_filter);
+       if (hist_browser__populate(browser, hists, nr_hists, session_total, msg) < 0)
+               goto out;
 
        while (1) {
-               char annotate[512];
-               const char *options[2];
-               int nr_options = 0, choice = 0;
+               const struct thread *thread;
+               const struct dso *dso;
+               char *options[16];
+               int nr_options = 0, choice = 0, i,
+                   annotate = -2, zoom_dso = -2, zoom_thread = -2;
 
-               newtFormRun(form, &es);
+               newtFormRun(browser->form, &es);
                if (es.reason == NEWT_EXIT_HOTKEY) {
                        if (toupper(es.u.key) == 'A')
                                goto do_annotate;
@@ -469,32 +619,87 @@ int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists,
                        }
                }
 
-               if (selection->sym != NULL) {
-                       snprintf(annotate, sizeof(annotate),
-                                "Annotate %s", selection->sym->name);
-                       options[nr_options++] = annotate;
-               }
+               if (browser->selection->sym != NULL &&
+                   asprintf(&options[nr_options], "Annotate %s",
+                            browser->selection->sym->name) > 0)
+                       annotate = nr_options++;
+
+               thread = hist_browser__selected_thread(browser);
+               if (thread != NULL &&
+                   asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
+                            (thread_filter ? "out of" : "into"),
+                            (thread->comm_set ? thread->comm : ""),
+                            thread->pid) > 0)
+                       zoom_thread = nr_options++;
+
+               dso = browser->selection->map ? browser->selection->map->dso : NULL;
+               if (dso != NULL &&
+                   asprintf(&options[nr_options], "Zoom %s %s DSO",
+                            (dso_filter ? "out of" : "into"),
+                            (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
+                       zoom_dso = nr_options++;
+
+               options[nr_options++] = (char *)"Exit";
 
-               options[nr_options++] = "Exit";
                choice = popup_menu(nr_options, options);
+
+               for (i = 0; i < nr_options - 1; ++i)
+                       free(options[i]);
+
                if (choice == nr_options - 1)
                        break;
+
+               if (choice == -1)
+                       continue;
 do_annotate:
-               if (selection->sym != NULL && choice >= 0) {
-                       if (selection->map->dso->origin == DSO__ORIG_KERNEL) {
+               if (choice == annotate) {
+                       if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
                                newtPopHelpLine();
                                newtPushHelpLine("No vmlinux file found, can't "
                                                 "annotate with just a "
                                                 "kallsyms file");
                                continue;
                        }
-                       map_symbol__annotate_browser(selection);
+                       map_symbol__annotate_browser(browser->selection, input_name);
+               } else if (choice == zoom_dso) {
+                       if (dso_filter) {
+                               newtPopHelpLine();
+                               dso_filter = NULL;
+                       } else {
+                               snprintf(msg, sizeof(msg),
+                                        "To zoom out press -> + \"Zoom out of %s DSO\"",
+                                        dso->kernel ? "the Kernel" : dso->short_name);
+                               newtPushHelpLine(msg);
+                               dso_filter = dso;
+                       }
+                       nr_hists = hists__filter_by_dso(hists, dso_filter, &session_total);
+                       hist_browser__title(msg, sizeof(msg), input_name,
+                                           dso_filter, thread_filter);
+                       if (hist_browser__populate(browser, hists, nr_hists, session_total, msg) < 0)
+                               goto out;
+               } else if (choice == zoom_thread) {
+                       if (thread_filter) {
+                               newtPopHelpLine();
+                               thread_filter = NULL;
+                       } else {
+                               snprintf(msg, sizeof(msg),
+                                        "To zoom out press -> + \"Zoom out of %s(%d) thread\"",
+                                        (thread->comm_set ? thread->comm : ""),
+                                        thread->pid);
+                               newtPushHelpLine(msg);
+                               thread_filter = thread;
+                       }
+                       nr_hists = hists__filter_by_thread(hists, thread_filter, &session_total);
+                       hist_browser__title(msg, sizeof(msg), input_name,
+                                           dso_filter, thread_filter);
+                       if (hist_browser__populate(browser, hists, nr_hists, session_total, msg) < 0)
+                               goto out;
                }
        }
-
-       newtFormDestroy(form);
-       newtPopWindow();
-       return 0;
+       err = 0;
+out:
+       hist_browser__delete(browser);
+       return err;
 }
 
 void setup_browser(void)