[PATCH v6 4/6] perf: Add per perf_cpu_context min_heap storage

From: Ian Rogers
Date: Fri Feb 14 2020 - 02:51:57 EST


The storage required for visit_groups_merge's min heap needs to vary in
order to support more iterators, such as when multiple nested cgroups'
events are being visited. This change allows for 2 iterators and doesn't
support growth.
Based-on-work-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
include/linux/perf_event.h | 7 +++++
kernel/events/core.c | 53 ++++++++++++++++++++++++++------------
2 files changed, 44 insertions(+), 16 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 68e21e828893..5060e31b32cc 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -862,6 +862,13 @@ struct perf_cpu_context {
int sched_cb_usage;

int online;
+ /*
+ * Per-CPU storage for iterators used in visit_groups_merge. The default
+ * storage is of size 2 to hold the CPU and any CPU event iterators.
+ */
+ int itr_storage_cap;
+ struct perf_event **itr_storage;
+ struct perf_event *itr_default[2];
};

struct perf_output_handle {
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 832e2a56a663..18e4bb871d85 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3419,36 +3419,48 @@ static void __heap_add(struct min_heap *heap, struct perf_event *event)
}
}

-static noinline int visit_groups_merge(struct perf_event_groups *groups,
- int cpu,
+static noinline int visit_groups_merge(struct perf_cpu_context *cpuctx,
+ struct perf_event_groups *groups, int cpu,
int (*func)(struct perf_event *, void *),
void *data)
{
/* Space for per CPU and/or any CPU event iterators. */
struct perf_event *itrs[2];
- struct min_heap event_heap = {
- .data = itrs,
- .size = 0,
- .cap = ARRAY_SIZE(itrs),
- };
+ struct min_heap event_heap;
+ struct perf_event **evt;
struct perf_event *next;
int ret;

- __heap_add(&event_heap, perf_event_groups_first(groups, -1));
+ if (cpuctx) {
+ event_heap = (struct min_heap){
+ .data = cpuctx->itr_storage,
+ .size = 0,
+ .cap = cpuctx->itr_storage_cap,
+ };
+ } else {
+ event_heap = (struct min_heap){
+ .data = itrs,
+ .size = 0,
+ .cap = ARRAY_SIZE(itrs),
+ };
+ /* Events not within a CPU context may be on any CPU. */
+ __heap_add(&event_heap, perf_event_groups_first(groups, -1));
+ }
+ evt = event_heap.data;
+
__heap_add(&event_heap, perf_event_groups_first(groups, cpu));

min_heapify_all(&event_heap, &perf_min_heap);

while (event_heap.size) {
- ret = func(itrs[0], data);
+ ret = func(*evt, data);
if (ret)
return ret;

- next = perf_event_groups_next(itrs[0]);
- if (next) {
- min_heap_pop_push(&event_heap, &next,
- &perf_min_heap);
- } else
+ next = perf_event_groups_next(*evt);
+ if (next)
+ min_heap_pop_push(&event_heap, &next, &perf_min_heap);
+ else
min_heap_pop(&event_heap, &perf_min_heap);
}

@@ -3519,7 +3531,10 @@ ctx_pinned_sched_in(struct perf_event_context *ctx,
.can_add_hw = 1,
};

- visit_groups_merge(&ctx->pinned_groups,
+ if (ctx != &cpuctx->ctx)
+ cpuctx = NULL;
+
+ visit_groups_merge(cpuctx, &ctx->pinned_groups,
smp_processor_id(),
pinned_sched_in, &sid);
}
@@ -3534,7 +3549,10 @@ ctx_flexible_sched_in(struct perf_event_context *ctx,
.can_add_hw = 1,
};

- visit_groups_merge(&ctx->flexible_groups,
+ if (ctx != &cpuctx->ctx)
+ cpuctx = NULL;
+
+ visit_groups_merge(cpuctx, &ctx->flexible_groups,
smp_processor_id(),
flexible_sched_in, &sid);
}
@@ -10395,6 +10413,9 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type)
cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask);

__perf_mux_hrtimer_init(cpuctx, cpu);
+
+ cpuctx->itr_storage_cap = ARRAY_SIZE(cpuctx->itr_default);
+ cpuctx->itr_storage = cpuctx->itr_default;
}

got_cpu_context:
--
2.25.0.265.gbab2e86ba0-goog