Re: [PATCH bpf-next] bpf: Add bpf_read_raw_record() helper
From: Namhyung Kim
Date: Fri Aug 26 2022 - 01:22:32 EST
On Thu, Aug 25, 2022 at 7:35 PM Song Liu <songliubraving@xxxxxx> wrote:
>
>
>
> > On Aug 25, 2022, at 4:03 PM, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote:
> >
> > On Thu, Aug 25, 2022 at 3:08 PM Song Liu <songliubraving@xxxxxx> wrote:
> >>
> >>
> >>
> >>> On Aug 25, 2022, at 2:33 PM, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote:
> >>>
> >>> On Tue, Aug 23, 2022 at 2:04 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
> >>>> + * long bpf_read_raw_record(struct bpf_perf_event_data *ctx, void *buf, u32 size, u64 flags)
> >>>> + * Description
> >>>> + * For an eBPF program attached to a perf event, retrieve the
> >>>> + * raw record associated to *ctx* and store it in the buffer
> >>>> + * pointed by *buf* up to size *size* bytes.
> >>>> + * Return
> >>>> + * On success, number of bytes written to *buf*. On error, a
> >>>> + * negative value.
> >>>> + *
> >>>> + * The *flags* can be set to **BPF_F_GET_RAW_RECORD_SIZE** to
> >>>> + * instead return the number of bytes required to store the raw
> >>>> + * record. If this flag is set, *buf* may be NULL.
> >>>
> >>> It looks pretty ugly from a usability standpoint to have one helper
> >>> doing completely different things and returning two different values
> >>> based on BPF_F_GET_RAW_RECORD_SIZE.
> >>
> >> Yeah, I had the same thought when I first looked at it. But that's the
> >> exact syntax with bpf_read_branch_records(). Well, we still have time
> >> to fix the new helper..
> >>
> >>>
> >>> I'm not sure what's best, but I have two alternative proposals:
> >>>
> >>> 1. Add two helpers: one to get perf record information (and size will
> >>> be one of them). Something like bpf_perf_record_query(ctx, flags)
> >>> where you pass perf ctx and what kind of information you want to read
> >>> (through flags), and u64 return result returns that (see
> >>> bpf_ringbuf_query() for such approach). And then have separate helper
> >>> to read data.
> >>>
> >>> 2. Keep one helper, but specify that it always returns record size,
> >>> even if user specified smaller size to read. And then allow passing
> >>> buf==NULL && size==0. So passing NULL, 0 -- you get record size.
> >>> Passing non-NULL buf -- you read data.
> >>
> >> AFAICT, this is also confusing.
> >>
> >
> > this is analogous to snprintf() behavior, so not that new and
> > surprising when you think about it. But if query + read makes more
> > sense, then it's fine by me
>
> Given the name discussion (the other email), I now like one API better.
>
> Actually, since we are on this, can we make it more generic, and handle
> all possible PERF_SAMPLE_* (in enum perf_event_sample_format)? Something
> like:
>
> long bpf_perf_event_read_sample(void *ctx, void *buf, u64 size, u64 flags);
>
> WDYT Namhyung?
Do you mean reading the whole sample data at once?
Then it needs to parse the sample data format properly
which is non trivial due to a number of variable length
fields like callchains and branch stack, etc.
Also I'm afraid I might need event configuration info
other than sample data like attr.type, attr.config,
attr.sample_type and so on.
Hmm.. maybe we can add it to the ctx directly like ctx.attr_type?
>
> Another idea is to add another parameter, so that we can pick which
> PERF_SAMPLE_* to output via bpf_perf_event_read_sample().
>
> I think this will cover all cases with sample perf_event. Thoughts?
Yeah, I like this more and it looks easier to use.
Thanks,
Namhyung