Skip to content

Commit c0443df

Browse files
committed
perf top: Introduce slang based TUI
Disabled by default as there are features found in the stdio based one that aren't implemented, like live annotation, filtering knobs data entry. Annotation hopefully will get somehow merged with the 'perf annotate' code. To use it: perf top --tui Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Cc: Tom Zanussi <tzanussi@gmail.com> LKML-Reference: <new-submission> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 229ade9 commit c0443df

5 files changed

Lines changed: 189 additions & 8 deletions

File tree

‎tools/perf/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ else
621621
LIB_OBJS += $(OUTPUT)util/ui/browsers/annotate.o
622622
LIB_OBJS += $(OUTPUT)util/ui/browsers/hists.o
623623
LIB_OBJS += $(OUTPUT)util/ui/browsers/map.o
624+
LIB_OBJS += $(OUTPUT)util/ui/browsers/top.o
624625
LIB_OBJS += $(OUTPUT)util/ui/helpline.o
625626
LIB_OBJS += $(OUTPUT)util/ui/progress.o
626627
LIB_OBJS += $(OUTPUT)util/ui/util.o
@@ -1050,6 +1051,9 @@ $(OUTPUT)util/ui/browser.o: util/ui/browser.c $(OUTPUT)PERF-CFLAGS
10501051
$(OUTPUT)util/ui/browsers/annotate.o: util/ui/browsers/annotate.c $(OUTPUT)PERF-CFLAGS
10511052
$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DENABLE_SLFUTURE_CONST $<
10521053

1054+
$(OUTPUT)util/ui/browsers/top.o: util/ui/browsers/top.c $(OUTPUT)PERF-CFLAGS
1055+
$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DENABLE_SLFUTURE_CONST $<
1056+
10531057
$(OUTPUT)util/ui/browsers/hists.o: util/ui/browsers/hists.c $(OUTPUT)PERF-CFLAGS
10541058
$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DENABLE_SLFUTURE_CONST $<
10551059

‎tools/perf/builtin-top.c‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "perf.h"
2222

23+
#include "util/cache.h"
2324
#include "util/color.h"
2425
#include "util/evlist.h"
2526
#include "util/evsel.h"
@@ -75,6 +76,8 @@ static struct perf_top top = {
7576

7677
static bool system_wide = false;
7778

79+
static bool use_tui, use_stdio;
80+
7881
static int default_interval = 0;
7982

8083
static bool inherit = false;
@@ -96,11 +99,6 @@ static int sym_pcnt_filter = 5;
9699
* Source functions
97100
*/
98101

99-
static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
100-
{
101-
return ((void *)self) + symbol_conf.priv_size;
102-
}
103-
104102
void get_term_dimensions(struct winsize *ws)
105103
{
106104
char *s = getenv("LINES");
@@ -695,6 +693,14 @@ static void handle_keypress(struct perf_session *session, int c)
695693
}
696694
}
697695

696+
static void *display_thread_tui(void *arg __used)
697+
{
698+
perf_top__tui_browser(&top);
699+
exit_browser(0);
700+
exit(0);
701+
return NULL;
702+
}
703+
698704
static void *display_thread(void *arg __used)
699705
{
700706
struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
@@ -1005,7 +1011,8 @@ static int __cmd_top(void)
10051011

10061012
perf_session__mmap_read(session);
10071013

1008-
if (pthread_create(&thread, NULL, display_thread, session)) {
1014+
if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
1015+
display_thread), session)) {
10091016
printf("Could not create display thread.\n");
10101017
exit(-1);
10111018
}
@@ -1078,6 +1085,8 @@ static const struct option options[] = {
10781085
"display this many functions"),
10791086
OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
10801087
"hide user symbols"),
1088+
OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
1089+
OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
10811090
OPT_INCR('v', "verbose", &verbose,
10821091
"be more verbose (show counter open errors, etc)"),
10831092
OPT_END()
@@ -1098,6 +1107,20 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
10981107
if (argc)
10991108
usage_with_options(top_usage, options);
11001109

1110+
/*
1111+
* XXX For now start disabled, only using TUI if explicitely asked for.
1112+
* Change that when handle_keys equivalent gets written, live annotation
1113+
* done, etc.
1114+
*/
1115+
use_browser = 0;
1116+
1117+
if (use_stdio)
1118+
use_browser = 0;
1119+
else if (use_tui)
1120+
use_browser = 1;
1121+
1122+
setup_browser(false);
1123+
11011124
/* CPU and PID are mutually exclusive */
11021125
if (top.target_tid > 0 && top.cpu_list) {
11031126
printf("WARNING: PID switch overriding CPU\n");

‎tools/perf/util/top.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ float perf_top__decay_samples(struct perf_top *top, struct rb_root *root)
158158
syme = list_entry(top->active_symbols.next, struct sym_entry, node);
159159
pthread_mutex_unlock(&top->active_symbols_lock);
160160

161+
top->rb_entries = 0;
161162
list_for_each_entry_safe_from(syme, n, &top->active_symbols, node) {
162163
syme->snap_count = syme->count[snap];
163164
if (syme->snap_count != 0) {
@@ -170,7 +171,11 @@ float perf_top__decay_samples(struct perf_top *top, struct rb_root *root)
170171
continue;
171172
}
172173
syme->weight = sym_weight(syme, top);
173-
rb_insert_active_sym(root, syme);
174+
175+
if ((int)syme->snap_count >= top->count_filter) {
176+
rb_insert_active_sym(root, syme);
177+
++top->rb_entries;
178+
}
174179
sum_ksamples += syme->snap_count;
175180

176181
for (j = 0; j < top->evlist->nr_entries; j++)

‎tools/perf/util/top.h‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ struct sym_entry {
3838
unsigned long count[0];
3939
};
4040

41+
static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
42+
{
43+
return ((void *)self) + symbol_conf.priv_size;
44+
}
45+
4146
struct perf_top {
4247
struct perf_evlist *evlist;
4348
/*
@@ -51,7 +56,7 @@ struct perf_top {
5156
u64 exact_samples;
5257
u64 guest_us_samples, guest_kernel_samples;
5358
int print_entries, count_filter, delay_secs;
54-
int display_weighted, freq;
59+
int display_weighted, freq, rb_entries;
5560
int sym_counter, target_pid, target_tid;
5661
bool hide_kernel_symbols, hide_user_symbols, zero;
5762
const char *cpu_list;
@@ -64,4 +69,12 @@ float perf_top__decay_samples(struct perf_top *top, struct rb_root *root);
6469
void perf_top__find_widths(struct perf_top *top, struct rb_root *root,
6570
int *dso_width, int *dso_short_width, int *sym_width);
6671

72+
#ifdef NO_NEWT_SUPPORT
73+
static inline int perf_top__tui_browser(struct perf_top *top __used)
74+
{
75+
return 0;
76+
}
77+
#else
78+
int perf_top__tui_browser(struct perf_top *top);
79+
#endif
6780
#endif /* __PERF_TOP_H */

‎tools/perf/util/ui/browsers/top.c‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3+
*
4+
* Parts came from builtin-{top,stat,record}.c, see those files for further
5+
* copyright notes.
6+
*
7+
* Released under the GPL v2. (and only v2, not any later version)
8+
*/
9+
#include "../browser.h"
10+
#include "../helpline.h"
11+
#include "../libslang.h"
12+
#include "../../evlist.h"
13+
#include "../../hist.h"
14+
#include "../../sort.h"
15+
#include "../../symbol.h"
16+
#include "../../top.h"
17+
18+
struct perf_top_browser {
19+
struct ui_browser b;
20+
struct rb_root root;
21+
float sum_ksamples;
22+
int dso_width;
23+
int dso_short_width;
24+
int sym_width;
25+
};
26+
27+
static void perf_top_browser__write(struct ui_browser *browser, void *entry, int row)
28+
{
29+
struct perf_top_browser *top_browser = container_of(browser, struct perf_top_browser, b);
30+
struct sym_entry *syme = rb_entry(entry, struct sym_entry, rb_node);
31+
bool current_entry = ui_browser__is_current_entry(browser, row);
32+
struct symbol *symbol = sym_entry__symbol(syme);
33+
struct perf_top *top = browser->priv;
34+
int width = browser->width;
35+
double pcnt;
36+
37+
pcnt = 100.0 - (100.0 * ((top_browser->sum_ksamples - syme->snap_count) /
38+
top_browser->sum_ksamples));
39+
ui_browser__set_percent_color(browser, pcnt, current_entry);
40+
41+
if (top->evlist->nr_entries == 1 || !top->display_weighted) {
42+
slsmg_printf("%20.2f ", syme->weight);
43+
width -= 24;
44+
} else {
45+
slsmg_printf("%9.1f %10ld ", syme->weight, syme->snap_count);
46+
width -= 23;
47+
}
48+
49+
slsmg_printf("%4.1f%%", pcnt);
50+
width -= 7;
51+
52+
if (verbose) {
53+
slsmg_printf(" %016" PRIx64, symbol->start);
54+
width -= 17;
55+
}
56+
57+
slsmg_printf(" %-*.*s ", top_browser->sym_width, top_browser->sym_width,
58+
symbol->name);
59+
width -= top_browser->sym_width;
60+
slsmg_write_nstring(width >= syme->map->dso->long_name_len ?
61+
syme->map->dso->long_name :
62+
syme->map->dso->short_name, width);
63+
}
64+
65+
static void perf_top_browser__update_rb_tree(struct perf_top_browser *browser)
66+
{
67+
struct perf_top *top = browser->b.priv;
68+
69+
browser->root = RB_ROOT;
70+
browser->b.top = NULL;
71+
browser->sum_ksamples = perf_top__decay_samples(top, &browser->root);
72+
perf_top__find_widths(top, &browser->root, &browser->dso_width,
73+
&browser->dso_short_width,
74+
&browser->sym_width);
75+
if (browser->sym_width + browser->dso_width > browser->b.width - 29) {
76+
browser->dso_width = browser->dso_short_width;
77+
if (browser->sym_width + browser->dso_width > browser->b.width - 29)
78+
browser->sym_width = browser->b.width - browser->dso_width - 29;
79+
}
80+
browser->b.nr_entries = top->rb_entries;
81+
}
82+
83+
static int perf_top_browser__run(struct perf_top_browser *browser)
84+
{
85+
int key;
86+
char title[160];
87+
struct perf_top *top = browser->b.priv;
88+
int delay_msecs = top->delay_secs * 1000;
89+
90+
perf_top_browser__update_rb_tree(browser);
91+
perf_top__header_snprintf(top, title, sizeof(title));
92+
perf_top__reset_sample_counters(top);
93+
94+
if (ui_browser__show(&browser->b, title, "ESC: exit") < 0)
95+
return -1;
96+
97+
newtFormSetTimer(browser->b.form, delay_msecs);
98+
99+
while (1) {
100+
key = ui_browser__run(&browser->b);
101+
102+
switch (key) {
103+
case -1:
104+
/* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
105+
perf_top_browser__update_rb_tree(browser);
106+
perf_top__header_snprintf(top, title, sizeof(title));
107+
perf_top__reset_sample_counters(top);
108+
ui_browser__set_color(&browser->b, NEWT_COLORSET_ROOT);
109+
SLsmg_gotorc(0, 0);
110+
slsmg_write_nstring(title, browser->b.width);
111+
break;
112+
case NEWT_KEY_TAB:
113+
default:
114+
goto out;
115+
}
116+
}
117+
out:
118+
ui_browser__hide(&browser->b);
119+
return key;
120+
}
121+
122+
int perf_top__tui_browser(struct perf_top *top)
123+
{
124+
struct perf_top_browser browser = {
125+
.b = {
126+
.entries = &browser.root,
127+
.refresh = ui_browser__rb_tree_refresh,
128+
.seek = ui_browser__rb_tree_seek,
129+
.write = perf_top_browser__write,
130+
.priv = top,
131+
},
132+
};
133+
134+
ui_helpline__push("Press <- or ESC to exit");
135+
return perf_top_browser__run(&browser);
136+
}

0 commit comments

Comments
 (0)