perf/core: Reduce PMU access to adjust sample freq
authorNamhyung Kim <namhyung@kernel.org>
Wed, 7 Feb 2024 05:05:45 +0000 (21:05 -0800)
committerIngo Molnar <mingo@kernel.org>
Wed, 10 Apr 2024 04:13:57 +0000 (06:13 +0200)
In perf_adjust_freq_unthr_context(), it first starts the event and then
stop unnecessarily to adjust the sampling frequency if the event is
throttled.

For a throttled non-frequency event, it doesn't have a freq so no need
to adjust.  Just starting the event would be ok.

For a frequency event, whether it's throttled or not, it needs to stop
before adjusting the frequency.  That means it should not start the
even if it was throttled.  I tried to skip calling the stop callback,
but it didn't work well since the event count might not be up to date.
It should call the stop callback with PERF_EF_UPDATE anyway.

However not calling start would prevent unnecessary MSR accesses (which
can be costly) for already stopped events as stop state is saved in the
hw config.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Link: https://lore.kernel.org/r/20240207050545.2727923-2-namhyung@kernel.org
kernel/events/core.c

index 9566cfb273551c416eb77c1ad84517c0fa8ce4a3..fd94e45a9d869294c2e3add556e4e712e6a883a5 100644 (file)
@@ -4146,7 +4146,8 @@ static void perf_adjust_freq_unthr_events(struct list_head *event_list)
                if (hwc->interrupts == MAX_INTERRUPTS) {
                        hwc->interrupts = 0;
                        perf_log_throttle(event, 1);
-                       event->pmu->start(event, 0);
+                       if (!event->attr.freq || !event->attr.sample_freq)
+                               event->pmu->start(event, 0);
                }
 
                if (!event->attr.freq || !event->attr.sample_freq)