perf tools: BPF related update and other improvements
Subsystems (3)
v6 cover perf tools: BPF related update and other improvements
- Use WARN_ONCE in patch 06/16 'perf tools: Prevent calling machine__delete() on non-allocated machine' and add machine__exit() back.
- Introduce perf_evsel__is_bpf_output() in 15/16 and use it in 16/16.
- Resend PowerPC building breakage fix.
He Kuang (1):
perf tools: Support perf event alias name
Naveen N. Rao (1):
perf: bpf: Fix build breakage due to libbpf
Wang Nan (14):
perf tools: Fix PowerPC native building
tools: Move Makefile.arch from perf/config to tools/scripts perf test: Fix false TEST_OK result for 'perf test hist' perf test: Reset err after using it hold errcode in hist testcases perf tools: Prevent calling machine__delete() on non-allocated machine perf test: Check environment before start real BPF test perf tools: Add API to config maps in bpf object
perf tools: Enable BPF object configure syntax
perf record: Apply config to BPF objects before recording perf tools: Enable passing event to BPF object
perf tools: Support setting different slots in a BPF map separately perf tools: Enable indices setting syntax for BPF maps perf tools: Introduce bpf-output event
perf data: Support converting data from bpf_perf_event_output()
tools/build/feature/test-bpf.c | 20 +-
tools/lib/bpf/Makefile | 9 +-
tools/lib/bpf/bpf.c | 4 +-
tools/perf/builtin-record.c | 11 +
tools/perf/config/Makefile | 2 +-
tools/perf/tests/bpf.c | 37 ++
tools/perf/tests/hists_common.c | 5 -
tools/perf/tests/hists_cumulate.c | 1 +
tools/perf/tests/hists_filter.c | 1 +
tools/perf/tests/hists_link.c | 1 +
tools/perf/tests/hists_output.c | 1 +
tools/perf/tests/make | 2 +-
tools/perf/tests/vmlinux-kallsyms.c | 4 +-
tools/perf/util/bpf-loader.c | 699 +++++++++++++++++++++++++++
tools/perf/util/bpf-loader.h | 59 +++
tools/perf/util/data-convert-bt.c | 112 ++++-
tools/perf/util/evlist.c | 16 +
tools/perf/util/evlist.h | 3 +
tools/perf/util/evsel.c | 6 +
tools/perf/util/evsel.h | 9 +
tools/perf/util/machine.c | 13 +-
tools/perf/util/machine.h | 3 +-
tools/perf/util/parse-events.c | 125 ++++-
tools/perf/util/parse-events.h | 20 +-
tools/perf/util/parse-events.l | 16 +-
tools/perf/util/parse-events.y | 123 ++++-
tools/{perf/config => scripts}/Makefile.arch | 0
27 files changed, 1267 insertions(+), 35 deletions(-)
rename tools/{perf/config => scripts}/Makefile.arch (100%)Signature · 1 line
1.8.3.4
PATCH v6 1/16 perf tools: Fix PowerPC native building
Checks BPF syscall number, turn off libbpf building on platform doesn't correctly support sys_bpf instead of blocking compiling.
--- tools/build/feature/test-bpf.c | 20 +++++++++++++++++++- tools/lib/bpf/bpf.c | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-)
tools/build/feature/test-bpf.c +19 -1
@@ -1,9 +1,23 @@ +#include <asm/unistd.h> #include <linux/bpf.h> +#include <unistd.h> + +#ifndef __NR_bpf +# if defined(__i386__) +# define __NR_bpf 357 +# elif defined(__x86_64__) +# define __NR_bpf 321 +# elif defined(__aarch64__) +# define __NR_bpf 280 +# error __NR_bpf not defined. libbpf does not support your arch. +# endif +#endif int main (void ) {union bpf_attr attr; +/* Check fields in attr */ attr.prog_type =BPF_PROG_TYPE_KPROBE ; attr.insn_cnt =0 ; attr.insns =0 ;
@@ -14,5 +28,9 @@ int main(void) attr.kern_version =0 ; attr = attr; -return 0 ; +/* +* Test existence of __NR_bpf and BPF_PROG_LOAD. +* This call should fail if we run the testcase. +*/ +return syscall (__NR_bpf,BPF_PROG_LOAD , attr,sizeof (attr)); }
tools/lib/bpf/bpf.c +2 -2
@@ -14,8 +14,8 @@#include "bpf.h" /* -* When building perf, unistd.h is override. Define __NR_bpf is -* required to be defined. +* When building perf, unistd.h is overrided. __NR_bpf is +* required to be defined explicitly. */ #ifndef __NR_bpf# if defined(__i386__)
Signature · 1 line
1.8.3.4
PATCH v6 2/16 tools: Move Makefile.arch from perf/config to tools/scripts
In reply to Wang NanAfter this patch other directories can use this architecture detector without directly including it from perf's directory. Libbpf would utilize it to get proper $(ARCH) so it can receive correct uapi include directory.
---
tools/perf/config/Makefile | 2 +-
tools/perf/tests/make | 2 +-
tools/{perf/config => scripts}/Makefile.arch | 0
3 files changed, 2 insertions(+), 2 deletions(-)
rename tools/{perf/config => scripts}/Makefile.arch (100%)tools/perf/config/Makefile +1 -1
@@ -17,7 +17,7 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected) CFLAGS := $(EXTRA_CFLAGS) $(EXTRA_WARNINGS) -include $(src-perf)/config/Makefile.arch +include $(srctree)/tools/scripts/Makefile.arch $(call detected_var,ARCH)
tools/perf/tests/make +1 -1
@@ -12,7 +12,7 @@ endif else PERF := . -include config/Makefile.arch +include $(srctree)/scripts/Makefile.arch # FIXME looks like x86 is the only arch running tests ;-) # we need some IS_(32/64) flag to make this generic
tools/scripts/Makefile.arch renamed from tools/perf/config/Makefile.arch +0 -0
Signature · 1 line
1.8.3.4
PATCH v6 3/16 perf: bpf: Fix build breakage due to libbpf
In reply to Wang NanFrom: "Naveen N. Rao" <[email protected]>
perf build is currently (v4.4-rc5) broken on powerpc:
bpf.c:28:4: error: #error __NR_bpf not defined. libbpf does not support your arch.
# error __NR_bpf not defined. libbpf does not support your arch.
^
Fix this by including tools/scripts/Makefile.arch for the proper $ARCH macro. While at it, remove redundant LP64 macro definition.
Also, since libbpf require $(srctree) now, detect the path of srctree like perf.
--- tools/lib/bpf/Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/lib/bpf/Makefile +8 -1
@@ -6,6 +6,12 @@ BPF_EXTRAVERSION = 1 MAKEFLAGS += --no-print-directory +ifeq ($(srctree),) +srctree := $(patsubst %/,%,$(dir $(shell pwd))) +srctree := $(patsubst %/,%,$(dir $(srctree))) +srctree := $(patsubst %/,%,$(dir $(srctree))) +#$(info Determined 'srctree' to be $(srctree)) +endif # Makefiles suck: This macro sets a default value of $(2) for the # variable named by $(1), unless the variable has been set by
@@ -31,7 +37,8 @@ INSTALL = install DESTDIR ?= DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' -LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1) +include $(srctree)/tools/scripts/Makefile.arch + ifeq ($(LP64), 1) libdir_relative = lib64 else
Signature · 1 line
1.8.3.4
PATCH v6 4/16 perf test: Fix false TEST_OK result for 'perf test hist'
In reply to Wang NanCommit 71d6de64feddd4b455555326fba2111b3006d9e0 ('perf test: Fix hist testcases when kptr_restrict is on') solves a double free problem when 'perf test hist' calling setup_fake_machine(). However, the result is still incorrect. For example:
$ ./perf test -v 'filtering hist entries' 25: Test filtering hist entries : --- start --- test child forked, pid 4186 Cannot create kernel maps test child finished with 0 ---- end ---- Test filtering hist entries: Ok
In this case the body of this test is not get executed at all, but the result is 'Ok'.
Actually, in setup_fake_machine() there's no need to create real kernel maps. What we want is the fake maps. This patch removes the machine__create_kernel_maps() in setup_fake_machine(), so it won't be affected by kptr_restrict setting.
Test result:
$ cat /proc/sys/kernel/kptr_restrict 1 $ ~/perf test -v hist 15: Test matching and linking multiple hists : --- start --- test child forked, pid 24031 test child finished with 0 ---- end ---- Test matching and linking multiple hists: Ok [SNIP]
--- tools/perf/tests/hists_common.c | 5 ----- 1 file changed, 5 deletions(-)
tools/perf/tests/hists_common.c +0 -5
@@ -87,11 +87,6 @@ struct machine *setup_fake_machine(struct machines *machines)return NULL ; } -if (machine__create_kernel_maps (machine)) { -pr_debug ("Cannot create kernel maps\n" ); -return NULL ; - } -for (i =0 ; i <ARRAY_SIZE (fake_threads); i++) {struct thread *thread;
Signature · 1 line
1.8.3.4
PATCH v6 5/16 perf test: Reset err after using it hold errcode in hist testcases
In reply to Wang NanAll hists test cases forget to reset err after using it to hold an error code. If error occure in setup_fake_machine() it incorrectly return TEST_OK.
This patch fixes it.
--- tools/perf/tests/hists_cumulate.c | 1 + tools/perf/tests/hists_filter.c | 1 + tools/perf/tests/hists_link.c | 1 + tools/perf/tests/hists_output.c | 1 + 4 files changed, 4 insertions(+)
tools/perf/tests/hists_cumulate.c +1 -0
@@ -706,6 +706,7 @@ int test__hists_cumulate(int subtest __maybe_unused) err =parse_events (evlist,"cpu-clock" ,NULL );if (err) goto out; + err =TEST_FAIL ;machines__init (&machines);
tools/perf/tests/hists_filter.c +1 -0
@@ -120,6 +120,7 @@ int test__hists_filter(int subtest __maybe_unused) err =parse_events (evlist,"task-clock" ,NULL );if (err) goto out; + err =TEST_FAIL ;/* default sort order (comm,dso,sym) will be used */ if (setup_sorting () <0 )
tools/perf/tests/hists_link.c +1 -0
@@ -293,6 +293,7 @@ int test__hists_link(int subtest __maybe_unused)if (err) goto out; + err =TEST_FAIL ;/* default sort order (comm,dso,sym) will be used */ if (setup_sorting () <0 ) goto out;
tools/perf/tests/hists_output.c +1 -0
@@ -597,6 +597,7 @@ int test__hists_output(int subtest __maybe_unused) err =parse_events (evlist,"cpu-clock" ,NULL );if (err) goto out; + err =TEST_FAIL ;machines__init (&machines);
Signature · 1 line
1.8.3.4
PATCH v6 6/16 perf tools: Prevent calling machine__delete() on non-allocated machine
In reply to Wang NanTo prevent futher commits calling machine__delete() on non-allocated 'struct machine' (which would cause memory corruption), this patch enforces machine__init(), record whether a machine structure is dynamically allocated or not, and warn if machine__delete() is called on incorrect object.
--- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/machine.c | 13 ++++++++----- tools/perf/util/machine.h | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-)
tools/perf/tests/vmlinux-kallsyms.c +2 -2
@@ -35,8 +35,8 @@ int test__vmlinux_matches_kallsyms(int subtest __maybe_unused)* Init the machines that will hold kernel, modules obtained from * both vmlinux + .ko files and from /proc/kallsyms split by modules. */ -machine__init (&kallsyms,"" ,HOST_KERNEL_ID ); -machine__init (&vmlinux,"" ,HOST_KERNEL_ID ); +machine__init (&kallsyms,"" ,HOST_KERNEL_ID , false); +machine__init (&vmlinux,"" ,HOST_KERNEL_ID , false);/* * Step 2:
tools/perf/util/machine.c +8 -5
@@ -1,3 +1,4 @@ +#include <asm/bug.h> #include "callchain.h" #include "debug.h" #include "event.h"
@@ -23,7 +24,7 @@ static void dsos__init(struct dsos *dsos)pthread_rwlock_init (&dsos->lock,NULL ); } -int machine__init (struct machine *machine,const char *root_dir,pid_t pid) +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated) {memset (machine,0 ,sizeof (*machine));map_groups__init (&machine->kmaps, machine);
@@ -65,6 +66,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) } machine->current_tid =NULL ; + machine->allocated = allocated;return 0 ; }
@@ -74,7 +76,7 @@ struct machine *machine__new_host(void)struct machine *machine =malloc (sizeof (*machine));if (machine !=NULL ) { -machine__init (machine,"" ,HOST_KERNEL_ID ); +machine__init (machine,"" ,HOST_KERNEL_ID , true);if (machine__create_kernel_maps (machine) <0 ) goto out_delete;
@@ -137,12 +139,13 @@ void machine__exit(struct machine *machine)void machine__delete (struct machine *machine) {machine__exit (machine); -free (machine); +WARN_ONCE ((machine->allocated ?free (machine),0 :-1 ), +"WARNING: deleting a non-allocated machine. Skip.\n" ); }void machines__init (struct machines *machines) { -machine__init (&machines->host,"" ,HOST_KERNEL_ID ); +machine__init (&machines->host,"" ,HOST_KERNEL_ID , false); machines->guests =RB_ROOT ; machines->symbol_filter =NULL ; }
@@ -163,7 +166,7 @@ struct machine *machines__add(struct machines *machines, pid_t pid,if (machine ==NULL )return NULL ; -if (machine__init (machine, root_dir, pid) !=0 ) { +if (machine__init (machine, root_dir, pid, true) !=0 ) {free (machine);return NULL ; }
tools/perf/util/machine.h +2 -1
@@ -28,6 +28,7 @@ struct machine {pid_t pid;u16 id_hdr_size;bool comm_exec; +bool allocated;char *root_dir;struct rb_root threads;pthread_rwlock_t threads_lock;
@@ -131,7 +132,7 @@ void machines__set_symbol_filter(struct machines *machines,void machines__set_comm_exec (struct machines *machines,bool comm_exec);struct machine *machine__new_host (void ); -int machine__init (struct machine *machine,const char *root_dir,pid_t pid); +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated);void machine__exit (struct machine *machine);void machine__delete_threads (struct machine *machine);void machine__delete (struct machine *machine);
Signature · 1 line
1.8.3.4
PATCH v6 7/16 perf test: Check environment before start real BPF test
In reply to Wang NanCopying perf to old kernel system results:
# perf test bpf 37: Test BPF filter : 37.1: Test basic BPF filtering : FAILED! 37.2: Test BPF prologue generation : Skip
However, in case when kernel doesn't support a test case it should return 'Skip', 'FAILED!' should be reserved for kernel tests for when the kernel supports a feature that then fails to work as advertised.
This patch checks environment before real testcase.
--- tools/perf/tests/bpf.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
tools/perf/tests/bpf.c +37 -0
@@ -1,7 +1,11 @@#include <stdio.h> #include <sys/epoll.h> +#include <util/util.h> #include <util/bpf-loader.h> #include <util/evlist.h> +#include <linux/bpf.h> +#include <linux/filter.h> +#include <bpf/bpf.h> #include "tests.h" #include "llvm.h" #include "debug.h"
@@ -227,6 +231,36 @@ const char *test__bpf_subtest_get_desc(int i)return bpf_testcase_table[i].desc; } +static int check_env (void ) +{ +int err; +unsigned int kver_int; +char license[] ="GPL" ; + +struct bpf_insn insns[] = { +BPF_MOV64_IMM (BPF_REG_0 ,1 ), +BPF_EXIT_INSN (), + }; + + err =fetch_kernel_version (&kver_int,NULL ,0 ); +if (err) { +pr_debug ("Unable to get kernel version\n" ); +return err; + } + + err =bpf_load_program (BPF_PROG_TYPE_KPROBE , insns, +sizeof (insns) /sizeof (insns[0 ]), + license, kver_int,NULL ,0 ); +if (err <0 ) { +pr_err ("Missing basic BPF support, skip this test: %s\n" , +strerror (errno)); +return err; + } +close (err); + +return 0 ; +} +int test__bpf (int i) {int err;
@@ -239,6 +273,9 @@ int test__bpf(int i)return TEST_SKIP ; } +if (check_env ()) +return TEST_SKIP ; + err =__test__bpf (i);return err; }
Signature · 1 line
1.8.3.4
PATCH v6 8/16 perf tools: Add API to config maps in bpf object
In reply to Wang Nanbpf__config_obj() is introduced as a core API to config BPF object after loading. One configuration option of maps is introduced. After this patch BPF object can accept configuration like:
maps:my_map.value=1234
(maps.my_map.value looks pretty. However, there's a small but hard to fixed problem related to flex's greedy matching. Please see [1]. Choose ':' to avoid it in a simpler way.)
This patch is more complex than the work it really does because the consideration of extension. In designing of BPF map configuration, following things should be considered:
1. Array indices selection: perf should allow user setting different value to different slots in an array, with syntax like:
maps:my_map.value[0,3...6]=1234;
2. A map can be config by different config terms, each for a part
of it. For example, set each slot to pid of a thread;
3. Type of value: integer is not the only valid value type. Perf
event can also be put into a map after commit 35578d7984003097af2b1e3
(bpf: Implement function bpf_perf_event_read() that get the selected
hardware PMU conuter);4. For hash table, it is possible to use string or other as key;
5. It is possible that map configuration is unable to be setup during parsing. Perf event is an example.
Therefore, this patch does following:
1. Instead of updating map element during parsing, this patch stores map config options in 'struct bpf_map_priv'. Following patches would apply those configs at proper time;
2. Link map operations to a list so a map can have multiple config
terms attached, so different parts can be configured separately;
3. Make 'struct bpf_map_priv' extensible so following patches can
add new types of keys and operations;
4. Use bpf_config_map_funcs array to support more maps config options.
Since the patch changing event parser to parse BPF object config is relative large, I put in another commit. Code in this patch could be tested after applying next patch.
[1] http://lkml.kernel.org/g/[email protected]
--- tools/perf/util/bpf-loader.c | 266 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 38 +++++++ 2 files changed, 304 insertions(+)
tools/perf/util/bpf-loader.c +266 -0
@@ -739,6 +739,251 @@ int bpf__foreach_tev(struct bpf_object *obj,return 0 ; } +enum bpf_map_op_type { +BPF_MAP_OP_SET_VALUE , +}; + +enum bpf_map_key_type { +BPF_MAP_KEY_ALL , +}; + +struct bpf_map_op { +struct list_head list; +enum bpf_map_op_type op_type; +enum bpf_map_key_type key_type; +union { +u64 value; + } v; +}; + +struct bpf_map_priv { +struct list_head ops_list; +}; + +static void +bpf_map_op__free (struct bpf_map_op *op) +{ +struct list_head *list = &op->list; +/* +* bpf_map_op__free() needs to consider following cases: +* 1. When the op is created but not linked to any list: +* impossible. This only happen in bpf_map_op__alloc() +* and it would be freed directly; +* 2. Normal case, when the op is linked to a list; +* 3. After the op has already be removed. +* Thanks to list.h, if it has removed by list_del() then +* list->{next,prev} should have been set to LIST_POISON{1,2}. +*/ +if ((list->next !=LIST_POISON1 ) && (list->prev !=LIST_POISON2 )) +list_del (list); +free (op); +} + +static void +bpf_map_priv__clear (struct bpf_map *map__maybe_unused , +void *_priv) +{ +struct bpf_map_priv *priv = _priv; +struct bpf_map_op *pos, *n; + +list_for_each_entry_safe (pos, n, &priv->ops_list, list) +bpf_map_op__free (pos); +free (priv); +} + +static struct bpf_map_op * +bpf_map_op__alloc (struct bpf_map *map) +{ +struct bpf_map_op *op; +struct bpf_map_priv *priv; +const char *map_name; +int err; + + map_name =bpf_map__get_name (map); + err =bpf_map__get_private (map, (void **)&priv); +if (err) { +pr_debug ("Failed to get private from map %s\n" , map_name); +return ERR_PTR (err); + } + +if (!priv) { + priv =zalloc (sizeof (*priv)); +if (!priv) { +pr_debug ("No enough memory to alloc map private\n" ); +return ERR_PTR (-ENOMEM ); + } +INIT_LIST_HEAD (&priv->ops_list); + +if (bpf_map__set_private (map, priv, bpf_map_priv__clear)) { +free (priv); +return ERR_PTR (-BPF_LOADER_ERRNO__INTERNAL ); + } + } + + op =zalloc (sizeof (*op)); +if (!op) { +pr_debug ("Failed to alloc bpf_map_op\n" ); +return ERR_PTR (-ENOMEM ); + } + + op->key_type =BPF_MAP_KEY_ALL ; +list_add_tail (&op->list, &priv->ops_list); +return op; +} + +static int +bpf__obj_config_map_array_value (struct bpf_map *map, +struct parse_events_term *term) +{ +struct bpf_map_def def; +struct bpf_map_op *op; +const char *map_name; +int err; + + map_name =bpf_map__get_name (map); + + err =bpf_map__get_def (map, &def); +if (err) { +pr_debug ("Unable to get map definition from '%s'\n" , + map_name); +return -BPF_LOADER_ERRNO__INTERNAL ; + } + +if (def.type !=BPF_MAP_TYPE_ARRAY ) { +pr_debug ("Map %s type is not BPF_MAP_TYPE_ARRAY\n" , + map_name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE ; + } +if (def.key_size <sizeof (unsigned int )) { +pr_debug ("Map %s has incorrect key size\n" , map_name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE ; + } +switch (def.value_size) { +case 1 : +case 2 : +case 4 : +case 8 : +break ; +default : +pr_debug ("Map %s has incorrect value size\n" , map_name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE ; + } + + op =bpf_map_op__alloc (map); +if (IS_ERR (op)) +return PTR_ERR (op); + op->op_type =BPF_MAP_OP_SET_VALUE ; + op->v.value = term->val.num; +return 0 ; +} + +static int +bpf__obj_config_map_value (struct bpf_map *map, +struct parse_events_term *term, +struct perf_evlist *evlist__maybe_unused ) +{ +if (!term->err_val) { +pr_debug ("Config value not set\n" ); +return -BPF_LOADER_ERRNO__OBJCONF_CONF ; + } + +if (term->type_val ==PARSE_EVENTS__TERM_TYPE_NUM ) +return bpf__obj_config_map_array_value (map, term); + +pr_debug ("ERROR: wrong value type\n" ); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE ; +} + +struct bpf_obj_config_map_func { +const char *config_opt; +int (*config_func)(struct bpf_map *,struct parse_events_term *, +struct perf_evlist *); +}; + +struct bpf_obj_config_map_func bpf_obj_config_map_funcs[] = { + {"value" , bpf__obj_config_map_value}, +}; + +static int +bpf__obj_config_map (struct bpf_object *obj, +struct parse_events_term *term, +struct perf_evlist *evlist, +int *key_scan_pos) +{ +/* key is "maps:<mapname>.<config opt>" */ +char *map_name =strdup (term->config +sizeof ("maps:" ) -1 ); +struct bpf_map *map; +int err = -BPF_LOADER_ERRNO__OBJCONF_OPT ; +char *map_opt; +size_t i; + +if (!map_name) +return -ENOMEM ; + + map_opt =strchr (map_name,'.' ); +if (!map_opt) { +pr_debug ("ERROR: Invalid map config: %s\n" , map_name); + goto out; + } + + *map_opt++ ='\0' ; +if (*map_opt =='\0' ) { +pr_debug ("ERROR: Invalid map option: %s\n" , term->config); + goto out; + } + + map =bpf_object__get_map_by_name (obj, map_name); +if (!map) { +pr_debug ("ERROR: Map %s is not exist\n" , map_name); + err = -BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST ; + goto out; + } + + *key_scan_pos += map_opt - map_name; +for (i =0 ; i <ARRAY_SIZE (bpf_obj_config_map_funcs); i++) { +struct bpf_obj_config_map_func *func = + &bpf_obj_config_map_funcs[i]; + +if (strcmp (map_opt, func->config_opt) ==0 ) { + err = func->config_func (map, term, evlist); + goto out; + } + } + +pr_debug ("ERROR: invalid config option '%s' for maps\n" , + map_opt); + err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT ; +out: +free (map_name); +if (!err) + key_scan_pos +=strlen (map_opt); +return err; +} + +int bpf__config_obj (struct bpf_object *obj, +struct parse_events_term *term, +struct perf_evlist *evlist, +int *error_pos) +{ +int key_scan_pos =0 ; +int err; + +if (!obj || !term || !term->config) +return -EINVAL ; + +if (!prefixcmp (term->config,"maps:" )) { + key_scan_pos =sizeof ("maps:" ) -1 ; + err =bpf__obj_config_map (obj, term, evlist, &key_scan_pos); + goto out; + } + err = -BPF_LOADER_ERRNO__OBJCONF_OPT ; +out: +if (error_pos) + *error_pos = key_scan_pos; +return err; + +} +#define ERRNO_OFFSET (e) ((e) - __BPF_LOADER_ERRNO__START)#define ERRCODE_OFFSET (c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)#define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
@@ -753,6 +998,14 @@ static const char *bpf_loader_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(PROLOGUE)] = "Failed to generate prologue", [ERRCODE_OFFSET(PROLOGUE2BIG)] = "Prologue too big for program", [ERRCODE_OFFSET(PROLOGUEOOB)] = "Offset out of bound for prologue", + [ERRCODE_OFFSET(OBJCONF_OPT)] = "Invalid object config option", + [ERRCODE_OFFSET(OBJCONF_CONF)] = "Config value not set (lost '=')", + [ERRCODE_OFFSET(OBJCONF_MAP_OPT)] = "Invalid object maps config option", + [ERRCODE_OFFSET(OBJCONF_MAP_NOTEXIST)] = "Target map not exist", + [ERRCODE_OFFSET(OBJCONF_MAP_VALUE)] = "Incorrect value type for map", + [ERRCODE_OFFSET(OBJCONF_MAP_TYPE)] = "Incorrect map type", + [ERRCODE_OFFSET(OBJCONF_MAP_KEYSIZE)] = "Incorrect map key size", + [ERRCODE_OFFSET(OBJCONF_MAP_VALUESIZE)] = "Incorrect map value size", }; static int
@@ -872,3 +1125,16 @@ int bpf__strerror_load(struct bpf_object *obj,bpf__strerror_end (buf, size);return 0 ; } + +int bpf__strerror_config_obj (struct bpf_object *obj__maybe_unused , +struct parse_events_term *term__maybe_unused , +struct perf_evlist *evlist__maybe_unused , +int *error_pos__maybe_unused ,int err, +char *buf,size_t size) +{ +bpf__strerror_head (err, buf, size); +bpf__strerror_entry (BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE , +"Can't use this config term to this type of map" ); +bpf__strerror_end (buf, size); +return 0 ; +}
tools/perf/util/bpf-loader.h +38 -0
@@ -10,6 +10,7 @@#include <string.h> #include <bpf/libbpf.h> #include "probe-event.h" +#include "evlist.h" #include "debug.h" enum bpf_loader_errno {
@@ -24,10 +25,19 @@ enum bpf_loader_errno {BPF_LOADER_ERRNO__PROLOGUE ,/* Failed to generate prologue */ BPF_LOADER_ERRNO__PROLOGUE2BIG ,/* Prologue too big for program */ BPF_LOADER_ERRNO__PROLOGUEOOB ,/* Offset out of bound for prologue */ +BPF_LOADER_ERRNO__OBJCONF_OPT ,/* Invalid object config option */ +BPF_LOADER_ERRNO__OBJCONF_CONF ,/* Config value not set (lost '=')) */ +BPF_LOADER_ERRNO__OBJCONF_MAP_OPT ,/* Invalid object maps config option */ +BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST ,/* Target map not exist */ +BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE ,/* Incorrect value type for map */ +BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE ,/* Incorrect map type */ +BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE ,/* Incorrect map key size */ +BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE ,/* Incorrect map value size */ __BPF_LOADER_ERRNO__END, };struct bpf_object ; +struct parse_events_term ;#define PERF_BPF_PROBE_GROUP "perf_bpf_probe" typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
@@ -53,6 +63,14 @@ int bpf__strerror_load(struct bpf_object *obj, int err, char *buf, size_t size);int bpf__foreach_tev (struct bpf_object *obj,bpf_prog_iter_callback_t func,void *arg); + +int bpf__config_obj (struct bpf_object *obj,struct parse_events_term *term, +struct perf_evlist *evlist,int *error_pos); +int bpf__strerror_config_obj (struct bpf_object *obj, +struct parse_events_term *term, +struct perf_evlist *evlist, +int *error_pos,int err,char *buf, +size_t size);#else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused,
@@ -84,6 +102,15 @@ bpf__foreach_tev(struct bpf_object *obj __maybe_unused, }static inline int +bpf__config_obj (struct bpf_object *obj__maybe_unused , +struct parse_events_term *term__maybe_unused , +struct perf_evlist *evlist__maybe_unused , +int *error_pos__maybe_unused ) +{ +return 0 ; +} + +static inline int __bpf_strerror (char *buf,size_t size) {if (!size)
@@ -118,5 +145,16 @@ static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, {return __bpf_strerror (buf, size); } + +static inline int +bpf__strerror_config_obj (struct bpf_object *obj__maybe_unused , +struct parse_events_term *term__maybe_unused , +struct perf_evlist *evlist__maybe_unused , +int *error_pos__maybe_unused , +int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
Signature · 1 line
1.8.3.4
PATCH v6 9/16 perf tools: Enable BPF object configure syntax
In reply to Wang NanThis patch adds the final step for BPF map configuration. A new syntax is appended into parser so user can config BPF objects through '/' '/' enclosed config terms.
After this patch, following syntax is available:
# perf record -e ./test_bpf_map_1.c/maps:channel.value=10/ ...
It would takes effect after appling following commits.
Test result:
# cat ./test_bpf_map_1.c
/************************ BEGIN **************************/
#include <uapi/linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
static void *(*map_lookup_elem)(struct bpf_map_def *, void *) =
(void *)BPF_FUNC_map_lookup_elem;
static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
(void *)BPF_FUNC_trace_printk;
struct bpf_map_def SEC("maps") channel = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 1,
};
SEC("func=sys_nanosleep")
int func(void *ctx)
{
int key = 0;
char fmt[] = "%d\n";
int *pval = map_lookup_elem(&channel, &key);
if (!pval)
return 0;
trace_printk(fmt, sizeof(fmt), *pval);
return 0;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
/************************* END ***************************/- Normal case:
# ./perf record -e './test_bpf_map_1.c/maps:channel.value=10/' usleep 10 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.012 MB perf.data ]
- Error case:
# ./perf record -e './test_bpf_map_1.c/maps:channel.value/' usleep 10
event syntax error: '..ps:channel:value/'
\___ Config value not set (lost '=')
Hint: Valid config term:
maps:[<arraymap>]:value=[value]
(add -v to see detail)
Run 'perf list' for a list of valid events Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
# ./perf record -e './test_bpf_map_1.c/xmaps:channel.value=10/' usleep 10
event syntax error: '..pf_map_1.c/xmaps:channel.value=10/'
\___ Invalid object config option
[SNIP] # ./perf record -e './test_bpf_map_1.c/maps:xchannel.value=10/' usleep 10
event syntax error: '..p_1.c/maps:xchannel.value=10/'
\___ Target map not exist
[SNIP] # ./perf record -e './test_bpf_map_1.c/maps:channel.xvalue=10/' usleep 10
event syntax error: '..ps:channel.xvalue=10/'
\___ Invalid object maps config option
[SNIP] # ./perf record -e './test_bpf_map_1.c/maps:channel.value=x10/' usleep 10
event syntax error: '..nnel.value=x10/'
\___ Incorrect value type for map
[SNIP] Change BPF_MAP_TYPE_ARRAY to '1':
# ./perf record -e './test_bpf_map_1.c/maps:channel.value=10/' usleep 10
event syntax error: '..ps:channel.value=10/'
\___ Can't use this config term to this type of map Hint: Valid config term:
maps:[<arraymap>].value=[value]
(add -v to see detail)
--- tools/perf/util/parse-events.c | 56 +++++++++++++++++++++++++++++++++++++++--- tools/perf/util/parse-events.h | 3 ++- tools/perf/util/parse-events.l | 2 +- tools/perf/util/parse-events.y | 23 ++++++++++++++--- 4 files changed, 75 insertions(+), 9 deletions(-)
tools/perf/util/parse-events.c +53 -3
@@ -628,17 +628,64 @@ errout:return err; } +static int +parse_events_config_bpf (struct parse_events_evlist *data, +struct bpf_object *obj, +struct list_head *head_config) +{ +struct parse_events_term *term; +int error_pos; + +if (!head_config ||list_empty (head_config)) +return 0 ; + +list_for_each_entry (term, head_config, list) { +char errbuf[BUFSIZ ]; +int err; + +if (term->type_term !=PARSE_EVENTS__TERM_TYPE_USER ) { +snprintf (errbuf,sizeof (errbuf), +"Invalid config term for BPF object" ); + errbuf[BUFSIZ -1 ] ='\0' ; + + data->error->idx = term->err_term; + data->error->str =strdup (errbuf); +return -EINVAL ; + } + + err =bpf__config_obj (obj, term,NULL , &error_pos); +if (err) { +bpf__strerror_config_obj (obj, term,NULL , + &error_pos, err, errbuf, +sizeof (errbuf)); + data->error->help =strdup ( +"Hint:\tValid config term:\n" +" \tmaps:[<arraymap>].value=[value]\n" +" \t(add -v to see detail)" ); + data->error->str =strdup (errbuf); +if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE ) + data->error->idx = term->err_val; +else + data->error->idx = term->err_term + error_pos; +return err; + } + } +return 0 ; + +} +int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list,char *bpf_file_name, -bool source) +bool source, +struct list_head *head_config) {struct bpf_object *obj; +int err; obj =bpf__prepare_load (bpf_file_name, source);if (IS_ERR (obj)) {char errbuf[BUFSIZ ]; -int err; err =PTR_ERR (obj);
@@ -656,7 +703,10 @@ int parse_events_load_bpf(struct parse_events_evlist *data,return err; } -return parse_events_load_bpf_obj (data, list, obj); + err =parse_events_load_bpf_obj (data, list, obj); +if (err) +return err; +return parse_events_config_bpf (data, obj, head_config); } static int
tools/perf/util/parse-events.h +2 -1
@@ -126,7 +126,8 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx,int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list,char *bpf_file_name, -bool source); +bool source, +struct list_head *head_config);/* Provide this function for perf test */ struct bpf_object ; int parse_events_load_bpf_obj(struct parse_events_evlist *data,
tools/perf/util/parse-events.l +1 -1
@@ -122,7 +122,7 @@ num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+ num_raw_hex [a-fA-F0-9]+ name [a-zA-Z_*?][a-zA-Z0-9_*?.]* -name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.]* +name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]* /* If you add a modifier you need to update check_modifier() */ modifier_event [ukhpPGHSDI]+ modifier_bp [rwx]{1,3}
tools/perf/util/parse-events.y +19 -4
@@ -64,6 +64,7 @@ static inc_group_count(struct list_head *list, %type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT %type <num> value_sym %type <head> event_config +%type <head> event_bpf_config %type <term> event_term %type <head> event_pmu %type <head> event_legacy_symbol
@@ -455,27 +456,41 @@ PE_RAW } event_bpf_file: -PE_BPF_OBJECT +PE_BPF_OBJECT event_bpf_config { struct parse_events_evlist *data = _data; struct parse_events_error *error = data->error; struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1, false)); + ABORT_ON(parse_events_load_bpf(data, list, $1, false, $2)); + if ($2) + parse_events__free_terms($2); $$ = list; } | -PE_BPF_SOURCE +PE_BPF_SOURCE event_bpf_config { struct parse_events_evlist *data = _data; struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1, true)); + ABORT_ON(parse_events_load_bpf(data, list, $1, true, $2)); + if ($2) + parse_events__free_terms($2); $$ = list; } +event_bpf_config: +'/' event_config '/' +{ + $$ = $2; +} +| +{ + $$ = NULL; +} + start_terms: event_config { struct parse_events_terms *data = _data;
Signature · 1 line
1.8.3.4
PATCH v6 10/16 perf record: Apply config to BPF objects before recording
In reply to Wang Nanbpf__apply_obj_config() is introduced as the core API to apply object config options to all BPF objects. This patch also does the real work for setting values for BPF_MAP_TYPE_PERF_ARRAY maps by inserting value stored in map's private field into the BPF map.
This patch is required because we are not always able to set all BPF config during parsing. Further patch will set events created by perf to BPF_MAP_TYPE_PERF_EVENT_ARRAY maps, which is not exist until perf_evsel__open().
bpf_map_foreach_key() is introduced to iterate over each key needs to be configured. This function would be extended to support more map types and different key settings.
In perf record, before start recording, call bpf__apply_config() to turn on all BPF config options.
Test result:
# cat ./test_bpf_map_1.c
/************************ BEGIN **************************/
#include <uapi/linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
static void *(*map_lookup_elem)(struct bpf_map_def *, void *) =
(void *)BPF_FUNC_map_lookup_elem;
static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
(void *)BPF_FUNC_trace_printk;
struct bpf_map_def SEC("maps") channel = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 1,
};
SEC("func=sys_nanosleep")
int func(void *ctx)
{
int key = 0;
char fmt[] = "%d\n";
int *pval = map_lookup_elem(&channel, &key);
if (!pval)
return 0;
trace_printk(fmt, sizeof(fmt), *pval);
return 0;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
/************************* END ***************************/ # echo "" > /sys/kernel/debug/tracing/trace
# ./perf record -e './test_bpf_map_1.c/maps:channel.value=11/' usleep 10
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data ]
# cat /sys/kernel/debug/tracing/trace
# tracer: nop
#
# entries-in-buffer/entries-written: 1/1 #P:8
[SNIP]
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |
usleep-18593 [007] d... 2394714.395539: : 11
# ./perf record -e './test_bpf_map.c/maps:channel.value=101/' usleep 10
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data ]
# cat /sys/kernel/debug/tracing/trace
# tracer: nop
#
# entries-in-buffer/entries-written: 1/1 #P:8
[SNIP]
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |
usleep-18593 [007] d... 2394714.395539: : 11
usleep-19000 [006] d... 2394831.057840: : 101--- tools/perf/builtin-record.c | 11 +++ tools/perf/util/bpf-loader.c | 180 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 15 ++++ 3 files changed, 206 insertions(+)
tools/perf/builtin-record.c +11 -0
@@ -32,6 +32,7 @@#include "util/parse-branch-options.h" #include "util/parse-regs-options.h" #include "util/llvm-utils.h" +#include "util/bpf-loader.h" #include <unistd.h> #include <sched.h>
@@ -524,6 +525,16 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) goto out_child; } + err =bpf__apply_obj_config (); +if (err) { +char errbuf[BUFSIZ ]; + +bpf__strerror_apply_obj_config (err, errbuf,sizeof (errbuf)); +pr_err ("ERROR: Apply config to BPF failed: %s\n" , + errbuf); + goto out_child; + } +/* * Normally perf_session__new would do this, but it doesn't have the * evlist.
tools/perf/util/bpf-loader.c +180 -0
195 lines not on this page · raw message
tools/perf/util/bpf-loader.h +15 -0
@@ -71,6 +71,8 @@ int bpf__strerror_config_obj(struct bpf_object *obj,struct perf_evlist *evlist, int *error_pos, int err, char *buf, size_t size); +int bpf__apply_obj_config (void ); +int bpf__strerror_apply_obj_config (int err,char *buf,size_t size);#else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused,
@@ -111,6 +113,12 @@ bpf__config_obj(struct bpf_object *obj __maybe_unused, }static inline int +bpf__apply_obj_config (void ) +{ +return 0 ; +} + +static inline int __bpf_strerror (char *buf,size_t size) {if (!size)
@@ -156,5 +164,12 @@ bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, {return __bpf_strerror (buf, size); } + +static inline int +bpf__strerror_apply_obj_config (int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
Signature · 1 line
1.8.3.4
PATCH v6 11/16 perf tools: Enable passing event to BPF object
In reply to Wang NanA new syntax is appended into parser so user can pass predefined perf events into BPF objects.
After this patch, BPF programs for perf are finally able to utilize bpf_perf_event_read() introduced in commit 35578d7984003097af2b1e3 (bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter).
Test result:
# cat ./test_bpf_map_2.c
/************************ BEGIN **************************/
#include <uapi/linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
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_read)(struct bpf_map_def *, int) =
(void *)BPF_FUNC_perf_event_read; struct bpf_map_def SEC("maps") pmu_map = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = __NR_CPUS__,
};
SEC("func_write=sys_write")
int func_write(void *ctx)
{
unsigned long long val;
char fmt[] = "sys_write: pmu=%llu\n";
val = perf_event_read(&pmu_map, get_smp_processor_id());
trace_printk(fmt, sizeof(fmt), val);
return 0;}
SEC("func_write_return=sys_write%return")
int func_write_return(void *ctx)
{
unsigned long long val = 0;
char fmt[] = "sys_write_return: pmu=%llu\n";
val = perf_event_read(&pmu_map, get_smp_processor_id());
trace_printk(fmt, sizeof(fmt), val);
return 0; }
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE; /************************* END ***************************/
Normal case:
# echo "" > /sys/kernel/debug/tracing/trace
# ./perf record -i -e cycles -e './test_bpf_map_2.c/maps:pmu_map.event=cycles/' ls /
[SNIP]
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data (7 samples) ]
# cat /sys/kernel/debug/tracing/trace | grep ls
ls-17066 [000] d... 938449.863301: : sys_write: pmu=1157327
ls-17066 [000] dN.. 938449.863342: : sys_write_return: pmu=1225218
ls-17066 [000] d... 938449.863349: : sys_write: pmu=1241922
ls-17066 [000] dN.. 938449.863369: : sys_write_return: pmu=1267445Normal case (system wide):
# echo "" > /sys/kernel/debug/tracing/trace # ./perf record -i -e cycles -e './test_bpf_map_2.c/maps:pmu_map.event=cycles/' -a ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.811 MB perf.data (120 samples) ]
# cat /sys/kernel/debug/tracing/trace | grep -v '18446744073709551594' | grep -v perf | head -n 20
[SNIP]
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |
gmain-30828 [002] d... 2740551.068992: : sys_write: pmu=84373
gmain-30828 [002] d... 2740551.068992: : sys_write_return: pmu=87696
gmain-30828 [002] d... 2740551.068996: : sys_write: pmu=100658
gmain-30828 [002] d... 2740551.068997: : sys_write_return: pmu=102572Error case 1:
# ./perf record -e './test_bpf_map_2.c' ls /
[SNIP]
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.014 MB perf.data ]
# cat /sys/kernel/debug/tracing/trace | grep ls
ls-17115 [007] d... 2724279.665625: : sys_write: pmu=18446744073709551614
ls-17115 [007] dN.. 2724279.665651: : sys_write_return: pmu=18446744073709551614
ls-17115 [007] d... 2724279.665658: : sys_write: pmu=18446744073709551614
ls-17115 [007] dN.. 2724279.665677: : sys_write_return: pmu=18446744073709551614 (18446744073709551614 is 0xfffffffffffffffe (-2))
Error case 2:
# ./perf record -e cycles -e './test_bpf_map_2.c/maps:pmu_map.event=evt/' -a
event syntax error: '..ps:pmu_map.event=evt/'
\___ Event not found for map settingHint: Valid config terms:
maps:[<arraymap>].value=[value]
maps:[<eventmap>].event=[event][SNIP]
Error case 3:
# ls /proc/2348/task/ 2348 2505 2506 2507 2508 # ./perf record -i -e cycles -e './test_bpf_map_2.c/maps:pmu_map.event=cycles/' -p 2348 ERROR: Apply config to BPF failed: Cannot set event to BPF maps in multi-thread tracing
Error case 4:
# ./perf record -e cycles -e './test_bpf_map_2.c/maps:pmu_map.event=cycles/' ls / ERROR: Apply config to BPF failed: Doesn't support inherit event (Hint: use -i to turn off inherit)
Error case 5:
# ./perf record -i -e raw_syscalls:sys_enter -e './test_bpf_map_2.c/maps:pmu_map.event=raw_syscalls:sys_enter/' ls ERROR: Apply config to BPF failed: Can only put raw, hardware and BPF output event into a BPF map
--- tools/perf/util/bpf-loader.c | 138 ++++++++++++++++++++++++++++++++++++++++- tools/perf/util/bpf-loader.h | 5 ++ tools/perf/util/evlist.c | 16 +++++ tools/perf/util/evlist.h | 3 + tools/perf/util/parse-events.c | 15 +++-- tools/perf/util/parse-events.h | 1 + 6 files changed, 171 insertions(+), 7 deletions(-)
tools/perf/util/bpf-loader.c +137 -1
@@ -742,6 +742,7 @@ int bpf__foreach_tev(struct bpf_object *obj,enum bpf_map_op_type {BPF_MAP_OP_SET_VALUE , +BPF_MAP_OP_SET_EVSEL , };enum bpf_map_key_type {
@@ -754,6 +755,7 @@ struct bpf_map_op {enum bpf_map_key_type key_type;union {u64 value; +struct perf_evsel *evsel; } v; };
@@ -891,10 +893,73 @@ bpf__obj_config_map_value(struct bpf_map *map,if (term->type_val ==PARSE_EVENTS__TERM_TYPE_NUM )return bpf__obj_config_map_array_value (map, term); -pr_debug ("ERROR: wrong value type\n" ); +pr_debug ("ERROR: wrong value type for 'value'\n" );return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE ; } +static int +bpf__obj_config_map_array_event (struct bpf_map *map, +struct parse_events_term *term, +struct perf_evlist *evlist) +{ +struct perf_evsel *evsel; +struct bpf_map_def def; +struct bpf_map_op *op; +const char *map_name; +int err; + + map_name =bpf_map__get_name (map); + evsel =perf_evlist__find_evsel_by_str (evlist, term->val.str); +if (!evsel) { +pr_debug ("Event (for '%s') '%s' doesn't exist\n" , + map_name, term->val.str); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT ; + } + + err =bpf_map__get_def (map, &def); +if (err) { +pr_debug ("Unable to get map definition from '%s'\n" , + map_name); +return err; + } + +/* +* No need to check key_size and value_size: +* kernel has already checked them. +*/ +if (def.type !=BPF_MAP_TYPE_PERF_EVENT_ARRAY ) { +pr_debug ("Map %s type is not BPF_MAP_TYPE_PERF_EVENT_ARRAY\n" , + map_name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE ; + } + + op =bpf_map_op__alloc (map); +if (IS_ERR (op)) +return PTR_ERR (op); + + op->v.evsel = evsel; + op->op_type =BPF_MAP_OP_SET_EVSEL ; +return 0 ; +} + +static int +bpf__obj_config_map_event (struct bpf_map *map, +struct parse_events_term *term, +struct perf_evlist *evlist) +{ +if (!term->err_val) { +pr_debug ("Config value not set\n" ); +return -BPF_LOADER_ERRNO__OBJCONF_CONF ; + } + +if (term->type_val ==PARSE_EVENTS__TERM_TYPE_STR ) +return bpf__obj_config_map_array_event (map, term, evlist); + +pr_debug ("ERROR: wrong value type for 'event'\n" ); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE ; +} + +struct bpf_obj_config_map_func {const char *config_opt; int (*config_func)(struct bpf_map *, struct parse_events_term *,
@@ -903,6 +968,7 @@ struct bpf_obj_config_map_func {struct bpf_obj_config_map_func bpf_obj_config_map_funcs[] = { {"value" , bpf__obj_config_map_value}, + {"event" , bpf__obj_config_map_event}, }; static int
@@ -1047,6 +1113,7 @@ bpf_map_config_foreach_key(struct bpf_map *map,list_for_each_entry (op, &priv->ops_list, list) {switch (def.type) {case BPF_MAP_TYPE_ARRAY : +case BPF_MAP_TYPE_PERF_EVENT_ARRAY :switch (op->key_type) {case BPF_MAP_KEY_ALL : return foreach_key_array_all(func, arg, name,
@@ -1101,6 +1168,60 @@ apply_config_value_for_key(int map_fd, void *pkey, }static int +apply_config_evsel_for_key (const char *name,int map_fd,void *pkey, +struct perf_evsel *evsel) +{ +struct xyarray *xy = evsel->fd; +struct perf_event_attr *attr; +unsigned int key, events; +bool check_pass = false; +int *evt_fd; +int err; + +if (!xy) { +pr_debug ("ERROR: evsel not ready for map %s\n" , name); +return -BPF_LOADER_ERRNO__INTERNAL ; + } + +if (xy->row_size / xy->entry_size !=1 ) { +pr_debug ("ERROR: Dimension of target event is incorrect for map %s\n" , + name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM ; + } + + attr = &evsel->attr; +if (attr->inherit) { +pr_debug ("ERROR: Can't put inherit event into map %s\n" , name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH ; + } + +if (attr->type ==PERF_TYPE_RAW ) + check_pass = true; +if (attr->type ==PERF_TYPE_HARDWARE ) + check_pass = true; +if (attr->type ==PERF_TYPE_SOFTWARE && + attr->config ==PERF_COUNT_SW_BPF_OUTPUT ) + check_pass = true; +if (!check_pass) { +pr_debug ("ERROR: Event type is wrong for map %s\n" , name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE ; + } + + events = xy->entries / (xy->row_size / xy->entry_size); + key = *((unsigned int *)pkey); +if (key >= events) { +pr_debug ("ERROR: there is no event %d for map %s\n" , + key, name); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE ; + } + evt_fd =xyarray__entry (xy, key,0 ); + err =bpf_map_update_elem (map_fd, pkey, evt_fd,BPF_ANY ); +if (err && errno) + err = -errno; +return err; +} + +static int apply_obj_config_map_for_key(const char *name, int map_fd, struct bpf_map_def *pdef __maybe_unused, struct bpf_map_op *op,
@@ -1114,6 +1235,10 @@ apply_obj_config_map_for_key(const char *name, int map_fd, pdef->value_size, op->v.value);break ; +case BPF_MAP_OP_SET_EVSEL : + err =apply_config_evsel_for_key (name, map_fd, pkey, + op->v.evsel); +break ;default :pr_debug ("ERROR: unknown value type for '%s'\n" , name); err = -BPF_LOADER_ERRNO__INTERNAL ;
@@ -1179,6 +1304,11 @@ static const char *bpf_loader_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(OBJCONF_MAP_TYPE)] = "Incorrect map type", [ERRCODE_OFFSET(OBJCONF_MAP_KEYSIZE)] = "Incorrect map key size", [ERRCODE_OFFSET(OBJCONF_MAP_VALUESIZE)] = "Incorrect map value size", + [ERRCODE_OFFSET(OBJCONF_MAP_NOEVT)] = "Event not found for map setting", + [ERRCODE_OFFSET(OBJCONF_MAP_MAPSIZE)] = "Invalid map size for event setting", + [ERRCODE_OFFSET(OBJCONF_MAP_EVTDIM)] = "Event dimension too large", + [ERRCODE_OFFSET(OBJCONF_MAP_EVTINH)] = "Doesn't support inherit event", + [ERRCODE_OFFSET(OBJCONF_MAP_EVTTYPE)] = "Wrong event type for map", }; static int
@@ -1315,6 +1445,12 @@ int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,int bpf__strerror_apply_obj_config (int err,char *buf,size_t size) {bpf__strerror_head (err, buf, size); +bpf__strerror_entry (BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM , +"Cannot set event to BPF maps in multi-thread tracing" ); +bpf__strerror_entry (BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH , +"%s (Hint: use -i to turn off inherit)" , emsg); +bpf__strerror_entry (BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE , +"Can only put raw, hardware and BPF output event into a BPF map" );bpf__strerror_end (buf, size);return 0 ; }
tools/perf/util/bpf-loader.h +5 -0
@@ -33,6 +33,11 @@ enum bpf_loader_errno {BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE ,/* Incorrect map type */ BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE ,/* Incorrect map key size */ BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE ,/* Incorrect map value size */ +BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT ,/* Event not found for map setting */ +BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE ,/* Invalid map size for event setting */ +BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM ,/* Event dimension too large */ +BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH ,/* Doesn't support inherit event */ +BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE ,/* Wrong event type for map */ __BPF_LOADER_ERRNO__END, };
tools/perf/util/evlist.c +16 -0
@@ -1737,3 +1737,19 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, tracking_evsel->tracking = true; } + +struct perf_evsel * +perf_evlist__find_evsel_by_str (struct perf_evlist *evlist, +const char *str) +{ +struct perf_evsel *evsel; + +evlist__for_each (evlist, evsel) { +if (!evsel->name) +continue ; +if (strcmp (str, evsel->name) ==0 ) +return evsel; + } + +return NULL ; +}
tools/perf/util/evlist.h +3 -0
@@ -292,4 +292,7 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, struct perf_evsel *tracking_evsel);void perf_event_attr__set_max_precise_ip (struct perf_event_attr *attr); + +struct perf_evsel * +perf_evlist__find_evsel_by_str (struct perf_evlist *evlist,const char *str);#endif /* __PERF_EVLIST_H */
tools/perf/util/parse-events.c +9 -6
@@ -653,14 +653,16 @@ parse_events_config_bpf(struct parse_events_evlist *data,return -EINVAL ; } - err =bpf__config_obj (obj, term,NULL , &error_pos); + err =bpf__config_obj (obj, term, data->evlist, &error_pos);if (err) { -bpf__strerror_config_obj (obj, term,NULL , +bpf__strerror_config_obj (obj, term, data->evlist, &error_pos, err, errbuf,sizeof (errbuf)); data->error->help =strdup ( -"Hint:\tValid config term:\n" +"Hint:\tValid config terms:\n" " \tmaps:[<arraymap>].value=[value]\n" +" \tmaps:[<eventmap>].event=[event]\n" +"\n" " \t(add -v to see detail)" ); data->error->str =strdup (errbuf);if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE )
@@ -1442,9 +1444,10 @@ int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *err) {struct parse_events_evlist data = { - .list =LIST_HEAD_INIT (data.list), - .idx = evlist->nr_entries, - .error = err, + .list =LIST_HEAD_INIT (data.list), + .idx = evlist->nr_entries, + .error = err, + .evlist = evlist, };int ret;
tools/perf/util/parse-events.h +1 -0
@@ -98,6 +98,7 @@ struct parse_events_evlist {int idx;int nr_groups;struct parse_events_error *error; +struct perf_evlist *evlist; };struct parse_events_terms {
Signature · 1 line
1.8.3.4
PATCH v6 12/16 perf tools: Support perf event alias name
In reply to Wang NanFrom: He Kuang <[email protected]>
This patch is useful when trying to pass a perf event to BPF map. Before this patch we are unable to pass an event with config term to BPF maps. For example:
# perf record -a -e cycles/no-inherit,period=0x7fffffffffffffff/ \
-e './test_bpf_map_2.c/maps:pmu_map.event=cycles/no-inherit,period=0x7fffffffffffffff//' ls /
event syntax error: '..ps:pmu_map.event=cycles/'
\___ Event not found for map settingBecause those '/' and ',' embarrass parser.
This patch adds new bison rules for specifying an alias name to a perf event, which allows cmdline refer to previous defined perf event through its name. With this patch user can give alias name to a perf event using following cmdline. The above goal can be achieved using:
# perf record -a -e cyc=cycles/no-inherit,period=0x7fffffffffffffff/ \
-e './test_bpf_map_2.c/maps:pmu_map.event=cyc/' ls /If alias is not provided (normal case):
# perf record -e cycles ...
It will be set to event's name automatically ('cycles' in the above example).
To allow parser refer to existing event selector, pass event list to 'struct parse_events_evlist'. perf_evlist__find_evsel_by_alias() is introduced to get evsel through its alias.
Test result:
# cat ./test_bpf_map_2.c
/************************ BEGIN **************************/
#include <uapi/linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
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_read)(struct bpf_map_def *, int) =
(void *)BPF_FUNC_perf_event_read; struct bpf_map_def SEC("maps") pmu_map = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = __NR_CPUS__,
};
SEC("func_write=sys_write")
int func_write(void *ctx)
{
unsigned long long val;
char fmt[] = "sys_write: pmu=%llu\n";
val = perf_event_read(&pmu_map, get_smp_processor_id());
trace_printk(fmt, sizeof(fmt), val);
return 0;}
SEC("func_write_return=sys_write%return")
int func_write_return(void *ctx)
{
unsigned long long val = 0;
char fmt[] = "sys_write_return: pmu=%llu\n";
val = perf_event_read(&pmu_map, get_smp_processor_id());
trace_printk(fmt, sizeof(fmt), val);
return 0;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
/************************* END ***************************/
# echo "" > /sys/kernel/debug/tracing/trace
# ./perf record -a -e cyc=cycles/no-inherit,period=0x7fffffffffffffff/ \
-e './test_bpf_map_2.c/maps:pmu_map.event=cyc/' ls /
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.755 MB perf.data ]
# cat /sys/kernel/debug/tracing/trace | grep ls
ls-25328 [002] d... 940138.313178: : sys_write: pmu=4503165
ls-25328 [002] dN.. 940138.313207: : sys_write_return: pmu=4582975
ls-25328 [002] d... 940138.313211: : sys_write: pmu=4599840
ls-25328 [002] dN.. 940138.313220: : sys_write_return: pmu=4633352
# ./perf report --stdio
Error:
The perf.data file has no samples!
...
(This is expected because we set period of cycles to a very large
value to period of cycles event because we want to use this event
as a counter only, don't need sampling)# ./perf record -e cycles -e './test_bpf_map_2.c/maps:pmu_map.event=cycles/' ls / ERROR: Apply config to BPF failed: Doesn't support inherit event (Hint: use -i or use /no-inherit/ to turn off inherit)
--- tools/perf/util/bpf-loader.c | 2 +- tools/perf/util/evlist.c | 4 ++-- tools/perf/util/evsel.c | 1 + tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 26 ++++++++++++++++++++++++++ tools/perf/util/parse-events.h | 4 ++++ tools/perf/util/parse-events.y | 15 ++++++++++++++- 7 files changed, 49 insertions(+), 4 deletions(-)
tools/perf/util/bpf-loader.c +1 -1
@@ -1448,7 +1448,7 @@ int bpf__strerror_apply_obj_config(int err, char *buf, size_t size)bpf__strerror_entry (BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM ,"Cannot set event to BPF maps in multi-thread tracing" );bpf__strerror_entry (BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH , -"%s (Hint: use -i to turn off inherit)" , emsg); +"%s (Hint: use -i or use /no-inherit/ to turn off inherit)" , emsg);bpf__strerror_entry (BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE ,"Can only put raw, hardware and BPF output event into a BPF map" );bpf__strerror_end (buf, size);
tools/perf/util/evlist.c +2 -2
@@ -1745,9 +1745,9 @@ perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,struct perf_evsel *evsel;evlist__for_each (evlist, evsel) { -if (!evsel->name) +if (!evsel->alias)continue ; -if (strcmp (str, evsel->name) ==0 ) +if (strcmp (str, evsel->alias) ==0 )return evsel; }
tools/perf/util/evsel.c +1 -0
@@ -1076,6 +1076,7 @@ void perf_evsel__exit(struct perf_evsel *evsel)thread_map__put (evsel->threads);zfree (&evsel->group_name);zfree (&evsel->name); +zfree (&evsel->alias); perf_evsel__object.fini (evsel); }
tools/perf/util/evsel.h +1 -0
@@ -89,6 +89,7 @@ struct perf_evsel {int idx;u32 ids;char *name; +char *alias;double scale;const char *unit;struct event_format *tp_format;
tools/perf/util/parse-events.c +26 -0
@@ -1091,6 +1091,30 @@ int parse_events__modifier_group(struct list_head *list,return parse_events__modifier_event (list, event_mod, true); } +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_) +{ +struct perf_evsel *evsel; +YYLTYPE *loc_alias = loc_alias_; + +if (!str) +return 0 ; + +if (!list_is_singular (list)) { +struct parse_events_error *err = data->error; + + err->idx = loc_alias->first_column; + err->str =strdup ("One alias can be applied to one event only" ); +return -EINVAL ; + } + + evsel = list_first_entry(list, struct perf_evsel, node); + evsel->alias =strdup (str); +return evsel->alias ?0 : -ENOMEM ; +} +void parse_events__set_leader (char *name,struct list_head *list) {struct perf_evsel *leader;
@@ -1283,6 +1307,8 @@ int parse_events_name(struct list_head *list, char *name)__evlist__for_each (list, evsel) {if (!evsel->name) evsel->name =strdup (name); +if (!evsel->alias) + evsel->alias =strdup (name); }return 0 ;
tools/perf/util/parse-events.h +4 -0
@@ -172,4 +172,8 @@ extern int is_valid_tracepoint(const char *event_string);int valid_event_mount (const char *eventfs);char *parse_events_formats_error_string (char *additional_terms); +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_);#endif /* __PERF_PARSE_EVENTS_H */
tools/perf/util/parse-events.y +14 -1
@@ -77,6 +77,7 @@ static inc_group_count(struct list_head *list, %type <head> event_bpf_file %type <head> event_def %type <head> event_mod +%type <head> event_alias %type <head> event_name %type <head> event %type <head> events
@@ -193,13 +194,25 @@ event_name PE_MODIFIER_EVENT event_name event_name: -PE_EVENT_NAME event_def +PE_EVENT_NAME event_alias { ABORT_ON(parse_events_name($2, $1)); free($1); $$ = $2; } | +event_alias + +event_alias: +PE_NAME '=' event_def +{ + struct list_head *list = $3; + struct parse_events_evlist *data = _data; + + ABORT_ON(parse_events__set_event_alias(data, list, $1, &@1)); + $$ = list; +} +| event_def event_def: event_pmu |
Signature · 1 line
1.8.3.4
PATCH v6 13/16 perf tools: Support setting different slots in a BPF map separately
In reply to Wang NanThis patch introduces basic facilities to support config different slots in a BPF map one by one.
array.nr_ranges and array.ranges are introduced into 'struct parse_events_term', where ranges is an array of indices range (start, length) which will be configured by this config term. nr_ranges is the size of the array. The array is passed to 'struct bpf_map_priv'. To indicate the new type of configuration, BPF_MAP_KEY_RANGES is added as a new key type. bpf_map_config_foreach_key() is extended to iterate over those indices instead of all possible keys.
Code in this commit will be enabled by following commit which enables the indices syntax for array configuration.
--- tools/perf/util/bpf-loader.c | 132 ++++++++++++++++++++++++++++++++++++++--- tools/perf/util/bpf-loader.h | 1 + tools/perf/util/parse-events.c | 33 ++++++++++- tools/perf/util/parse-events.h | 12 ++++ 4 files changed, 170 insertions(+), 8 deletions(-)
tools/perf/util/bpf-loader.c +125 -7
@@ -17,6 +17,7 @@#include "llvm-utils.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES +#include "parse-events.h" #include "llvm-utils.h" #define DEFINE_PRINT_FN (name, level) \
@@ -747,6 +748,7 @@ enum bpf_map_op_type {enum bpf_map_key_type {BPF_MAP_KEY_ALL , +BPF_MAP_KEY_RANGES , };struct bpf_map_op {
@@ -754,6 +756,9 @@ struct bpf_map_op {enum bpf_map_op_type op_type;enum bpf_map_key_type key_type;union { +struct parse_events_array array; + } k; +union {u64 value;struct perf_evsel *evsel; } v;
@@ -779,6 +784,8 @@ bpf_map_op__free(struct bpf_map_op *op)*/ if ((list->next !=LIST_POISON1 ) && (list->prev !=LIST_POISON2 ))list_del (list); +if (op->key_type ==BPF_MAP_KEY_RANGES ) +parse_events__clear_array (&op->k.array);free (op); }
@@ -794,8 +801,30 @@ bpf_map_priv__clear(struct bpf_map *map __maybe_unused,free (priv); } +static int +bpf_map_op_setkey (struct bpf_map_op *op,struct parse_events_term *term, +const char *map_name) +{ + op->key_type =BPF_MAP_KEY_ALL ; + +if (term->array.nr_ranges) { +size_t memsz = term->array.nr_ranges * +sizeof (op->k.array.ranges[0 ]); + + op->k.array.ranges =memdup (term->array.ranges, memsz); +if (!op->k.array.ranges) { +pr_debug ("No enough memory to alloc indices for %s\n" , + map_name); +return -ENOMEM ; + } + op->key_type =BPF_MAP_KEY_RANGES ; + op->k.array.nr_ranges = term->array.nr_ranges; + } +return 0 ; +} +static struct bpf_map_op * -bpf_map_op__alloc (struct bpf_map *map) +bpf_map_op__alloc (struct bpf_map *map,struct parse_events_term *term) {struct bpf_map_op *op;struct bpf_map_priv *priv;
@@ -829,7 +858,12 @@ bpf_map_op__alloc(struct bpf_map *map)return ERR_PTR (-ENOMEM ); } - op->key_type =BPF_MAP_KEY_ALL ; + err =bpf_map_op_setkey (op, term, map_name); +if (err) { +free (op); +return ERR_PTR (err); + } +list_add_tail (&op->list, &priv->ops_list);return op; }
@@ -872,7 +906,7 @@ bpf__obj_config_map_array_value(struct bpf_map *map,return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE ; } - op =bpf_map_op__alloc (map); + op =bpf_map_op__alloc (map, term);if (IS_ERR (op))return PTR_ERR (op); op->op_type =BPF_MAP_OP_SET_VALUE ;
@@ -933,7 +967,7 @@ bpf__obj_config_map_array_event(struct bpf_map *map,return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE ; } - op =bpf_map_op__alloc (map); + op =bpf_map_op__alloc (map, term);if (IS_ERR (op))return PTR_ERR (op);
@@ -972,6 +1006,44 @@ struct bpf_obj_config_map_func bpf_obj_config_map_funcs[] = { };static int +config_map_indices_range_check (struct parse_events_term *term, +struct bpf_map *map, +const char *map_name) +{ +struct parse_events_array *array = &term->array; +struct bpf_map_def def; +unsigned int i; +int err; + +if (!array->nr_ranges) +return 0 ; +if (!array->ranges) { +pr_debug ("ERROR: map %s: array->nr_ranges is %d but range array is NULL\n" , + map_name, (int )array->nr_ranges); +return -BPF_LOADER_ERRNO__INTERNAL ; + } + + err =bpf_map__get_def (map, &def); +if (err) { +pr_debug ("ERROR: Unable to get map definition from '%s'\n" , + map_name); +return -BPF_LOADER_ERRNO__INTERNAL ; + } + +for (i =0 ; i < array->nr_ranges; i++) { +unsigned int start = array->ranges[i].start; +size_t length = array->ranges[i].length; +unsigned int idx = start + length -1 ; + +if (idx >= def.max_entries) { +pr_debug ("ERROR: index %d too large\n" , idx); +return -BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG ; + } + } +return 0 ; +} + +static int bpf__obj_config_map(struct bpf_object *obj, struct parse_events_term *term, struct perf_evlist *evlist,
@@ -1007,6 +1079,13 @@ bpf__obj_config_map(struct bpf_object *obj, } *key_scan_pos += map_opt - map_name; + + *key_scan_pos +=strlen (map_opt); + err =config_map_indices_range_check (term, map, map_name); +if (err) + goto out; + *key_scan_pos -=strlen (map_opt); +for (i =0 ; i <ARRAY_SIZE (bpf_obj_config_map_funcs); i++) {struct bpf_obj_config_map_func *func = &bpf_obj_config_map_funcs[i];
@@ -1077,6 +1156,33 @@ foreach_key_array_all(map_config_func_t func, }static int +foreach_key_array_ranges (map_config_func_t func,void *arg, +const char *name,int map_fd, +struct bpf_map_def *pdef, +struct bpf_map_op *op) +{ +unsigned int i, j; +int err; + +for (i =0 ; i < op->k.array.nr_ranges; i++) { +unsigned int start = op->k.array.ranges[i].start; +size_t length = op->k.array.ranges[i].length; + +for (j =0 ; j < length; j++) { +unsigned int idx = start + j; + + err =func (name, map_fd, pdef, op, &idx, arg); +if (err) { +pr_debug ("ERROR: failed to insert value to %s[%u]\n" , + name, idx); +return err; + } + } + } +return 0 ; +} + +static int bpf_map_config_foreach_key (struct bpf_map *map,map_config_func_t func,void *arg)
@@ -1116,13 +1222,24 @@ bpf_map_config_foreach_key(struct bpf_map *map,case BPF_MAP_TYPE_PERF_EVENT_ARRAY :switch (op->key_type) {case BPF_MAP_KEY_ALL : -return foreach_key_array_all (func, arg, name, - map_fd, &def, op); + err =foreach_key_array_all (func, arg, name, + map_fd, &def, op); +if (err) +return err; +break ; +case BPF_MAP_KEY_RANGES : + err =foreach_key_array_ranges (func, arg, name, + map_fd, &def, + op); +if (err) +return err; +break ;default :pr_debug ("ERROR: keytype for map '%s' invalid\n" , name);return -BPF_LOADER_ERRNO__INTERNAL ; - } + } +break ;default :pr_debug ("ERROR: type of '%s' incorrect\n" , name);return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE ;
@@ -1309,6 +1426,7 @@ static const char *bpf_loader_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(OBJCONF_MAP_EVTDIM)] = "Event dimension too large", [ERRCODE_OFFSET(OBJCONF_MAP_EVTINH)] = "Doesn't support inherit event", [ERRCODE_OFFSET(OBJCONF_MAP_EVTTYPE)] = "Wrong event type for map", + [ERRCODE_OFFSET(OBJCONF_MAP_IDX2BIG)] = "Index too large", }; static int
tools/perf/util/bpf-loader.h +1 -0
@@ -38,6 +38,7 @@ enum bpf_loader_errno {BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM ,/* Event dimension too large */ BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH ,/* Doesn't support inherit event */ BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE ,/* Wrong event type for map */ +BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG ,/* Index too large */ __BPF_LOADER_ERRNO__END, };
tools/perf/util/parse-events.c +32 -1
@@ -2148,8 +2148,39 @@ void parse_events__free_terms(struct list_head *terms) {struct parse_events_term *term, *h; -list_for_each_entry_safe (term, h, terms, list) +list_for_each_entry_safe (term, h, terms, list) { +if (term->array.nr_ranges) +free (term->array.ranges);free (term); + } +} + +int parse_events__merge_arrays (struct parse_events_array *dest, +struct parse_events_array *another) +{ +struct parse_events_array new; + +if (!dest || !another) +return -EINVAL ; + + new.nr_ranges = dest->nr_ranges + another->nr_ranges; + new.ranges =malloc (sizeof (new.ranges[0 ]) * new.nr_ranges); +if (!new.ranges) +return -ENOMEM ; + +memcpy (&new.ranges[0 ], dest->ranges, +sizeof (new.ranges[0 ]) * dest->nr_ranges); +memcpy (&new.ranges[dest->nr_ranges], another->ranges, +sizeof (new.ranges[0 ]) * another->nr_ranges); +free (dest->ranges); +free (another->ranges); + *dest = new; +return 0 ; +} + +void parse_events__clear_array (struct parse_events_array *a) +{ +free (a->ranges); } void parse_events_evlist_error(struct parse_events_evlist *data,
tools/perf/util/parse-events.h +12 -0
@@ -71,8 +71,17 @@ enum {PARSE_EVENTS__TERM_TYPE_INHERIT }; +struct parse_events_array { +size_t nr_ranges; +struct { +unsigned int start; +size_t length; + } *ranges; +}; +struct parse_events_term {char *config; +struct parse_events_array array;union {char *str;u64 num;
@@ -117,6 +126,9 @@ int parse_events_term__sym_hw(struct parse_events_term **term,int parse_events_term__clone (struct parse_events_term **new,struct parse_events_term *term);void parse_events__free_terms (struct list_head *terms); +int parse_events__merge_arrays (struct parse_events_array *dest, +struct parse_events_array *another); +void parse_events__clear_array (struct parse_events_array *a);int parse_events__modifier_event (struct list_head *list,char *str,bool add);int parse_events__modifier_group (struct list_head *list,char *event_mod);int parse_events_name (struct list_head *list,char *name);
Signature · 1 line
1.8.3.4
PATCH v6 14/16 perf tools: Enable indices setting syntax for BPF maps
In reply to Wang NanThis patch introduce a new syntax to perf event parser:
# perf record -e './test_bpf_map_3.c/maps:channel.value[0,1,2,3...5]=101/' usleep 2
By utilizing the basic facilities in bpf-loader.c which allow setting different slots in a BPF map separately, the newly introduced syntax allows perf to control specific elements in a BPF map.
Test result:
# cat ./test_bpf_map_3.c
/************************ BEGIN **************************/
#include <uapi/linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
static void *(*map_lookup_elem)(struct bpf_map_def *, void *) =
(void *)BPF_FUNC_map_lookup_elem;
static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
(void *)BPF_FUNC_trace_printk;
struct bpf_map_def SEC("maps") channel = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(unsigned char),
.max_entries = 100,
};
SEC("func=hrtimer_nanosleep rqtp->tv_nsec")
int func(void *ctx, int err, long nsec)
{
char fmt[] = "%ld\n";
long usec = nsec * 0x10624dd3 >> 38; // nsec / 1000
int key = (int)usec;
unsigned char *pval = map_lookup_elem(&channel, &key); if (!pval)
return 0;
trace_printk(fmt, sizeof(fmt), (unsigned char)*pval); return 0;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE; /************************* END ***************************/
Normal case:
# echo "" > /sys/kernel/debug/tracing/trace
# ./perf record -e './test_bpf_map_3.c/maps:channel.value[0,1,2,3...5]=101/' usleep 2
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data ]
# cat /sys/kernel/debug/tracing/trace | grep usleep
usleep-405 [004] d... 2745423.547822: : 101
# ./perf record -e './test_bpf_map_3.c/maps:channel.value[0...9,20...29]=102,maps:channel.value[10...19]=103/' usleep 3
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data ]
# ./perf record -e './test_bpf_map_3.c/maps:channel.value[0...9,20...29]=102,maps:channel.value[10...19]=103/' usleep 15
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data ]
# cat /sys/kernel/debug/tracing/trace | grep usleep
usleep-405 [004] d... 2745423.547822: : 101
usleep-655 [006] d... 2745434.122814: : 102
usleep-904 [006] d... 2745439.916264: : 103
# ./perf record -e './test_bpf_map_3.c/maps:channel.value[all]=104/' usleep 99
# cat /sys/kernel/debug/tracing/trace | grep usleep
usleep-405 [004] d... 2745423.547822: : 101
usleep-655 [006] d... 2745434.122814: : 102
usleep-904 [006] d... 2745439.916264: : 103
usleep-1537 [003] d... 2745538.053737: : 104Error case:
# ./perf record -e './test_bpf_map_3.c/maps:channel.value[10...1000]=104/' usleep 99
event syntax error: '..annel.value[10...1000]=104/'
\___ Index too large
Hint: Valid config terms:
maps:[<arraymap>].value<indices>=[value]
maps:[<eventmap>].event<indices>=[event] where <indices> is something like [0,3...5] or [all] (add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
--- tools/perf/util/parse-events.c | 5 ++- tools/perf/util/parse-events.l | 13 ++++++- tools/perf/util/parse-events.y | 85 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-)
tools/perf/util/parse-events.c +3 -2
@@ -660,9 +660,10 @@ parse_events_config_bpf(struct parse_events_evlist *data, sizeof(errbuf)); data->error->help =strdup ("Hint:\tValid config terms:\n" -" \tmaps:[<arraymap>].value=[value]\n" -" \tmaps:[<eventmap>].event=[event]\n" +" \tmaps:[<arraymap>].value<indices>=[value]\n" +" \tmaps:[<eventmap>].event<indices>=[event]\n" "\n" +" \twhere <indices> is something like [0,3...5] or [all]\n" " \t(add -v to see detail)" ); data->error->str =strdup (errbuf);if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE )
tools/perf/util/parse-events.l +12 -1
@@ -9,8 +9,8 @@ %{ #include <errno.h> #include "../perf.h" -#include "parse-events-bison.h" #include "parse-events.h" +#include "parse-events-bison.h" char *parse_events_get_text(yyscan_t yyscanner); YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
@@ -111,6 +111,7 @@ do { \ %x mem %s config %x event +%x array group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]*
@@ -176,6 +177,14 @@ modifier_bp [rwx]{1,3} } +<array>{ +"]" { BEGIN(config); return ']'; } +{num_dec} { return value(yyscanner, 10); } +{num_hex} { return value(yyscanner, 16); } +, { return ','; } +"\.\.\." { return PE_ARRAY_RANGE; } +} + <config>{ /* * Please update parse_events_formats_error_string any time
@@ -196,6 +205,8 @@ no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); } , { return ','; } "/" { BEGIN(INITIAL); return '/'; } {name_minus} { return str(yyscanner, PE_NAME); } +\[all\] { return PE_ARRAY_ALL; } +"[" { BEGIN(array); return '['; } } <mem>{
tools/perf/util/parse-events.y +85 -0
109 lines not on this page · raw message
Signature · 1 line
1.8.3.4
PATCH v6 15/16 perf tools: Introduce bpf-output event
In reply to Wang NanCommit a43eec304259a6c637f4014a6d4767159b6a3aa3 (bpf: introduce bpf_perf_event_output() helper) add a helper to enable BPF program output data to perf ring buffer through a new type of perf event PERF_COUNT_SW_BPF_OUTPUT. This patch enable perf to create perf event of that type. Now perf user can use following cmdline to receive output data from BPF programs:
# ./perf record -a -e evt=bpf-output/no-inherit/ \
-e ./test_bpf_output.c/maps:channel.event=evt/ ls /
# ./perf script
perf 12927 [004] 355971.129276: 0 evt=bpf-output/no-inherit/: ffffffff811ed5f1 sys_write
perf 12927 [004] 355971.129279: 0 evt=bpf-output/no-inherit/: ffffffff811ed5f1 sys_write
...Test result:
# cat ./test_bpf_output.c
/************************ 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__,
};
SEC("func_write=sys_write")
int func_write(void *ctx)
{
struct {
u64 ktime;
int cpuid;
} __attribute__((packed)) output_data; char error_data[] = "Error: failed to output: %d\n";
output_data.cpuid = get_smp_processor_id(); output_data.ktime = ktime_get_ns();
int err = perf_event_output(ctx, &channel, get_smp_processor_id(),
&output_data, sizeof(output_data));
if (err)
trace_printk(error_data, sizeof(error_data), err);
return 0;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE; /************************ END ***************************/
# ./perf record -a -e evt=bpf-output/no-inherit/ \
-e ./test_bpf_output.c/maps:channel.event=evt/ ls /
# ./perf script | grep ls
ls 4085 [000] 2746114.230215: evt=bpf-output/no-inherit/: ffffffff811ed5f1 sys_write (/lib/modules/4.3.0-rc4+/build/vmlinux)
ls 4085 [000] 2746114.230244: evt=bpf-output/no-inherit/: ffffffff811ed5f1 sys_write (/lib/modules/4.3.0-rc4+/build/vmlinux)--- tools/perf/util/bpf-loader.c | 5 ++--- tools/perf/util/evsel.c | 5 +++++ tools/perf/util/evsel.h | 8 ++++++++ tools/perf/util/parse-events.l | 1 + 4 files changed, 16 insertions(+), 3 deletions(-)
tools/perf/util/bpf-loader.c +2 -3
@@ -1312,13 +1312,12 @@ apply_config_evsel_for_key(const char *name, int map_fd, void *pkey,return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH ; } +if (perf_evsel__is_bpf_output (evsel)) + check_pass = true;if (attr->type ==PERF_TYPE_RAW ) check_pass = true;if (attr->type ==PERF_TYPE_HARDWARE ) check_pass = true; -if (attr->type ==PERF_TYPE_SOFTWARE && - attr->config ==PERF_COUNT_SW_BPF_OUTPUT ) - check_pass = true;if (!check_pass) {pr_debug ("ERROR: Event type is wrong for map %s\n" , name);return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE ;
tools/perf/util/evsel.c +5 -0
@@ -225,6 +225,11 @@ struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)if (evsel !=NULL )perf_evsel__init (evsel, attr, idx); +if (perf_evsel__is_bpf_output (evsel)) { + evsel->attr.sample_type |=PERF_SAMPLE_RAW ; + evsel->attr.sample_period =1 ; + } +return evsel; }
tools/perf/util/evsel.h +8 -0
@@ -365,6 +365,14 @@ static inline bool perf_evsel__is_function_event(struct perf_evsel *evsel)#undef FUNCTION_EVENT } +static inline bool perf_evsel__is_bpf_output (struct perf_evsel *evsel) +{ +struct perf_event_attr *attr = &evsel->attr; + +return (attr->config ==PERF_COUNT_SW_BPF_OUTPUT ) && + (attr->type ==PERF_TYPE_SOFTWARE ); +} +struct perf_attr_details {bool freq;bool verbose;
tools/perf/util/parse-events.l +1 -0
@@ -249,6 +249,7 @@ cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COU alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); } emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); } dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); } +bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); } /* * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
Signature · 1 line
1.8.3.4
PATCH v6 16/16 perf data: Support converting data from bpf_perf_event_output()
In reply to Wang Nanbpf_perf_event_output() outputs data through sample->raw_data. This patch adds support to convert those data into CTF. A python script then can be used to process output data from BPF programs.
Test result:
# cat ./test_bpf_output_2.c
/************************ 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)
{
struct {
u64 ktime;
int type;
} __attribute__((packed)) output_data; char error_data[] = "Error: failed to output\n"; int err;
output_data.type = type;
output_data.ktime = ktime_get_ns();
err = perf_event_output(ctx, &channel, get_smp_processor_id(),
&output_data, sizeof(output_data));
if (err)
trace_printk(error_data, sizeof(error_data));
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 ***************************/
# ./perf record -e evt=bpf-output/no-inherit/ \
-e ./test_bpf_output_2.c/maps:channel.event=evt/ \
usleep 100000
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data (2 samples) ] # ./perf script
usleep 14942 92503.198504: evt=bpf-output/no-inherit/: ffffffff810e0ba1 sys_nanosleep (/lib/modules/4.3.0....
usleep 14942 92503.298562: evt=bpf-output/no-inherit/: ffffffff810585e9 kretprobe_trampoline_holder (/lib....# ./perf data convert --to-ctf ./out.ctf [ perf data convert: Converted 'perf.data' into CTF data './out.ctf' ] [ perf data convert: Converted and wrote 0.000 MB (2 samples) ]
# babeltrace ./out.ctf
[01:41:43.198504134] (+?.?????????) evt=bpf-output/no-inherit/: { cpu_id = 0 }, { perf_ip = 0xFFFFFFFF810E0BA1, perf_tid = 14942, perf_pid = 14942, perf_id = 1044, raw_len = 3, raw_data = [ [0] = 0x32C0C07B, [1] = 0x5421, [2] = 0x1 ] }
[01:41:43.298562257] (+0.100058123) evt=bpf-output/no-inherit/: { cpu_id = 0 }, { perf_ip = 0xFFFFFFFF810585E9, perf_tid = 14942, perf_pid = 14942, perf_id = 1044, raw_len = 3, raw_data = [ [0] = 0x38B77FAA, [1] = 0x5421, [2] = 0x2 ] } # cat ./test_bpf_output_2.py
from babeltrace import TraceCollection
tc = TraceCollection(
tc.add_trace('./out.ctf', 'ctf')
d = {1:[], 2:[]}
for event in tc.events:
if not event.name.startswith('evt=bpf-output/no-inherit/'):
continue
raw_data = event['raw_data']
(time, type) = ((raw_data[0] + (raw_data[1] << 32)), raw_data[2])
d[type].append(time)
print(list(map(lambda i: d[2][i] - d[1][i], range(len(d[1]))))));# python3 ./test_bpf_output_2.py [100056879]
--- tools/perf/util/data-convert-bt.c | 112 +++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-)
tools/perf/util/data-convert-bt.c +111 -1
@@ -352,6 +352,84 @@ static int add_tracepoint_values(struct ctf_writer *cw,return ret; } +static int +add_bpf_output_values (struct bt_ctf_event_class *event_class, +struct bt_ctf_event *event, +struct perf_sample *sample) +{ +struct bt_ctf_field_type *len_type, *seq_type; +struct bt_ctf_field *len_field, *seq_field; +unsigned int raw_size = sample->raw_size; +unsigned int nr_elements = raw_size /sizeof (u32); +unsigned int i; +int ret; + +if (nr_elements *sizeof (u32) != raw_size) +pr_warning ("Incorrect raw_size (%u) in bpf output event, skip %lu bytes\n" , + raw_size, nr_elements *sizeof (u32) - raw_size); + + len_type =bt_ctf_event_class_get_field_by_name (event_class,"raw_len" ); + len_field =bt_ctf_field_create (len_type); +if (!len_field) { +pr_err ("failed to create 'raw_len' for bpf output event\n" ); + ret =-1 ; + goto put_len_type; + } + + ret =bt_ctf_field_unsigned_integer_set_value (len_field, nr_elements); +if (ret) { +pr_err ("failed to set field value for raw_len\n" ); + goto put_len_field; + } + ret =bt_ctf_event_set_payload (event,"raw_len" , len_field); +if (ret) { +pr_err ("failed to set payload to raw_len\n" ); + goto put_len_field; + } + + seq_type =bt_ctf_event_class_get_field_by_name (event_class,"raw_data" ); + seq_field =bt_ctf_field_create (seq_type); +if (!seq_field) { +pr_err ("failed to create 'raw_data' for bpf output event\n" ); + ret =-1 ; + goto put_seq_type; + } + + ret =bt_ctf_field_sequence_set_length (seq_field, len_field); +if (ret) { +pr_err ("failed to set length of 'raw_data'\n" ); + goto put_seq_field; + } + +for (i =0 ; i < nr_elements; i++) { +struct bt_ctf_field *elem_field = +bt_ctf_field_sequence_get_field (seq_field, i); + + ret =bt_ctf_field_unsigned_integer_set_value (elem_field, + ((u32 *)(sample->raw_data))[i]); + +bt_ctf_field_put (elem_field); +if (ret) { +pr_err ("failed to set raw_data[%d]\n" , i); + goto put_seq_field; + } + } + + ret =bt_ctf_event_set_payload (event,"raw_data" , seq_field); +if (ret) +pr_err ("failed to set payload for raw_data\n" ); + +put_seq_field: +bt_ctf_field_put (seq_field); +put_seq_type: +bt_ctf_field_type_put (seq_type); +put_len_field: +bt_ctf_field_put (len_field); +put_len_type: +bt_ctf_field_type_put (len_type); +return ret; +} + static int add_generic_values(struct ctf_writer *cw, struct bt_ctf_event *event, struct perf_evsel *evsel,
@@ -597,6 +675,12 @@ static int process_sample_event(struct perf_tool *tool,return -1 ; } +if (perf_evsel__is_bpf_output (evsel)) { + ret =add_bpf_output_values (event_class, event, sample); +if (ret) +return -1 ; + } + cs =ctf_stream (cw,get_sample_cpu (cw, sample, evsel));if (cs) {if (is_flush_needed (cs))
@@ -744,6 +828,25 @@ static int add_tracepoint_types(struct ctf_writer *cw,return ret; } +static int add_bpf_output_types (struct ctf_writer *cw, +struct bt_ctf_event_class *class) +{ +struct bt_ctf_field_type *len_type = cw->data.u32; +struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex; +struct bt_ctf_field_type *seq_type; +int ret; + + ret =bt_ctf_event_class_add_field (class, len_type,"raw_len" ); +if (ret) +return ret; + + seq_type =bt_ctf_field_type_sequence_create (seq_base_type,"raw_len" ); +if (!seq_type) +return -1 ; + +return bt_ctf_event_class_add_field (class, seq_type,"raw_data" ); +} +static int add_generic_types (struct ctf_writer *cw,struct perf_evsel *evsel,struct bt_ctf_event_class *event_class) {
@@ -755,7 +858,8 @@ static int add_generic_types(struct ctf_writer *cw, struct perf_evsel *evsel,* ctf event header * PERF_SAMPLE_READ - TODO * PERF_SAMPLE_CALLCHAIN - TODO -* PERF_SAMPLE_RAW - tracepoint fields are handled separately +* PERF_SAMPLE_RAW - tracepoint fields and BPF output +* are handled separately * PERF_SAMPLE_BRANCH_STACK - TODO * PERF_SAMPLE_REGS_USER - TODO * PERF_SAMPLE_STACK_USER - TODO
@@ -824,6 +928,12 @@ static int add_event(struct ctf_writer *cw, struct perf_evsel *evsel) goto err; } +if (perf_evsel__is_bpf_output (evsel)) { + ret =add_bpf_output_types (cw, event_class); +if (ret) + goto err; + } + ret =bt_ctf_stream_class_add_event_class (cw->stream_class, event_class);if (ret) {pr ("Failed to add event class into stream.\n" );
Signature · 1 line
1.8.3.4