Skip to contentSkip to conversation

x86/mbm : Intel Memory Bandwidth monitoring support

PATCH Superseded Vikas Shivappa 3 participants 15 messages Feb 11, 2016 – Feb 24, 2016

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover x86/mbm : Intel Memory Bandwidth monitoring support

The V4 version of MBM is almost a complete rewrite of the prior versions. It tries to address all of Thomas earlier comments.

The patch series has one preparatory patch for cqm and then 4 MBM patches. *Patches apply on 4.5-rc1*.

Memory bandwitdh monitoring(MBM) provides OS/VMM a way to monitor bandwidth from one level of cache to another. The current patches support L3 external bandwitch monitoring.
It supports both 'local bandwidth' and 'total bandwidth' monitoring for the socket. Local bandwidth measures the amount of data sent through the memory controller on the socket and total b/w measures the total system bandwidth.

The tasks are associated with a Resouce Monitoring ID(RMID) just like in cqm and OS uses a MSR write to indicate the RMID of the task during scheduling.

[PATCH 1/5] x86,perf/cqm : Fix cqm handling of grouping events into a [PATCH 2/5] x86/mbm: Intel Memory B/W Monitoring enumeration and init [PATCH 3/5] x86/mbm: Memory bandwidth monitoring event management [PATCH 4/5] x86/mbm: RMID Recycling MBM changes
[PATCH 5/5] x86/mbm: Add support for MBM counter overflow handling

#1 lore raw

PATCH v4 1/5 x86,perf/cqm : Fix cqm handling of grouping events into a cache_group

Currently cqm(cache quality of service monitoring) is grouping all events belonging to same PID to use one RMID. However its not counting all of these different events. Hence we end up with a count of zero for all events other than the group leader. The patch tries to address the issue by keeping a flag in the perf_event.hw which has other cqm related fields. The field is updated at event creation and during grouping.

---
 arch/x86/kernel/cpu/perf_event_intel_cqm.c | 13 ++++++++++---
 include/linux/perf_event.h                 |  1 +
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -3
index a316ca9..e6be335 100644
@@ -281,9 +281,13 @@ static bool __match_event(struct perf_event *a, struct perf_event *b)
 
 	/*
 	 * Events that target same task are placed into the same cache group.
+	 * Mark it as a multi event group, so that we update ->count
+	 * for every event rather than just the group leader later.
 	 */
-	if (a->hw.target == b->hw.target)
+	if (a->hw.target == b->hw.target) {
+		b->hw.is_group_event = true;
 		return true;
+	}
 
 	/*
 	 * Are we an inherited event?
@@ -849,6 +853,7 @@ static void intel_cqm_setup_event(struct perf_event *event,
 	bool conflict = false;
 	u32 rmid;
 
+	event->hw.is_group_event = false;
 	list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {
 		rmid = iter->hw.cqm_rmid;
 
@@ -940,7 +945,9 @@ static u64 intel_cqm_event_count(struct perf_event *event)
 		return __perf_event_count(event);
 
 	/*
-	 * Only the group leader gets to report values. This stops us
+	 * Only the group leader gets to report values except in case of
+	 * multiple events in the same group, we still need to read the
+	 * other events.This stops us
 	 * reporting duplicate values to userspace, and gives us a clear
 	 * rule for which task gets to report the values.
 	 *
@@ -948,7 +955,7 @@ static u64 intel_cqm_event_count(struct perf_event *event)
 	 * specific packages - we forfeit that ability when we create
 	 * task events.
 	 */
-	if (!cqm_group_leader(event))
+	if (!cqm_group_leader(event) && !event->hw.is_group_event)
 		return 0;
 
 	/*
include/linux/perf_event.h +1 -0
index f9828a4..f11c732 100644
@@ -121,6 +121,7 @@ struct hw_perf_event {
 		struct { /* intel_cqm */
 			int			cqm_state;
 			u32			cqm_rmid;
+			bool			is_group_event;
 			struct list_head	cqm_events_entry;
 			struct list_head	cqm_groups_entry;
 			struct list_head	cqm_group_entry;
Signature · 1 line
1.9.1
#2 lore raw

PATCH v4 2/5 x86/mbm: Intel Memory B/W Monitoring enumeration and init

In reply to Vikas Shivappa

The MBM init patch enumerates the Intel (Memory b/w monitoring)MBM and initializes the perf events and datastructures for monitoring the memory b/w. Its based on original patch series by Kanaka Juvva.

Memory bandwidth monitoring(MBM) provides OS/VMM a way to monitor bandwidth from one level of cache to another. The current patches support L3 external bandwidth monitoring. It supports both 'local bandwidth' and 'total bandwidth' monitoring for the socket. Local bandwidth measures the amount of data sent through the memory controller on the socket and total b/w measures the total system bandwidth.

Extending the cache quality of service monitoring(CQM) we add four more events to the perf infrastructure:
intel_cqm_llc/local_bytes - bytes sent through local socket memory controller
intel_cqm_llc/total_bytes - total L3 external bytes sent intel_cqm_llc/local_bw - Current local b/w intel_cqm_llc/total_bw - current total b/w

The tasks are associated with a Resouce Monitoring ID(RMID) just like in cqm and OS uses a MSR write to indicate the RMID of the task during scheduling.

---
 arch/x86/include/asm/cpufeature.h          |   2 +
 arch/x86/kernel/cpu/common.c               |   4 +-
 arch/x86/kernel/cpu/perf_event_intel_cqm.c | 172 +++++++++++++++++++++++++++--
 3 files changed, 169 insertions(+), 9 deletions(-)
arch/x86/include/asm/cpufeature.h +2 -0
index 7ad8c94..fd80ef6 100644
@@ -241,6 +241,8 @@
 
 /* Intel-defined CPU QoS Sub-leaf, CPUID level 0x0000000F:1 (edx), word 12 */
 #define X86_FEATURE_CQM_OCCUP_LLC (12*32+ 0) /* LLC occupancy monitoring if 1 */
+#define X86_FEATURE_CQM_MBM_TOTAL (12*32+ 1) /* LLC Total MBM monitoring  */
+#define X86_FEATURE_CQM_MBM_LOCAL (12*32+ 2) /* LLC Local MBM monitoring  */
 
 /* AMD-defined CPU features, CPUID level 0x80000008 (ebx), word 13 */
 #define X86_FEATURE_CLZERO	(13*32+0) /* CLZERO instruction */
arch/x86/kernel/cpu/common.c +3 -1
index 37830de..f770221 100644
@@ -635,7 +635,9 @@ void get_cpu_cap(struct cpuinfo_x86 *c)
 			cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
 			c->x86_capability[CPUID_F_1_EDX] = edx;
 
-			if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
+			if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
+			      ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
+			       (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
 				c->x86_cache_max_rmid = ecx;
 				c->x86_cache_occ_scale = ebx;
 			}
arch/x86/kernel/cpu/perf_event_intel_cqm.c +164 -8

217 lines not on this page · raw message

Signature · 1 line
1.9.1
#3 lore raw

Re: [PATCH 2/5] x86/mbm: Intel Memory B/W Monitoring enumeration and init

On Wed, 10 Feb 2016, Vikas Shivappa wrote:ar

arch/x86/kernel/cpu/perf_event_intel_cqm.c static const struct x86_cpu_id intel_cqm_match[] = { · line 1434

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+static int intel_mbm_init(void)
+{
+	int ret = 0, array_size, maxid = cqm_max_rmid + 1;
+
+	mbm_socket_max = cpumask_weight(&cqm_cpumask);

This should use the new topology_max_packages() function, so you can alloc your array correctly even if not all sockets are online/plugged yet.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static const struct x86_cpu_id intel_cqm_match[] = { · line 1438

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+	array_size = sizeof(struct sample) * maxid * mbm_socket_max;
+	mbm_local = kmalloc(array_size, GFP_KERNEL);
+	if (!mbm_local) {
+		ret = -ENOMEM;

return -ENOMEM is sufficient here.

Quoted 15 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +13 -0
+		goto out;
+	}
+
+	mbm_total = kmalloc(array_size, GFP_KERNEL);
+	if (!mbm_total) {
+		kfree(mbm_local);
+		ret = -ENOMEM;
+	}
+out:
+
+	return ret;
+}
+
 	cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1521

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -0
@@ -1384,14 +1520,34 @@ static int __init intel_cqm_init(void)
 
 	__perf_cpu_notifier(intel_cqm_cpu_notifier);

You really should register the notifier _AFTER_ registering the pmu. That needs to be fixed anyway, because the existing code leaks the notifier AND memory in case perf_pmu_register fails.

Thanks,

tglx

#4 lore raw

Re: [PATCH 2/5] x86/mbm: Intel Memory B/W Monitoring enumeration and init

Quoted 9 lines · Thomas Gleixner

On Wed, 10 Feb 2016, Vikas Shivappa wrote:ar

arch/x86/kernel/cpu/perf_event_intel_cqm.c static const struct x86_cpu_id intel_cqm_match[] = { · line 1434

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+static int intel_mbm_init(void)
+{
+	int ret = 0, array_size, maxid = cqm_max_rmid + 1;
+
+	mbm_socket_max = cpumask_weight(&cqm_cpumask);

This should use the new topology_max_packages() function, so you can alloc your array correctly even if not all sockets are online/plugged yet.

Will fix. Thanks for pointing out.

Quoted 7 lines

arch/x86/kernel/cpu/perf_event_intel_cqm.c static const struct x86_cpu_id intel_cqm_match[] = { · line 1438

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+	array_size = sizeof(struct sample) * maxid * mbm_socket_max;
+	mbm_local = kmalloc(array_size, GFP_KERNEL);
+	if (!mbm_local) {
+		ret = -ENOMEM;

return -ENOMEM is sufficient here.

Will fix

Quoted 17 lines
Quoted 17 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +13 -0
+		goto out;
+	}
+
+	mbm_total = kmalloc(array_size, GFP_KERNEL);
+	if (!mbm_total) {
+		kfree(mbm_local);
+		ret = -ENOMEM;
+	}
+out:
+
+	return ret;
+}
+
 	cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;
@@ -1384,14 +1520,34 @@ static int __init intel_cqm_init(void)
 
Quoted 5 lines

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1521

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -0
 	__perf_cpu_notifier(intel_cqm_cpu_notifier);

You really should register the notifier _AFTER_ registering the pmu. That needs to be fixed anyway, because the existing code leaks the notifier AND memory in case perf_pmu_register fails.

Correct, Will fix the notifier leak

Thanks,
Vikas

Quoted 5 lines

Thanks,

tglx

#5 lore raw

Re: [PATCH 2/5] x86/mbm: Intel Memory B/W Monitoring enumeration and init

Quoted 6 lines · Vikas Shivappa
Quoted 3 lines · Thomas Gleixner

You really should register the notifier _AFTER_ registering the pmu. That needs to be fixed anyway, because the existing code leaks the notifier AND memory in case perf_pmu_register fails.

Correct, Will fix the notifier leak

It's not only a notifier leak. The existing code also leaks memory. Please fix that before adding the new stuff in a seperate patch.

Thanks,

tglx

#6 lore raw

Re: [PATCH 2/5] x86/mbm: Intel Memory B/W Monitoring enumeration and init

Quoted 10 lines · Thomas Gleixner
Quoted 6 lines · Vikas Shivappa
Quoted 3 lines · Thomas Gleixner

You really should register the notifier _AFTER_ registering the pmu. That needs to be fixed anyway, because the existing code leaks the notifier AND memory in case perf_pmu_register fails.

Correct, Will fix the notifier leak

It's not only a notifier leak. The existing code also leaks memory. Please fix that before adding the new stuff in a seperate patch.

ok..Makes sense. will send the notifier and memory leak fix in seperate patch as its a existing cqm issue.

I had added a kfree(str) in the end for the memory leak fix in the current patch.

out:

	cpu_notifier_register_done();
	if (ret) {
		mbm_enabled = false;
		cqm_enabled = false;
		kfree(str);
	}
Quoted 5 lines

Thanks,

tglx

#7 lore raw

PATCH v4 3/5 x86/mbm: Memory bandwidth monitoring event management

In reply to Vikas Shivappa

From: Tony Luck<[email protected]>

Includes all the core infrastructure to measure the total_bytes and bandwidth.

We have per socket counters for both total system wide L3 external bytes and local socket memory-controller bytes. The current b/w is calculated for a minimum diff time(time since it was last counted) of 100ms. The OS does MSR writes to MSR_IA32_QM_EVTSEL and MSR_IA32_QM_CTR to read the counters and uses the IA32_PQR_ASSOC_MSR to associate the RMID with the task. The tasks have a common RMID for cqm(cache quality of
service monitoring) and MBM. Hence most of the scheduling code is
reused from cqm.

---
 arch/x86/kernel/cpu/perf_event_intel_cqm.c | 159 ++++++++++++++++++++++++++++-
 1 file changed, 155 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_cqm.c +155 -4
index e45f5aa..b1c9663 100644
@@ -13,6 +13,11 @@
 #define MSR_IA32_QM_CTR		0x0c8e
 #define MSR_IA32_QM_EVTSEL	0x0c8d
 
+/*
+ * MBM Counter is 24bits wide. MBM_CNTR_MAX defines max counter
+ * value
+ */
+#define MBM_CNTR_MAX		0xffffff
 static u32 cqm_max_rmid = -1;
 static unsigned int cqm_l3_scale; /* supposedly cacheline size */
 static bool cqm_enabled, mbm_enabled;
@@ -69,6 +74,16 @@ static struct sample *mbm_total;
  */
 static struct sample *mbm_local;
 
+#define pkg_id	topology_physical_package_id(smp_processor_id())
+/*
+ * rmid_2_index returns the index for the rmid in mbm_local/mbm_total array.
+ * mbm_total[] and mbm_local[] are linearly indexed by socket# * max number of
+ * rmids per socket, an example is given below
+ * RMID1 of Socket0:  vrmid =  1
+ * RMID1 of Socket1:  vrmid =  1 * (cqm_max_rmid + 1) + 1
+ * RMID1 of Socket2:  vrmid =  2 * (cqm_max_rmid + 1) + 1
+ */
+#define rmid_2_index(rmid)  ((pkg_id * (cqm_max_rmid + 1)) + rmid)
 /*
  * Protects cache_cgroups and cqm_rmid_free_lru and cqm_rmid_limbo_lru.
  * Also protects event->hw.cqm_rmid
@@ -92,8 +107,19 @@ static cpumask_t cqm_cpumask;
 #define RMID_VAL_UNAVAIL	(1ULL << 62)
 
 #define QOS_L3_OCCUP_EVENT_ID	(1 << 0)
+/*
+ * MBM Event IDs as defined in SDM section 17.15.5
+ * Event IDs are used to program EVTSEL MSRs before reading mbm event counters
+ */
+enum mbm_evt_type {
+	QOS_MBM_TOTAL_EVENT_ID = 0x02,
+	QOS_MBM_LOCAL_EVENT_ID,
+	QOS_MBM_TOTAL_BW_EVENT_ID,
+	QOS_MBM_LOCAL_BW_EVENT_ID,
+};
 
-#define QOS_EVENT_MASK	QOS_L3_OCCUP_EVENT_ID
+#define QOS_MBM_BW_EVENT_MASK 0x04
+#define QOS_MBM_LOCAL_EVENT_MASK 0x01
 
 /*
  * This is central to the rotation algorithm in __intel_cqm_rmid_rotate().
@@ -423,9 +449,16 @@ static bool __conflict_event(struct perf_event *a, struct perf_event *b)
 struct rmid_read {
 	u32 rmid;
 	atomic64_t value;
+	enum mbm_evt_type evt_type;
 };
 
 static void __intel_cqm_event_count(void *info);
+static void init_mbm_sample(u32 rmid, enum mbm_evt_type evt_type);
+
+static bool is_mbm_event(int e)
+{
+	return (e >= QOS_MBM_TOTAL_EVENT_ID && e <= QOS_MBM_LOCAL_BW_EVENT_ID);
+}
 
 /*
  * Exchange the RMID of a group of events.
@@ -867,6 +900,98 @@ static void intel_cqm_rmid_rotate(struct work_struct *work)
 	schedule_delayed_work(&intel_cqm_rmid_work, delay);
 }
 
+static struct sample *update_sample(unsigned int rmid,
+				    enum mbm_evt_type evt_type, int first)
+{
+	ktime_t cur_time;
+	struct sample *mbm_current;
+	u32 vrmid = rmid_2_index(rmid);
+	u64 val, bytes, diff_time;
+	u32 eventid;
+
+	if (evt_type & QOS_MBM_LOCAL_EVENT_MASK) {
+		mbm_current = &mbm_local[vrmid];
+		eventid     =  QOS_MBM_LOCAL_EVENT_ID;
+	} else {
+		mbm_current = &mbm_total[vrmid];
+		eventid     = QOS_MBM_TOTAL_EVENT_ID;
+	}
+
+	cur_time = ktime_get();
+	wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
+	rdmsrl(MSR_IA32_QM_CTR, val);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return mbm_current;
+	val &= MBM_CNTR_MAX;
+
+	if (first) {
+		mbm_current->interval_start = cur_time;
+		mbm_current->prev_msr = val;
+		mbm_current->total_bytes = 0;
+		mbm_current->interval_bytes = 0;
+		mbm_current->bandwidth = 0;
+		return mbm_current;
+	}
+
+	if (val < mbm_current->prev_msr)
+		bytes = MBM_CNTR_MAX - mbm_current->prev_msr + val + 1;
+	else
+		bytes = val - mbm_current->prev_msr;
+	bytes *= cqm_l3_scale;
+
+	mbm_current->total_bytes += bytes;
+	mbm_current->interval_bytes += bytes;
+	mbm_current->prev_msr = val;
+	diff_time = ktime_ms_delta(cur_time, mbm_current->interval_start);
+
+	/*
+	 * The b/w measured is really the most recent/current b/w.
+	 * We wait till enough time has passed to avoid
+	 * arthmetic rounding problems.Having it at >=100ms,
+	 * such errors would be <=1%.
+	 */
+	if (diff_time > 100) {
+		bytes = mbm_current->interval_bytes * MSEC_PER_SEC;
+		do_div(bytes, diff_time);
+		mbm_current->bandwidth = bytes;
+		mbm_current->interval_bytes = 0;
+		mbm_current->interval_start = cur_time;
+	}
+
+	return mbm_current;
+}
+
+static u64 rmid_read_mbm(unsigned int rmid, enum mbm_evt_type evt_type)
+{
+	struct sample *mbm_current;
+
+	mbm_current = update_sample(rmid, evt_type, 0);
+
+	if (evt_type & QOS_MBM_BW_EVENT_MASK)
+		return mbm_current->bandwidth;
+	else
+		return mbm_current->total_bytes;
+}
+
+static void __intel_mbm_event_init(void *info)
+{
+	struct rmid_read *rr = info;
+
+	update_sample(rr->rmid, rr->evt_type, 1);
+}
+
+static void init_mbm_sample(u32 rmid, enum mbm_evt_type evt_type)
+{
+	struct rmid_read rr = {
+		.value = ATOMIC64_INIT(0),
+	};
+
+	rr.rmid = rmid;
+	rr.evt_type = evt_type;
+	/* on each socket, init sample */
+	on_each_cpu_mask(&cqm_cpumask, __intel_mbm_event_init, &rr, 1);
+}
+
 /*
  * Find a group and setup RMID.
  *
@@ -887,6 +1012,9 @@ static void intel_cqm_setup_event(struct perf_event *event,
 			/* All tasks in a group share an RMID */
 			event->hw.cqm_rmid = rmid;
 			*group = iter;
+			if (is_mbm_event(event->attr.config) &&
+			    __rmid_valid(rmid))
+				init_mbm_sample(rmid, event->attr.config);
 			return;
 		}
 
@@ -903,6 +1031,9 @@ static void intel_cqm_setup_event(struct perf_event *event,
 	else
 		rmid = __get_rmid();
 
+	if (is_mbm_event(event->attr.config) && __rmid_valid(rmid))
+		init_mbm_sample(rmid, event->attr.config);
+
 	event->hw.cqm_rmid = rmid;
 }
 
@@ -924,7 +1055,10 @@ static void intel_cqm_event_read(struct perf_event *event)
 	if (!__rmid_valid(rmid))
 		goto out;
 
-	val = __rmid_read(rmid);
+	if (is_mbm_event(event->attr.config))
+		val = rmid_read_mbm(rmid, event->attr.config);
+	else
+		val = __rmid_read(rmid);
 
 	/*
 	 * Ignore this reading on error states and do not update the value.
@@ -955,6 +1089,17 @@ static inline bool cqm_group_leader(struct perf_event *event)
 	return !list_empty(&event->hw.cqm_groups_entry);
 }
 
+static void __intel_mbm_event_count(void *info)
+{
+	struct rmid_read *rr = info;
+	u64 val;
+
+	val = rmid_read_mbm(rr->rmid, rr->evt_type);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return;
+	atomic64_add(val, &rr->value);
+}
+
 static u64 intel_cqm_event_count(struct perf_event *event)
 {
 	unsigned long flags;
@@ -1008,7 +1153,12 @@ static u64 intel_cqm_event_count(struct perf_event *event)
 	if (!__rmid_valid(rr.rmid))
 		goto out;
 
-	on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count, &rr, 1);
+	if (is_mbm_event(event->attr.config)) {
+		rr.evt_type = event->attr.config;
+		on_each_cpu_mask(&cqm_cpumask, __intel_mbm_event_count, &rr, 1);
+	} else {
+		on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count, &rr, 1);
+	}
 
 	raw_spin_lock_irqsave(&cache_lock, flags);
 	if (event->hw.cqm_rmid == rr.rmid)
@@ -1123,7 +1273,8 @@ static int intel_cqm_event_init(struct perf_event *event)
 	if (event->attr.type != intel_cqm_pmu.type)
 		return -ENOENT;
 
-	if (event->attr.config & ~QOS_EVENT_MASK)
+	if ((event->attr.config < QOS_L3_OCCUP_EVENT_ID) ||
+	     (event->attr.config > QOS_MBM_LOCAL_BW_EVENT_ID))
 		return -EINVAL;
 
 	/* unsupported modes and filters */
Signature · 1 line
1.9.1
#8 lore raw

PATCH v4 4/5 x86/mbm: RMID Recycling MBM changes

In reply to Vikas Shivappa

RMID could be allocated or deallocated as part of RMID recycling. When an RMID is allocated for mbm event, the mbm counter needs to be initialized because next time we read the counter we need the previous value to account for total bytes that went to the memory controller. Similarly, when RMID is deallocated we need to update the ->count variable.

---
 arch/x86/kernel/cpu/perf_event_intel_cqm.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_cqm.c +25 -2
index b1c9663..fea22c8 100644
@@ -454,6 +454,7 @@ struct rmid_read {
 
 static void __intel_cqm_event_count(void *info);
 static void init_mbm_sample(u32 rmid, enum mbm_evt_type evt_type);
+static void __intel_mbm_event_count(void *info);
 
 static bool is_mbm_event(int e)
 {
@@ -480,8 +481,14 @@ static u32 intel_cqm_xchg_rmid(struct perf_event *group, u32 rmid)
 			.rmid = old_rmid,
 		};
 
-		on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count,
-				 &rr, 1);
+		if (is_mbm_event(group->attr.config)) {
+			rr.evt_type = group->attr.config;
+			on_each_cpu_mask(&cqm_cpumask, __intel_mbm_event_count,
+					 &rr, 1);
+		} else {
+			on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count,
+					 &rr, 1);
+		}
 		local64_set(&group->count, atomic64_read(&rr.value));
 	}
 
@@ -493,6 +500,22 @@ static u32 intel_cqm_xchg_rmid(struct perf_event *group, u32 rmid)
 
 	raw_spin_unlock_irq(&cache_lock);
 
+	/*
+	 * If the allocation is for mbm, init the mbm stats.
+	 * Need to check if each event in the group is mbm event
+	 * because there could be multiple type of events in the same group.
+	 */
+	if (__rmid_valid(rmid)) {
+		event = group;
+		if (is_mbm_event(event->attr.config))
+			init_mbm_sample(rmid, event->attr.config);
+
+		list_for_each_entry(event, head, hw.cqm_group_entry) {
+			if (is_mbm_event(event->attr.config))
+				init_mbm_sample(rmid, event->attr.config);
+		}
+	}
+
 	return old_rmid;
 }
 
Signature · 1 line
1.9.1
#9 lore raw

PATCH v4 5/5 x86/mbm: Add support for MBM counter overflow handling

In reply to Vikas Shivappa

This patch adds a per package timer which periodically updates the Memory bandwidth counters for the events that are currently active. Current patch has a periodic timer every 1s since the SDM guarantees that the counter will not overflow in 1s but this time can be definitely improved by calibrating on the system. The overflow is really a function of the max memory b/w that the socket can support, max counter value and scaling factor.

---
 arch/x86/kernel/cpu/perf_event_intel_cqm.c | 111 ++++++++++++++++++++++++++++-
 1 file changed, 110 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/perf_event_intel_cqm.c +110 -1
index fea22c8..8245dbd 100644
@@ -18,6 +18,11 @@
  * value
  */
 #define MBM_CNTR_MAX		0xffffff
+/*
+ * Guaranteed time in ms as per SDM where MBM counters will not overflow.
+ */
+#define MBM_CTR_OVERFLOW_TIME	1000
+
 static u32 cqm_max_rmid = -1;
 static unsigned int cqm_l3_scale; /* supposedly cacheline size */
 static bool cqm_enabled, mbm_enabled;
@@ -49,6 +54,7 @@ struct intel_pqr_state {
  * interrupts disabled, which is sufficient for the protection.
  */
 static DEFINE_PER_CPU(struct intel_pqr_state, pqr_state);
+static struct hrtimer *mbm_timers;
 /**
  * struct sample - mbm event's (local or total) data
  * @interval_start Time this interval began
@@ -1123,6 +1129,84 @@ static void __intel_mbm_event_count(void *info)
 	atomic64_add(val, &rr->value);
 }
 
+static enum hrtimer_restart mbm_hrtimer_handle(struct hrtimer *hrtimer)
+{
+	struct perf_event *iter, *iter1;
+	struct list_head *head;
+	unsigned long flags;
+	u32 grp_rmid;
+
+	/*
+	 * Need to cache_lock as the timer Event Select MSR reads
+	 * can race with the mbm/cqm count() and mbm_init() reads.
+	 */
+	mutex_lock(&cache_mutex);
+	raw_spin_lock_irqsave(&cache_lock, flags);
+
+	if (list_empty(&cache_groups))
+		goto out;
+
+	list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {
+		grp_rmid = iter->hw.cqm_rmid;
+		if (!__rmid_valid(grp_rmid))
+			continue;
+		if (is_mbm_event(iter->attr.config))
+			update_sample(grp_rmid, iter->attr.config, 0);
+
+		head = &iter->hw.cqm_group_entry;
+		if (list_empty(head))
+			continue;
+		list_for_each_entry(iter1, head, hw.cqm_group_entry) {
+			if (!iter1->hw.is_group_event)
+				break;
+			if (is_mbm_event(iter1->attr.config))
+				update_sample(iter1->hw.cqm_rmid,
+					      iter1->attr.config, 0);
+		}
+	}
+
+out:
+	raw_spin_unlock_irqrestore(&cache_lock, flags);
+	mutex_unlock(&cache_mutex);
+
+	hrtimer_forward_now(hrtimer, ms_to_ktime(MBM_CTR_OVERFLOW_TIME));
+
+	return HRTIMER_RESTART;
+}
+
+static void __mbm_start_timer(void *info)
+{
+	hrtimer_start(&mbm_timers[pkg_id], ms_to_ktime(MBM_CTR_OVERFLOW_TIME),
+			     HRTIMER_MODE_REL_PINNED);
+}
+
+static void __mbm_stop_timer(void *info)
+{
+	hrtimer_cancel(&mbm_timers[pkg_id]);
+}
+
+static void mbm_start_timers(void)
+{
+	on_each_cpu_mask(&cqm_cpumask, __mbm_start_timer, NULL, 1);
+}
+
+static void mbm_stop_timers(void)
+{
+	on_each_cpu_mask(&cqm_cpumask, __mbm_stop_timer, NULL, 1);
+}
+
+static void mbm_hrtimer_init(void)
+{
+	struct hrtimer *hr;
+	int i;
+
+	for (i = 0; i < mbm_socket_max; i++) {
+		hr = &mbm_timers[i];
+		hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hr->function = mbm_hrtimer_handle;
+	}
+}
+
 static u64 intel_cqm_event_count(struct perf_event *event)
 {
 	unsigned long flags;
@@ -1285,6 +1369,12 @@ static void intel_cqm_event_destroy(struct perf_event *event)
 		}
 	}
 
+	/*
+	 * Stop the mbm overflow timers when the last event is destroyed.
+	*/
+	if (list_empty(&cache_groups))
+		mbm_stop_timers();
+
 	mutex_unlock(&cache_mutex);
 }
 
@@ -1317,6 +1407,12 @@ static int intel_cqm_event_init(struct perf_event *event)
 
 	mutex_lock(&cache_mutex);
 
+	/*
+	 * Start the mbm overflow timers when the first event is created.
+	*/
+	if (list_empty(&cache_groups))
+		mbm_start_timers();
+
 	/* Will also set rmid */
 	intel_cqm_setup_event(event, &group);
 
@@ -1615,10 +1711,23 @@ static int intel_mbm_init(void)
 
 	mbm_total = kmalloc(array_size, GFP_KERNEL);
 	if (!mbm_total) {
-		kfree(mbm_local);
 		ret = -ENOMEM;
+		goto out;
+	}
+
+	array_size = sizeof(struct hrtimer) * mbm_socket_max;
+	mbm_timers = kmalloc(array_size, GFP_KERNEL);
+	if (!mbm_timers) {
+		ret = -ENOMEM;
+		goto out;
 	}
+	mbm_hrtimer_init();
+
 out:
+	if (ret) {
+		kfree(mbm_local);
+		kfree(mbm_total);
+	}
 
 	return ret;
 }
Signature · 1 line
1.9.1
#10 lore raw

Re: [PATCH 5/5] x86/mbm: Add support for MBM counter overflow handling

Quoted 6 lines · Vikas Shivappa
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
+static enum hrtimer_restart mbm_hrtimer_handle(struct hrtimer *hrtimer)
+{
+	if (list_empty(&cache_groups))
+		goto out;
+
+	list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {

....

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void __intel_mbm_event_count(void *info) · line 1174

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+		}
+	}
+
+out:
+	raw_spin_unlock_irqrestore(&cache_lock, flags);
+	mutex_unlock(&cache_mutex);
+
+	hrtimer_forward_now(hrtimer, ms_to_ktime(MBM_CTR_OVERFLOW_TIME));
+
+	return HRTIMER_RESTART;

Why is that timer restarted if cache_groups is empty?

Thanks,

tglx

#11 lore raw

Re: [PATCH 5/5] x86/mbm: Add support for MBM counter overflow handling

Quoted 10 lines · Thomas Gleixner
Quoted 6 lines · Vikas Shivappa
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
+static enum hrtimer_restart mbm_hrtimer_handle(struct hrtimer *hrtimer)
+{
+	if (list_empty(&cache_groups))
+		goto out;
+
+	list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {

....

Quoted 12 lines

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void __intel_mbm_event_count(void *info) · line 1174

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+		}
+	}
+
+out:
+	raw_spin_unlock_irqrestore(&cache_lock, flags);
+	mutex_unlock(&cache_mutex);
+
+	hrtimer_forward_now(hrtimer, ms_to_ktime(MBM_CTR_OVERFLOW_TIME));
+
+	return HRTIMER_RESTART;

Why is that timer restarted if cache_groups is empty?

Will fix. It should not be as the timers may be stopped by now as well. Also this patch incorrectly uses mutex(above) in the timer context. Will fix that as well.

Thanks,
Vikas

Quoted 5 lines

Thanks,

tglx

#12 lore raw

Re: [PATCH 5/5] x86/mbm: Add support for MBM counter overflow handling

Quoted 6 lines · Vikas Shivappa
Quoted 1 line · Thomas Gleixner

Why is that timer restarted if cache_groups is empty?

Will fix. It should not be as the timers may be stopped by now as well. Also this patch incorrectly uses mutex(above) in the timer context. Will fix that as well.

Indeed. Did not notice.

Thanks,

tglx

#13 lore raw
In reply to Vikas Shivappa

Hello Thomas,

Was wondering if you had any feedback on the MBM patches. These are almost a complete rewrite (the core part 3/5 from Tony Luck). The reused parts are mostly the declarations of datastructures. This has tried to address all your comments on V3 about the commenting, the core calculation, init, lot of unnecessary and confusing constants and code..

http://lkml.kernel.org/r/alpine.DEB.2.11.1508192243081.3873@nanos

Thanks,
Vikas

Quoted 26 lines · Vikas Shivappa

The V4 version of MBM is almost a complete rewrite of the prior versions. It tries to address all of Thomas earlier comments.

The patch series has one preparatory patch for cqm and then 4 MBM patches. *Patches apply on 4.5-rc1*.

Memory bandwitdh monitoring(MBM) provides OS/VMM a way to monitor bandwidth from one level of cache to another. The current patches support L3 external bandwitch monitoring.
It supports both 'local bandwidth' and 'total bandwidth' monitoring for the socket. Local bandwidth measures the amount of data sent through the memory controller on the socket and total b/w measures the total system bandwidth.

The tasks are associated with a Resouce Monitoring ID(RMID) just like in cqm and OS uses a MSR write to indicate the RMID of the task during scheduling.

[PATCH 1/5] x86,perf/cqm : Fix cqm handling of grouping events into a [PATCH 2/5] x86/mbm: Intel Memory B/W Monitoring enumeration and init [PATCH 3/5] x86/mbm: Memory bandwidth monitoring event management [PATCH 4/5] x86/mbm: RMID Recycling MBM changes
[PATCH 5/5] x86/mbm: Add support for MBM counter overflow handling

#14 lore raw

Vikas,

Quoted 5 lines · Vikas Shivappa

Was wondering if you had any feedback on the MBM patches. These are almost a complete rewrite (the core part 3/5 from Tony Luck). The reused parts are mostly the declarations of datastructures. This has tried to address all your comments on V3 about the commenting, the core calculation, init, lot of unnecessary and confusing constants and code..

It's on my list. I tend to it, when I cleaned up the unholy mess in perf_intel_* et al.

Thanks,

tglx