Re: [PATCH -next v3 1/2] perf stat: Support inherit events during fork() for bperf

Namhyung Kim
2024-10-10T00:31:36Z

On Wed, Oct 09, 2024 at 10:18:44AM -0700, Song Liu wrote:
> On Sun, Sep 15, 2024 at 6:53 PM Tengda Wu <wutengda@huaweicloud.com> wrote:
> >
> > bperf has a nice ability to share PMUs, but it still does not support
> > inherit events during fork(), resulting in some deviations in its stat
> > results compared with perf.
> >
> > perf stat result:
> > $ ./perf stat -e cycles,instructions -- ./perf test -w sqrtloop
> >
> > Performance counter stats for './perf test -w sqrtloop':
> >
> > 2,316,038,116 cycles
> > 2,859,350,725 instructions
> >
> > 1.009603637 seconds time elapsed
> >
> > 1.004196000 seconds user
> > 0.003950000 seconds sys
> >
> > bperf stat result:
> > $ ./perf stat --bpf-counters -e cycles,instructions -- \
> > ./perf test -w sqrtloop
> >
> > Performance counter stats for './perf test -w sqrtloop':
> >
> > 18,762,093 cycles
> > 23,487,766 instructions
> >
> > 1.008913769 seconds time elapsed
> >
> > 1.003248000 seconds user
> > 0.004069000 seconds sys
> >
> > In order to support event inheritance, two new bpf programs are added
> > to monitor the fork and exit of tasks respectively. When a task is
> > created, add it to the filter map to enable counting, and reuse the
> > `accum_key` of its parent task to count together with the parent task.
> > When a task exits, remove it from the filter map to disable counting.
> >
> > After support:
> > $ ./perf stat --bpf-counters -e cycles,instructions -- \
> > ./perf test -w sqrtloop
> >
> > Performance counter stats for './perf test -w sqrtloop':
> >
> > 2,316,252,189 cycles
> > 2,859,946,547 instructions
> >
> > 1.009422314 seconds time elapsed
> >
> > 1.003597000 seconds user
> > 0.004270000 seconds sys
> >
> > Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
>
> The solution looks good to me. Question on the UI: do we always
> want the inherit behavior from PID and TGID monitoring? If not,
> maybe we should add a flag for it. (I think we do need the flag).

I think it should depend on the value of attr.inherit. Maybe we can
disable the autoload for !inherit.

>
> One nitpick below.
>
> Thanks,
> Song
>
> [...]
> >
> > +struct bperf_filter_value {
> > + __u32 accum_key;
> > + __u8 exited;
> > +};
> nit:
> Can we use a special value of accum_key to replace exited==1
> case?

I'm not sure. I guess it still needs to use the accum_key to save the
final value when exited = 1.

Thanks,
Namhyung

>
> > +
> > #endif /* __BPERF_STAT_U_H */
> > --
> > 2.34.1
> >