From: Mark Rutland <mark.rutland@arm.com>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
Mingwei Zhang <mizhang@google.com>,
Ian Rogers <irogers@google.com>,
Kan Liang <kan.liang@linux.intel.com>
Subject: Re: [PATCH RESEND 1/2] perf/core: Update perf_adjust_freq_unthr_context()
Date: Thu, 11 Jan 2024 10:41:27 +0000 [thread overview]
Message-ID: <ZZ_F1zohd7W0oVat@FVFF77S0Q05N> (raw)
In-Reply-To: <CAM9d7cimwXVYpN7Tk3T6OMRAVo843AHHewndXkefn3r5g8549g@mail.gmail.com>
On Wed, Jan 10, 2024 at 10:27:27AM -0800, Namhyung Kim wrote:
> On Wed, Jan 10, 2024 at 6:49 AM Mark Rutland <mark.rutland@arm.com> wrote:
> > On Tue, Jan 09, 2024 at 01:36:22PM -0800, Namhyung Kim wrote:
> > > It was unnecessarily disabling and enabling PMUs for each event. It
> > > should be done at PMU level. Add pmu_ctx->nr_freq counter to check it
> > > at each PMU. As pmu context has separate active lists for pinned group
> > > and flexible group, factor out a new function to do the job.
> > >
> > > Another minor optimization is that it can skip PMUs w/ CAP_NO_INTERRUPT
> > > even if it needs to unthrottle sampling events.
> > >
> > > Reviewed-by: Ian Rogers <irogers@google.com>
> > > Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
> > > Tested-by: Mingwei Zhang <mizhang@google.com>
> > > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> >
> > Hi,
> >
> > I've taken a quick look and I don't think this is quite right for
> > hybrid/big.LITTLE, but I think that should be relatively simple to fix (more on
> > that below).
>
> Thanks for your review!
No problem!
> > This seems to be a bunch of optimizations; was that based on inspection alone,
> > or have you found a workload where this has a measureable impact?
>
> It's from a code inspection but I think Mingwei reported some excessive
> MSR accesses for KVM use cases. Anyway it'd increase the interrupt \
> latency if you have slow (uncore) PMUs and lots of events on those PMUs.
Makes sense; it would be good if we could put smoething in the commit message
mentioning that.
[...]
> > > +static void
> > > +perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle)
> > > +{
> > > + struct perf_event_pmu_context *pmu_ctx;
> > > +
> > > + /*
> > > + * only need to iterate over all events iff:
> > > + * - context have events in frequency mode (needs freq adjust)
> > > + * - there are events to unthrottle on this cpu
> > > + */
> > > + if (!(ctx->nr_freq || unthrottle))
> > > + return;
> > > +
> > > + raw_spin_lock(&ctx->lock);
> > > +
> > > + list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) {
> > > + if (!(pmu_ctx->nr_freq || unthrottle))
> > > + continue;
> > > + if (pmu_ctx->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT)
> > > + continue;
> > > +
> > > + perf_pmu_disable(pmu_ctx->pmu);
> > > + perf_adjust_freq_unthr_events(&pmu_ctx->pinned_active);
> > > + perf_adjust_freq_unthr_events(&pmu_ctx->flexible_active);
> > > + perf_pmu_enable(pmu_ctx->pmu);
> > > }
> >
> > I don't think this is correct for big.LITTLE/hybrid systems.
> >
> > Imagine a system where CPUs 0-1 have pmu_a, CPUs 2-3 have pmu_b, and a task has
> > events for both pmu_a and pmu_b. The perf_event_context for that task will have
> > a perf_event_pmu_context for each PMU in its pmu_ctx_list.
> >
> > Say that task is run on CPU0, and perf_event_task_tick() is called. That will
> > call perf_adjust_freq_unthr_context(), and it will iterate over the
> > pmu_ctx_list. Note that regardless of pmu_ctx->nr_freq, if 'unthottle' is true,
> > we'll go ahead and call the following for all of the pmu contexts in the
> > pmu_ctx_list:
> >
> > perf_pmu_disable(pmu_ctx->pmu);
> > perf_adjust_freq_unthr_events(&pmu_ctx->pinned_active);
> > perf_adjust_freq_unthr_events(&pmu_ctx->flexible_active);
> > perf_pmu_enable(pmu_ctx->pmu);
> >
> > ... and that means we might call that for pmu_b, even though it's not
> > associated with CPU0. That could be fatal depending on what those callbacks do.
>
> Thanks for pointing that out. I missed the hybrid cases.
>
> > The old logic avoided that possibility implicitly, since the events for pmu_b
> > couldn't be active, and so the check at the start of the look would skip all of
> > pmu_b's events:
> >
> > if (event->state != PERF_EVENT_STATE_ACTIVE)
> > continue;
> >
> > We could do similar by keeping track of how many active events each
> > perf_event_pmu_context has, which'd allow us to do something like:
> >
> > if (pmu_ctx->nr_active == 0)
> > continue;
> >
> > How does that sound to you?
>
> Sounds good. Maybe we can just test if both active lists are empty.
Good idea, I think that'd be simpler and less fragile.
Thanks,
Mark.
prev parent reply other threads:[~2024-01-11 10:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-09 21:36 [PATCH RESEND 1/2] perf/core: Update perf_adjust_freq_unthr_context() Namhyung Kim
2024-01-09 21:36 ` [PATCH RESEND 2/2] perf/core: Reduce PMU access to adjust sample freq Namhyung Kim
2024-01-10 14:49 ` [PATCH RESEND 1/2] perf/core: Update perf_adjust_freq_unthr_context() Mark Rutland
2024-01-10 18:27 ` Namhyung Kim
2024-01-10 18:44 ` Mingwei Zhang
2024-01-11 10:41 ` Mark Rutland [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZZ_F1zohd7W0oVat@FVFF77S0Q05N \
--to=mark.rutland@arm.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=mizhang@google.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox