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

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/events/core.c+51−16
@@ -49,6 +49,7 @@ · 48 unmodified lines
49#include <linux/sched/mm.h>49#include <linux/sched/mm.h>
50#include <linux/proc_ns.h>50#include <linux/proc_ns.h>
51#include <linux/mount.h>51#include <linux/mount.h>
52#include <linux/min_heap.h>
52 53
53#include "internal.h"54#include "internal.h"
54 55
@@ -3392,32 +3393,66 @@ static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, · 3337 unmodified lines
3392 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);3393 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
3393}3394}
3394 3395
3395static int visit_groups_merge(struct perf_event_groups *groups, int cpu,3396static bool perf_less_group_idx(const void *l, const void *r)
3396 int (*func)(struct perf_event *, void *), void *data)
3397{3397{
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
3403static void swap_ptr(void *l, void *r)
3404{
3405 void **lp = l, **rp = r;
3406
3407 swap(*lp, *rp);
3408}
3409
3410static 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
3416static 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
3426static 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;
3399 int ret;3439 int ret;
3400 3440
3401 evt1 = perf_event_groups_first(groups, -1);3441 __heap_add(&event_heap, perf_event_groups_first(groups, -1));
3402 evt2 = perf_event_groups_first(groups, cpu);3442 __heap_add(&event_heap, perf_event_groups_first(groups, cpu));
3403 3443
3404 while (evt1 || evt2) {3444 min_heapify_all(&event_heap, &perf_min_heap);
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 }
3415 3445
3446 while (event_heap.nr) {
3416 ret = func(*evt, data);3447 ret = func(*evt, data);
3417 if (ret)3448 if (ret)
3418 return ret;3449 return ret;
3419 3450
3420 *evt = perf_event_groups_next(*evt);3451 *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);
3421 }3456 }
3422 3457
3423 return 0;3458 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