bpf: add two helpers to read perf event enabled/running time
Subsystems (10)
v3 cover bpf: add two helpers to read perf event enabled/running time
Hardware pmu counters are limited resources. When there are more pmu based perf events opened than available counters, kernel will multiplex these events so each event gets certain percentage (but not 100%) of the pmu time. In case that multiplexing happens, the number of samples or counter value will not reflect the case compared to no multiplexing. This makes comparison between different runs difficult.
Typically, the number of samples or counter value should be normalized before comparing to other experiments. The typical normalization is done like:
normalized_num_samples = num_samples * time_enabled / time_running normalized_counter_value = counter_value * time_enabled / time_running
where time_enabled is the time enabled for event and time_running is the time running for event since last normalization.
This patch set implements two helper functions.
The helper bpf_perf_event_read_value reads counter/time_enabled/time_running for perf event array map. The helper bpf_perf_prog_read_value read counter/time_enabled/time_running for bpf prog with type BPF_PROG_TYPE_PERF_EVENT.
Changelogs:v2->v3:
. counters should be read in order to read enabled/running time. This is to
prevent that counters and enabled/running time may be read separately.v1->v2:
. reading enabled/running time should be together with reading counters
which contains the logic to ensure the result is valid.
Yonghong Song (4):
bpf: add helper bpf_perf_event_read_value for perf event array map
bpf: add a test case for helper bpf_perf_event_read_value
bpf: add helper bpf_perf_prog_read_value
bpf: add a test case for helper bpf_perf_prog_read_value
include/linux/perf_event.h | 4 +- include/uapi/linux/bpf.h | 26 +++++++++++- kernel/bpf/arraymap.c | 2 +- kernel/bpf/verifier.c | 4 +- kernel/events/core.c | 16 ++++++-- kernel/trace/bpf_trace.c | 67 +++++++++++++++++++++++++++++-- samples/bpf/trace_event_kern.c | 10 +++++ samples/bpf/trace_event_user.c | 13 +++--- samples/bpf/tracex6_kern.c | 26 ++++++++++++ samples/bpf/tracex6_user.c | 13 +++++- tools/include/uapi/linux/bpf.h | 4 +- tools/testing/selftests/bpf/bpf_helpers.h | 6 +++ 12 files changed, 173 insertions(+), 18 deletions(-)
Signature · 1 line
2.9.5
PATCH v3 1/4 bpf: add helper bpf_perf_event_read_value for perf event array map
Hardware pmu counters are limited resources. When there are more pmu based perf events opened than available counters, kernel will multiplex these events so each event gets certain percentage (but not 100%) of the pmu time. In case that multiplexing happens, the number of samples or counter value will not reflect the case compared to no multiplexing. This makes comparison between different runs difficult.
Typically, the number of samples or counter value should be normalized before comparing to other experiments. The typical normalization is done like:
normalized_num_samples = num_samples * time_enabled / time_running normalized_counter_value = counter_value * time_enabled / time_running
where time_enabled is the time enabled for event and time_running is the time running for event since last normalization.
This patch adds helper bpf_perf_event_read_value for kprobed based perf event array map, to read perf counter and enabled/running time. The enabled/running time is accumulated since the perf event open. To achieve scaling factor between two bpf invocations, users can can use cpu_id as the key (which is typical for perf array usage model) to remember the previous value and do the calculation inside the bpf program.
--- include/linux/perf_event.h | 3 ++- include/uapi/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/arraymap.c | 2 +- kernel/bpf/verifier.c | 4 +++- kernel/events/core.c | 15 ++++++++++++--- kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 75 insertions(+), 11 deletions(-)
include/linux/perf_event.h +2 -1
@@ -884,7 +884,8 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, void *context);extern void perf_pmu_migrate_context (struct pmu *pmu,int src_cpu,int dst_cpu); -int perf_event_read_local (struct perf_event *event,u64 *value); +int perf_event_read_local (struct perf_event *event,u64 *value, +u64 *enabled,u64 *running);extern u64 perf_event_read_value (struct perf_event *event,u64 *enabled,u64 *running);
include/uapi/linux/bpf.h +17 -1
@@ -582,6 +582,14 @@ union bpf_attr {* @map: pointer to sockmap to update * @key: key to insert/update sock in map * @flags: same flags as map update elem +* +* int bpf_perf_event_read_value(map, flags, buf, buf_size) +* read perf event counter value and perf event enabled/running time +* @map: pointer to perf_event_array map +* @flags: index of event in the map or bitmask flags +* @buf: buf to fill +* @buf_size: size of the buf +* Return: 0 on success or negative error code */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -638,6 +646,7 @@ union bpf_attr {FN (redirect_map), \FN (sk_redirect_map), \FN (sock_map_update), \ +FN (perf_event_read_value), \/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -681,7 +690,8 @@ enum bpf_func_id {#define BPF_F_ZERO_CSUM_TX (1ULL << 1)#define BPF_F_DONT_FRAGMENT (1ULL << 2) -/* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */ +/* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and +* BPF_FUNC_perf_event_read_value flags. */ #define BPF_F_INDEX_MASK 0xffffffffULL#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK/* BPF_FUNC_perf_event_output for sk_buff input context. */
@@ -864,4 +874,10 @@ enum {#define TCP_BPF_IW 1001/* Set TCP initial congestion window */ #define TCP_BPF_SNDCWND_CLAMP 1002/* Set sndcwnd_clamp */ +struct bpf_perf_event_value { +__u64 counter; +__u64 enabled; +__u64 running; +}; +#endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/arraymap.c +1 -1
@@ -492,7 +492,7 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, ee =ERR_PTR (-EOPNOTSUPP ); event = perf_file->private_data; -if (perf_event_read_local (event, &value) == -EOPNOTSUPP ) +if (perf_event_read_local (event, &value,NULL ,NULL ) == -EOPNOTSUPP ) goto err_out; ee =bpf_event_entry_gen (perf_file, map_file);
kernel/bpf/verifier.c +3 -1
@@ -1494,7 +1494,8 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)break ;case BPF_MAP_TYPE_PERF_EVENT_ARRAY :if (func_id != BPF_FUNC_perf_event_read && - func_id != BPF_FUNC_perf_event_output) + func_id != BPF_FUNC_perf_event_output && + func_id != BPF_FUNC_perf_event_read_value) goto error;break ;case BPF_MAP_TYPE_STACK_TRACE :
@@ -1537,6 +1538,7 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)break ;case BPF_FUNC_perf_event_read:case BPF_FUNC_perf_event_output: +case BPF_FUNC_perf_event_read_value:if (map->map_type !=BPF_MAP_TYPE_PERF_EVENT_ARRAY ) goto error;break ;
kernel/events/core.c +12 -3
@@ -3684,10 +3684,12 @@ static inline u64 perf_event_count(struct perf_event *event)* will not be local and we cannot read them atomically * - must not have a pmu::count method */ -int perf_event_read_local (struct perf_event *event,u64 *value) +int perf_event_read_local (struct perf_event *event,u64 *value, +u64 *enabled,u64 *running) {unsigned long flags;int ret =0 ; +u64 now;/* * Disabling interrupts avoids all counter scheduling (context
@@ -3718,14 +3720,21 @@ int perf_event_read_local(struct perf_event *event, u64 *value) goto out; } + now = event->shadow_ctx_time +perf_clock (); +if (enabled) + *enabled = now - event->tstamp_enabled;/* * If the event is currently on this CPU, its either a per-task event, * or local to this CPU. Furthermore it means its ACTIVE (otherwise * oncpu == -1). */ -if (event->oncpu ==smp_processor_id ()) +if (event->oncpu ==smp_processor_id ()) { event->pmu->read (event); - +if (running) + *running = now - event->tstamp_running; + }else if (running) { + *running = event->total_time_running; + } *value =local64_read (&event->count); out:local_irq_restore (flags);
kernel/trace/bpf_trace.c +40 -4
78 lines not on this page · raw message
Signature · 1 line
2.9.5
kbuild test robot
build botRe: [PATCH net-next v3 1/4] bpf: add helper bpf_perf_event_read_value for perf event array map
url: https://github.com/0day-ci/linux/commits/Yonghong-Song/bpf-add-two-helpers-to-read-perf-event-enabled-running-time/20170919-134113 config: m68k-allyesconfig (attached as .config) compiler…
Hi Yonghong,
[auto build test ERROR on net-next/master]
url: https://github.com/0day-ci/linux/commits/Yonghong-Song/bpf-add-two-helpers-to-read-perf-event-enabled-running-time/20170919-134113
config: m68k-allyesconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 4.9.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=m68k
All errors (new ones prefixed by >>):
kernel/bpf/arraymap.c: In function 'perf_event_fd_array_get_ptr':
>> kernel/bpf/arraymap.c:495:6: error: too many arguments to function 'perf_event_read_local'
if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
^ In file included from kernel/bpf/arraymap.c:18:0:
include/linux/perf_event.h:1290:19: note: declared here
static inline int perf_event_read_local(struct perf_event *event, u64 *value)
^
vim +/perf_event_read_local +495 kernel/bpf/arraymap.c
480
481 static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
482 struct file *map_file, int fd)
483 {
484 struct bpf_event_entry *ee;
485 struct perf_event *event;
486 struct file *perf_file;
487 u64 value;
488
489 perf_file = perf_event_get(fd);
490 if (IS_ERR(perf_file))
491 return perf_file;
492
493 ee = ERR_PTR(-EOPNOTSUPP);
494 event = perf_file->private_data;
> 495 if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
496 goto err_out;
497
498 ee = bpf_event_entry_gen(perf_file, map_file);
499 if (ee)
500 return ee;
501 ee = ERR_PTR(-ENOMEM);
502 err_out:
503 fput(perf_file);
504 return ee;
505 }
506
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
PATCH v3 2/4 bpf: add a test case for helper bpf_perf_event_read_value
In reply to Yonghong SongThe bpf sample program tracex6 is enhanced to use the new helper to read enabled/running time as well.
--- samples/bpf/tracex6_kern.c | 26 ++++++++++++++++++++++++++ samples/bpf/tracex6_user.c | 13 ++++++++++++- tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/bpf_helpers.h | 3 +++ 4 files changed, 43 insertions(+), 2 deletions(-)
samples/bpf/tracex6_kern.c +26 -0
@@ -15,6 +15,12 @@ struct bpf_map_def SEC("maps") values = { .value_size = sizeof(u64), .max_entries = 64, }; +struct bpf_map_def SEC("maps") values2 = { + .type = BPF_MAP_TYPE_HASH, + .key_size = sizeof(int), + .value_size = sizeof(struct bpf_perf_event_value), + .max_entries = 64, +}; SEC("kprobe/htab_map_get_next_key")int bpf_prog1 (struct pt_regs *ctx)
@@ -37,5 +43,25 @@ int bpf_prog1(struct pt_regs *ctx)return 0 ; } +SEC("kprobe/htab_map_lookup_elem") +int bpf_prog2 (struct pt_regs *ctx) +{ +u32 key =bpf_get_smp_processor_id (); +struct bpf_perf_event_value *val, buf; +int error; + + error =bpf_perf_event_read_value (&counters, key, &buf,sizeof (buf)); +if (error) +return 0 ; + + val =bpf_map_lookup_elem (&values2, &key); +if (val) + *val = buf; +else +bpf_map_update_elem (&values2, &key, &buf,BPF_NOEXIST ); + +return 0 ; +} + char _license[] SEC("license") = "GPL"; u32 _version SEC("version") = LINUX_VERSION_CODE;
samples/bpf/tracex6_user.c +12 -1
@@ -22,6 +22,7 @@static void check_on_cpu (int cpu,struct perf_event_attr *attr) { +struct bpf_perf_event_value value2;int pmu_fd, error =0 ;cpu_set_t set;__u64 value;
@@ -46,8 +47,18 @@ static void check_on_cpu(int cpu, struct perf_event_attr *attr)fprintf (stderr,"Value missing for CPU %d\n" , cpu); error =1 ; goto on_exit; + }else { +fprintf (stderr,"CPU %d: %llu\n" , cpu, value); + } +/* The above bpf_map_lookup_elem should trigger the second kprobe */ +if (bpf_map_lookup_elem (map_fd[2 ], &cpu, &value2)) { +fprintf (stderr,"Value2 missing for CPU %d\n" , cpu); + error =1 ; + goto on_exit; + }else { +fprintf (stderr,"CPU %d: counter: %llu, enabled: %llu, running: %llu\n" , cpu, + value2.counter, value2.enabled, value2.running); } -fprintf (stderr,"CPU %d: %llu\n" , cpu, value); on_exit:assert (bpf_map_delete_elem (map_fd[0 ], &cpu) ==0 || error);
tools/include/uapi/linux/bpf.h +2 -1
@@ -632,7 +632,8 @@ union bpf_attr {FN (skb_adjust_room), \FN (redirect_map), \FN (sk_redirect_map), \ -FN (sock_map_update), +FN (sock_map_update), \ +FN (perf_event_read_value),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
@@ -70,6 +70,9 @@ static int (*bpf_sk_redirect_map)(void *map, int key, int flags) =static int (*bpf_sock_map_update)(void *map,void *key,void *value,unsigned long long flags) = (void *) BPF_FUNC_sock_map_update; +static int (*bpf_perf_event_read_value)(void *map,unsigned long long flags, +void *buf,unsigned int buf_size) = + (void *) BPF_FUNC_perf_event_read_value;/* llvm builtin functions that eBPF C program may use to
Signature · 1 line
2.9.5
PATCH v3 3/4 bpf: add helper bpf_perf_prog_read_value
In reply to Yonghong SongThis patch adds helper bpf_perf_prog_read_cvalue for perf event based bpf programs, to read event counter and enabled/running time.
The enabled/running time is accumulated since the perf event open.
The typical use case for perf event based bpf program is to attach itself to a single event. In such cases, if it is desirable to get scaling factor between two bpf invocations, users can can save the time values in a map, and use the value from the map and the current value to calculate the scaling factor.
--- include/linux/perf_event.h | 1 + include/uapi/linux/bpf.h | 8 ++++++++ kernel/events/core.c | 1 + kernel/trace/bpf_trace.c | 23 +++++++++++++++++++++++ 4 files changed, 33 insertions(+)
include/linux/perf_event.h +1 -0
@@ -806,6 +806,7 @@ struct perf_output_handle {struct bpf_perf_event_data_kern {struct pt_regs *regs;struct perf_sample_data *data; +struct perf_event *event; };#ifdef CONFIG_CGROUP_PERF
include/uapi/linux/bpf.h +8 -0
@@ -590,6 +590,13 @@ union bpf_attr {* @buf: buf to fill * @buf_size: size of the buf * Return: 0 on success or negative error code +* +* int bpf_perf_prog_read_value(ctx, buf, buf_size) +* read perf prog attached perf event counter and enabled/running time +* @ctx: pointer to ctx +* @buf: buf to fill +* @buf_size: size of the buf +* Return : 0 on success or negative error code */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -647,6 +654,7 @@ union bpf_attr {FN (sk_redirect_map), \FN (sock_map_update), \FN (perf_event_read_value), \ +FN (perf_prog_read_value), \/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/events/core.c +1 -0
@@ -8081,6 +8081,7 @@ static void bpf_overflow_handler(struct perf_event *event,struct bpf_perf_event_data_kern ctx = { .data = data, .regs = regs, + .event = event, };int ret =0 ;
kernel/trace/bpf_trace.c +23 -0
@@ -603,6 +603,18 @@ BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, flags, 0, 0); } +BPF_CALL_3(bpf_perf_prog_read_value_tp, void *, ctx, struct bpf_perf_event_value *, + buf, u32, size) +{ +struct bpf_perf_event_data_kern *kctx = (struct bpf_perf_event_data_kern *)ctx; + +if (size !=sizeof (struct bpf_perf_event_value )) +return -EINVAL ; + +return perf_event_read_local (kctx->event, &buf->counter, &buf->enabled, + &buf->running); +} +static const struct bpf_func_proto bpf_get_stackid_proto_tp = { .func = bpf_get_stackid_tp, .gpl_only = true,
@@ -612,6 +624,15 @@ static const struct bpf_func_proto bpf_get_stackid_proto_tp = { .arg3_type = ARG_ANYTHING, }; +static const struct bpf_func_proto bpf_perf_prog_read_value_proto_tp = { + .func = bpf_perf_prog_read_value_tp, + .gpl_only = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_PTR_TO_UNINIT_MEM , + .arg3_type =ARG_CONST_SIZE , +}; +static const struct bpf_func_proto *tp_prog_func_proto (enum bpf_func_id func_id) {switch (func_id) {
@@ -619,6 +640,8 @@ static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)return &bpf_perf_event_output_proto_tp;case BPF_FUNC_get_stackid:return &bpf_get_stackid_proto_tp; +case BPF_FUNC_perf_prog_read_value: +return &bpf_perf_prog_read_value_proto_tp;default :return tracing_func_proto (func_id); }
Signature · 1 line
2.9.5
PATCH v3 4/4 bpf: add a test case for helper bpf_perf_prog_read_value
In reply to Yonghong SongThe bpf sample program trace_event is enhanced to use the new helper to print out enabled/running time.
--- samples/bpf/trace_event_kern.c | 10 ++++++++++ samples/bpf/trace_event_user.c | 13 ++++++++----- tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/bpf_helpers.h | 3 +++ 4 files changed, 23 insertions(+), 6 deletions(-)
samples/bpf/trace_event_kern.c +10 -0
@@ -37,10 +37,14 @@ struct bpf_map_def SEC("maps") stackmap = { SEC("perf_event")int bpf_prog1 (struct bpf_perf_event_data *ctx) { +char time_fmt1[] ="Time Enabled: %llu, Time Running: %llu" ; +char time_fmt2[] ="Get Time Failed, ErrCode: %d" ;char fmt[] ="CPU-%d period %lld ip %llx" ;u32 cpu =bpf_get_smp_processor_id (); +struct bpf_perf_event_value value_buf;struct key_t key;u64 *val, one =1 ; +int ret;if (ctx->sample_period <10000 )/* ignore warmup */
@@ -54,6 +58,12 @@ int bpf_prog1(struct bpf_perf_event_data *ctx)return 0 ; } + ret =bpf_perf_prog_read_value (ctx, (void *)&value_buf,sizeof (struct bpf_perf_event_value )); +if (!ret) +bpf_trace_printk (time_fmt1,sizeof (time_fmt1), value_buf.enabled, value_buf.running); +else +bpf_trace_printk (time_fmt2,sizeof (time_fmt2), ret); + val =bpf_map_lookup_elem (&counts, &key);if (val) (*val)++;
samples/bpf/trace_event_user.c +8 -5
@@ -127,6 +127,9 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr)int *pmu_fd =malloc (nr_cpus *sizeof (int ));int i, error =0 ; +/* system wide perf event, no need to inherit */ + attr->inherit =0 ; +/* open perf_event on all cpus */ for (i =0 ; i < nr_cpus; i++) { pmu_fd[i] =sys_perf_event_open (attr,-1 , i,-1 ,0 );
@@ -154,6 +157,11 @@ static void test_perf_event_task(struct perf_event_attr *attr) {int pmu_fd; +/* per task perf event, enable inherit so the "dd ..." command can be traced properly. +* Enabling inherit will cause bpf_perf_prog_read_time helper failure. +*/ + attr->inherit =1 ; +/* open task bound event */ pmu_fd =sys_perf_event_open (attr,0 ,-1 ,-1 ,0 );if (pmu_fd <0 ) {
@@ -175,14 +183,12 @@ static void test_bpf_perf_event(void) .freq = 1, .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES, - .inherit = 1, };struct perf_event_attr attr_type_sw = { .sample_freq =SAMPLE_FREQ , .freq =1 , .type =PERF_TYPE_SOFTWARE , .config =PERF_COUNT_SW_CPU_CLOCK , - .inherit =1 , };struct perf_event_attr attr_hw_cache_l1d = { .sample_freq =SAMPLE_FREQ ,
@@ -192,7 +198,6 @@ static void test_bpf_perf_event(void)PERF_COUNT_HW_CACHE_L1D | (PERF_COUNT_HW_CACHE_OP_READ <<8 ) | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16), - .inherit = 1, };struct perf_event_attr attr_hw_cache_branch_miss = { .sample_freq = SAMPLE_FREQ,
@@ -202,7 +207,6 @@ static void test_bpf_perf_event(void)PERF_COUNT_HW_CACHE_BPU | (PERF_COUNT_HW_CACHE_OP_READ <<8 ) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), - .inherit = 1, };struct perf_event_attr attr_type_raw = { .sample_freq = SAMPLE_FREQ,
@@ -210,7 +214,6 @@ static void test_bpf_perf_event(void) .type = PERF_TYPE_RAW, /* Intel Instruction Retired */ .config = 0xc0, - .inherit = 1, };printf ("Test HW_CPU_CYCLES\n" );
tools/include/uapi/linux/bpf.h +2 -1
@@ -633,7 +633,8 @@ union bpf_attr {FN (redirect_map), \FN (sk_redirect_map), \FN (sock_map_update), \ -FN (perf_event_read_value), +FN (perf_event_read_value), \ +FN (perf_prog_read_value),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
@@ -73,6 +73,9 @@ static int (*bpf_sock_map_update)(void *map, void *key, void *value,static int (*bpf_perf_event_read_value)(void *map,unsigned long long flags,void *buf,unsigned int buf_size) = (void *) BPF_FUNC_perf_event_read_value; +static int (*bpf_perf_prog_read_value)(void *ctx,void *buf, +unsigned int buf_size) = + (void *) BPF_FUNC_perf_prog_read_value;/* llvm builtin functions that eBPF C program may use to
Signature · 1 line
2.9.5