perf script: Add API for filtering via dynamically loaded shared object
Adrian Hunter
reviewerv2 cover perf script: Add API for filtering via dynamically loaded shared object
Hi
In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
This is V2.
The main patch is patch 1.
The other patches add more functionality, except for patch 5 which installs the C API header file.
Changes in V2:
perf script: Move filter_cpu() earlier perf script: Move filtering before scripting perf script: Share addr_al between functions
Dropped because they have now been applied.
perf script: Add API for filtering via dynamically loaded shared object
Move 2 members of struct perf_dlfilter_sample
Add 'ctx' as an argument to 'start' and 'stop'
Find dlfilter .so files in current directory or exec-path/dlfilters
perf script: Add option to list dlfilters
New patch
perf script: Add option to pass arguments to dlfilters
New patch
Adrian Hunter (10):
perf script: Add API for filtering via dynamically loaded shared object perf script: Add dlfilter__filter_event_early()
perf script: Add option to list dlfilters
perf script: Add option to pass arguments to dlfilters
perf build: Install perf_dlfilter.h
perf dlfilter: Add resolve_address() to perf_dlfilter_fns perf dlfilter: Add insn() to perf_dlfilter_fns
perf dlfilter: Add srcline() to perf_dlfilter_fns
perf dlfilter: Add attr() to perf_dlfilter_fns
perf dlfilter: Add object_code() to perf_dlfilter_fns
tools/perf/Documentation/perf-dlfilter.txt | 251 ++++++++++++ tools/perf/Documentation/perf-script.txt | 15 +- tools/perf/Makefile.config | 3 + tools/perf/Makefile.perf | 4 +- tools/perf/builtin-script.c | 86 +++- tools/perf/util/Build | 1 + tools/perf/util/dlfilter.c | 615 +++++++++++++++++++++++++++++ tools/perf/util/dlfilter.h | 97 +++++ tools/perf/util/perf_dlfilter.h | 150 +++++++ 9 files changed, 1211 insertions(+), 11 deletions(-) create mode 100644 tools/perf/Documentation/perf-dlfilter.txt create mode 100644 tools/perf/util/dlfilter.c create mode 100644 tools/perf/util/dlfilter.h create mode 100644 tools/perf/util/perf_dlfilter.h
Regards
Adrian
Adrian Hunter
reviewerPATCH v2 1/10 perf script: Add API for filtering via dynamically loaded shared object
In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
--- tools/perf/Documentation/perf-dlfilter.txt | 222 ++++++++++++++ tools/perf/Documentation/perf-script.txt | 7 +- tools/perf/builtin-script.c | 25 +- tools/perf/util/Build | 1 + tools/perf/util/dlfilter.c | 330 +++++++++++++++++++++ tools/perf/util/dlfilter.h | 74 +++++ tools/perf/util/perf_dlfilter.h | 123 ++++++++ 7 files changed, 780 insertions(+), 2 deletions(-) create mode 100644 tools/perf/Documentation/perf-dlfilter.txt create mode 100644 tools/perf/util/dlfilter.c create mode 100644 tools/perf/util/dlfilter.h create mode 100644 tools/perf/util/perf_dlfilter.h
tools/perf/Documentation/perf-dlfilter.txt new file +222 -0
@@ -0,0 +1,222 @@ +perf-dlfilter(1) +================ + +NAME +---- +perf-dlfilter - Filter sample events using a dynamically loaded shared +object file + +SYNOPSIS +-------- +[verse] +'perf script' [--dlfilter file.so ] + +DESCRIPTION +----------- + +This option is used to process data through a custom filter provided by a +dynamically loaded shared object file. + +If 'file.so' does not contain "/", then it will be found either in the current +directory, or perf tools exec path which is ~/libexec/perf-core/dlfilters for +a local build and install (refer perf --exec-path), or the dynamic linker +paths. + +API +--- + +The API for filtering consists of the following: + +[source,c] +---- +#include <perf/perf_dlfilter.h> + +const struct perf_dlfilter_fns perf_dlfilter_fns; + +int start(void **data, void *ctx); +int stop(void *data, void *ctx); +int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx); +---- + +If implemented, 'start' will be called at the beginning, before any +calls to 'filter_event' . Return 0 to indicate success, +or return a negative error code. '*data' can be assigned for use by other +functions. 'ctx' is needed for calls to perf_dlfilter_fns, but most +perf_dlfilter_fns are not valid when called from 'start'. + +If implemented, 'stop' will be called at the end, after any calls to +'filter_event'. Return 0 to indicate success, or +return a negative error code. 'data' is set by 'start'. 'ctx' is needed +for calls to perf_dlfilter_fns, but most perf_dlfilter_fns are not valid +when called from 'stop'. + +If implemented, 'filter_event' will be called for each sample event. +Return 0 to keep the sample event, 1 to filter it out, or return a negative +error code. 'data' is set by 'start'. 'ctx' is needed for calls to +'perf_dlfilter_fns'. + +The perf_dlfilter_sample structure +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +'filter_event' is passed a perf_dlfilter_sample +structure, which contains the following fields: +[source,c] +---- +/* + * perf sample event information (as per perf script and <linux/perf_event.h>) + */ +struct perf_dlfilter_sample { + __u32 size; /* Size of this structure (for compatibility checking) */ + __u16 ins_lat; /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */ + __u16 p_stage_cyc; /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */ + __u64 ip; + __s32 pid; + __s32 tid; + __u64 time; + __u64 addr; + __u64 id; + __u64 stream_id; + __u64 period; + __u64 weight; /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */ + __u64 transaction; /* Refer PERF_SAMPLE_TRANSACTION in <linux/perf_event.h> */ + __u64 insn_cnt; /* For instructions-per-cycle (IPC) */ + __u64 cyc_cnt; /* For instructions-per-cycle (IPC) */ + __s32 cpu; + __u32 flags; /* Refer PERF_DLFILTER_FLAG_* above */ + __u64 data_src; /* Refer PERF_SAMPLE_DATA_SRC in <linux/perf_event.h> */ + __u64 phys_addr; /* Refer PERF_SAMPLE_PHYS_ADDR in <linux/perf_event.h> */ + __u64 data_page_size; /* Refer PERF_SAMPLE_DATA_PAGE_SIZE in <linux/perf_event.h> */ + __u64 code_page_size; /* Refer PERF_SAMPLE_CODE_PAGE_SIZE in <linux/perf_event.h> */ + __u64 cgroup; /* Refer PERF_SAMPLE_CGROUP in <linux/perf_event.h> */ + __u8 cpumode; /* Refer CPUMODE_MASK etc in <linux/perf_event.h> */ + __u8 addr_correlates_sym; /* True => resolve_addr() can be called */ + __u16 misc; /* Refer perf_event_header in <linux/perf_event.h> */ + __u32 raw_size; /* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */ + const void *raw_data; /* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */ + __u64 brstack_nr; /* Number of brstack entries */ + const struct perf_branch_entry *brstack; /* Refer <linux/perf_event.h> */ + __u64 raw_callchain_nr; /* Number of raw_callchain entries */ + const __u64 *raw_callchain; /* Refer <linux/perf_event.h> */ + const char *event; +}; +---- + +The perf_dlfilter_fns structure +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The 'perf_dlfilter_fns' structure is populated with function pointers when the +file is loaded. The functions can be called by 'filter_event'. + +[source,c] +---- +struct perf_dlfilter_fns { + const struct perf_dlfilter_al *(*resolve_ip)(void *ctx); + const struct perf_dlfilter_al *(*resolve_addr)(void *ctx); + void *(*reserved[126])(void *); +}; +---- + +'resolve_ip' returns information about ip. + +'resolve_addr' returns information about addr (if addr_correlates_sym). + +The perf_dlfilter_al structure +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The 'perf_dlfilter_al' structure contains information about an address. + +[source,c] +---- +/* + * Address location (as per perf script) + */ +struct perf_dlfilter_al { + __u32 size; /* Size of this structure (for compatibility checking) */ + __u32 symoff; + const char *sym; + __u64 addr; /* Mapped address (from dso) */ + __u64 sym_start; + __u64 sym_end; + const char *dso; + __u8 sym_binding; /* STB_LOCAL, STB_GLOBAL or STB_WEAK, refer <elf.h> */ + __u8 is_64_bit; /* Only valid if dso is not NULL */ + __u8 is_kernel_ip; /* True if in kernel space */ + __u32 buildid_size; + __u8 *buildid; + /* Below members are only populated by resolve_ip() */ + __u8 filtered; /* true if this sample event will be filtered out */ + const char *comm; +}; +---- + +perf_dlfilter_sample flags +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The 'flags' member of 'perf_dlfilter_sample' corresponds with the flags field +of perf script. The bits of the flags are as follows: + +[source,c] +---- +/* Definitions for perf_dlfilter_sample flags */ +enum { + PERF_DLFILTER_FLAG_BRANCH = 1ULL << 0, + PERF_DLFILTER_FLAG_CALL = 1ULL << 1, + PERF_DLFILTER_FLAG_RETURN = 1ULL << 2, + PERF_DLFILTER_FLAG_CONDITIONAL = 1ULL << 3, + PERF_DLFILTER_FLAG_SYSCALLRET = 1ULL << 4, + PERF_DLFILTER_FLAG_ASYNC = 1ULL << 5, + PERF_DLFILTER_FLAG_INTERRUPT = 1ULL << 6, + PERF_DLFILTER_FLAG_TX_ABORT = 1ULL << 7, + PERF_DLFILTER_FLAG_TRACE_BEGIN = 1ULL << 8, + PERF_DLFILTER_FLAG_TRACE_END = 1ULL << 9, + PERF_DLFILTER_FLAG_IN_TX = 1ULL << 10, + PERF_DLFILTER_FLAG_VMENTRY = 1ULL << 11, + PERF_DLFILTER_FLAG_VMEXIT = 1ULL << 12, +}; +---- + +EXAMPLE +------- + +Filter out everything except branches from "foo" to "bar": + +[source,c] +---- +#include <perf/perf_dlfilter.h> +#include <string.h> + +const struct perf_dlfilter_fns perf_dlfilter_fns; + +int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx) +{ + const struct perf_dlfilter_al *al; + const struct perf_dlfilter_al *addr_al; + + if (!sample->ip || !sample->addr_correlates_sym) + return 1; + + al = perf_dlfilter_fns.resolve_ip(ctx); + if (!al || !al->sym || strcmp(al->sym, "foo")) + return 1; + + addr_al = perf_dlfilter_fns.resolve_addr(ctx); + if (!addr_al || !addr_al->sym || strcmp(addr_al->sym, "bar")) + return 1; + + return 0; +} +---- + +To build the shared object, assuming perf has been installed for the local user +i.e. perf_dlfilter.h is in ~/include/perf : + + gcc -c -I ~/include -fpic dlfilter-example.c + gcc -shared -o dlfilter-example.so dlfilter-example.o + +To use the filter with perf script: + + perf script --dlfilter dlfilter-example.so + +SEE ALSO +-------- +linkperf:perf-script[1]
tools/perf/Documentation/perf-script.txt +6 -1
@@ -98,6 +98,10 @@ OPTIONS Generate perf-script.[ext] starter script for given language, using current perf.data. +--dlfilter=<file>:: + Filter sample events using the given shared object file. + Refer linkperf:perf-dlfilter[1] + -a:: Force system-wide collection. Scripts run without a <command> normally use -a by default, while scripts run with a <command>
@@ -483,4 +487,5 @@ include::itrace.txt[] SEE ALSO -------- linkperf:perf-record[1], linkperf:perf-script-perl[1], -linkperf:perf-script-python[1], linkperf:perf-intel-pt[1] +linkperf:perf-script-python[1], linkperf:perf-intel-pt[1], +linkperf:perf-dlfilter[1]
tools/perf/builtin-script.c +24 -1
@@ -55,6 +55,7 @@#include <subcmd/pager.h> #include <perf/evlist.h> #include <linux/err.h> +#include "util/dlfilter.h" #include "util/record.h" #include "util/util.h" #include "perf.h"
@@ -79,6 +80,7 @@ static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);static struct perf_stat_config stat_config;static int max_blocks;static bool native_arch; +static struct dlfilter *dlfilter;unsigned int scripting_max_stack =PERF_MAX_STACK_DEPTH ;
@@ -2175,6 +2177,7 @@ static int process_sample_event(struct perf_tool *tool, struct perf_script *scr = container_of(tool, struct perf_script, tool);struct addr_location al;struct addr_location addr_al; +int ret =0 ;if (perf_time__ranges_skip_sample (scr->ptime_range, scr->range_num, sample->time)) {
@@ -2213,6 +2216,13 @@ static int process_sample_event(struct perf_tool *tool,if (evswitch__discard (&scr->evswitch, evsel)) goto out_put; + ret =dlfilter__filter_event (dlfilter, event, sample, evsel, machine, &al, &addr_al); +if (ret) { +if (ret >0 ) + ret =0 ; + goto out_put; + } +if (scripting_ops) {struct addr_location *addr_al_ptr =NULL ;
@@ -2229,7 +2239,7 @@ static int process_sample_event(struct perf_tool *tool, out_put:addr_location__put (&al); -return 0 ; +return ret; } static int process_attr(struct perf_tool *tool, union perf_event *event,
@@ -3568,6 +3578,7 @@ int cmd_script(int argc, const char **argv) };struct utsname uts;char *script_path =NULL ; +const char *dlfilter_file =NULL ;const char **__argv;int i, j, err =0 ;struct perf_script script = {
@@ -3615,6 +3626,7 @@ int cmd_script(int argc, const char **argv) parse_scriptname),OPT_STRING ('g' ,"gen-script" , &generate_script_lang,"lang" ,"generate perf-script.xx script in specified language" ), +OPT_STRING (0 ,"dlfilter" , &dlfilter_file,"file" ,"filter .so file name" ),OPT_STRING ('i' ,"input" , &input_name,"file" ,"input file name" ),OPT_BOOLEAN ('d' ,"debug-mode" , &debug_mode, "do various checks like samples ordering and lost events"),
@@ -3933,6 +3945,12 @@ int cmd_script(int argc, const char **argv)exit (-1 ); } +if (dlfilter_file) { + dlfilter =dlfilter__new (dlfilter_file); +if (!dlfilter) +return -1 ; + } +if (!script_name) {setup_pager (); use_browser =0 ;
@@ -4032,6 +4050,10 @@ int cmd_script(int argc, const char **argv) goto out_delete; } + err =dlfilter__start (dlfilter, session); +if (err) + goto out_delete; +if (script_name) { err = scripting_ops->start_script (script_name, argc, argv, session);if (err)
@@ -4081,6 +4103,7 @@ int cmd_script(int argc, const char **argv)if (script_started)cleanup_scripting (); +dlfilter__cleanup (dlfilter); out:return err; }
tools/perf/util/Build +1 -0
@@ -126,6 +126,7 @@ perf-y += parse-regs-options.o perf-y += parse-sublevel-options.o perf-y += term.o perf-y += help-unknown-cmd.o +perf-y += dlfilter.o perf-y += mem-events.o perf-y += vsprintf.o perf-y += units.o
tools/perf/util/dlfilter.c new file +330 -0
330 lines not on this page · raw message
tools/perf/util/dlfilter.h new file +74 -0
74 lines not on this page · raw message
tools/perf/util/perf_dlfilter.h new file +123 -0
123 lines not on this page · raw message
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 2/10 perf script: Add dlfilter__filter_event_early()
In reply to Adrian Hunterfilter_event_early() can be more than 30% faster than filter_event() because it is called before internal filtering. In other respects it is the same as filter_event(), except that it will be passed events that have yet to be filtered out.
--- tools/perf/Documentation/perf-dlfilter.txt | 13 +++++++---- tools/perf/builtin-script.c | 26 +++++++++++++++------- tools/perf/util/dlfilter.c | 9 ++++++-- tools/perf/util/dlfilter.h | 21 +++++++++++++++-- tools/perf/util/perf_dlfilter.h | 6 +++++ 5 files changed, 59 insertions(+), 16 deletions(-)
tools/perf/Documentation/perf-dlfilter.txt +9 -4
@@ -36,16 +36,17 @@ const struct perf_dlfilter_fns perf_dlfilter_fns; int start(void **data, void *ctx); int stop(void *data, void *ctx); int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx); +int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx); ---- If implemented, 'start' will be called at the beginning, before any -calls to 'filter_event' . Return 0 to indicate success, +calls to 'filter_event' or 'filter_event_early'. Return 0 to indicate success, or return a negative error code. '*data' can be assigned for use by other functions. 'ctx' is needed for calls to perf_dlfilter_fns, but most perf_dlfilter_fns are not valid when called from 'start'. If implemented, 'stop' will be called at the end, after any calls to -'filter_event'. Return 0 to indicate success, or +'filter_event' or 'filter_event_early'. Return 0 to indicate success, or return a negative error code. 'data' is set by 'start'. 'ctx' is needed for calls to perf_dlfilter_fns, but most perf_dlfilter_fns are not valid when called from 'stop'.
@@ -55,10 +56,13 @@ Return 0 to keep the sample event, 1 to filter it out, or return a negative error code. 'data' is set by 'start'. 'ctx' is needed for calls to 'perf_dlfilter_fns'. +'filter_event_early' is the same as 'filter_event' except it is called before +internal filtering. + The perf_dlfilter_sample structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -'filter_event' is passed a perf_dlfilter_sample +'filter_event' and 'filter_event_early' are passed a perf_dlfilter_sample structure, which contains the following fields: [source,c] ----
@@ -105,7 +109,8 @@ The perf_dlfilter_fns structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The 'perf_dlfilter_fns' structure is populated with function pointers when the -file is loaded. The functions can be called by 'filter_event'. +file is loaded. The functions can be called by 'filter_event' or +'filter_event_early'. [source,c] ----
tools/perf/builtin-script.c +18 -8
@@ -2179,9 +2179,20 @@ static int process_sample_event(struct perf_tool *tool,struct addr_location addr_al;int ret =0 ; +/* Set thread to NULL to indicate addr_al and al are not initialized */ + addr_al.thread =NULL ; + al.thread =NULL ; + + ret =dlfilter__filter_event_early (dlfilter, event, sample, evsel, machine, &al, &addr_al); +if (ret) { +if (ret >0 ) + ret =0 ; + goto out_put; + } +if (perf_time__ranges_skip_sample (scr->ptime_range, scr->range_num, sample->time)) { -return 0 ; + goto out_put; }if (debug_mode) {
@@ -2192,24 +2203,22 @@ static int process_sample_event(struct perf_tool *tool, nr_unordered++; } last_timestamp = sample->time; -return 0 ; + goto out_put; }if (filter_cpu (sample)) -return 0 ; + goto out_put;if (machine__resolve (machine, &al, sample) <0 ) {pr_err ("problem processing %d event, skipping it.\n" , event->header.type); -return -1 ; + ret =-1 ; + goto out_put; }if (al.filtered) goto out_put; -/* Set thread to NULL to indicate addr_al is not initialized */ - addr_al.thread =NULL ; -if (!show_event (sample, evsel, al.thread, &al, &addr_al)) goto out_put;
@@ -2238,7 +2247,8 @@ static int process_sample_event(struct perf_tool *tool, } out_put: -addr_location__put (&al); +if (al.thread) +addr_location__put (&al);return ret; }
tools/perf/util/dlfilter.c +7 -2
@@ -175,6 +175,7 @@ static int dlfilter__open(struct dlfilter *d) } d->start =dlsym (d->handle,"start" ); d->filter_event =dlsym (d->handle,"filter_event" ); + d->filter_event_early =dlsym (d->handle,"filter_event_early" ); d->stop =dlsym (d->handle,"stop" ); d->fns =dlsym (d->handle,"perf_dlfilter_fns" );if (d->fns)
@@ -251,7 +252,8 @@ int dlfilter__do_filter_event(struct dlfilter *d,struct evsel *evsel, struct machine *machine, struct addr_location *al, - struct addr_location *addr_al) + struct addr_location *addr_al, + bool early) { struct perf_dlfilter_sample d_sample;struct perf_dlfilter_al d_ip_al;
@@ -322,7 +324,10 @@ int dlfilter__do_filter_event(struct dlfilter *d, d->ctx_valid = true; - ret = d->filter_event (d->data, &d_sample, d); +if (early) + ret = d->filter_event_early (d->data, &d_sample, d); +else + ret = d->filter_event (d->data, &d_sample, d); d->ctx_valid = false;
tools/perf/util/dlfilter.h +19 -2
@@ -40,6 +40,9 @@ struct dlfilter {int (*filter_event)(void *data,const struct perf_dlfilter_sample *sample,void *ctx); +int (*filter_event_early)(void *data, +const struct perf_dlfilter_sample *sample, +void *ctx);struct perf_dlfilter_fns *fns; };
@@ -54,7 +57,8 @@ int dlfilter__do_filter_event(struct dlfilter *d,struct evsel *evsel, struct machine *machine, struct addr_location *al, - struct addr_location *addr_al); + struct addr_location *addr_al, + bool early);void dlfilter__cleanup (struct dlfilter *d);
@@ -68,7 +72,20 @@ static inline int dlfilter__filter_event(struct dlfilter *d, {if (!d || !d->filter_event)return 0 ; -return dlfilter__do_filter_event (d, event, sample, evsel, machine, al, addr_al); +return dlfilter__do_filter_event (d, event, sample, evsel, machine, al, addr_al, false); +} + +static inline int dlfilter__filter_event_early (struct dlfilter *d, +union perf_event *event, +struct perf_sample *sample, +struct evsel *evsel, +struct machine *machine, +struct addr_location *al, +struct addr_location *addr_al) +{ +if (!d || !d->filter_event_early) +return 0 ; +return dlfilter__do_filter_event (d, event, sample, evsel, machine, al, addr_al, true); }#endif
tools/perf/util/perf_dlfilter.h +6 -0
@@ -120,4 +120,10 @@ int stop(void *data, void *ctx);*/ int filter_event (void *data,const struct perf_dlfilter_sample *sample,void *ctx); +/* +* The same as 'filter_event' except it is called before internal +* filtering. +*/ +int filter_event_early (void *data,const struct perf_dlfilter_sample *sample,void *ctx); +#endif
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 3/10 perf script: Add option to list dlfilters
In reply to Adrian HunterAdd option --list-dlfilters to list dlfilters in the current directory or the exec-path e.g. ~/libexec/perf-core/dlfilters. Use with option -v (must come before option --list-dlfilters) to show long descriptions.
--- tools/perf/Documentation/perf-dlfilter.txt | 4 + tools/perf/Documentation/perf-script.txt | 4 + tools/perf/builtin-script.c | 2 + tools/perf/util/dlfilter.c | 117 ++++++++++++++++++++- tools/perf/util/dlfilter.h | 2 + tools/perf/util/perf_dlfilter.h | 6 ++ 6 files changed, 134 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-dlfilter.txt +4 -0
@@ -37,6 +37,7 @@ int start(void **data, void *ctx); int stop(void *data, void *ctx); int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx); int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx); +const char *filter_description(const char **long_description); ---- If implemented, 'start' will be called at the beginning, before any
@@ -59,6 +60,9 @@ error code. 'data' is set by 'start'. 'ctx' is needed for calls to 'filter_event_early' is the same as 'filter_event' except it is called before internal filtering. +If implemented, 'filter_description' should return a one-line description +of the filter, and optionally a longer description. + The perf_dlfilter_sample structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/perf/Documentation/perf-script.txt +4 -0
@@ -102,6 +102,10 @@ OPTIONS Filter sample events using the given shared object file. Refer linkperf:perf-dlfilter[1] +--list-dlfilters=:: + Display a list of available dlfilters. Use with option -v (must come + before option --list-dlfilters) to show long descriptions. + -a:: Force system-wide collection. Scripts run without a <command> normally use -a by default, while scripts run with a <command>
tools/perf/builtin-script.c +2 -0
@@ -3631,6 +3631,8 @@ int cmd_script(int argc, const char **argv) "show latency attributes (irqs/preemption disabled, etc)"),OPT_CALLBACK_NOOPT ('l' ,"list" ,NULL ,NULL ,"list available scripts" , list_available_scripts), +OPT_CALLBACK_NOOPT (0 ,"list-dlfilters" ,NULL ,NULL ,"list available dlfilters" , + list_available_dlfilters),OPT_CALLBACK ('s' ,"script" ,NULL ,"name" ,"script file name (lang:script name, script name, or *)" , parse_scriptname),
tools/perf/util/dlfilter.c +116 -1
138 lines not on this page · raw message
tools/perf/util/dlfilter.h +2 -0
@@ -88,4 +88,6 @@ static inline int dlfilter__filter_event_early(struct dlfilter *d, return dlfilter__do_filter_event(d, event, sample, evsel, machine, al, addr_al, true); } +int list_available_dlfilters (const struct option *opt,const char *s,int unset); +#endif
tools/perf/util/perf_dlfilter.h +6 -0
@@ -126,4 +126,10 @@ int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ct*/ int filter_event_early (void *data,const struct perf_dlfilter_sample *sample,void *ctx); +/* +* If implemented, return a one-line description of the filter, and optionally +* a longer description. +*/ +const char *filter_description (const char **long_description); +#endif
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 4/10 perf script: Add option to pass arguments to dlfilters
In reply to Adrian HunterAdd option --dlarg to pass arguments to dlfilters. The --dlarg option can be repeated to pass more than 1 argument.
--- tools/perf/Documentation/perf-dlfilter.txt | 10 +++-- tools/perf/Documentation/perf-script.txt | 4 ++ tools/perf/builtin-script.c | 35 ++++++++++++++++- tools/perf/util/dlfilter.c | 45 ++++++++++++++++++---- tools/perf/util/dlfilter.h | 6 ++- tools/perf/util/perf_dlfilter.h | 4 +- 6 files changed, 91 insertions(+), 13 deletions(-)
tools/perf/Documentation/perf-dlfilter.txt +7 -3
@@ -9,13 +9,14 @@ object file SYNOPSIS -------- [verse] -'perf script' [--dlfilter file.so ] +'perf script' [--dlfilter file.so ] [ --dlarg arg ]... DESCRIPTION ----------- This option is used to process data through a custom filter provided by a -dynamically loaded shared object file. +dynamically loaded shared object file. Arguments can be passed using --dlarg +and retrieved using perf_dlfilter_fns.args(). If 'file.so' does not contain "/", then it will be found either in the current directory, or perf tools exec path which is ~/libexec/perf-core/dlfilters for
@@ -121,7 +122,8 @@ file is loaded. The functions can be called by 'filter_event' or struct perf_dlfilter_fns { const struct perf_dlfilter_al *(*resolve_ip)(void *ctx); const struct perf_dlfilter_al *(*resolve_addr)(void *ctx); - void *(*reserved[126])(void *); + char **(*args)(void *ctx, int *dlargc); + void *(*reserved[125])(void *); }; ----
@@ -129,6 +131,8 @@ struct perf_dlfilter_fns { 'resolve_addr' returns information about addr (if addr_correlates_sym). +'args' returns arguments from --dlarg options. + The perf_dlfilter_al structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/perf/Documentation/perf-script.txt +4 -0
@@ -102,6 +102,10 @@ OPTIONS Filter sample events using the given shared object file. Refer linkperf:perf-dlfilter[1] +--dlarg=<arg>:: + Pass 'arg' as an argument to the dlfilter. --dlarg may be repeated + to add more arguments. + --list-dlfilters=:: Display a list of available dlfilters. Use with option -v (must come before option --list-dlfilters) to show long descriptions.
tools/perf/builtin-script.c +34 -1
@@ -81,6 +81,8 @@ static struct perf_stat_config stat_config;static int max_blocks;static bool native_arch;static struct dlfilter *dlfilter; +static int dlargc; +static char **dlargv;unsigned int scripting_max_stack =PERF_MAX_STACK_DEPTH ;
@@ -3175,6 +3177,34 @@ static int list_available_scripts(const struct option *opt __maybe_unused,exit (0 ); } +static int add_dlarg (const struct option *opt__maybe_unused , +const char *s,int unset__maybe_unused ) +{ +char *arg =strdup (s); +void *a; + +if (!arg) +return -1 ; + + a =realloc (dlargv,sizeof (dlargv[0 ]) * (dlargc +1 )); +if (!a) { +free (arg); +return -1 ; + } + + dlargv = a; + dlargv[dlargc++] = arg; + +return 0 ; +} + +static void free_dlarg (void ) +{ +while (dlargc--) +free (dlargv[dlargc]); +free (dlargv); +} +/* * Some scripts specify the required events in their "xxx-record" file, * this function will check if the events in perf.data match those
@@ -3639,6 +3669,8 @@ int cmd_script(int argc, const char **argv)OPT_STRING ('g' ,"gen-script" , &generate_script_lang,"lang" ,"generate perf-script.xx script in specified language" ),OPT_STRING (0 ,"dlfilter" , &dlfilter_file,"file" ,"filter .so file name" ), +OPT_CALLBACK (0 ,"dlarg" ,NULL ,"argument" ,"filter argument" , + add_dlarg),OPT_STRING ('i' ,"input" , &input_name,"file" ,"input file name" ),OPT_BOOLEAN ('d' ,"debug-mode" , &debug_mode, "do various checks like samples ordering and lost events"),
@@ -3958,7 +3990,7 @@ int cmd_script(int argc, const char **argv) }if (dlfilter_file) { - dlfilter =dlfilter__new (dlfilter_file); + dlfilter =dlfilter__new (dlfilter_file, dlargc, dlargv);if (!dlfilter)return -1 ; }
@@ -4116,6 +4148,7 @@ int cmd_script(int argc, const char **argv)if (script_started)cleanup_scripting ();dlfilter__cleanup (dlfilter); +free_dlarg (); out:return err; }
tools/perf/util/dlfilter.c +38 -7
90 lines not on this page · raw message
tools/perf/util/dlfilter.h +5 -1
@@ -23,6 +23,10 @@ struct dlfilter {void *data;struct perf_session *session;bool ctx_valid; +bool in_start; +bool in_stop; +int dlargc; +char **dlargv;union perf_event *event;struct perf_sample *sample;
@@ -47,7 +51,7 @@ struct dlfilter {struct perf_dlfilter_fns *fns; }; -struct dlfilter *dlfilter__new (const char *file); +struct dlfilter *dlfilter__new (const char *file,int dlargc,char **dlargv);int dlfilter__start (struct dlfilter *d,struct perf_session *session);
tools/perf/util/perf_dlfilter.h +3 -1
@@ -90,8 +90,10 @@ struct perf_dlfilter_fns {const struct perf_dlfilter_al *(*resolve_ip)(void *ctx);/* Return information about addr (if addr_correlates_sym) */ const struct perf_dlfilter_al *(*resolve_addr)(void *ctx); +/* Return arguments from --dlarg option */ +char **(*args)(void *ctx,int *dlargc);/* Reserved */ -void *(*reserved[126 ])(void *); +void *(*reserved[125 ])(void *); };/*
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 5/10 perf build: Install perf_dlfilter.h
In reply to Adrian HunterUsers of the --dlfilter option need to include perf_dlfilter.h in their filters. Install it to the include path.
--- tools/perf/Makefile.config | 3 +++ tools/perf/Makefile.perf | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-)
tools/perf/Makefile.config +3 -0
@@ -1117,6 +1117,8 @@ prefix ?= $(HOME) endif bindir_relative = bin bindir = $(abspath $(prefix)/$(bindir_relative)) +includedir_relative = include +includedir = $(abspath $(prefix)/$(includedir_relative)) mandir = share/man infodir = share/info perfexecdir = libexec/perf-core
@@ -1149,6 +1151,7 @@ ETC_PERFCONFIG_SQ = $(subst ','\'',$(ETC_PERFCONFIG)) STRACE_GROUPS_DIR_SQ = $(subst ','\'',$(STRACE_GROUPS_DIR)) DESTDIR_SQ = $(subst ','\'',$(DESTDIR)) bindir_SQ = $(subst ','\'',$(bindir)) +includedir_SQ = $(subst ','\'',$(includedir)) mandir_SQ = $(subst ','\'',$(mandir)) infodir_SQ = $(subst ','\'',$(infodir)) perfexecdir_SQ = $(subst ','\'',$(perfexecdir))
tools/perf/Makefile.perf +3 -1
@@ -923,7 +923,9 @@ install-tools: all install-gtk $(call QUIET_INSTALL, binaries) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(bindir_SQ)'; \ $(INSTALL) $(OUTPUT)perf '$(DESTDIR_SQ)$(bindir_SQ)'; \ - $(LN) '$(DESTDIR_SQ)$(bindir_SQ)/perf' '$(DESTDIR_SQ)$(bindir_SQ)/trace' + $(LN) '$(DESTDIR_SQ)$(bindir_SQ)/perf' '$(DESTDIR_SQ)$(dir_SQ)/trace'; \ + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(includedir_SQ)/perf'; \ + $(INSTALL) util/perf_dlfilter.h -t '$(DESTDIR_SQ)$(includedir_SQ)/perf' ifndef NO_PERF_READ_VDSO32 $(call QUIET_INSTALL, perf-read-vdso32) \ $(INSTALL) $(OUTPUT)perf-read-vdso32 '$(DESTDIR_SQ)$(bindir_SQ)';
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 6/10 perf dlfilter: Add resolve_address() to perf_dlfilter_fns
In reply to Adrian HunterAdd a function, for use by dlfilters, to resolve addresses from branch stacks or callchains.
--- tools/perf/Documentation/perf-dlfilter.txt | 6 ++++- tools/perf/util/dlfilter.c | 29 ++++++++++++++++++++++ tools/perf/util/perf_dlfilter.h | 7 +++++- 3 files changed, 40 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-dlfilter.txt +5 -1
@@ -123,7 +123,8 @@ struct perf_dlfilter_fns { const struct perf_dlfilter_al *(*resolve_ip)(void *ctx); const struct perf_dlfilter_al *(*resolve_addr)(void *ctx); char **(*args)(void *ctx, int *dlargc); - void *(*reserved[125])(void *); + __s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al); + void *(*reserved[124])(void *); }; ----
@@ -133,6 +134,9 @@ struct perf_dlfilter_fns { 'args' returns arguments from --dlarg options. +'resolve_address' provides information about 'address'. al->size must be set +before calling. Returns 0 on success, -1 otherwise. + The perf_dlfilter_al structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/perf/util/dlfilter.c +29 -0
@@ -149,10 +149,39 @@ static char **dlfilter__args(void *ctx, int *dlargc)return d->dlargv; } +static __s32 dlfilter__resolve_address (void *ctx,__u64 address,struct perf_dlfilter_al *d_al_p) +{ +struct dlfilter *d = (struct dlfilter *)ctx; +struct perf_dlfilter_al d_al; +struct addr_location al; +struct thread *thread; +__u32 sz; + +if (!d->ctx_valid || !d_al_p) +return -1 ; + + thread =get_thread (d); +if (!thread) +return -1 ; + +thread__find_symbol_fb (thread, d->sample->cpumode, address, &al); + +al_to_d_al (&al, &d_al); + + d_al.is_kernel_ip =machine__kernel_ip (d->machine, address); + + sz = d_al_p->size; +memcpy (d_al_p, &d_al,min ((size_t )sz,sizeof (d_al))); + d_al_p->size = sz; + +return 0 ; +} +static const struct perf_dlfilter_fns perf_dlfilter_fns = { .resolve_ip = dlfilter__resolve_ip, .resolve_addr = dlfilter__resolve_addr, .args = dlfilter__args, + .resolve_address = dlfilter__resolve_address, };static char *find_dlfilter (const char *file)
tools/perf/util/perf_dlfilter.h +6 -1
@@ -92,8 +92,13 @@ struct perf_dlfilter_fns {const struct perf_dlfilter_al *(*resolve_addr)(void *ctx);/* Return arguments from --dlarg option */ char **(*args)(void *ctx,int *dlargc); +/* +* Return information about address (al->size must be set before +* calling). Returns 0 on success, -1 otherwise. +*/ +__s32 (*resolve_address)(void *ctx,__u64 address,struct perf_dlfilter_al *al);/* Reserved */ -void *(*reserved[125 ])(void *); +void *(*reserved[124 ])(void *); };/*
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 7/10 perf dlfilter: Add insn() to perf_dlfilter_fns
In reply to Adrian HunterAdd a function, for use by dlfilters, to return instruction bytes.
--- tools/perf/Documentation/perf-dlfilter.txt | 5 +++- tools/perf/util/dlfilter.c | 32 ++++++++++++++++++++++ tools/perf/util/perf_dlfilter.h | 4 ++- 3 files changed, 39 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-dlfilter.txt +4 -1
@@ -124,7 +124,8 @@ struct perf_dlfilter_fns { const struct perf_dlfilter_al *(*resolve_addr)(void *ctx); char **(*args)(void *ctx, int *dlargc); __s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al); - void *(*reserved[124])(void *); + const __u8 *(*insn)(void *ctx, __u32 *length); + void *(*reserved[123])(void *); }; ----
@@ -137,6 +138,8 @@ struct perf_dlfilter_fns { 'resolve_address' provides information about 'address'. al->size must be set before calling. Returns 0 on success, -1 otherwise. +'insn' returns instruction bytes and length. + The perf_dlfilter_al structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/perf/util/dlfilter.c +32 -0
@@ -17,6 +17,7 @@#include "dso.h" #include "map.h" #include "thread.h" +#include "trace-event.h" #include "symbol.h" #include "dlfilter.h" #include "perf_dlfilter.h"
@@ -177,11 +178,42 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlfreturn 0 ; } +static const __u8 *dlfilter__insn (void *ctx,__u32 *len) +{ +struct dlfilter *d = (struct dlfilter *)ctx; + +if (!len) +return NULL ; + + *len =0 ; + +if (!d->ctx_valid) +return NULL ; + +if (d->sample->ip && !d->sample->insn_len) { +struct addr_location *al = d->al; + +if (!al->thread &&machine__resolve (d->machine, al, d->sample) <0 ) +return NULL ; + +if (al->thread->maps && al->thread->maps->machine) +script_fetch_insn (d->sample, al->thread, al->thread->maps->machine); + } + +if (!d->sample->insn_len) +return NULL ; + + *len = d->sample->insn_len; + +return (__u8 *)d->sample->insn; +} +static const struct perf_dlfilter_fns perf_dlfilter_fns = { .resolve_ip = dlfilter__resolve_ip, .resolve_addr = dlfilter__resolve_addr, .args = dlfilter__args, .resolve_address = dlfilter__resolve_address, + .insn = dlfilter__insn, };static char *find_dlfilter (const char *file)
tools/perf/util/perf_dlfilter.h +3 -1
@@ -97,8 +97,10 @@ struct perf_dlfilter_fns {* calling). Returns 0 on success, -1 otherwise. */ __s32 (*resolve_address)(void *ctx,__u64 address,struct perf_dlfilter_al *al); +/* Return instruction bytes and length */ +const __u8 *(*insn)(void *ctx,__u32 *length);/* Reserved */ -void *(*reserved[124 ])(void *); +void *(*reserved[123 ])(void *); };/*
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 8/10 perf dlfilter: Add srcline() to perf_dlfilter_fns
In reply to Adrian HunterAdd a function, for use by dlfilters, to return source code file name and line number.
--- tools/perf/Documentation/perf-dlfilter.txt | 5 +++- tools/perf/util/dlfilter.c | 28 ++++++++++++++++++++++ tools/perf/util/perf_dlfilter.h | 4 +++- 3 files changed, 35 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-dlfilter.txt +4 -1
@@ -125,7 +125,8 @@ struct perf_dlfilter_fns { char **(*args)(void *ctx, int *dlargc); __s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al); const __u8 *(*insn)(void *ctx, __u32 *length); - void *(*reserved[123])(void *); + const char *(*srcline)(void *ctx, __u32 *line_number); + void *(*reserved[122])(void *); }; ----
@@ -140,6 +141,8 @@ before calling. Returns 0 on success, -1 otherwise. 'insn' returns instruction bytes and length. +'srcline' return source file name and line number. + The perf_dlfilter_al structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/perf/util/dlfilter.c +28 -0
@@ -19,6 +19,7 @@#include "thread.h" #include "trace-event.h" #include "symbol.h" +#include "srcline.h" #include "dlfilter.h" #include "perf_dlfilter.h"
@@ -208,12 +209,39 @@ static const __u8 *dlfilter__insn(void *ctx, __u32 *len)return (__u8 *)d->sample->insn; } +static const char *dlfilter__srcline (void *ctx,__u32 *line_no) +{ +struct dlfilter *d = (struct dlfilter *)ctx; +struct addr_location *al; +unsigned int line =0 ; +char *srcfile =NULL ; +struct map *map; +u64 addr; + +if (!d->ctx_valid || !line_no) +return NULL ; + + al =get_al (d); +if (!al) +return NULL ; + + map = al->map; + addr = al->addr; + +if (map && map->dso) + srcfile =get_srcline_split (map->dso,map__rip_2objdump (map, addr), &line); + + *line_no = line; +return srcfile; +} +static const struct perf_dlfilter_fns perf_dlfilter_fns = { .resolve_ip = dlfilter__resolve_ip, .resolve_addr = dlfilter__resolve_addr, .args = dlfilter__args, .resolve_address = dlfilter__resolve_address, .insn = dlfilter__insn, + .srcline = dlfilter__srcline, };static char *find_dlfilter (const char *file)
tools/perf/util/perf_dlfilter.h +3 -1
@@ -99,8 +99,10 @@ struct perf_dlfilter_fns {__s32 (*resolve_address)(void *ctx,__u64 address,struct perf_dlfilter_al *al);/* Return instruction bytes and length */ const __u8 *(*insn)(void *ctx,__u32 *length); +/* Return source file name and line number */ +const char *(*srcline)(void *ctx,__u32 *line_number);/* Reserved */ -void *(*reserved[123 ])(void *); +void *(*reserved[122 ])(void *); };/*
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 9/10 perf dlfilter: Add attr() to perf_dlfilter_fns
In reply to Adrian HunterAdd a function, for use by dlfilters, to return the perf_event_attr structure.
--- tools/perf/Documentation/perf-dlfilter.txt | 5 ++++- tools/perf/util/dlfilter.c | 11 +++++++++++ tools/perf/util/perf_dlfilter.h | 4 +++- 3 files changed, 18 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-dlfilter.txt +4 -1
@@ -126,7 +126,8 @@ struct perf_dlfilter_fns { __s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al); const __u8 *(*insn)(void *ctx, __u32 *length); const char *(*srcline)(void *ctx, __u32 *line_number); - void *(*reserved[122])(void *); + struct perf_event_attr *(*attr)(void *ctx); + void *(*reserved[121])(void *); }; ----
@@ -143,6 +144,8 @@ before calling. Returns 0 on success, -1 otherwise. 'srcline' return source file name and line number. +'attr' returns perf_event_attr, refer <linux/perf_event.h>. + The perf_dlfilter_al structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/perf/util/dlfilter.c +11 -0
@@ -235,6 +235,16 @@ static const char *dlfilter__srcline(void *ctx, __u32 *line_no)return srcfile; } +static struct perf_event_attr *dlfilter__attr (void *ctx) +{ +struct dlfilter *d = (struct dlfilter *)ctx; + +if (!d->ctx_valid) +return NULL ; + +return &d->evsel->core.attr; +} +static const struct perf_dlfilter_fns perf_dlfilter_fns = { .resolve_ip = dlfilter__resolve_ip, .resolve_addr = dlfilter__resolve_addr,
@@ -242,6 +252,7 @@ static const struct perf_dlfilter_fns perf_dlfilter_fns = { .resolve_address = dlfilter__resolve_address, .insn = dlfilter__insn, .srcline = dlfilter__srcline, + .attr = dlfilter__attr, };static char *find_dlfilter (const char *file)
tools/perf/util/perf_dlfilter.h +3 -1
@@ -101,8 +101,10 @@ struct perf_dlfilter_fns {const __u8 *(*insn)(void *ctx,__u32 *length);/* Return source file name and line number */ const char *(*srcline)(void *ctx,__u32 *line_number); +/* Return perf_event_attr, refer <linux/perf_event.h> */ +struct perf_event_attr *(*attr)(void *ctx);/* Reserved */ -void *(*reserved[122 ])(void *); +void *(*reserved[121 ])(void *); };/*
Signature · 1 line
2.17.1
Adrian Hunter
reviewerPATCH v2 10/10 perf dlfilter: Add object_code() to perf_dlfilter_fns
In reply to Adrian HunterAdd a function, for use by dlfilters, to read object code.
--- tools/perf/Documentation/perf-dlfilter.txt | 5 +++- tools/perf/util/dlfilter.c | 34 ++++++++++++++++++++++ tools/perf/util/perf_dlfilter.h | 4 ++- 3 files changed, 41 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-dlfilter.txt +4 -1
@@ -127,7 +127,8 @@ struct perf_dlfilter_fns { const __u8 *(*insn)(void *ctx, __u32 *length); const char *(*srcline)(void *ctx, __u32 *line_number); struct perf_event_attr *(*attr)(void *ctx); - void *(*reserved[121])(void *); + __s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len); + void *(*reserved[120])(void *); }; ----
@@ -146,6 +147,8 @@ before calling. Returns 0 on success, -1 otherwise. 'attr' returns perf_event_attr, refer <linux/perf_event.h>. +'object_code' reads object code and returns the number of bytes read. + The perf_dlfilter_al structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/perf/util/dlfilter.c +34 -0
@@ -245,6 +245,39 @@ static struct perf_event_attr *dlfilter__attr(void *ctx)return &d->evsel->core.attr; } +static __s32 dlfilter__object_code (void *ctx,__u64 ip,void *buf,__u32 len) +{ +struct dlfilter *d = (struct dlfilter *)ctx; +struct addr_location *al; +struct addr_location a; +struct map *map; +u64 offset; + +if (!d->ctx_valid) +return -1 ; + + al =get_al (d); +if (!al) +return -1 ; + + map = al->map; + +if (map && ip >= map->start && ip < map->end && +machine__kernel_ip (d->machine, ip) ==machine__kernel_ip (d->machine, d->sample->ip)) + goto have_map; + +thread__find_map_fb (al->thread, d->sample->cpumode, ip, &a); +if (!a.map) +return -1 ; + + map = a.map; +have_map: + offset = map->map_ip (map, ip); +if (ip + len >= map->end) + len = map->end - ip; +return dso__data_read_offset (map->dso, d->machine, offset, buf, len); +} +static const struct perf_dlfilter_fns perf_dlfilter_fns = { .resolve_ip = dlfilter__resolve_ip, .resolve_addr = dlfilter__resolve_addr,
@@ -253,6 +286,7 @@ static const struct perf_dlfilter_fns perf_dlfilter_fns = { .insn = dlfilter__insn, .srcline = dlfilter__srcline, .attr = dlfilter__attr, + .object_code = dlfilter__object_code, };static char *find_dlfilter (const char *file)
tools/perf/util/perf_dlfilter.h +3 -1
@@ -103,8 +103,10 @@ struct perf_dlfilter_fns {const char *(*srcline)(void *ctx,__u32 *line_number);/* Return perf_event_attr, refer <linux/perf_event.h> */ struct perf_event_attr *(*attr)(void *ctx); +/* Read object code, return numbers of bytes read */ +__s32 (*object_code)(void *ctx,__u64 ip,void *buf,__u32 len);/* Reserved */ -void *(*reserved[121 ])(void *); +void *(*reserved[120 ])(void *); };/*
Signature · 1 line
2.17.1
Quoted 6 lines · Adrian Hunter
Hi
In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
While I appreciate this for complex cases, in my experience filtering is usually just a simple expression. It would be nice to also have a way to do this reasonably fast without having to write a custom C file. Is the 10x-20x overhead just the python interpreter, or is it related to perf? Maybe we could have some kind of python fast path just for filters? Or maybe the alternative would be to have a frontend in perf that can automatically generate/compile such a C filter based on a simple expression, but I'm not sure if that would be much simpler.
-Andi
Adrian Hunter
reviewerQuoted 11 lines · Andi Kleen
Quoted 5 lines · Adrian Hunter
Hi In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
While I appreciate this for complex cases, in my experience filtering is usually just a simple expression. It would be nice to also have a way to do this reasonably fast without having to write a custom C
I do not agree that writing C filters is a hassle e.g. a minimal do-nothing filter is only a few lines:
#include <perf/perf_dlfilter.h>
int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
{
return 0;
}(Actually, the filter program does not have to have any LOC at all, but that is not much of an example)
Additionally, a script to do the build is fairly trivial e.g. I use this:
$ cat `which make-dlfilter.sh ` #!/bin/bash
set -ex
if test -z "${1}" ; then
echo "Name required"
exit 1
fi
name="${1%.c}"
if test "${name}" = "${1}" ; then
name="${1%.so}"fi
gcc -c -I ~/include -fpic "${name}.c"
gcc -shared -o "${name}.so" "${name}.o"
Quoted 2 lines
file. Is the 10x-20x overhead just the python interpreter, or is it
related to perf?
AFAICT the Python C API used to interface to Python performs fairly similarly to the Python interpreter.
Quoted 2 lines
Maybe we could have some kind of python fast path
just for filters?
I expect there are ways to make it more efficient, but I doubt it would ever come close to C.
Quoted 4 lines
just for filters? Or maybe the alternative would be to have a frontend in perf that can automatically generate/compile such a C filter based on a simple expression, but I'm not sure if that would be much simpler.
If gcc is available, perf script could, in fact, build the .so on the fly since the compile time is very quick.
Another point is that filters can be used for more than just filtering. Here is an example which sums cycles per-cpu and prints them, and the difference to the last print, at the beginning of each line. I think this was something you were interested in doing?
#include <perf/perf_dlfilter.h>
#include <stdio.h>
#define MAX_CPU 4096
__u64 cycles[MAX_CPU];__u64 cycles_rpt[MAX_CPU];
int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
{
__s32 cpu = sample->cpu;
if (cpu >=0 && cpu < MAX_CPU)
cycles[cpu] += sample->cyc_cnt;
return 0;
}int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
{
__s32 cpu = sample->cpu;
if (cpu >=0 && cpu < MAX_CPU) {
printf("%10llu %10llu ", cycles[cpu], cycles[cpu] - cycles_rpt[cpu]);
cycles_rpt[cpu] = cycles[cpu];
} else {
printf("%22s", "");
}
return 0;
}const char *filter_description(const char **long_description)
{
return "Print the number of cycles at the start of each line";
}Quoted 7 lines · Adrian Hunter
Quoted 6 lines · Andi Kleen
Quoted 5 lines · Adrian Hunter
Hi In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
Quoted 5 lines
Quoted 3 lines
While I appreciate this for complex cases, in my experience filtering is usually just a simple expression. It would be nice to also have a way to do this reasonably fast without having to write a custom C
I do not agree that writing C filters is a hassle e.g. a minimal do-nothing filter is only a few lines:
It still doesn't seem user friendly. Maybe it's obvious to you, but I suspect we left behind most of even the sophisticated perf users here.
Quoted 5 lines
Quoted 3 lines
Maybe we could have some kind of python fast path
just for filters?I expect there are ways to make it more efficient, but I doubt it would ever come close to C.
If it's within 2-3x I guess it would be ok. For any larger data files we should parallelize anyways, and that works fine with the --time x/y method (although it usually also needs some custom scripting, perhaps need to figure out how to make it more user friendly)
Quoted 5 lines
Quoted 5 lines
just for filters? Or maybe the alternative would be to have a frontend in perf that can automatically generate/compile such a C filter based on a simple expression, but I'm not sure if that would be much simpler.
Quoted 7 lines
If gcc is available, perf script could, in fact, build the .so on the fly since the compile time is very quick.
Another point is that filters can be used for more than just filtering. Here is an example which sums cycles per-cpu and prints them, and the difference to the last print, at the beginning of each line. I think this was something you were interested in doing?
Yes that's great and useful, but I would prefer to not maintain custom plugins for it. Often when I write a script it has to run in all kinds of weird environments that some random person installed, and it's not clear how portable building C will be there. And I doubt I can just copy the .so files around.
BTW I'm not arguing to not do the plugin (I can imagine extreme cases where such a plugin is the best option), but really for most of these things there should be easier and more portable alternatives, even if they are slightly slower.
-Andi
Adrian Hunter
reviewerQuoted 9 lines · Andi Kleen
Quoted 7 lines · Adrian Hunter
Quoted 6 lines · Andi Kleen
Quoted 5 lines · Adrian Hunter
Hi In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
While I appreciate this for complex cases, in my experience filtering is usually just a simple expression. It would be nice to also have a way to do this reasonably fast without having to write a custom C
I do not agree that writing C filters is a hassle e.g. a minimal do-nothing filter is only a few lines:
It still doesn't seem user friendly. Maybe it's obvious to you, but I suspect we left behind most of even the sophisticated perf users here.
Fair enough.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Maybe we could have some kind of python fast path just for filters?
I expect there are ways to make it more efficient, but I doubt it would ever come close to C.
If it's within 2-3x I guess it would be ok. For any larger data files we should parallelize anyways, and that works fine with the --time x/y method (although it usually also needs some custom scripting, perhaps need to figure out how to make it more user friendly)
I am not sure Python could do that, maybe something else.
Parallelization is on the list of things to do. Splitting by time is OK but gets trickier if you want to put the results back together in time order. Also call chains get broken at the splits in time.
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
just for filters? Or maybe the alternative would be to have a frontend in perf that can automatically generate/compile such a C filter based on a simple expression, but I'm not sure if that would be much simpler.
If gcc is available, perf script could, in fact, build the .so on the fly since the compile time is very quick.
Quoted 6 lines
Quoted 4 lines
Another point is that filters can be used for more than just filtering. Here is an example which sums cycles per-cpu and prints them, and the difference to the last print, at the beginning of each line. I think this was something you were interested in doing?
Yes that's great and useful, but I would prefer to not maintain custom plugins for it. Often when I write a script it has to run in all kinds of weird environments that some random person installed, and it's not clear how portable building C will be there. And I doubt I can just copy the .so files around.
That is true. .so files have limitations.
Quoted 2 lines
BTW I'm not arguing to not do the plugin (I can imagine extreme cases where such a plugin is the best option), but really for most of these things there should be easier and more portable alternatives, even if they are slightly slower.
Right. The documentation could definitely point out limitations and more user-friendly alternatives like using Python scripting.
Namhyung Kim
maintainerHi Adrian,
Quoted 7 lines · Adrian Hunter
Hi
In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
Thanks for your work! I guess we can use this for perf report (and others) to have a custom filter too.
Thanks,
Namhyung
Quoted 57 lines
This is V2.
The main patch is patch 1.
The other patches add more functionality, except for patch 5 which installs the C API header file.
Changes in V2:
perf script: Movefilter_cpu()earlier perf script: Move filtering before scripting perf script: Shareaddr_albetween functions
Dropped because they have now been applied.perf script: Add API for filtering via dynamically loaded shared object
Move 2 members of structperf_dlfilter_sample
Add 'ctx' as an argument to 'start' and 'stop'
Find dlfilter .so files in current directory or exec-path/dlfiltersperf script: Add option to list dlfilters
New patchperf script: Add option to pass arguments to dlfilters
New patchAdrian Hunter (10):
perf script: Add API for filtering via dynamically loaded shared object perf script: Adddlfilter__filter_event_early()
perf script: Add option to list dlfilters
perf script: Add option to pass arguments to dlfilters
perf build: Installperf_dlfilter.h
perf dlfilter: Addresolve_address()toperf_dlfilter_fnsperf dlfilter: Addinsn()toperf_dlfilter_fns
perf dlfilter: Addsrcline()toperf_dlfilter_fns
perf dlfilter: Addattr()toperf_dlfilter_fns
perf dlfilter: Addobject_code()toperf_dlfilter_fnstools/perf/Documentation/perf-dlfilter.txt | 251 ++++++++++++ tools/perf/Documentation/perf-script.txt | 15 +- tools/perf/Makefile.config | 3 + tools/perf/Makefile.perf | 4 +- tools/perf/builtin-script.c | 86 +++- tools/perf/util/Build | 1 + tools/perf/util/dlfilter.c | 615 +++++++++++++++++++++++++++++ tools/perf/util/dlfilter.h | 97 +++++ tools/perf/util/perf_dlfilter.h | 150 +++++++ 9 files changed, 1211 insertions(+), 11 deletions(-) create mode 100644 tools/perf/Documentation/perf-dlfilter.txt create mode 100644 tools/perf/util/dlfilter.c create mode 100644 tools/perf/util/dlfilter.h create mode 100644 tools/perf/util/perf_dlfilter.hRegards
Adrian
Em Sun, Jun 27, 2021 at 04:18:08PM +0300, Adrian Hunter escreveu:
Quoted 7 lines
Hi
In some cases, users want to filter very large amounts of data (e.g. from AUX area tracing like Intel PT) looking for something specific. While scripting such as Python can be used, Python is 10 to 20 times slower than C. So define a C API so that custom filters can be written and loaded.
Quoted 6 lines
This is V2.
The main patch is patch 1.
The other patches add more functionality, except for patch 5 which installs the C API header file.
Thanks! applied.
Please consider adding a 'perf test' entry to check if what is produced is what is expected, also to exercise this code so that we get some 'perf test' segfault if we break something it uses somehow.
- Arnaldo
Quoted 49 lines
Changes in V2:
perf script: Movefilter_cpu()earlier perf script: Move filtering before scripting perf script: Shareaddr_albetween functions
Dropped because they have now been applied.perf script: Add API for filtering via dynamically loaded shared object
Move 2 members of structperf_dlfilter_sample
Add 'ctx' as an argument to 'start' and 'stop'
Find dlfilter .so files in current directory or exec-path/dlfiltersperf script: Add option to list dlfilters
New patchperf script: Add option to pass arguments to dlfilters
New patchAdrian Hunter (10):
perf script: Add API for filtering via dynamically loaded shared object perf script: Adddlfilter__filter_event_early()
perf script: Add option to list dlfilters
perf script: Add option to pass arguments to dlfilters
perf build: Installperf_dlfilter.h
perf dlfilter: Addresolve_address()toperf_dlfilter_fnsperf dlfilter: Addinsn()toperf_dlfilter_fns
perf dlfilter: Addsrcline()toperf_dlfilter_fns
perf dlfilter: Addattr()toperf_dlfilter_fns
perf dlfilter: Addobject_code()toperf_dlfilter_fnstools/perf/Documentation/perf-dlfilter.txt | 251 ++++++++++++ tools/perf/Documentation/perf-script.txt | 15 +- tools/perf/Makefile.config | 3 + tools/perf/Makefile.perf | 4 +- tools/perf/builtin-script.c | 86 +++- tools/perf/util/Build | 1 + tools/perf/util/dlfilter.c | 615 +++++++++++++++++++++++++++++ tools/perf/util/dlfilter.h | 97 +++++ tools/perf/util/perf_dlfilter.h | 150 +++++++ 9 files changed, 1211 insertions(+), 11 deletions(-) create mode 100644 tools/perf/Documentation/perf-dlfilter.txt create mode 100644 tools/perf/util/dlfilter.c create mode 100644 tools/perf/util/dlfilter.h create mode 100644 tools/perf/util/perf_dlfilter.hRegards
Adrian
Signature · 1 line
- Arnaldo