Updated perf offcore patchkit
Subsystems (2)
Rebased on top of tip perf/perf-core with the new initcall sequence. Now uses reference counts and kzalloc_node() to allocate the per node data.
I dropped the patch that was already in tip.
-Andi
PATCH 1/3 perf: Document enhanced event encoding for OFFCORE_MSR
From: Andi Kleen <[email protected]>
--- tools/perf/Documentation/perf-list.txt | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
tools/perf/Documentation/perf-list.txt +7 -0
@@ -58,6 +58,13 @@ raw encoding of 0x1A8 can be used: perf stat -e r1a8 -a sleep 1 perf record -e r1a8 ... +Some special events on x86 encode additional data in the normally unused +[32;63] bits of the raw value. This is particularly used for +the OFFCORE_RESPONSE events on Intel Core i7. The 16bit +mask in the OFFCORE_RESPONSE register is put into bits [32;48]. +For example the OFFCORE_RESPONSE_0.ANY_DATA.ANY_CACHE_DRAM event +is encoded as r00007f11004301b7. + You should refer to the processor specific documentation for getting these details. Some of them are referenced in the SEE ALSO section below.
Signature · 1 line
1.7.1
PATCH 2/3 perf-events: Add support for supplementary event registers v4
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Intel Nehalem/Westmere have a special OFFCORE_RESPONSE event that can be used to monitor any offcore accesses from a core. This is a very useful event for various tunings, and it's also needed to implement the generic LLC-* events correctly.
Unfortunately this event requires programming a mask in a separate register. And worse this separate register is per core, not per CPU thread.
This patch adds:
- Teaches perf_events that OFFCORE_RESPONSE needs extra parameters. The extra parameters are passed by user space in the unused upper 32bits of the config word.
- Add support to the Intel perf_event core to schedule per core resources. This adds fairly generic infrastructure that can be also used for other per core resources.
The basic code has is patterned after the similar AMD northbridge constraints code.
Thanks to Stephane Eranian who pointed out some problems in the original version and suggested improvements.
--- arch/x86/kernel/cpu/perf_event.c | 70 +++++++++++ arch/x86/kernel/cpu/perf_event_intel.c | 198 ++++++++++++++++++++++++++++++++ include/linux/perf_event.h | 2 + 3 files changed, 270 insertions(+), 0 deletions(-)
arch/x86/kernel/cpu/perf_event.c +70 -0
@@ -93,6 +93,8 @@ struct amd_nb {struct event_constraint event_constraints[X86_PMC_IDX_MAX ]; }; +struct intel_percore ; +#define MAX_LBR_ENTRIES 16struct cpu_hw_events {
@@ -128,6 +130,13 @@ struct cpu_hw_events {struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES ];/* +* Intel percore register state. +* Coordinate shared resources between HT threads. +*/ +int percore_used;/* Used by this CPU? */ +struct intel_percore *per_core; + +/* * AMD specific bits */ struct amd_nb *amd_nb;
@@ -175,6 +184,32 @@ struct cpu_hw_events {#define for_each_event_constraint (e, c) \ for ((e) = (c); (e)->weight; (e)++) +/* +* Extra registers for specific events. +* Some events need large masks and require external MSRs. +* Define a mapping to these extra registers. +* The actual contents are still encoded in unused parts of the +* original config u64. +*/ +struct extra_reg { +unsigned int event; +unsigned int msr; +unsigned int extra_shift; +u64 config_mask; +u64 valid_mask; +}; + +#define EVENT_EXTRA_REG (e, ms, m, vm, es) { \ + .event = (e), \ + .msr = (ms), \ + .config_mask = (m), \ + .valid_mask = (vm), \ + .extra_shift = (es), \ + } +#define INTEL_EVENT_EXTRA_REG (event, msr, vm, es) \ + EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, es) +#define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, 0) + union perf_capabilities { struct { u64 lbr_format : 6;
@@ -219,6 +254,7 @@ struct x86_pmu {void (*put_event_constraints)(struct cpu_hw_events *cpuc,struct perf_event *event);struct event_constraint *event_constraints; +struct event_constraint *percore_constraints;void (*quirks)(void );int perfctr_second_write;
@@ -247,6 +283,11 @@ struct x86_pmu {*/ unsigned long lbr_tos, lbr_from, lbr_to;/* MSR base regs */ int lbr_nr;/* hardware stack size */ + +/* +* Extra registers for events +*/ +struct extra_reg *extra_regs; };static struct x86_pmu x86_pmu__read_mostly ;
@@ -321,6 +362,33 @@ again:return new_raw_count; } +/* +* Find and validate any extra registers to set up. +*/ +static int x86_pmu_extra_regs (u64 config,struct perf_event *event) +{ +struct extra_reg *er; +u64 extra; + + event->hw.extra_reg =0 ; + event->hw.extra_config =0 ; + +if (!x86_pmu.extra_regs) +return 0 ; + +for (er = x86_pmu.extra_regs; er->msr; er++) { +if (er->event != (config & er->config_mask)) +continue ; + event->hw.extra_reg = er->msr; + extra = config >> er->extra_shift; +if (extra & ~er->valid_mask) +return -EINVAL ; + event->hw.extra_config = extra; +break ; + } +return 0 ; +} +static atomic_t active_events;static DEFINE_MUTEX (pmc_reserve_mutex);
@@ -880,6 +948,8 @@ static void x86_pmu_enable(struct pmu *pmu)static inline void __x86_pmu_enable_event (struct hw_perf_event *hwc,u64 enable_mask) { +if (hwc->extra_reg) +wrmsrl (hwc->extra_reg, hwc->extra_config);wrmsrl (hwc->config_base + hwc->idx, hwc->config | enable_mask); }
arch/x86/kernel/cpu/perf_event_intel.c +198 -0
268 lines not on this page · raw message
include/linux/perf_event.h +2 -0
@@ -529,6 +529,8 @@ struct hw_perf_event {unsigned long event_base;int idx;int last_cpu; +unsigned int extra_reg; +u64 extra_config; };struct {/* software */ struct hrtimer hrtimer;
Signature · 1 line
1.7.1
Peter Zijlstra
maintainerRe: [PATCH 2/3] perf-events: Add support for supplementary event registers v4
Quoted 118 lines · Andi Kleen
arch/x86/kernel/cpu/perf_event.c +65
-0@@ -93,6 +93,8 @@ struct amd_nb {struct event_constraint event_constraints[X86_PMC_IDX_MAX ]; }; +struct intel_percore ; +#define MAX_LBR_ENTRIES 16struct cpu_hw_events {@@ -128,6 +130,13 @@ struct cpu_hw_events {struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES ];/* +* Intel percore register state. +* Coordinate shared resources between HT threads. +*/ +int percore_used;/* Used by this CPU? */ +struct intel_percore *per_core; + +/* * AMD specific bits */ struct amd_nb *amd_nb; +/* +* Per core state +* This used to coordinate shared registers for HT threads. +*/ +struct intel_percore { +raw_spinlock_t lock;/* protect structure */ +struct er_account regs[MAX_EXTRA_REGS ]; +int refcnt;/* number of threads */ +unsigned core_id; +}; +/* * Intel PerfMon, used on Core and later. */ +static int intel_pmu_cpu_prepare (int cpu) +{ +struct cpu_hw_events *cpuc = &per_cpu (cpu_hw_events, cpu); + + cpuc->per_core =kzalloc_node (sizeof (struct intel_percore ), +GFP_KERNEL ,cpu_to_node (cpu)); +if (!cpuc->per_core) +return NOTIFY_BAD ; + +raw_spin_lock_init (&cpuc->per_core->lock); + cpuc->per_core->core_id =-1 ; +return NOTIFY_OK ; +} +static void intel_pmu_cpu_starting (int cpu) { +struct cpu_hw_events *cpuc = &per_cpu (cpu_hw_events, cpu); +int core_id =topology_core_id (cpu); +int i; + +for_each_online_cpu (i) { +struct intel_percore *pc =per_cpu (cpu_hw_events, i).per_core; + +if (pc && pc->core_id == core_id) { +kfree (cpuc->per_core); + cpuc->per_core = pc; +break ; + } + } + + cpuc->per_core->core_id = core_id; + cpuc->per_core->refcnt++; +init_debug_store_on_cpu (cpu);/* * Deal with CPUs that don't clear their LBRs on power-up. @@ -868,6 +1049,15 @@ static void intel_pmu_cpu_starting(int cpu)static void intel_pmu_cpu_dying (int cpu) { +struct cpu_hw_events *cpuc = &per_cpu (cpu_hw_events, cpu); +struct intel_percore *pc = cpuc->per_core; + +if (pc) { +if (pc->core_id ==-1 || --pc->refcnt ==0 ) +kfree (pc); + cpuc->per_core =NULL ; + } +fini_debug_store_on_cpu (cpu); }@@ -892,7 +1082,9 @@ static __initconst const struct x86_pmu intel_pmu = {*/ .max_period = (1ULL << 31) - 1, .get_event_constraints = intel_get_event_constraints, + .put_event_constraints = intel_put_event_constraints, + .cpu_prepare = intel_pmu_cpu_prepare, .cpu_starting = intel_pmu_cpu_starting, .cpu_dying = intel_pmu_cpu_dying, };@@ -1010,7 +1202,10 @@ static __init int intel_pmu_init(void)intel_pmu_lbr_init_nhm (); x86_pmu.event_constraints = intel_nehalem_event_constraints; + x86_pmu.percore_constraints = + intel_nehalem_percore_constraints; x86_pmu.enable_all = intel_pmu_nhm_enable_all; + x86_pmu.extra_regs = intel_nehalem_extra_regs;pr_cont ("Nehalem events, " );break ;
arch/x86/kernel/cpu/perf_event.c static __init int intel_pmu_init(void) · line 1236
Quoted 10 lines
arch/x86/kernel/cpu/perf_event.c +3
-0@@ -1032,7 +1227,10 @@ static __init int intel_pmu_init(void)intel_pmu_lbr_init_nhm (); x86_pmu.event_constraints = intel_westmere_event_constraints; + x86_pmu.percore_constraints = + intel_westmere_percore_constraints; x86_pmu.enable_all = intel_pmu_nhm_enable_all; + x86_pmu.extra_regs = intel_westmere_extra_regs;pr_cont ("Westmere events, " );break ;
You seem to have lost the needs_percore stuff.
PATCH 3/3 perf-events: Fix LLC-* events on Intel Nehalem/Westmere v2
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
The generic perf LLC-* events do count the L2 caches, not the real L3 LLC on Intel Nehalem and Westmere. This lead to quite some confusion.
Fixing this properly requires use of the special OFFCORE_RESPONSE events which need a separate mask register. This has been implemented in a earlier patch.
Now use this infrastructure to set correct events for the LLC-* on Nehalem and Westmere
--- arch/x86/kernel/cpu/perf_event.c | 12 ++++---- arch/x86/kernel/cpu/perf_event_intel.c | 46 +++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 19 deletions(-)
arch/x86/kernel/cpu/perf_event.c +6 -6
@@ -472,8 +472,9 @@ static inline int x86_pmu_initialized(void) }static inline int -set_ext_hw_attr (struct hw_perf_event *hwc,struct perf_event_attr *attr) +set_ext_hw_attr (struct hw_perf_event *hwc,struct perf_event *event) { +struct perf_event_attr *attr = &event->attr;unsigned int cache_type, cache_op, cache_result;u64 config, val;
@@ -499,9 +500,8 @@ set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr)if (val ==-1 )return -EINVAL ; - hwc->config |= val; - -return 0 ; + hwc->config |= val &X86_RAW_EVENT_MASK ; +return x86_pmu_extra_regs (val, event); }static int x86_setup_perfctr (struct perf_event *event)
@@ -526,10 +526,10 @@ static int x86_setup_perfctr(struct perf_event *event) }if (attr->type ==PERF_TYPE_RAW ) -return 0 ; +return x86_pmu_extra_regs (event->attr.config, event);if (attr->type ==PERF_TYPE_HW_CACHE ) -return set_ext_hw_attr (hwc, attr); +return set_ext_hw_attr (hwc, event);if (attr->config >= x86_pmu.max_events)return -EINVAL ;
arch/x86/kernel/cpu/perf_event_intel.c +33 -13
72 lines not on this page · raw message
Signature · 1 line
1.7.1