Skip to content

Commit 6f3da20

Browse files
Andi Kleenacmel
authored andcommitted
perf report: Support builtin perf script in scripts menu
The scripts menu traditionally only showed custom perf scripts. Allow to run standard perf script with useful default options too. - Normal perf script - perf script with assembler (needs xed installed) - perf script with source code output (needs debuginfo) - perf script with custom arguments Then we automatically select the right options to display the information in the perf.data file. For example with -b display branch contexts. It's not easily possible to check for xed's existence in advance. perf script usually gives sensible error messages when it's not available. Signed-off-by: Andi Kleen <ak@linux.intel.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: http://lkml.kernel.org/r/20190311144502.15423-7-andi@firstfloor.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 1d6c49d commit 6f3da20

4 files changed

Lines changed: 120 additions & 40 deletions

File tree

‎tools/perf/ui/browsers/annotate.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ static int annotate_browser__run(struct annotate_browser *browser,
750750
continue;
751751
case 'r':
752752
{
753-
script_browse(NULL);
753+
script_browse(NULL, NULL);
754754
continue;
755755
}
756756
case 'k':

‎tools/perf/ui/browsers/hists.c‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,7 @@ struct popup_action {
23442344
struct thread *thread;
23452345
struct map_symbol ms;
23462346
int socket;
2347+
struct perf_evsel *evsel;
23472348

23482349
int (*fn)(struct hist_browser *browser, struct popup_action *act);
23492350
};
@@ -2566,7 +2567,7 @@ do_run_script(struct hist_browser *browser __maybe_unused,
25662567
n += snprintf(script_opt + n, len - n, " --time %s,%s", start, end);
25672568
}
25682569

2569-
script_browse(script_opt);
2570+
script_browse(script_opt, act->evsel);
25702571
free(script_opt);
25712572
return 0;
25722573
}
@@ -2575,7 +2576,7 @@ static int
25752576
add_script_opt_2(struct hist_browser *browser __maybe_unused,
25762577
struct popup_action *act, char **optstr,
25772578
struct thread *thread, struct symbol *sym,
2578-
const char *tstr)
2579+
struct perf_evsel *evsel, const char *tstr)
25792580
{
25802581

25812582
if (thread) {
@@ -2593,19 +2594,21 @@ add_script_opt_2(struct hist_browser *browser __maybe_unused,
25932594

25942595
act->thread = thread;
25952596
act->ms.sym = sym;
2597+
act->evsel = evsel;
25962598
act->fn = do_run_script;
25972599
return 1;
25982600
}
25992601

26002602
static int
26012603
add_script_opt(struct hist_browser *browser,
26022604
struct popup_action *act, char **optstr,
2603-
struct thread *thread, struct symbol *sym)
2605+
struct thread *thread, struct symbol *sym,
2606+
struct perf_evsel *evsel)
26042607
{
26052608
int n, j;
26062609
struct hist_entry *he;
26072610

2608-
n = add_script_opt_2(browser, act, optstr, thread, sym, "");
2611+
n = add_script_opt_2(browser, act, optstr, thread, sym, evsel, "");
26092612

26102613
he = hist_browser__selected_entry(browser);
26112614
if (sort_order && strstr(sort_order, "time")) {
@@ -2618,10 +2621,9 @@ add_script_opt(struct hist_browser *browser,
26182621
sizeof tstr - j);
26192622
j += sprintf(tstr + j, "-");
26202623
timestamp__scnprintf_usec(he->time + symbol_conf.time_quantum,
2621-
tstr + j,
2622-
sizeof tstr - j);
2624+
tstr + j, sizeof tstr - j);
26232625
n += add_script_opt_2(browser, act, optstr, thread, sym,
2624-
tstr);
2626+
evsel, tstr);
26252627
act->time = he->time;
26262628
}
26272629
return n;
@@ -3092,7 +3094,7 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
30923094
nr_options += add_script_opt(browser,
30933095
&actions[nr_options],
30943096
&options[nr_options],
3095-
thread, NULL);
3097+
thread, NULL, evsel);
30963098
}
30973099
/*
30983100
* Note that browser->selection != NULL
@@ -3107,11 +3109,12 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
31073109
nr_options += add_script_opt(browser,
31083110
&actions[nr_options],
31093111
&options[nr_options],
3110-
NULL, browser->selection->sym);
3112+
NULL, browser->selection->sym,
3113+
evsel);
31113114
}
31123115
}
31133116
nr_options += add_script_opt(browser, &actions[nr_options],
3114-
&options[nr_options], NULL, NULL);
3117+
&options[nr_options], NULL, NULL, evsel);
31153118
nr_options += add_switch_opt(browser, &actions[nr_options],
31163119
&options[nr_options]);
31173120
skip_scripting:

‎tools/perf/ui/browsers/scripts.c‎

Lines changed: 100 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,111 @@
1717
*/
1818
#define SCRIPT_FULLPATH_LEN 256
1919

20+
struct script_config {
21+
const char **names;
22+
char **paths;
23+
int index;
24+
const char *perf;
25+
char extra_format[256];
26+
};
27+
28+
void attr_to_script(char *extra_format, struct perf_event_attr *attr)
29+
{
30+
extra_format[0] = 0;
31+
if (attr->read_format & PERF_FORMAT_GROUP)
32+
strcat(extra_format, " -F +metric");
33+
if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK)
34+
strcat(extra_format, " -F +brstackinsn --xed");
35+
if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
36+
strcat(extra_format, " -F +iregs");
37+
if (attr->sample_type & PERF_SAMPLE_REGS_USER)
38+
strcat(extra_format, " -F +uregs");
39+
if (attr->sample_type & PERF_SAMPLE_PHYS_ADDR)
40+
strcat(extra_format, " -F +phys_addr");
41+
}
42+
43+
static int add_script_option(const char *name, const char *opt,
44+
struct script_config *c)
45+
{
46+
c->names[c->index] = name;
47+
if (asprintf(&c->paths[c->index],
48+
"%s script %s -F +metric %s %s",
49+
c->perf, opt, symbol_conf.inline_name ? " --inline" : "",
50+
c->extra_format) < 0)
51+
return -1;
52+
c->index++;
53+
return 0;
54+
}
55+
2056
/*
2157
* When success, will copy the full path of the selected script
2258
* into the buffer pointed by script_name, and return 0.
2359
* Return -1 on failure.
2460
*/
25-
static int list_scripts(char *script_name)
61+
static int list_scripts(char *script_name, bool *custom,
62+
struct perf_evsel *evsel)
2663
{
27-
char *buf, *names[SCRIPT_MAX_NO], *paths[SCRIPT_MAX_NO];
28-
int i, num, choice, ret = -1;
64+
char *buf, *paths[SCRIPT_MAX_NO], *names[SCRIPT_MAX_NO];
65+
int i, num, choice;
66+
int ret = 0;
67+
int max_std, custom_perf;
68+
char pbuf[256];
69+
const char *perf = perf_exe(pbuf, sizeof pbuf);
70+
struct script_config scriptc = {
71+
.names = (const char **)names,
72+
.paths = paths,
73+
.perf = perf
74+
};
75+
76+
script_name[0] = 0;
2977

3078
/* Preset the script name to SCRIPT_NAMELEN */
3179
buf = malloc(SCRIPT_MAX_NO * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN));
3280
if (!buf)
33-
return ret;
81+
return -1;
82+
83+
if (evsel)
84+
attr_to_script(scriptc.extra_format, &evsel->attr);
85+
add_script_option("Show individual samples", "", &scriptc);
86+
add_script_option("Show individual samples with assembler", "-F +insn --xed",
87+
&scriptc);
88+
add_script_option("Show individual samples with source", "-F +srcline,+srccode",
89+
&scriptc);
90+
custom_perf = scriptc.index;
91+
add_script_option("Show samples with custom perf script arguments", "", &scriptc);
92+
i = scriptc.index;
93+
max_std = i;
3494

35-
for (i = 0; i < SCRIPT_MAX_NO; i++) {
36-
names[i] = buf + i * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN);
95+
for (; i < SCRIPT_MAX_NO; i++) {
96+
names[i] = buf + (i - max_std) * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN);
3797
paths[i] = names[i] + SCRIPT_NAMELEN;
3898
}
3999

40-
num = find_scripts(names, paths);
41-
if (num > 0) {
42-
choice = ui__popup_menu(num, names);
43-
if (choice < num && choice >= 0) {
44-
strcpy(script_name, paths[choice]);
45-
ret = 0;
46-
}
100+
num = find_scripts(names + max_std, paths + max_std);
101+
if (num < 0)
102+
num = 0;
103+
choice = ui__popup_menu(num + max_std, (char * const *)names);
104+
if (choice < 0) {
105+
ret = -1;
106+
goto out;
47107
}
108+
if (choice == custom_perf) {
109+
char script_args[50];
110+
int key = ui_browser__input_window("perf script command",
111+
"Enter perf script command line (without perf script prefix)",
112+
script_args, "", 0);
113+
if (key != K_ENTER)
114+
return -1;
115+
sprintf(script_name, "%s script %s", perf, script_args);
116+
} else if (choice < num + max_std) {
117+
strcpy(script_name, paths[choice]);
118+
}
119+
*custom = choice >= max_std;
48120

121+
out:
49122
free(buf);
123+
for (i = 0; i < max_std; i++)
124+
free(paths[i]);
50125
return ret;
51126
}
52127

@@ -66,27 +141,25 @@ static void run_script(char *cmd)
66141
SLsmg_refresh();
67142
}
68143

69-
int script_browse(const char *script_opt)
144+
int script_browse(const char *script_opt, struct perf_evsel *evsel)
70145
{
71-
char cmd[SCRIPT_FULLPATH_LEN*2], script_name[SCRIPT_FULLPATH_LEN];
146+
char *cmd, script_name[SCRIPT_FULLPATH_LEN];
147+
bool custom = false;
72148

73149
memset(script_name, 0, SCRIPT_FULLPATH_LEN);
74-
if (list_scripts(script_name))
150+
if (list_scripts(script_name, &custom, evsel))
75151
return -1;
76152

77-
sprintf(cmd, "perf script -s %s ", script_name);
78-
79-
if (script_opt)
80-
strcat(cmd, script_opt);
81-
82-
if (input_name) {
83-
strcat(cmd, " -i ");
84-
strcat(cmd, input_name);
85-
}
86-
87-
strcat(cmd, " 2>&1 | less");
153+
if (asprintf(&cmd, "%s%s %s %s%s 2>&1 | less",
154+
custom ? "perf script -s " : "",
155+
script_name,
156+
script_opt ? script_opt : "",
157+
input_name ? "-i " : "",
158+
input_name ? input_name : "") < 0)
159+
return -1;
88160

89161
run_script(cmd);
162+
free(cmd);
90163

91164
return 0;
92165
}

‎tools/perf/util/hist.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ struct annotation_options;
436436

437437
#ifdef HAVE_SLANG_SUPPORT
438438
#include "../ui/keysyms.h"
439+
void attr_to_script(char *buf, struct perf_event_attr *attr);
440+
439441
int map_symbol__tui_annotate(struct map_symbol *ms, struct perf_evsel *evsel,
440442
struct hist_browser_timer *hbt,
441443
struct annotation_options *annotation_opts);
@@ -450,7 +452,8 @@ int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
450452
struct perf_env *env,
451453
bool warn_lost_event,
452454
struct annotation_options *annotation_options);
453-
int script_browse(const char *script_opt);
455+
456+
int script_browse(const char *script_opt, struct perf_evsel *evsel);
454457
#else
455458
static inline
456459
int perf_evlist__tui_browse_hists(struct perf_evlist *evlist __maybe_unused,
@@ -479,7 +482,8 @@ static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
479482
return 0;
480483
}
481484

482-
static inline int script_browse(const char *script_opt __maybe_unused)
485+
static inline int script_browse(const char *script_opt __maybe_unused,
486+
struct perf_evsel *evsel __maybe_unused)
483487
{
484488
return 0;
485489
}

0 commit comments

Comments
 (0)