perf tools: Print bpf-output events in 'perf script'
cover perf tools: Print bpf-output events in 'perf script'
Hi Arnaldo and Jiri,
Glad to see the progress we made today.
Before you trying libbaleltrace, please consider these two patches,
which allows print BPF output data directly through 'perf script'. I think it would be useful for people don't have libbabeltrace ready. With these two patch they can write scripts by their own:
# ./perf script
usleep 4882 21384.532523: evt: ffffffff810e97d1 sys_nanosleep ([kernel.kallsyms])
BPF output: 0000: 52 61 69 73 65 20 61 20 Raise a
0008: 42 50 46 20 65 76 65 6e BPF even
0010: 74 21 00 00 t!..
BPF string: "Raise a BPF event!" usleep 4882 21384.632606: evt: ffffffff8105c609 kretprobe_trampoline_holder ([kernel.kallsyms
BPF output: 0000: 52 61 69 73 65 20 61 20 Raise a
0008: 42 50 46 20 65 76 65 6e BPF even
0010: 74 21 00 00 t!..
BPF string: "Raise a BPF event!"Thank you.
Wang Nan (2):
perf tools: Make binary data printer code in trace_event public
available
perf script: Print bpf-output events in 'perf script'
tools/perf/builtin-script.c | 93 ++++++++++++++++++++++++++++++++++++++++++--- tools/perf/util/debug.c | 75 +++++++++++++++++++++++------------- tools/perf/util/util.c | 37 ++++++++++++++++++ tools/perf/util/util.h | 20 ++++++++++ 4 files changed, 193 insertions(+), 32 deletions(-)
Signature · 1 line
1.8.3.4
RFC PATCH 1/2 perf tools: Make binary data printer code in trace_event public available
Move code printing binray data from trace_event() to utils.c and allows passing different printer. Further commits will use this logic to print bpf output event.
--- tools/perf/util/debug.c | 75 +++++++++++++++++++++++++++++++------------------ tools/perf/util/util.c | 37 ++++++++++++++++++++++++ tools/perf/util/util.h | 20 +++++++++++++ 3 files changed, 105 insertions(+), 27 deletions(-)
tools/perf/util/debug.c +48 -27
@@ -106,40 +106,61 @@ int dump_printf(const char *fmt, ...)return ret; } +static void trace_event_printer (enum binary_printer_ops op, +unsigned int val,void *extra) +{ +const char *color =PERF_COLOR_BLUE ; +union perf_event *event = (union perf_event *)extra; +unsigned char ch = (unsigned char )val; + +switch (op) { +case BINARY_PRINT_DATA_BEGIN : +printf ("." ); +color_fprintf (stdout, color,"\n. ... raw event: size %d bytes\n" , + event->header.size); +break ; +case BINARY_PRINT_LINE_BEGIN : +printf ("." ); +break ; +case BINARY_PRINT_ADDR : +color_fprintf (stdout, color," %04x: " , val); +break ; +case BINARY_PRINT_NUM_DATA : +color_fprintf (stdout, color," %02x" , val); +break ; +case BINARY_PRINT_NUM_PAD : +color_fprintf (stdout, color," " ); +break ; +case BINARY_PRINT_SEP : +color_fprintf (stdout, color," " ); +break ; +case BINARY_PRINT_CHAR_DATA : +color_fprintf (stdout, color,"%c" , +isprint (ch) ? ch :'.' ); +break ; +case BINARY_PRINT_CHAR_PAD : +color_fprintf (stdout, color," " ); +break ; +case BINARY_PRINT_LINE_END : +color_fprintf (stdout, color,"\n" ); +break ; +case BINARY_PRINT_DATA_END : +printf ("\n" ); +break ; +default : +break ; + } +} +void trace_event (union perf_event *event) {unsigned char *raw_event = (void *)event; -const char *color =PERF_COLOR_BLUE ; -int i, j;if (!dump_trace)return ; -printf ("." ); -color_fprintf (stdout, color,"\n. ... raw event: size %d bytes\n" , - event->header.size); - -for (i =0 ; i < event->header.size; i++) { -if ((i &15 ) ==0 ) { -printf ("." ); -color_fprintf (stdout, color," %04x: " , i); - } - -color_fprintf (stdout, color," %02x" , raw_event[i]); - -if (((i &15 ) ==15 ) || i == event->header.size-1 ) { -color_fprintf (stdout, color," " ); -for (j =0 ; j <15 -(i &15 ); j++) -color_fprintf (stdout, color," " ); -for (j = i & ~15 ; j <= i; j++) { -color_fprintf (stdout, color,"%c" , -isprint (raw_event[j]) ? - raw_event[j] :'.' ); - } -color_fprintf (stdout, color,"\n" ); - } - } -printf (".\n" ); +print_binary (raw_event, event->header.size,16 , + trace_event_printer, event); } static struct debug_variable {
tools/perf/util/util.c +37 -0
@@ -14,6 +14,7 @@#include <limits.h> #include <byteswap.h> #include <linux/kernel.h> +#include <linux/log2.h> #include <unistd.h> #include "callchain.h" #include "strlist.h"
@@ -670,3 +671,39 @@ int fetch_current_timestamp(char *buf, size_t sz)return 0 ; } + +void print_binary (unsigned char *data,size_t len, +size_t bytes_per_line,print_binary_t printer, +void *extra) +{ +size_t i, j, mask; + +if (!printer) +return ; + + bytes_per_line =roundup_pow_of_two (bytes_per_line); + mask = bytes_per_line -1 ; + +printer (BINARY_PRINT_DATA_BEGIN ,0 , extra); +for (i =0 ; i < len; i++) { +if ((i & mask) ==0 ) { +printer (BINARY_PRINT_LINE_BEGIN ,-1 , extra); +printer (BINARY_PRINT_ADDR , i, extra); + } + +printer (BINARY_PRINT_NUM_DATA , data[i], extra); + +if (((i & mask) == mask) || i == len -1 ) { +for (j =0 ; j < mask-(i & mask); j++) +printer (BINARY_PRINT_NUM_PAD ,-1 , extra); + +printer (BINARY_PRINT_SEP , i, extra); +for (j = i & ~mask; j <= i; j++) +printer (BINARY_PRINT_CHAR_DATA , data[j], extra); +for (j =0 ; j < mask-(i & mask); j++) +printer (BINARY_PRINT_CHAR_PAD , i, extra); +printer (BINARY_PRINT_LINE_END ,-1 , extra); + } + } +printer (BINARY_PRINT_DATA_END ,-1 , extra); +}
tools/perf/util/util.h +20 -0
@@ -345,4 +345,24 @@ const char *perf_tip(const char *dirpath);bool is_regular_file (const char *file);int fetch_current_timestamp (char *buf,size_t sz); +enum binary_printer_ops { +BINARY_PRINT_DATA_BEGIN , +BINARY_PRINT_LINE_BEGIN , +BINARY_PRINT_ADDR , +BINARY_PRINT_NUM_DATA , +BINARY_PRINT_NUM_PAD , +BINARY_PRINT_SEP , +BINARY_PRINT_CHAR_DATA , +BINARY_PRINT_CHAR_PAD , +BINARY_PRINT_LINE_END , +BINARY_PRINT_DATA_END , +}; + +typedef void (*print_binary_t )(enum binary_printer_ops , +unsigned int val, +void *extra); + +void print_binary (unsigned char *data,size_t len, +size_t bytes_per_line,print_binary_t printer, +void *extra);#endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.8.3.4
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Make binary data printer code in trace_event public available
perf tools: Make binary data printer code in trace_event public available
Commit-ID: c339b1a90e6cd638a1d99cbbf49d870ce233198e Gitweb: http://git.kernel.org/tip/c339b1a90e6cd638a1d99cbbf49d870ce233198e Author: Wang Nan <[email protected]> AuthorDate: Wed, 24 Feb 2016 11:20:44 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 24 Feb 2016 11:38:01 -0300
perf tools: Make binary data printer code in trace_event public available
Move code printing binray data from trace_event() to utils.c and allows passing different printer. Further commits will use this logic to print bpf output event.
--- tools/perf/util/debug.c | 75 +++++++++++++++++++++++++++++++------------------ tools/perf/util/util.c | 37 ++++++++++++++++++++++++ tools/perf/util/util.h | 20 +++++++++++++ 3 files changed, 105 insertions(+), 27 deletions(-)
tools/perf/util/debug.c +48 -27
@@ -106,40 +106,61 @@ int dump_printf(const char *fmt, ...)return ret; } +static void trace_event_printer (enum binary_printer_ops op, +unsigned int val,void *extra) +{ +const char *color =PERF_COLOR_BLUE ; +union perf_event *event = (union perf_event *)extra; +unsigned char ch = (unsigned char )val; + +switch (op) { +case BINARY_PRINT_DATA_BEGIN : +printf ("." ); +color_fprintf (stdout, color,"\n. ... raw event: size %d bytes\n" , + event->header.size); +break ; +case BINARY_PRINT_LINE_BEGIN : +printf ("." ); +break ; +case BINARY_PRINT_ADDR : +color_fprintf (stdout, color," %04x: " , val); +break ; +case BINARY_PRINT_NUM_DATA : +color_fprintf (stdout, color," %02x" , val); +break ; +case BINARY_PRINT_NUM_PAD : +color_fprintf (stdout, color," " ); +break ; +case BINARY_PRINT_SEP : +color_fprintf (stdout, color," " ); +break ; +case BINARY_PRINT_CHAR_DATA : +color_fprintf (stdout, color,"%c" , +isprint (ch) ? ch :'.' ); +break ; +case BINARY_PRINT_CHAR_PAD : +color_fprintf (stdout, color," " ); +break ; +case BINARY_PRINT_LINE_END : +color_fprintf (stdout, color,"\n" ); +break ; +case BINARY_PRINT_DATA_END : +printf ("\n" ); +break ; +default : +break ; + } +} +void trace_event (union perf_event *event) {unsigned char *raw_event = (void *)event; -const char *color =PERF_COLOR_BLUE ; -int i, j;if (!dump_trace)return ; -printf ("." ); -color_fprintf (stdout, color,"\n. ... raw event: size %d bytes\n" , - event->header.size); - -for (i =0 ; i < event->header.size; i++) { -if ((i &15 ) ==0 ) { -printf ("." ); -color_fprintf (stdout, color," %04x: " , i); - } - -color_fprintf (stdout, color," %02x" , raw_event[i]); - -if (((i &15 ) ==15 ) || i == event->header.size-1 ) { -color_fprintf (stdout, color," " ); -for (j =0 ; j <15 -(i &15 ); j++) -color_fprintf (stdout, color," " ); -for (j = i & ~15 ; j <= i; j++) { -color_fprintf (stdout, color,"%c" , -isprint (raw_event[j]) ? - raw_event[j] :'.' ); - } -color_fprintf (stdout, color,"\n" ); - } - } -printf (".\n" ); +print_binary (raw_event, event->header.size,16 , + trace_event_printer, event); } static struct debug_variable {
tools/perf/util/util.c +37 -0
@@ -14,6 +14,7 @@#include <limits.h> #include <byteswap.h> #include <linux/kernel.h> +#include <linux/log2.h> #include <unistd.h> #include "callchain.h" #include "strlist.h"
@@ -670,3 +671,39 @@ int fetch_current_timestamp(char *buf, size_t sz)return 0 ; } + +void print_binary (unsigned char *data,size_t len, +size_t bytes_per_line,print_binary_t printer, +void *extra) +{ +size_t i, j, mask; + +if (!printer) +return ; + + bytes_per_line =roundup_pow_of_two (bytes_per_line); + mask = bytes_per_line -1 ; + +printer (BINARY_PRINT_DATA_BEGIN ,0 , extra); +for (i =0 ; i < len; i++) { +if ((i & mask) ==0 ) { +printer (BINARY_PRINT_LINE_BEGIN ,-1 , extra); +printer (BINARY_PRINT_ADDR , i, extra); + } + +printer (BINARY_PRINT_NUM_DATA , data[i], extra); + +if (((i & mask) == mask) || i == len -1 ) { +for (j =0 ; j < mask-(i & mask); j++) +printer (BINARY_PRINT_NUM_PAD ,-1 , extra); + +printer (BINARY_PRINT_SEP , i, extra); +for (j = i & ~mask; j <= i; j++) +printer (BINARY_PRINT_CHAR_DATA , data[j], extra); +for (j =0 ; j < mask-(i & mask); j++) +printer (BINARY_PRINT_CHAR_PAD , i, extra); +printer (BINARY_PRINT_LINE_END ,-1 , extra); + } + } +printer (BINARY_PRINT_DATA_END ,-1 , extra); +}
tools/perf/util/util.h +20 -0
@@ -345,4 +345,24 @@ const char *perf_tip(const char *dirpath);bool is_regular_file (const char *file);int fetch_current_timestamp (char *buf,size_t sz); +enum binary_printer_ops { +BINARY_PRINT_DATA_BEGIN , +BINARY_PRINT_LINE_BEGIN , +BINARY_PRINT_ADDR , +BINARY_PRINT_NUM_DATA , +BINARY_PRINT_NUM_PAD , +BINARY_PRINT_SEP , +BINARY_PRINT_CHAR_DATA , +BINARY_PRINT_CHAR_PAD , +BINARY_PRINT_LINE_END , +BINARY_PRINT_DATA_END , +}; + +typedef void (*print_binary_t )(enum binary_printer_ops , +unsigned int val, +void *extra); + +void print_binary (unsigned char *data,size_t len, +size_t bytes_per_line,print_binary_t printer, +void *extra);#endif /* GIT_COMPAT_UTIL_H */
RFC PATCH 2/2 perf script: Print bpf-output events in 'perf script'
In reply to Wang NanThis patch allows 'perf script' output messages from BPF program. For example, use test_bpf_output_3.c at the end of this commit message,
# ./perf record -e bpf-output/no-inherit,name=evt/ \
-e ./test_bpf_output_3.c/map:channel.event=evt/ \
usleep 100000 # ./perf script
usleep 4882 21384.532523: evt: ffffffff810e97d1 sys_nanosleep ([kernel.kallsyms])
BPF output: 0000: 52 61 69 73 65 20 61 20 Raise a
0008: 42 50 46 20 65 76 65 6e BPF even
0010: 74 21 00 00 t!..
BPF string: "Raise a BPF event!" usleep 4882 21384.632606: evt: ffffffff8105c609 kretprobe_trampoline_holder ([kernel.kallsyms
BPF output: 0000: 52 61 69 73 65 20 61 20 Raise a
0008: 42 50 46 20 65 76 65 6e BPF even
0010: 74 21 00 00 t!..
BPF string: "Raise a BPF event!"Two samples from BPF output are printed by both binary and string format.
If BPF program output something unprintable, string format is suppressed.
/************************ BEGIN **************************/ #include <uapi/linux/bpf.h>
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
#define SEC(NAME) __attribute__((section(NAME), used))
static u64 (*ktime_get_ns)(void) =
(void *)BPF_FUNC_ktime_get_ns;
static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
(void *)BPF_FUNC_trace_printk;
static int (*get_smp_processor_id)(void) =
(void *)BPF_FUNC_get_smp_processor_id;
static int (*perf_event_output)(void *, struct bpf_map_def *, int, void *, unsigned long) =
(void *)BPF_FUNC_perf_event_output; struct bpf_map_def SEC("maps") channel = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
};
static inline int __attribute__((always_inline))
func(void *ctx, int type)
{
char output_str[] = "Raise a BPF event!";
perf_event_output(ctx, &channel, get_smp_processor_id(),
&output_str, sizeof(output_str));
return 0; }
SEC("func_begin=sys_nanosleep")
int func_begin(void *ctx) {return func(ctx, 1);} SEC("func_end=sys_nanosleep%return")
int func_end(void *ctx) { return func(ctx, 2);} char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE; /************************* END ***************************/
--- tools/perf/builtin-script.c | 93 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 5 deletions(-)
tools/perf/builtin-script.c +88 -5
@@ -60,6 +60,7 @@ enum perf_output_field {PERF_OUTPUT_BRSTACKSYM =1U <<16 ,PERF_OUTPUT_DATA_SRC =1U <<17 ,PERF_OUTPUT_WEIGHT =1U <<18 , + PERF_OUTPUT_BPF_OUTPUT = 1U << 19, }; struct output_option {
@@ -85,6 +86,7 @@ struct output_option { {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM}, {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC}, {.str = "weight", .field = PERF_OUTPUT_WEIGHT}, + {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT}, };/* default set to maintain compatibility with current format */
@@ -105,7 +107,7 @@ static struct {PERF_OUTPUT_SYM |PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD, - .invalid_fields = PERF_OUTPUT_TRACE, + .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, }, [PERF_TYPE_SOFTWARE] = {
@@ -115,7 +117,7 @@ static struct {PERF_OUTPUT_CPU |PERF_OUTPUT_TIME |PERF_OUTPUT_EVNAME |PERF_OUTPUT_IP |PERF_OUTPUT_SYM |PERF_OUTPUT_DSO | - PERF_OUTPUT_PERIOD, + PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT, .invalid_fields = PERF_OUTPUT_TRACE, },
@@ -125,7 +127,7 @@ static struct { .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |PERF_OUTPUT_CPU |PERF_OUTPUT_TIME | - PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE, +PERF_OUTPUT_EVNAME |PERF_OUTPUT_TRACE }, [PERF_TYPE_RAW] = {
@@ -138,7 +140,7 @@ static struct {PERF_OUTPUT_PERIOD |PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT, - .invalid_fields = PERF_OUTPUT_TRACE, + .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, }, [PERF_TYPE_BREAKPOINT] = {
@@ -150,7 +152,7 @@ static struct {PERF_OUTPUT_SYM |PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD, - .invalid_fields = PERF_OUTPUT_TRACE, + .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, }, };
@@ -623,6 +625,84 @@ static void print_sample_flags(u32 flags)printf (" %-4s " , str); } +struct printer_data { +int line_no; +bool hit_nul; +bool is_printable; +}; + +static void +print_sample_bpf_output_printer (enum binary_printer_ops op, +unsigned int val, +void *extra) +{ +unsigned char ch = (unsigned char )val; +struct printer_data *printer_data = extra; + +switch (op) { +case BINARY_PRINT_DATA_BEGIN : +printf ("\n" ); +break ; +case BINARY_PRINT_LINE_BEGIN : +printf ("%17s" , !printer_data->line_no ?"BPF output:" : +" " ); +break ; +case BINARY_PRINT_ADDR : +printf (" %04x:" , val); +break ; +case BINARY_PRINT_NUM_DATA : +printf (" %02x" , val); +break ; +case BINARY_PRINT_NUM_PAD : +printf (" " ); +break ; +case BINARY_PRINT_SEP : +printf (" " ); +break ; +case BINARY_PRINT_CHAR_DATA : +if (printer_data->hit_nul && ch) + printer_data->is_printable = false; + +if (!isprint (ch)) { +printf ("%c" ,'.' ); + +if (!printer_data->is_printable) +break ; + +if (ch =='\0' ) + printer_data->hit_nul = true; +else + printer_data->is_printable = false; + }else { +printf ("%c" , ch); + } +break ; +case BINARY_PRINT_CHAR_PAD : +printf (" " ); +break ; +case BINARY_PRINT_LINE_END : +printf ("\n" ); + printer_data->line_no++; +break ; +case BINARY_PRINT_DATA_END : +default : +break ; + } +} + +static void print_sample_bpf_output (struct perf_sample *sample) +{ +unsigned int nr_bytes = sample->raw_size; +struct printer_data printer_data = {0 , false, true}; + +print_binary (sample->raw_data, nr_bytes,8 , + print_sample_bpf_output_printer, &printer_data); + +if (printer_data.is_printable && printer_data.hit_nul) +printf ("%17s \"%s\"\n" ,"BPF string:" , + (char *)(sample->raw_data)); +} +struct perf_script {struct perf_tool tool;struct perf_session *session;
@@ -713,6 +793,9 @@ static void process_event(struct perf_script *script, union perf_event *event,else if (PRINT_FIELD (BRSTACKSYM ))print_sample_brstacksym (event, sample, thread, attr); +if (perf_evsel__is_bpf_output (evsel) &&PRINT_FIELD (BPF_OUTPUT )) +print_sample_bpf_output (sample); +printf ("\n" ); }
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [RFC PATCH 2/2] perf script: Print bpf-output events in 'perf script'
Em Wed, Feb 24, 2016 at 11:20:45AM +0000, Wang Nan escreveu:
Quoted 7 lines
This patch allows 'perf script' output messages from BPF program. For example, use
test_bpf_output_3.cat the end of this commit message,# ./perf record -e bpf-output/no-inherit,name=evt/ \ -e ./test_bpf_output_3.c/map:channel.event=evt/ \ usleep 100000
Trying to test this with 'perf trace', i.e. to shortcircuit record + analysis:
[root@jouet bpf]# perf trace -a --ev bpf-output/no-inherit,name=evt/ --ev ./test_bpf_output_3.c/map:channel.event=evt/ --ev 'perf_bpf_probe:*' usleep 100000 Failed to set filter "common_pid != 23954" on event evt with 22 (Invalid argument)
Can't we set tracepoint filters on these events?
- Arnaldo
Re: [RFC PATCH 2/2] perf script: Print bpf-output events in 'perf script'
Quoted 8 lines · Arnaldo Carvalho de Melo
Em Wed, Feb 24, 2016 at 11:20:45AM +0000, Wang Nan escreveu:
Quoted 7 lines
This patch allows 'perf script' output messages from BPF program. For example, use
test_bpf_output_3.cat the end of this commit message,# ./perf record -e bpf-output/no-inherit,name=evt/ \ -e ./test_bpf_output_3.c/map:channel.event=evt/ \ usleep 100000
Quoted 7 lines
Trying to test this with 'perf trace', i.e. to shortcircuit record + analysis:
[root@jouet bpf]# perf trace -a --ev bpf-output/no-inherit,name=evt/ --ev ./test_bpf_output_3.c/map:channel.event=evt/ --ev 'perf_bpf_probe:*' usleep 100000 Failed to set filter "
common_pid!= 23954" on event evt with 22 (Invalid argument)Can't we set tracepoint filters on these events?
No we can't. filters can only be set to tracepoint events, but bpf-output is a software event.
In addition, you don't need
... --ev 'perf_bpf_probe:*' ...
because they should be already collected by '--ev ./test_bpf_output_3.c'.
You don't see them because the two kprobe events are filtered out by BPF program. See 'return 0' in func.
Thank you.
Arnaldo Carvalho de Melo
maintainerRe: [RFC PATCH 2/2] perf script: Print bpf-output events in 'perf script'
Em Thu, Feb 25, 2016 at 12:11:11PM +0800, Wangnan (F) escreveu:
Quoted 14 lines · Arnaldo Carvalho de Melo
Quoted 11 lines
Em Wed, Feb 24, 2016 at 11:20:45AM +0000, Wang Nan escreveu:
Quoted 7 lines
This patch allows 'perf script' output messages from BPF program. For example, use
test_bpf_output_3.cat the end of this commit message,# ./perf record -e bpf-output/no-inherit,name=evt/ \ -e ./test_bpf_output_3.c/map:channel.event=evt/ \ usleep 100000Trying to test this with 'perf trace', i.e. to shortcircuit record + analysis:
Quoted 7 lines
Quoted 4 lines
[root@jouet bpf]# perf trace -a --ev bpf-output/no-inherit,name=evt/ --ev ./test_bpf_output_3.c/map:channel.event=evt/ --ev 'perf_bpf_probe:*' usleep 100000 Failed to set filter "
common_pid!= 23954" on event evt with 22 (Invalid argument)Can't we set tracepoint filters on these events?
No we can't. filters can only be set to tracepoint events, but bpf-output is a software event.
Gack, should've checked that, I somehow thought that it was a tracepoint event of some sort.
Quoted 4 lines
In addition, you don't need
... --ev 'perf_bpf_probe:*' ...because they should be already collected by '--ev ./test_bpf_output_3.c'.
Right, that was me trying to be clever 8-)
Quoted 2 lines
You don't see them because the two kprobe events are filtered out by BPF program. See 'return 0' in func.
Thanks for reminding about this part...
Quoted 1 line
Thank you.
tip-bot for Wang Nan
merge bot[tip:perf/core] perf script: Print bpf-output events in 'perf script'
In reply to Wang Nanperf script: Print bpf-output events in 'perf script'
Commit-ID: 30372f04c9dc159f99f1f09c61e5e0dbe4c91251 Gitweb: http://git.kernel.org/tip/30372f04c9dc159f99f1f09c61e5e0dbe4c91251 Author: Wang Nan <[email protected]> AuthorDate: Wed, 24 Feb 2016 11:20:45 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 24 Feb 2016 12:09:24 -0300
perf script: Print bpf-output events in 'perf script'
This patch allows 'perf script' output messages from BPF program. For example, use test_bpf_output_3.c at the end of this commit message,
# ./perf record -e bpf-output/no-inherit,name=evt/ \
-e ./test_bpf_output_3.c/map:channel.event=evt/ \
usleep 100000 # ./perf script
usleep 4882 21384.532523: evt: ffffffff810e97d1 sys_nanosleep ([kernel.kallsyms])
BPF output: 0000: 52 61 69 73 65 20 61 20 Raise a
0008: 42 50 46 20 65 76 65 6e BPF even
0010: 74 21 00 00 t!..
BPF string: "Raise a BPF event!" usleep 4882 21384.632606: evt: ffffffff8105c609 kretprobe_trampoline_holder ([kernel.kallsyms
BPF output: 0000: 52 61 69 73 65 20 61 20 Raise a
0008: 42 50 46 20 65 76 65 6e BPF even
0010: 74 21 00 00 t!..
BPF string: "Raise a BPF event!"Two samples from BPF output are printed by both binary and string format.
If BPF program output something unprintable, string format is suppressed.
/************************ BEGIN **************************/
#include <uapi/linux/bpf.h>
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
#define SEC(NAME) __attribute__((section(NAME), used))
static u64 (*ktime_get_ns)(void) =
(void *)BPF_FUNC_ktime_get_ns;
static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
(void *)BPF_FUNC_trace_printk;
static int (*get_smp_processor_id)(void) =
(void *)BPF_FUNC_get_smp_processor_id;
static int (*perf_event_output)(void *, struct bpf_map_def *, int, void *, unsigned long) =
(void *)BPF_FUNC_perf_event_output;
struct bpf_map_def SEC("maps") channel = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
}; static inline int __attribute__((always_inline)) func(void *ctx, int type)
{
char output_str[] = "Raise a BPF event!";
perf_event_output(ctx, &channel, get_smp_processor_id(),
&output_str, sizeof(output_str));
return 0; }
SEC("func_begin=sys_nanosleep")
int func_begin(void *ctx) {return func(ctx, 1);} SEC("func_end=sys_nanosleep%return")
int func_end(void *ctx) { return func(ctx, 2);} char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE; /************************* END ***************************/
--- tools/perf/builtin-script.c | 93 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 5 deletions(-)
tools/perf/builtin-script.c +88 -5
@@ -61,6 +61,7 @@ enum perf_output_field {PERF_OUTPUT_BRSTACKSYM =1U <<16 ,PERF_OUTPUT_DATA_SRC =1U <<17 ,PERF_OUTPUT_WEIGHT =1U <<18 , + PERF_OUTPUT_BPF_OUTPUT = 1U << 19, }; struct output_option {
@@ -86,6 +87,7 @@ struct output_option { {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM}, {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC}, {.str = "weight", .field = PERF_OUTPUT_WEIGHT}, + {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT}, };/* default set to maintain compatibility with current format */
@@ -106,7 +108,7 @@ static struct {PERF_OUTPUT_SYM |PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD, - .invalid_fields = PERF_OUTPUT_TRACE, + .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, }, [PERF_TYPE_SOFTWARE] = {
@@ -116,7 +118,7 @@ static struct {PERF_OUTPUT_CPU |PERF_OUTPUT_TIME |PERF_OUTPUT_EVNAME |PERF_OUTPUT_IP |PERF_OUTPUT_SYM |PERF_OUTPUT_DSO | - PERF_OUTPUT_PERIOD, + PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT, .invalid_fields = PERF_OUTPUT_TRACE, },
@@ -126,7 +128,7 @@ static struct { .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |PERF_OUTPUT_CPU |PERF_OUTPUT_TIME | - PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE, +PERF_OUTPUT_EVNAME |PERF_OUTPUT_TRACE }, [PERF_TYPE_RAW] = {
@@ -139,7 +141,7 @@ static struct {PERF_OUTPUT_PERIOD |PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT, - .invalid_fields = PERF_OUTPUT_TRACE, + .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, }, [PERF_TYPE_BREAKPOINT] = {
@@ -151,7 +153,7 @@ static struct {PERF_OUTPUT_SYM |PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD, - .invalid_fields = PERF_OUTPUT_TRACE, + .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, }, };
@@ -624,6 +626,84 @@ static void print_sample_flags(u32 flags)printf (" %-4s " , str); } +struct printer_data { +int line_no; +bool hit_nul; +bool is_printable; +}; + +static void +print_sample_bpf_output_printer (enum binary_printer_ops op, +unsigned int val, +void *extra) +{ +unsigned char ch = (unsigned char )val; +struct printer_data *printer_data = extra; + +switch (op) { +case BINARY_PRINT_DATA_BEGIN : +printf ("\n" ); +break ; +case BINARY_PRINT_LINE_BEGIN : +printf ("%17s" , !printer_data->line_no ?"BPF output:" : +" " ); +break ; +case BINARY_PRINT_ADDR : +printf (" %04x:" , val); +break ; +case BINARY_PRINT_NUM_DATA : +printf (" %02x" , val); +break ; +case BINARY_PRINT_NUM_PAD : +printf (" " ); +break ; +case BINARY_PRINT_SEP : +printf (" " ); +break ; +case BINARY_PRINT_CHAR_DATA : +if (printer_data->hit_nul && ch) + printer_data->is_printable = false; + +if (!isprint (ch)) { +printf ("%c" ,'.' ); + +if (!printer_data->is_printable) +break ; + +if (ch =='\0' ) + printer_data->hit_nul = true; +else + printer_data->is_printable = false; + }else { +printf ("%c" , ch); + } +break ; +case BINARY_PRINT_CHAR_PAD : +printf (" " ); +break ; +case BINARY_PRINT_LINE_END : +printf ("\n" ); + printer_data->line_no++; +break ; +case BINARY_PRINT_DATA_END : +default : +break ; + } +} + +static void print_sample_bpf_output (struct perf_sample *sample) +{ +unsigned int nr_bytes = sample->raw_size; +struct printer_data printer_data = {0 , false, true}; + +print_binary (sample->raw_data, nr_bytes,8 , + print_sample_bpf_output_printer, &printer_data); + +if (printer_data.is_printable && printer_data.hit_nul) +printf ("%17s \"%s\"\n" ,"BPF string:" , + (char *)(sample->raw_data)); +} +struct perf_script {struct perf_tool tool;struct perf_session *session;
@@ -731,6 +811,9 @@ static void process_event(struct perf_script *script, union perf_event *event,else if (PRINT_FIELD (BRSTACKSYM ))print_sample_brstacksym (event, sample, thread, attr); +if (perf_evsel__is_bpf_output (evsel) &&PRINT_FIELD (BPF_OUTPUT )) +print_sample_bpf_output (sample); +printf ("\n" ); }
Arnaldo Carvalho de Melo
maintainerEm Wed, Feb 24, 2016 at 11:20:43AM +0000, Wang Nan escreveu:
Quoted 3 lines
Hi Arnaldo and Jiri,
Glad to see the progress we made today.
Indeed, thanks, tested and applied both of these patches,
- Arnaldo