Re: [PATCH] perf, x86: Make cycles:p working on SNB

From: Stephane Eranian
Date: Thu May 24 2012 - 03:41:43 EST


On Thu, May 24, 2012 at 9:27 AM, Peter Zijlstra <a.p.zijlstra@xxxxxxxxx> wrote:
> On Thu, 2012-05-24 at 12:02 +0900, Namhyung Kim wrote:
>
>> --- a/arch/x86/kernel/cpu/perf_event_intel.c
>> +++ b/arch/x86/kernel/cpu/perf_event_intel.c
>> @@ -1329,6 +1329,12 @@ static int intel_pmu_hw_config(struct perf_event *event)
>> Â Â Â Â Â Â Â Â*/
>> Â Â Â Â Â Â Â u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
>>
>> + Â Â Â Â Â Â /*
>> + Â Â Â Â Â Â Â* SNB introduced INST_RETIRED.PREC_DIST for this purpose.
>> + Â Â Â Â Â Â Â*/
>> + Â Â Â Â Â Â if (x86_pmu.pebs_constraints == intel_snb_pebs_event_constraints)
>> + Â Â Â Â Â Â Â Â Â Â alt_config = X86_CONFIG(.event=0xc0, .umask=0x01,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â .inv=1, .cmask=16);
>>
>> Â Â Â Â Â Â Â alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
>> Â Â Â Â Â Â Â event->hw.config = alt_config;
>
> That's rather ugly.. but that's okay, I've actually got the patch for
> this still laying around, it needs a bit of an update though.
>
You cannot simply use PREC_DIST. This umask has some severe
restriction. When you measure it, NO other event on the the entire PMU
can be measured at the same time. It needs exclusive mode on SNB.

I don't buy cycles:p in general, but if you really want that what's the
problem with using uops_retired instead?


> Also I'm thinking you're using SNB-EP (you didn't say) since regular SNB
> has PEBS disabled as per (6a600a8b).
>
> Stephane, you could never trigger the badness on EP, but ISTR you saying
> it was in fact affected by whatever Intel found? So should we mark that
> as bad as well?
>
I never could but was told it was there too.

> Also, do you happen to know if/when a u-code update would appear?
>
I am hoping Intel will release the ucode update very soon now. I will
post a patch
to re-enable PEBS on model 42 when that happens.

> ---
> Subject: perf, x86: Fix cycles:pp for SandyBridge
> From: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Date: Fri, 15 Jul 2011 21:17:34 +0200
>
> Intel SNB doesn't support INST_RETIRED as a PEBS event, so implement
> the CPU_CLK_UNHALTED alias using UOPS_RETIRED in much the same fasion.
>
> The UOPS_RETIRED thing would work for NHM,WSM,SNB, but Core2 and Atom
> really need the old one, so for now only use the new one for SNB.
>
> Reported-and-tested-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
> ---
> Âarch/x86/kernel/cpu/perf_event.c    |  Â1
> Âarch/x86/kernel/cpu/perf_event_intel.c | Â 68 +++++++++++++++++++++++++--------
> Â2 files changed, 53 insertions(+), 16 deletions(-)
>
> Index: linux-2.6/arch/x86/kernel/cpu/perf_event.c
> ===================================================================
> --- linux-2.6.orig/arch/x86/kernel/cpu/perf_event.c
> +++ linux-2.6/arch/x86/kernel/cpu/perf_event.c
> @@ -316,6 +316,7 @@ struct x86_pmu {
>    Âint       pebs_record_size;
>    Âvoid      Â(*drain_pebs)(struct pt_regs *regs);
> Â Â Â Âstruct event_constraint *pebs_constraints;
> +    void      Â(*pebs_aliases)(struct perf_event *event);
>
> Â Â Â Â/*
> Â Â Â Â * Intel LBR
> Index: linux-2.6/arch/x86/kernel/cpu/perf_event_intel.c
> ===================================================================
> --- linux-2.6.orig/arch/x86/kernel/cpu/perf_event_intel.c
> +++ linux-2.6/arch/x86/kernel/cpu/perf_event_intel.c
> @@ -1241,8 +1241,30 @@ static int intel_pmu_hw_config(struct pe
> Â Â Â Âif (ret)
> Â Â Â Â Â Â Â Âreturn ret;
>
> - Â Â Â if (event->attr.precise_ip &&
> - Â Â Â Â Â (event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
> + Â Â Â if (event->attr.precise_ip && x86_pmu.pebs_aliases)
> + Â Â Â Â Â Â Â x86_pmu.pebs_aliases(event);
> +
> +
> + Â Â Â if (event->attr.type != PERF_TYPE_RAW)
> + Â Â Â Â Â Â Â return 0;
> +
> + Â Â Â if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
> + Â Â Â Â Â Â Â return 0;
> +
> + Â Â Â if (x86_pmu.version < 3)
> + Â Â Â Â Â Â Â return -EINVAL;
> +
> + Â Â Â if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
> + Â Â Â Â Â Â Â return -EACCES;
> +
> + Â Â Â event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
> +
> + Â Â Â return 0;
> +}
> +
> +static void intel_pebs_aliases_core2(struct perf_event *event)
> +{
> + Â Â Â if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
> Â Â Â Â Â Â Â Â/*
> Â Â Â Â Â Â Â Â * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
> Â Â Â Â Â Â Â Â * (0x003c) so that we can use it with PEBS.
> @@ -1266,22 +1288,34 @@ static int intel_pmu_hw_config(struct pe
> Â Â Â Â Â Â Â Âalt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
> Â Â Â Â Â Â Â Âevent->hw.config = alt_config;
> Â Â Â Â}
> +}
>
> - Â Â Â if (event->attr.type != PERF_TYPE_RAW)
> - Â Â Â Â Â Â Â return 0;
> -
> - Â Â Â if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
> - Â Â Â Â Â Â Â return 0;
> -
> - Â Â Â if (x86_pmu.version < 3)
> - Â Â Â Â Â Â Â return -EINVAL;
> -
> - Â Â Â if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
> - Â Â Â Â Â Â Â return -EACCES;
> -
> - Â Â Â event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
> +static void intel_pebs_aliases_snb(struct perf_event *event)
> +{
> + Â Â Â if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
> + Â Â Â Â Â Â Â /*
> + Â Â Â Â Â Â Â Â* Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
> + Â Â Â Â Â Â Â Â* (0x003c) so that we can use it with PEBS.
> + Â Â Â Â Â Â Â Â*
> + Â Â Â Â Â Â Â Â* The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
> + Â Â Â Â Â Â Â Â* PEBS capable. However we can use UOPS_RETIRED.ALL
> + Â Â Â Â Â Â Â Â* (0x01c2), which is a PEBS capable event, to get the same
> + Â Â Â Â Â Â Â Â* count.
> + Â Â Â Â Â Â Â Â*
> + Â Â Â Â Â Â Â Â* UOPS_RETIRED.ALL counts the number of cycles that retires
> + Â Â Â Â Â Â Â Â* CNTMASK uops. By setting CNTMASK to a value (16)
> + Â Â Â Â Â Â Â Â* larger than the maximum number of uops that can be
> + Â Â Â Â Â Â Â Â* retired per cycle (4) and then inverting the condition, we
> + Â Â Â Â Â Â Â Â* count all cycles that retire 16 or less uops, which
> + Â Â Â Â Â Â Â Â* is every cycle.
> + Â Â Â Â Â Â Â Â*
> + Â Â Â Â Â Â Â Â* Thereby we gain a PEBS capable cycle counter.
> + Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â u64 alt_config = 0x108001c2; /* UOPS_RETIRED.TOTAL_CYCLES */
>
> - Â Â Â return 0;
> + Â Â Â Â Â Â Â alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
> + Â Â Â Â Â Â Â event->hw.config = alt_config;
> + Â Â Â }
> Â}
>
> Âstatic __initconst const struct x86_pmu core_pmu = {
> @@ -1409,6 +1443,7 @@ static __initconst const struct x86_pmu
>    Â.max_period       = (1ULL << 31) - 1,
> Â Â Â Â.get_event_constraints Â= intel_get_event_constraints,
> Â Â Â Â.put_event_constraints Â= intel_put_event_constraints,
> +    .pebs_aliases      = intel_pebs_aliases_core2,
>
>    Â.cpu_prepare      Â= intel_pmu_cpu_prepare,
>    Â.cpu_starting      = intel_pmu_cpu_starting,
> @@ -1597,6 +1632,7 @@ static __init int intel_pmu_init(void)
>
> Â Â Â Â Â Â Â Âx86_pmu.event_constraints = intel_snb_event_constraints;
> Â Â Â Â Â Â Â Âx86_pmu.pebs_constraints = intel_snb_pebs_events;
> + Â Â Â Â Â Â Â x86_pmu.pebs_aliases = intel_pebs_aliases_snb,
> Â Â Â Â Â Â Â Âx86_pmu.extra_regs = intel_snb_extra_regs;
> Â Â Â Â Â Â Â Â/* all extra regs are per-cpu when HT is on */
> Â Â Â Â Â Â Â Âx86_pmu.er_flags |= ERF_HAS_RSP_1;
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/