History

perf/core: Use min_heap in visit_groups_merge()

visit_groups_merge will pick the next event based on when it was inserted in to the context (perf_event group_index). Events may be per CPU or for any CPU, but in the future we'd also like to have per cgroup events to avoid searching all events for the events to schedule for a cgroup. Introduce a min heap for the events that maintains a property that the earliest inserted event is always at the 0th element. Initialize the heap with per-CPU and any-CPU events for the context.

Ian Rogers committed 6 years ago

Commit 6eef8a71 parent 6e246281 file · +51 −16Browse files

Unified · Split

kernel/events/core.c+51−16
@@ -49,6 +49,7 @@ · 48 unmodified lines
4949 #include <linux/sched/mm.h>
5050 #include <linux/proc_ns.h>
5151 #include <linux/mount.h>
52+#include <linux/min_heap.h>
5253
5354 #include "internal.h"
5455
@@ -3392,32 +3393,66 @@ static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, · 3337 unmodified lines
33923393 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
33933394 }
33943395
3395−static int visit_groups_merge(struct perf_event_groups *groups, int cpu,
3396− int (*func)(struct perf_event *, void *), void *data)
3396+static bool perf_less_group_idx(const void *l, const void *r)
33973397 {
3398− struct perf_event **evt, *evt1, *evt2;
3398+ const struct perf_event *le = l, *re = r;
3399+
3400+ return le->group_index < re->group_index;
3401+}
3402+
3403+static void swap_ptr(void *l, void *r)
3404+{
3405+ void **lp = l, **rp = r;
3406+
3407+ swap(*lp, *rp);
3408+}
3409+
3410+static const struct min_heap_callbacks perf_min_heap = {
3411+ .elem_size = sizeof(struct perf_event *),
3412+ .less = perf_less_group_idx,
3413+ .swp = swap_ptr,
3414+};
3415+
3416+static void __heap_add(struct min_heap *heap, struct perf_event *event)
3417+{
3418+ struct perf_event **itrs = heap->data;
3419+
3420+ if (event) {
3421+ itrs[heap->nr] = event;
3422+ heap->nr++;
3423+ }
3424+}
3425+
3426+static noinline int visit_groups_merge(struct perf_event_groups *groups,
3427+ int cpu,
3428+ int (*func)(struct perf_event *, void *),
3429+ void *data)
3430+{
3431+ /* Space for per CPU and/or any CPU event iterators. */
3432+ struct perf_event *itrs[2];
3433+ struct min_heap event_heap = {
3434+ .data = itrs,
3435+ .nr = 0,
3436+ .size = ARRAY_SIZE(itrs),
3437+ };
3438+ struct perf_event **evt = event_heap.data;
33993439 int ret;
34003440
3401− evt1 = perf_event_groups_first(groups, -1);
3402− evt2 = perf_event_groups_first(groups, cpu);
3441+ __heap_add(&event_heap, perf_event_groups_first(groups, -1));
3442+ __heap_add(&event_heap, perf_event_groups_first(groups, cpu));
34033443
3404− while (evt1 || evt2) {
3405− if (evt1 && evt2) {
3406− if (evt1->group_index < evt2->group_index)
3407− evt = &evt1;
3408− else
3409− evt = &evt2;
3410− } else if (evt1) {
3411− evt = &evt1;
3412− } else {
3413− evt = &evt2;
3414− }
3444+ min_heapify_all(&event_heap, &perf_min_heap);
34153445
3446+ while (event_heap.nr) {
34163447 ret = func(*evt, data);
34173448 if (ret)
34183449 return ret;
34193450
34203451 *evt = perf_event_groups_next(*evt);
3452+ if (*evt)
3453+ min_heapify(&event_heap, 0, &perf_min_heap);
3454+ else
3455+ min_heap_pop(&event_heap, &perf_min_heap);
34213456 }
34223457
34233458 return 0;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t