Return-Path: Received: from mail.linutronix.de (146.0.238.70:993) by crypto-ml.lab.linutronix.de with IMAP4-SSL for ; 06 Feb 2019 01:27:37 -0000 Received: from mga04.intel.com ([192.55.52.120]) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1grBwj-0003yB-Bi for speck@linutronix.de; Wed, 06 Feb 2019 02:24:13 +0100 From: Andi Kleen Subject: [MODERATED] [PATCH v2 2/8] PERFv2 Date: Tue, 5 Feb 2019 17:23:56 -0800 Message-Id: <6837c3d8994cbbc086e2a209fe00fb2f3d166b32.1549416008.git.ak@linux.intel.com> In-Reply-To: References: In-Reply-To: References: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit MIME-Version: 1.0 To: speck@linutronix.de Cc: Andi Kleen List-ID: Add support for excluding GP counters per event in the x86 event scheduler. This is right now used for a bug workaround in a followon patch, but will be also later useful for more efficient PMU virtualization in KVM. Signed-off-by: Andi Kleen --- arch/x86/events/core.c | 24 +++++++++++++++++------- arch/x86/events/intel/uncore.c | 2 +- arch/x86/events/perf_event.h | 3 ++- include/linux/perf_event.h | 1 + 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 58e659bfc2d9..4bad36dfcc8e 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -685,6 +685,7 @@ struct perf_sched { int max_events; int max_gp; int saved_states; + u64 exclude; struct event_constraint **constraints; struct sched_state state; struct sched_state saved[SCHED_STATES_MAX]; @@ -694,7 +695,7 @@ struct perf_sched { * Initialize interator that runs through all events and counters. */ static void perf_sched_init(struct perf_sched *sched, struct event_constraint **constraints, - int num, int wmin, int wmax, int gpmax) + int num, int wmin, int wmax, int gpmax, u64 exclude) { int idx; @@ -703,6 +704,7 @@ static void perf_sched_init(struct perf_sched *sched, struct event_constraint ** sched->max_weight = wmax; sched->max_gp = gpmax; sched->constraints = constraints; + sched->exclude = exclude; for (idx = 0; idx < num; idx++) { if (constraints[idx]->weight == wmin) @@ -745,6 +747,7 @@ static bool __perf_sched_find_counter(struct perf_sched *sched) { struct event_constraint *c; int idx; + u64 idxmsk; if (!sched->state.unassigned) return false; @@ -753,10 +756,12 @@ static bool __perf_sched_find_counter(struct perf_sched *sched) return false; c = sched->constraints[sched->state.event]; + idxmsk = c->idxmsk64 & ~sched->exclude; /* Prefer fixed purpose counters */ - if (c->idxmsk64 & (~0ULL << INTEL_PMC_IDX_FIXED)) { + if (idxmsk & (~0ULL << INTEL_PMC_IDX_FIXED)) { idx = INTEL_PMC_IDX_FIXED; - for_each_set_bit_from(idx, c->idxmsk, X86_PMC_IDX_MAX) { + for_each_set_bit_from(idx, (unsigned long *)&idxmsk, + X86_PMC_IDX_MAX) { if (!__test_and_set_bit(idx, sched->state.used)) goto done; } @@ -764,7 +769,7 @@ static bool __perf_sched_find_counter(struct perf_sched *sched) /* Grab the first unused counter starting with idx */ idx = sched->state.counter; - for_each_set_bit_from(idx, c->idxmsk, INTEL_PMC_IDX_FIXED) { + for_each_set_bit_from(idx, (unsigned long *)&idxmsk, INTEL_PMC_IDX_FIXED) { if (!__test_and_set_bit(idx, sched->state.used)) { if (sched->state.nr_gp++ >= sched->max_gp) return false; @@ -827,11 +832,12 @@ static bool perf_sched_next_event(struct perf_sched *sched) * Assign a counter for each event. */ int perf_assign_events(struct event_constraint **constraints, int n, - int wmin, int wmax, int gpmax, int *assign) + int wmin, int wmax, int gpmax, int *assign, + u64 exclude) { struct perf_sched sched; - perf_sched_init(&sched, constraints, n, wmin, wmax, gpmax); + perf_sched_init(&sched, constraints, n, wmin, wmax, gpmax, exclude); do { if (!perf_sched_find_counter(&sched)) @@ -851,6 +857,7 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) struct perf_event *e; int i, wmin, wmax, unsched = 0; struct hw_perf_event *hwc; + u64 exclude = 0; bitmap_zero(used_mask, X86_PMC_IDX_MAX); @@ -873,6 +880,8 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) hwc = &cpuc->event_list[i]->hw; c = cpuc->event_constraint[i]; + exclude |= hwc->exclude; + /* never assigned */ if (hwc->idx == -1) break; @@ -894,6 +903,7 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) if (i != n) { int gpmax = x86_pmu.num_counters; + gpmax = max_t(int, 0, gpmax - hweight32(exclude)); /* * Do not allow scheduling of more than half the available * generic counters. @@ -909,7 +919,7 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) gpmax /= 2; unsched = perf_assign_events(cpuc->event_constraint, n, wmin, - wmax, gpmax, assign); + wmax, gpmax, assign, exclude); } /* diff --git a/arch/x86/events/intel/uncore.c b/arch/x86/events/intel/uncore.c index d516161c00c4..cfaae41ffbb4 100644 --- a/arch/x86/events/intel/uncore.c +++ b/arch/x86/events/intel/uncore.c @@ -442,7 +442,7 @@ static int uncore_assign_events(struct intel_uncore_box *box, int assign[], int /* slow path */ if (i != n) ret = perf_assign_events(box->event_constraint, n, - wmin, wmax, n, assign); + wmin, wmax, n, assign, 0); if (!assign || ret) { for (i = 0; i < n; i++) diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index 78d7b7031bfc..5b08ae3edafd 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -783,7 +783,8 @@ static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, void x86_pmu_enable_all(int added); int perf_assign_events(struct event_constraint **constraints, int n, - int wmin, int wmax, int gpmax, int *assign); + int wmin, int wmax, int gpmax, int *assign, + u64 exclude); int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign); void x86_pmu_stop(struct perf_event *event, int flags); diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 6cb5d483ab34..e7caf100d2bb 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -125,6 +125,7 @@ struct hw_perf_event { u64 last_tag; unsigned long config_base; unsigned long event_base; + u64 exclude; int event_base_rdpmc; int idx; int last_cpu; -- 2.17.2